author | mduigou |
Thu, 02 May 2013 09:18:56 -0700 | |
changeset 17421 | f3fbcfe6e2cf |
parent 9676 | 5663e62f8d7e |
child 17910 | 82d10099a8a6 |
child 18251 | 3743160a4cb8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
7816
diff
changeset
|
2 |
* Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util.zip; |
|
27 |
||
3078 | 28 |
import java.io.Closeable; |
2 | 29 |
import java.io.InputStream; |
30 |
import java.io.IOException; |
|
31 |
import java.io.EOFException; |
|
32 |
import java.io.File; |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
33 |
import java.nio.charset.Charset; |
9676
5663e62f8d7e
7041612: Rename StandardCharset to StandardCharsets
mduigou
parents:
9526
diff
changeset
|
34 |
import java.nio.charset.StandardCharsets; |
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
35 |
import java.util.ArrayDeque; |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
36 |
import java.util.Deque; |
2 | 37 |
import java.util.Enumeration; |
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
38 |
import java.util.HashMap; |
17421
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
39 |
import java.util.Iterator; |
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
40 |
import java.util.Map; |
2 | 41 |
import java.util.NoSuchElementException; |
17421
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
42 |
import java.util.Spliterator; |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
43 |
import java.util.Spliterators; |
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
44 |
import java.util.WeakHashMap; |
17421
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
45 |
import java.util.stream.Stream; |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
46 |
import java.util.stream.StreamSupport; |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
47 |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
48 |
import static java.util.zip.ZipConstants64.*; |
2 | 49 |
|
50 |
/** |
|
51 |
* This class is used to read entries from a zip file. |
|
52 |
* |
|
53 |
* <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor |
|
54 |
* or method in this class will cause a {@link NullPointerException} to be |
|
55 |
* thrown. |
|
56 |
* |
|
57 |
* @author David Connelly |
|
58 |
*/ |
|
59 |
public |
|
3078 | 60 |
class ZipFile implements ZipConstants, Closeable { |
2 | 61 |
private long jzfile; // address of jzfile data |
62 |
private String name; // zip file name |
|
63 |
private int total; // total number of entries |
|
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
64 |
private volatile boolean closeRequested = false; |
2 | 65 |
|
66 |
private static final int STORED = ZipEntry.STORED; |
|
67 |
private static final int DEFLATED = ZipEntry.DEFLATED; |
|
68 |
||
69 |
/** |
|
70 |
* Mode flag to open a zip file for reading. |
|
71 |
*/ |
|
72 |
public static final int OPEN_READ = 0x1; |
|
73 |
||
74 |
/** |
|
75 |
* Mode flag to open a zip file and mark it for deletion. The file will be |
|
76 |
* deleted some time between the moment that it is opened and the moment |
|
77 |
* that it is closed, but its contents will remain accessible via the |
|
78 |
* <tt>ZipFile</tt> object until either the close method is invoked or the |
|
79 |
* virtual machine exits. |
|
80 |
*/ |
|
81 |
public static final int OPEN_DELETE = 0x4; |
|
82 |
||
83 |
static { |
|
84 |
/* Zip library is loaded from System.initializeSystemClass */ |
|
85 |
initIDs(); |
|
86 |
} |
|
87 |
||
88 |
private static native void initIDs(); |
|
89 |
||
5148
2bf5469864ea
6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents:
3849
diff
changeset
|
90 |
private static final boolean usemmap; |
2bf5469864ea
6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents:
3849
diff
changeset
|
91 |
|
2bf5469864ea
6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents:
3849
diff
changeset
|
92 |
static { |
2bf5469864ea
6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents:
3849
diff
changeset
|
93 |
// A system prpperty to disable mmap use to avoid vm crash when |
2bf5469864ea
6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents:
3849
diff
changeset
|
94 |
// in-use zip file is accidently overwritten by others. |
6882
637546039be3
6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents:
5506
diff
changeset
|
95 |
String prop = sun.misc.VM.getSavedProperty("sun.zip.disableMemoryMapping"); |
5148
2bf5469864ea
6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents:
3849
diff
changeset
|
96 |
usemmap = (prop == null || |
2bf5469864ea
6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents:
3849
diff
changeset
|
97 |
!(prop.length() == 0 || prop.equalsIgnoreCase("true"))); |
2bf5469864ea
6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents:
3849
diff
changeset
|
98 |
} |
2bf5469864ea
6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents:
3849
diff
changeset
|
99 |
|
2 | 100 |
/** |
101 |
* Opens a zip file for reading. |
|
102 |
* |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
103 |
* <p>First, if there is a security manager, its <code>checkRead</code> |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
104 |
* method is called with the <code>name</code> argument as its argument |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
105 |
* to ensure the read is allowed. |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
106 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
107 |
* <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
108 |
* decode the entry names and comments. |
2 | 109 |
* |
110 |
* @param name the name of the zip file |
|
111 |
* @throws ZipException if a ZIP format error has occurred |
|
112 |
* @throws IOException if an I/O error has occurred |
|
113 |
* @throws SecurityException if a security manager exists and its |
|
114 |
* <code>checkRead</code> method doesn't allow read access to the file. |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
115 |
* |
2 | 116 |
* @see SecurityManager#checkRead(java.lang.String) |
117 |
*/ |
|
118 |
public ZipFile(String name) throws IOException { |
|
119 |
this(new File(name), OPEN_READ); |
|
120 |
} |
|
121 |
||
122 |
/** |
|
123 |
* Opens a new <code>ZipFile</code> to read from the specified |
|
124 |
* <code>File</code> object in the specified mode. The mode argument |
|
125 |
* must be either <tt>OPEN_READ</tt> or <tt>OPEN_READ | OPEN_DELETE</tt>. |
|
126 |
* |
|
127 |
* <p>First, if there is a security manager, its <code>checkRead</code> |
|
128 |
* method is called with the <code>name</code> argument as its argument to |
|
129 |
* ensure the read is allowed. |
|
130 |
* |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
131 |
* <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
132 |
* decode the entry names and comments |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
133 |
* |
2 | 134 |
* @param file the ZIP file to be opened for reading |
135 |
* @param mode the mode in which the file is to be opened |
|
136 |
* @throws ZipException if a ZIP format error has occurred |
|
137 |
* @throws IOException if an I/O error has occurred |
|
138 |
* @throws SecurityException if a security manager exists and |
|
139 |
* its <code>checkRead</code> method |
|
140 |
* doesn't allow read access to the file, |
|
141 |
* or its <code>checkDelete</code> method doesn't allow deleting |
|
142 |
* the file when the <tt>OPEN_DELETE</tt> flag is set. |
|
143 |
* @throws IllegalArgumentException if the <tt>mode</tt> argument is invalid |
|
144 |
* @see SecurityManager#checkRead(java.lang.String) |
|
145 |
* @since 1.3 |
|
146 |
*/ |
|
147 |
public ZipFile(File file, int mode) throws IOException { |
|
9676
5663e62f8d7e
7041612: Rename StandardCharset to StandardCharsets
mduigou
parents:
9526
diff
changeset
|
148 |
this(file, mode, StandardCharsets.UTF_8); |
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
149 |
} |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
150 |
|
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
151 |
/** |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
152 |
* Opens a ZIP file for reading given the specified File object. |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
153 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
154 |
* <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
155 |
* decode the entry names and comments. |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
156 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
157 |
* @param file the ZIP file to be opened for reading |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
158 |
* @throws ZipException if a ZIP format error has occurred |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
159 |
* @throws IOException if an I/O error has occurred |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
160 |
*/ |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
161 |
public ZipFile(File file) throws ZipException, IOException { |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
162 |
this(file, OPEN_READ); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
163 |
} |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
164 |
|
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
165 |
private ZipCoder zc; |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
166 |
|
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
167 |
/** |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
168 |
* Opens a new <code>ZipFile</code> to read from the specified |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
169 |
* <code>File</code> object in the specified mode. The mode argument |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
170 |
* must be either <tt>OPEN_READ</tt> or <tt>OPEN_READ | OPEN_DELETE</tt>. |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
171 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
172 |
* <p>First, if there is a security manager, its <code>checkRead</code> |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
173 |
* method is called with the <code>name</code> argument as its argument to |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
174 |
* ensure the read is allowed. |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
175 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
176 |
* @param file the ZIP file to be opened for reading |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
177 |
* @param mode the mode in which the file is to be opened |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
178 |
* @param charset |
2704
a92617170304
6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents:
2592
diff
changeset
|
179 |
* the {@linkplain java.nio.charset.Charset charset} to |
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
180 |
* be used to decode the ZIP entry name and comment that are not |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
181 |
* encoded by using UTF-8 encoding (indicated by entry's general |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
182 |
* purpose flag). |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
183 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
184 |
* @throws ZipException if a ZIP format error has occurred |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
185 |
* @throws IOException if an I/O error has occurred |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
186 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
187 |
* @throws SecurityException |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
188 |
* if a security manager exists and its <code>checkRead</code> |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
189 |
* method doesn't allow read access to the file,or its |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
190 |
* <code>checkDelete</code> method doesn't allow deleting the |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
191 |
* file when the <tt>OPEN_DELETE</tt> flag is set |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
192 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
193 |
* @throws IllegalArgumentException if the <tt>mode</tt> argument is invalid |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
194 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
195 |
* @see SecurityManager#checkRead(java.lang.String) |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
196 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
197 |
* @since 1.7 |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
198 |
*/ |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
199 |
public ZipFile(File file, int mode, Charset charset) throws IOException |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
200 |
{ |
2 | 201 |
if (((mode & OPEN_READ) == 0) || |
202 |
((mode & ~(OPEN_READ | OPEN_DELETE)) != 0)) { |
|
203 |
throw new IllegalArgumentException("Illegal mode: 0x"+ |
|
204 |
Integer.toHexString(mode)); |
|
205 |
} |
|
206 |
String name = file.getPath(); |
|
207 |
SecurityManager sm = System.getSecurityManager(); |
|
208 |
if (sm != null) { |
|
209 |
sm.checkRead(name); |
|
210 |
if ((mode & OPEN_DELETE) != 0) { |
|
211 |
sm.checkDelete(name); |
|
212 |
} |
|
213 |
} |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
214 |
if (charset == null) |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
215 |
throw new NullPointerException("charset is null"); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
216 |
this.zc = ZipCoder.get(charset); |
3849 | 217 |
long t0 = System.nanoTime(); |
5148
2bf5469864ea
6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents:
3849
diff
changeset
|
218 |
jzfile = open(name, mode, file.lastModified(), usemmap); |
3849 | 219 |
sun.misc.PerfCounter.getZipFileOpenTime().addElapsedTimeFrom(t0); |
220 |
sun.misc.PerfCounter.getZipFileCount().increment(); |
|
2 | 221 |
this.name = name; |
222 |
this.total = getTotal(jzfile); |
|
223 |
} |
|
224 |
||
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
225 |
/** |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
226 |
* Opens a zip file for reading. |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
227 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
228 |
* <p>First, if there is a security manager, its <code>checkRead</code> |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
229 |
* method is called with the <code>name</code> argument as its argument |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
230 |
* to ensure the read is allowed. |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
231 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
232 |
* @param name the name of the zip file |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
233 |
* @param charset |
2704
a92617170304
6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents:
2592
diff
changeset
|
234 |
* the {@linkplain java.nio.charset.Charset charset} to |
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
235 |
* be used to decode the ZIP entry name and comment that are not |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
236 |
* encoded by using UTF-8 encoding (indicated by entry's general |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
237 |
* purpose flag). |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
238 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
239 |
* @throws ZipException if a ZIP format error has occurred |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
240 |
* @throws IOException if an I/O error has occurred |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
241 |
* @throws SecurityException |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
242 |
* if a security manager exists and its <code>checkRead</code> |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
243 |
* method doesn't allow read access to the file |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
244 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
245 |
* @see SecurityManager#checkRead(java.lang.String) |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
246 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
247 |
* @since 1.7 |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
248 |
*/ |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
249 |
public ZipFile(String name, Charset charset) throws IOException |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
250 |
{ |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
251 |
this(new File(name), OPEN_READ, charset); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
252 |
} |
2 | 253 |
|
254 |
/** |
|
255 |
* Opens a ZIP file for reading given the specified File object. |
|
256 |
* @param file the ZIP file to be opened for reading |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
257 |
* @param charset |
2704
a92617170304
6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents:
2592
diff
changeset
|
258 |
* The {@linkplain java.nio.charset.Charset charset} to be |
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
259 |
* used to decode the ZIP entry name and comment (ignored if |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
260 |
* the <a href="package-summary.html#lang_encoding"> language |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
261 |
* encoding bit</a> of the ZIP entry's general purpose bit |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
262 |
* flag is set). |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
263 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
264 |
* @throws ZipException if a ZIP format error has occurred |
2 | 265 |
* @throws IOException if an I/O error has occurred |
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
266 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
267 |
* @since 1.7 |
2 | 268 |
*/ |
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
269 |
public ZipFile(File file, Charset charset) throws IOException |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
270 |
{ |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
271 |
this(file, OPEN_READ, charset); |
2 | 272 |
} |
273 |
||
274 |
/** |
|
3078 | 275 |
* Returns the zip file comment, or null if none. |
276 |
* |
|
277 |
* @return the comment string for the zip file, or null if none |
|
278 |
* |
|
279 |
* @throws IllegalStateException if the zip file has been closed |
|
280 |
* |
|
281 |
* Since 1.7 |
|
282 |
*/ |
|
283 |
public String getComment() { |
|
284 |
synchronized (this) { |
|
285 |
ensureOpen(); |
|
286 |
byte[] bcomm = getCommentBytes(jzfile); |
|
287 |
if (bcomm == null) |
|
288 |
return null; |
|
289 |
return zc.toString(bcomm, bcomm.length); |
|
290 |
} |
|
291 |
} |
|
292 |
||
293 |
/** |
|
2 | 294 |
* Returns the zip file entry for the specified name, or null |
295 |
* if not found. |
|
296 |
* |
|
297 |
* @param name the name of the entry |
|
298 |
* @return the zip file entry, or null if not found |
|
299 |
* @throws IllegalStateException if the zip file has been closed |
|
300 |
*/ |
|
301 |
public ZipEntry getEntry(String name) { |
|
302 |
if (name == null) { |
|
303 |
throw new NullPointerException("name"); |
|
304 |
} |
|
305 |
long jzentry = 0; |
|
306 |
synchronized (this) { |
|
307 |
ensureOpen(); |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
308 |
jzentry = getEntry(jzfile, zc.getBytes(name), true); |
2 | 309 |
if (jzentry != 0) { |
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
310 |
ZipEntry ze = getZipEntry(name, jzentry); |
2 | 311 |
freeEntry(jzfile, jzentry); |
312 |
return ze; |
|
313 |
} |
|
314 |
} |
|
315 |
return null; |
|
316 |
} |
|
317 |
||
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
318 |
private static native long getEntry(long jzfile, byte[] name, |
2 | 319 |
boolean addSlash); |
320 |
||
321 |
// freeEntry releases the C jzentry struct. |
|
322 |
private static native void freeEntry(long jzfile, long jzentry); |
|
323 |
||
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
324 |
// the outstanding inputstreams that need to be closed, |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
325 |
// mapped to the inflater objects they use. |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
326 |
private final Map<InputStream, Inflater> streams = new WeakHashMap<>(); |
2915
92e31faf9ffc
6728376: Wrong error handling in Java_java_util_zip_Deflater_deflateBytes leads to size 0 if compress fails
sherman
parents:
2704
diff
changeset
|
327 |
|
2 | 328 |
/** |
329 |
* Returns an input stream for reading the contents of the specified |
|
330 |
* zip file entry. |
|
331 |
* |
|
332 |
* <p> Closing this ZIP file will, in turn, close all input |
|
333 |
* streams that have been returned by invocations of this method. |
|
334 |
* |
|
335 |
* @param entry the zip file entry |
|
336 |
* @return the input stream for reading the contents of the specified |
|
337 |
* zip file entry. |
|
338 |
* @throws ZipException if a ZIP format error has occurred |
|
339 |
* @throws IOException if an I/O error has occurred |
|
340 |
* @throws IllegalStateException if the zip file has been closed |
|
341 |
*/ |
|
342 |
public InputStream getInputStream(ZipEntry entry) throws IOException { |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
343 |
if (entry == null) { |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
344 |
throw new NullPointerException("entry"); |
2 | 345 |
} |
346 |
long jzentry = 0; |
|
347 |
ZipFileInputStream in = null; |
|
348 |
synchronized (this) { |
|
349 |
ensureOpen(); |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
350 |
if (!zc.isUTF8() && (entry.flag & EFS) != 0) { |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
351 |
jzentry = getEntry(jzfile, zc.getBytesUTF8(entry.name), false); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
352 |
} else { |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
353 |
jzentry = getEntry(jzfile, zc.getBytes(entry.name), false); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
354 |
} |
2 | 355 |
if (jzentry == 0) { |
356 |
return null; |
|
357 |
} |
|
358 |
in = new ZipFileInputStream(jzentry); |
|
359 |
||
7550
700dd1df3aa8
7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents:
6882
diff
changeset
|
360 |
switch (getEntryMethod(jzentry)) { |
700dd1df3aa8
7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents:
6882
diff
changeset
|
361 |
case STORED: |
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
362 |
synchronized (streams) { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
363 |
streams.put(in, null); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
364 |
} |
7550
700dd1df3aa8
7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents:
6882
diff
changeset
|
365 |
return in; |
700dd1df3aa8
7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents:
6882
diff
changeset
|
366 |
case DEFLATED: |
700dd1df3aa8
7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents:
6882
diff
changeset
|
367 |
// MORE: Compute good size for inflater stream: |
700dd1df3aa8
7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents:
6882
diff
changeset
|
368 |
long size = getEntrySize(jzentry) + 2; // Inflater likes a bit of slack |
700dd1df3aa8
7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents:
6882
diff
changeset
|
369 |
if (size > 65536) size = 8192; |
700dd1df3aa8
7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents:
6882
diff
changeset
|
370 |
if (size <= 0) size = 4096; |
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
371 |
Inflater inf = getInflater(); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
372 |
InputStream is = |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
373 |
new ZipFileInflaterInputStream(in, inf, (int)size); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
374 |
synchronized (streams) { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
375 |
streams.put(is, inf); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
376 |
} |
7550
700dd1df3aa8
7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents:
6882
diff
changeset
|
377 |
return is; |
700dd1df3aa8
7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents:
6882
diff
changeset
|
378 |
default: |
700dd1df3aa8
7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents:
6882
diff
changeset
|
379 |
throw new ZipException("invalid compression method"); |
700dd1df3aa8
7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents:
6882
diff
changeset
|
380 |
} |
2 | 381 |
} |
382 |
} |
|
383 |
||
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
384 |
private class ZipFileInflaterInputStream extends InflaterInputStream { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
385 |
private volatile boolean closeRequested = false; |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
386 |
private boolean eof = false; |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
387 |
private final ZipFileInputStream zfin; |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
388 |
|
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
389 |
ZipFileInflaterInputStream(ZipFileInputStream zfin, Inflater inf, |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
390 |
int size) { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
391 |
super(zfin, inf, size); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
392 |
this.zfin = zfin; |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
393 |
} |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
394 |
|
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
395 |
public void close() throws IOException { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
396 |
if (closeRequested) |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
397 |
return; |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
398 |
closeRequested = true; |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
399 |
|
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
400 |
super.close(); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
401 |
Inflater inf; |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
402 |
synchronized (streams) { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
403 |
inf = streams.remove(this); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
404 |
} |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
405 |
if (inf != null) { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
406 |
releaseInflater(inf); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
407 |
} |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
408 |
} |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
409 |
|
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
410 |
// Override fill() method to provide an extra "dummy" byte |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
411 |
// at the end of the input stream. This is required when |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
412 |
// using the "nowrap" Inflater option. |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
413 |
protected void fill() throws IOException { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
414 |
if (eof) { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
415 |
throw new EOFException("Unexpected end of ZLIB input stream"); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
416 |
} |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
417 |
len = in.read(buf, 0, buf.length); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
418 |
if (len == -1) { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
419 |
buf[0] = 0; |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
420 |
len = 1; |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
421 |
eof = true; |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
422 |
} |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
423 |
inf.setInput(buf, 0, len); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
424 |
} |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
425 |
|
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
426 |
public int available() throws IOException { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
427 |
if (closeRequested) |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
428 |
return 0; |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
429 |
long avail = zfin.size() - inf.getBytesWritten(); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
430 |
return (avail > (long) Integer.MAX_VALUE ? |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
431 |
Integer.MAX_VALUE : (int) avail); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
432 |
} |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
433 |
|
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
434 |
protected void finalize() throws Throwable { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
435 |
close(); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
436 |
} |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
437 |
} |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
438 |
|
2 | 439 |
/* |
440 |
* Gets an inflater from the list of available inflaters or allocates |
|
441 |
* a new one. |
|
442 |
*/ |
|
443 |
private Inflater getInflater() { |
|
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
444 |
Inflater inf; |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
445 |
synchronized (inflaterCache) { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
446 |
while (null != (inf = inflaterCache.poll())) { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
447 |
if (false == inf.ended()) { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
448 |
return inf; |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
449 |
} |
2 | 450 |
} |
451 |
} |
|
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
452 |
return new Inflater(true); |
2 | 453 |
} |
454 |
||
455 |
/* |
|
456 |
* Releases the specified inflater to the list of available inflaters. |
|
457 |
*/ |
|
458 |
private void releaseInflater(Inflater inf) { |
|
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
459 |
if (false == inf.ended()) { |
1158 | 460 |
inf.reset(); |
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
461 |
synchronized (inflaterCache) { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
462 |
inflaterCache.add(inf); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
463 |
} |
2 | 464 |
} |
465 |
} |
|
466 |
||
467 |
// List of available Inflater objects for decompression |
|
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
468 |
private Deque<Inflater> inflaterCache = new ArrayDeque<>(); |
2 | 469 |
|
470 |
/** |
|
471 |
* Returns the path name of the ZIP file. |
|
472 |
* @return the path name of the ZIP file |
|
473 |
*/ |
|
474 |
public String getName() { |
|
475 |
return name; |
|
476 |
} |
|
477 |
||
17421
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
478 |
private class ZipEntryIterator implements Enumeration<ZipEntry>, Iterator<ZipEntry> { |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
479 |
private int i = 0; |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
480 |
|
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
481 |
public ZipEntryIterator() { |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
482 |
ensureOpen(); |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
483 |
} |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
484 |
|
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
485 |
public boolean hasMoreElements() { |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
486 |
return hasNext(); |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
487 |
} |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
488 |
|
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
489 |
public boolean hasNext() { |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
490 |
synchronized (ZipFile.this) { |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
491 |
ensureOpen(); |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
492 |
return i < total; |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
493 |
} |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
494 |
} |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
495 |
|
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
496 |
public ZipEntry nextElement() { |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
497 |
return next(); |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
498 |
} |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
499 |
|
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
500 |
public ZipEntry next() { |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
501 |
synchronized (ZipFile.this) { |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
502 |
ensureOpen(); |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
503 |
if (i >= total) { |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
504 |
throw new NoSuchElementException(); |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
505 |
} |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
506 |
long jzentry = getNextEntry(jzfile, i++); |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
507 |
if (jzentry == 0) { |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
508 |
String message; |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
509 |
if (closeRequested) { |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
510 |
message = "ZipFile concurrently closed"; |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
511 |
} else { |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
512 |
message = getZipMessage(ZipFile.this.jzfile); |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
513 |
} |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
514 |
throw new ZipError("jzentry == 0" + |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
515 |
",\n jzfile = " + ZipFile.this.jzfile + |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
516 |
",\n total = " + ZipFile.this.total + |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
517 |
",\n name = " + ZipFile.this.name + |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
518 |
",\n i = " + i + |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
519 |
",\n message = " + message |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
520 |
); |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
521 |
} |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
522 |
ZipEntry ze = getZipEntry(null, jzentry); |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
523 |
freeEntry(jzfile, jzentry); |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
524 |
return ze; |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
525 |
} |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
526 |
} |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
527 |
} |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
528 |
|
2 | 529 |
/** |
530 |
* Returns an enumeration of the ZIP file entries. |
|
531 |
* @return an enumeration of the ZIP file entries |
|
532 |
* @throws IllegalStateException if the zip file has been closed |
|
533 |
*/ |
|
534 |
public Enumeration<? extends ZipEntry> entries() { |
|
17421
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
535 |
return new ZipEntryIterator(); |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
536 |
} |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
537 |
|
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
538 |
/** |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
539 |
* Return an ordered {@code Stream} over the ZIP file entries. |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
540 |
* Entries appear in the {@code Stream} in the order they appear in |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
541 |
* the central directory of the ZIP file. |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
542 |
* |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
543 |
* @return an ordered {@code Stream} of entries in this ZIP file |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
544 |
* @throws IllegalStateException if the zip file has been closed |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
545 |
* @since 1.8 |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
546 |
*/ |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
547 |
public Stream<? extends ZipEntry> stream() { |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
548 |
return StreamSupport.stream(Spliterators.spliterator( |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
549 |
new ZipEntryIterator(), size(), |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
550 |
Spliterator.ORDERED | Spliterator.DISTINCT | |
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
9676
diff
changeset
|
551 |
Spliterator.IMMUTABLE | Spliterator.NONNULL)); |
2 | 552 |
} |
553 |
||
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
554 |
private ZipEntry getZipEntry(String name, long jzentry) { |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
555 |
ZipEntry e = new ZipEntry(); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
556 |
e.flag = getEntryFlag(jzentry); // get the flag first |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
557 |
if (name != null) { |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
558 |
e.name = name; |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
559 |
} else { |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
560 |
byte[] bname = getEntryBytes(jzentry, JZENTRY_NAME); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
561 |
if (!zc.isUTF8() && (e.flag & EFS) != 0) { |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
562 |
e.name = zc.toStringUTF8(bname, bname.length); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
563 |
} else { |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
564 |
e.name = zc.toString(bname, bname.length); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
565 |
} |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
566 |
} |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
567 |
e.time = getEntryTime(jzentry); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
568 |
e.crc = getEntryCrc(jzentry); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
569 |
e.size = getEntrySize(jzentry); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
570 |
e. csize = getEntryCSize(jzentry); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
571 |
e.method = getEntryMethod(jzentry); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
572 |
e.extra = getEntryBytes(jzentry, JZENTRY_EXTRA); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
573 |
byte[] bcomm = getEntryBytes(jzentry, JZENTRY_COMMENT); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
574 |
if (bcomm == null) { |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
575 |
e.comment = null; |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
576 |
} else { |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
577 |
if (!zc.isUTF8() && (e.flag & EFS) != 0) { |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
578 |
e.comment = zc.toStringUTF8(bcomm, bcomm.length); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
579 |
} else { |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
580 |
e.comment = zc.toString(bcomm, bcomm.length); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
581 |
} |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
582 |
} |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
583 |
return e; |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
584 |
} |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
585 |
|
2 | 586 |
private static native long getNextEntry(long jzfile, int i); |
587 |
||
588 |
/** |
|
589 |
* Returns the number of entries in the ZIP file. |
|
590 |
* @return the number of entries in the ZIP file |
|
591 |
* @throws IllegalStateException if the zip file has been closed |
|
592 |
*/ |
|
593 |
public int size() { |
|
594 |
ensureOpen(); |
|
595 |
return total; |
|
596 |
} |
|
597 |
||
598 |
/** |
|
599 |
* Closes the ZIP file. |
|
600 |
* <p> Closing this ZIP file will close all of the input streams |
|
601 |
* previously returned by invocations of the {@link #getInputStream |
|
602 |
* getInputStream} method. |
|
603 |
* |
|
604 |
* @throws IOException if an I/O error has occurred |
|
605 |
*/ |
|
606 |
public void close() throws IOException { |
|
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
607 |
if (closeRequested) |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
608 |
return; |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
609 |
closeRequested = true; |
2 | 610 |
|
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
611 |
synchronized (this) { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
612 |
// Close streams, release their inflaters |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
613 |
synchronized (streams) { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
614 |
if (false == streams.isEmpty()) { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
615 |
Map<InputStream, Inflater> copy = new HashMap<>(streams); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
616 |
streams.clear(); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
617 |
for (Map.Entry<InputStream, Inflater> e : copy.entrySet()) { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
618 |
e.getKey().close(); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
619 |
Inflater inf = e.getValue(); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
620 |
if (inf != null) { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
621 |
inf.end(); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
622 |
} |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
623 |
} |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
624 |
} |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
625 |
} |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
626 |
|
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
627 |
// Release cached inflaters |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
628 |
Inflater inf; |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
629 |
synchronized (inflaterCache) { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
630 |
while (null != (inf = inflaterCache.poll())) { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
631 |
inf.end(); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
632 |
} |
2915
92e31faf9ffc
6728376: Wrong error handling in Java_java_util_zip_Deflater_deflateBytes leads to size 0 if compress fails
sherman
parents:
2704
diff
changeset
|
633 |
} |
92e31faf9ffc
6728376: Wrong error handling in Java_java_util_zip_Deflater_deflateBytes leads to size 0 if compress fails
sherman
parents:
2704
diff
changeset
|
634 |
|
2 | 635 |
if (jzfile != 0) { |
636 |
// Close the zip file |
|
637 |
long zf = this.jzfile; |
|
638 |
jzfile = 0; |
|
639 |
||
640 |
close(zf); |
|
641 |
} |
|
642 |
} |
|
643 |
} |
|
644 |
||
645 |
/** |
|
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
646 |
* Ensures that the system resources held by this ZipFile object are |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
647 |
* released when there are no more references to it. |
2 | 648 |
* |
649 |
* <p> |
|
650 |
* Since the time when GC would invoke this method is undetermined, |
|
651 |
* it is strongly recommended that applications invoke the <code>close</code> |
|
652 |
* method as soon they have finished accessing this <code>ZipFile</code>. |
|
653 |
* This will prevent holding up system resources for an undetermined |
|
654 |
* length of time. |
|
655 |
* |
|
656 |
* @throws IOException if an I/O error has occurred |
|
657 |
* @see java.util.zip.ZipFile#close() |
|
658 |
*/ |
|
659 |
protected void finalize() throws IOException { |
|
660 |
close(); |
|
661 |
} |
|
662 |
||
663 |
private static native void close(long jzfile); |
|
664 |
||
665 |
private void ensureOpen() { |
|
666 |
if (closeRequested) { |
|
667 |
throw new IllegalStateException("zip file closed"); |
|
668 |
} |
|
669 |
||
670 |
if (jzfile == 0) { |
|
671 |
throw new IllegalStateException("The object is not initialized."); |
|
672 |
} |
|
673 |
} |
|
674 |
||
675 |
private void ensureOpenOrZipException() throws IOException { |
|
676 |
if (closeRequested) { |
|
677 |
throw new ZipException("ZipFile closed"); |
|
678 |
} |
|
679 |
} |
|
680 |
||
681 |
/* |
|
682 |
* Inner class implementing the input stream used to read a |
|
683 |
* (possibly compressed) zip file entry. |
|
684 |
*/ |
|
685 |
private class ZipFileInputStream extends InputStream { |
|
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
686 |
private volatile boolean closeRequested = false; |
2 | 687 |
protected long jzentry; // address of jzentry data |
688 |
private long pos; // current position within entry data |
|
689 |
protected long rem; // number of remaining bytes within entry |
|
690 |
protected long size; // uncompressed size of this entry |
|
691 |
||
692 |
ZipFileInputStream(long jzentry) { |
|
693 |
pos = 0; |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
694 |
rem = getEntryCSize(jzentry); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
695 |
size = getEntrySize(jzentry); |
2 | 696 |
this.jzentry = jzentry; |
697 |
} |
|
698 |
||
699 |
public int read(byte b[], int off, int len) throws IOException { |
|
700 |
if (rem == 0) { |
|
701 |
return -1; |
|
702 |
} |
|
703 |
if (len <= 0) { |
|
704 |
return 0; |
|
705 |
} |
|
706 |
if (len > rem) { |
|
707 |
len = (int) rem; |
|
708 |
} |
|
709 |
synchronized (ZipFile.this) { |
|
710 |
ensureOpenOrZipException(); |
|
711 |
||
712 |
len = ZipFile.read(ZipFile.this.jzfile, jzentry, pos, b, |
|
713 |
off, len); |
|
714 |
} |
|
715 |
if (len > 0) { |
|
716 |
pos += len; |
|
717 |
rem -= len; |
|
718 |
} |
|
719 |
if (rem == 0) { |
|
720 |
close(); |
|
721 |
} |
|
722 |
return len; |
|
723 |
} |
|
724 |
||
725 |
public int read() throws IOException { |
|
726 |
byte[] b = new byte[1]; |
|
727 |
if (read(b, 0, 1) == 1) { |
|
728 |
return b[0] & 0xff; |
|
729 |
} else { |
|
730 |
return -1; |
|
731 |
} |
|
732 |
} |
|
733 |
||
734 |
public long skip(long n) { |
|
735 |
if (n > rem) |
|
736 |
n = rem; |
|
737 |
pos += n; |
|
738 |
rem -= n; |
|
739 |
if (rem == 0) { |
|
740 |
close(); |
|
741 |
} |
|
742 |
return n; |
|
743 |
} |
|
744 |
||
745 |
public int available() { |
|
746 |
return rem > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) rem; |
|
747 |
} |
|
748 |
||
749 |
public long size() { |
|
750 |
return size; |
|
751 |
} |
|
752 |
||
753 |
public void close() { |
|
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
754 |
if (closeRequested) |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
755 |
return; |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
756 |
closeRequested = true; |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
757 |
|
2 | 758 |
rem = 0; |
759 |
synchronized (ZipFile.this) { |
|
760 |
if (jzentry != 0 && ZipFile.this.jzfile != 0) { |
|
761 |
freeEntry(ZipFile.this.jzfile, jzentry); |
|
762 |
jzentry = 0; |
|
763 |
} |
|
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
764 |
} |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
765 |
synchronized (streams) { |
2915
92e31faf9ffc
6728376: Wrong error handling in Java_java_util_zip_Deflater_deflateBytes leads to size 0 if compress fails
sherman
parents:
2704
diff
changeset
|
766 |
streams.remove(this); |
2 | 767 |
} |
768 |
} |
|
9281
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
769 |
|
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
770 |
protected void finalize() { |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
771 |
close(); |
41e796d1b6c1
7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents:
9035
diff
changeset
|
772 |
} |
2 | 773 |
} |
774 |
||
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
775 |
|
5148
2bf5469864ea
6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents:
3849
diff
changeset
|
776 |
private static native long open(String name, int mode, long lastModified, |
2bf5469864ea
6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents:
3849
diff
changeset
|
777 |
boolean usemmap) throws IOException; |
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
778 |
private static native int getTotal(long jzfile); |
2 | 779 |
private static native int read(long jzfile, long jzentry, |
780 |
long pos, byte[] b, int off, int len); |
|
781 |
||
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
782 |
// access to the native zentry object |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
783 |
private static native long getEntryTime(long jzentry); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
784 |
private static native long getEntryCrc(long jzentry); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
785 |
private static native long getEntryCSize(long jzentry); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
786 |
private static native long getEntrySize(long jzentry); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
787 |
private static native int getEntryMethod(long jzentry); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
788 |
private static native int getEntryFlag(long jzentry); |
3078 | 789 |
private static native byte[] getCommentBytes(long jzfile); |
2 | 790 |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
791 |
private static final int JZENTRY_NAME = 0; |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
792 |
private static final int JZENTRY_EXTRA = 1; |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
793 |
private static final int JZENTRY_COMMENT = 2; |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
1158
diff
changeset
|
794 |
private static native byte[] getEntryBytes(long jzentry, int type); |
2 | 795 |
|
796 |
private static native String getZipMessage(long jzfile); |
|
797 |
} |