author | sherman |
Fri, 03 Aug 2012 13:40:03 -0700 | |
changeset 13409 | fe3ab27ef1a6 |
parent 9035 | 1255eb81cc2f |
child 14342 | 8435a30053c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
7815
diff
changeset
|
2 |
* Copyright (c) 1996, 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 |
||
28 |
/** |
|
29 |
* This class provides support for general purpose decompression using the |
|
30 |
* popular ZLIB compression library. The ZLIB compression library was |
|
31 |
* initially developed as part of the PNG graphics standard and is not |
|
32 |
* protected by patents. It is fully described in the specifications at |
|
33 |
* the <a href="package-summary.html#package_description">java.util.zip |
|
34 |
* package description</a>. |
|
35 |
* |
|
36 |
* <p>The following code fragment demonstrates a trivial compression |
|
37 |
* and decompression of a string using <tt>Deflater</tt> and |
|
38 |
* <tt>Inflater</tt>. |
|
39 |
* |
|
40 |
* <blockquote><pre> |
|
41 |
* try { |
|
42 |
* // Encode a String into bytes |
|
43 |
* String inputString = "blahblahblah\u20AC\u20AC"; |
|
44 |
* byte[] input = inputString.getBytes("UTF-8"); |
|
45 |
* |
|
46 |
* // Compress the bytes |
|
47 |
* byte[] output = new byte[100]; |
|
48 |
* Deflater compresser = new Deflater(); |
|
49 |
* compresser.setInput(input); |
|
50 |
* compresser.finish(); |
|
51 |
* int compressedDataLength = compresser.deflate(output); |
|
52 |
* |
|
53 |
* // Decompress the bytes |
|
54 |
* Inflater decompresser = new Inflater(); |
|
55 |
* decompresser.setInput(output, 0, compressedDataLength); |
|
56 |
* byte[] result = new byte[100]; |
|
57 |
* int resultLength = decompresser.inflate(result); |
|
58 |
* decompresser.end(); |
|
59 |
* |
|
60 |
* // Decode the bytes into a String |
|
61 |
* String outputString = new String(result, 0, resultLength, "UTF-8"); |
|
62 |
* } catch(java.io.UnsupportedEncodingException ex) { |
|
63 |
* // handle |
|
64 |
* } catch (java.util.zip.DataFormatException ex) { |
|
65 |
* // handle |
|
66 |
* } |
|
67 |
* </pre></blockquote> |
|
68 |
* |
|
69 |
* @see Deflater |
|
70 |
* @author David Connelly |
|
71 |
* |
|
72 |
*/ |
|
73 |
public |
|
74 |
class Inflater { |
|
5173 | 75 |
|
76 |
private final ZStreamRef zsRef; |
|
1158 | 77 |
private byte[] buf = defaultBuf; |
2 | 78 |
private int off, len; |
79 |
private boolean finished; |
|
80 |
private boolean needDict; |
|
13409
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
9035
diff
changeset
|
81 |
private long bytesRead; |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
9035
diff
changeset
|
82 |
private long bytesWritten; |
2 | 83 |
|
1158 | 84 |
private static final byte[] defaultBuf = new byte[0]; |
85 |
||
2 | 86 |
static { |
87 |
/* Zip library is loaded from System.initializeSystemClass */ |
|
88 |
initIDs(); |
|
89 |
} |
|
90 |
||
91 |
/** |
|
92 |
* Creates a new decompressor. If the parameter 'nowrap' is true then |
|
93 |
* the ZLIB header and checksum fields will not be used. This provides |
|
94 |
* compatibility with the compression format used by both GZIP and PKZIP. |
|
95 |
* <p> |
|
96 |
* Note: When using the 'nowrap' option it is also necessary to provide |
|
97 |
* an extra "dummy" byte as input. This is required by the ZLIB native |
|
98 |
* library in order to support certain optimizations. |
|
99 |
* |
|
100 |
* @param nowrap if true then support GZIP compatible compression |
|
101 |
*/ |
|
102 |
public Inflater(boolean nowrap) { |
|
5173 | 103 |
zsRef = new ZStreamRef(init(nowrap)); |
2 | 104 |
} |
105 |
||
106 |
/** |
|
107 |
* Creates a new decompressor. |
|
108 |
*/ |
|
109 |
public Inflater() { |
|
110 |
this(false); |
|
111 |
} |
|
112 |
||
113 |
/** |
|
114 |
* Sets input data for decompression. Should be called whenever |
|
115 |
* needsInput() returns true indicating that more input data is |
|
116 |
* required. |
|
117 |
* @param b the input data bytes |
|
118 |
* @param off the start offset of the input data |
|
119 |
* @param len the length of the input data |
|
120 |
* @see Inflater#needsInput |
|
121 |
*/ |
|
5173 | 122 |
public void setInput(byte[] b, int off, int len) { |
2 | 123 |
if (b == null) { |
124 |
throw new NullPointerException(); |
|
125 |
} |
|
126 |
if (off < 0 || len < 0 || off > b.length - len) { |
|
127 |
throw new ArrayIndexOutOfBoundsException(); |
|
128 |
} |
|
5173 | 129 |
synchronized (zsRef) { |
130 |
this.buf = b; |
|
131 |
this.off = off; |
|
132 |
this.len = len; |
|
133 |
} |
|
2 | 134 |
} |
135 |
||
136 |
/** |
|
137 |
* Sets input data for decompression. Should be called whenever |
|
138 |
* needsInput() returns true indicating that more input data is |
|
139 |
* required. |
|
140 |
* @param b the input data bytes |
|
141 |
* @see Inflater#needsInput |
|
142 |
*/ |
|
143 |
public void setInput(byte[] b) { |
|
144 |
setInput(b, 0, b.length); |
|
145 |
} |
|
146 |
||
147 |
/** |
|
148 |
* Sets the preset dictionary to the given array of bytes. Should be |
|
149 |
* called when inflate() returns 0 and needsDictionary() returns true |
|
150 |
* indicating that a preset dictionary is required. The method getAdler() |
|
151 |
* can be used to get the Adler-32 value of the dictionary needed. |
|
152 |
* @param b the dictionary data bytes |
|
153 |
* @param off the start offset of the data |
|
154 |
* @param len the length of the data |
|
155 |
* @see Inflater#needsDictionary |
|
156 |
* @see Inflater#getAdler |
|
157 |
*/ |
|
5173 | 158 |
public void setDictionary(byte[] b, int off, int len) { |
159 |
if (b == null) { |
|
2 | 160 |
throw new NullPointerException(); |
161 |
} |
|
162 |
if (off < 0 || len < 0 || off > b.length - len) { |
|
163 |
throw new ArrayIndexOutOfBoundsException(); |
|
164 |
} |
|
5173 | 165 |
synchronized (zsRef) { |
166 |
ensureOpen(); |
|
167 |
setDictionary(zsRef.address(), b, off, len); |
|
168 |
needDict = false; |
|
169 |
} |
|
2 | 170 |
} |
171 |
||
172 |
/** |
|
173 |
* Sets the preset dictionary to the given array of bytes. Should be |
|
174 |
* called when inflate() returns 0 and needsDictionary() returns true |
|
175 |
* indicating that a preset dictionary is required. The method getAdler() |
|
176 |
* can be used to get the Adler-32 value of the dictionary needed. |
|
177 |
* @param b the dictionary data bytes |
|
178 |
* @see Inflater#needsDictionary |
|
179 |
* @see Inflater#getAdler |
|
180 |
*/ |
|
181 |
public void setDictionary(byte[] b) { |
|
182 |
setDictionary(b, 0, b.length); |
|
183 |
} |
|
184 |
||
185 |
/** |
|
186 |
* Returns the total number of bytes remaining in the input buffer. |
|
187 |
* This can be used to find out what bytes still remain in the input |
|
188 |
* buffer after decompression has finished. |
|
189 |
* @return the total number of bytes remaining in the input buffer |
|
190 |
*/ |
|
5173 | 191 |
public int getRemaining() { |
192 |
synchronized (zsRef) { |
|
193 |
return len; |
|
194 |
} |
|
2 | 195 |
} |
196 |
||
197 |
/** |
|
198 |
* Returns true if no data remains in the input buffer. This can |
|
199 |
* be used to determine if #setInput should be called in order |
|
200 |
* to provide more input. |
|
201 |
* @return true if no data remains in the input buffer |
|
202 |
*/ |
|
5173 | 203 |
public boolean needsInput() { |
204 |
synchronized (zsRef) { |
|
205 |
return len <= 0; |
|
206 |
} |
|
2 | 207 |
} |
208 |
||
209 |
/** |
|
210 |
* Returns true if a preset dictionary is needed for decompression. |
|
211 |
* @return true if a preset dictionary is needed for decompression |
|
212 |
* @see Inflater#setDictionary |
|
213 |
*/ |
|
5173 | 214 |
public boolean needsDictionary() { |
215 |
synchronized (zsRef) { |
|
216 |
return needDict; |
|
217 |
} |
|
2 | 218 |
} |
219 |
||
220 |
/** |
|
221 |
* Returns true if the end of the compressed data stream has been |
|
222 |
* reached. |
|
223 |
* @return true if the end of the compressed data stream has been |
|
224 |
* reached |
|
225 |
*/ |
|
5173 | 226 |
public boolean finished() { |
227 |
synchronized (zsRef) { |
|
228 |
return finished; |
|
229 |
} |
|
2 | 230 |
} |
231 |
||
232 |
/** |
|
233 |
* Uncompresses bytes into specified buffer. Returns actual number |
|
234 |
* of bytes uncompressed. A return value of 0 indicates that |
|
235 |
* needsInput() or needsDictionary() should be called in order to |
|
236 |
* determine if more input data or a preset dictionary is required. |
|
237 |
* In the latter case, getAdler() can be used to get the Adler-32 |
|
238 |
* value of the dictionary required. |
|
239 |
* @param b the buffer for the uncompressed data |
|
240 |
* @param off the start offset of the data |
|
241 |
* @param len the maximum number of uncompressed bytes |
|
242 |
* @return the actual number of uncompressed bytes |
|
243 |
* @exception DataFormatException if the compressed data format is invalid |
|
244 |
* @see Inflater#needsInput |
|
245 |
* @see Inflater#needsDictionary |
|
246 |
*/ |
|
5173 | 247 |
public int inflate(byte[] b, int off, int len) |
2 | 248 |
throws DataFormatException |
249 |
{ |
|
250 |
if (b == null) { |
|
251 |
throw new NullPointerException(); |
|
252 |
} |
|
253 |
if (off < 0 || len < 0 || off > b.length - len) { |
|
254 |
throw new ArrayIndexOutOfBoundsException(); |
|
255 |
} |
|
5173 | 256 |
synchronized (zsRef) { |
257 |
ensureOpen(); |
|
13409
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
9035
diff
changeset
|
258 |
int thisLen = this.len; |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
9035
diff
changeset
|
259 |
int n = inflateBytes(zsRef.address(), b, off, len); |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
9035
diff
changeset
|
260 |
bytesWritten += n; |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
9035
diff
changeset
|
261 |
bytesRead += (thisLen - this.len); |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
9035
diff
changeset
|
262 |
return n; |
5173 | 263 |
} |
2 | 264 |
} |
265 |
||
266 |
/** |
|
267 |
* Uncompresses bytes into specified buffer. Returns actual number |
|
268 |
* of bytes uncompressed. A return value of 0 indicates that |
|
269 |
* needsInput() or needsDictionary() should be called in order to |
|
270 |
* determine if more input data or a preset dictionary is required. |
|
271 |
* In the latter case, getAdler() can be used to get the Adler-32 |
|
272 |
* value of the dictionary required. |
|
273 |
* @param b the buffer for the uncompressed data |
|
274 |
* @return the actual number of uncompressed bytes |
|
275 |
* @exception DataFormatException if the compressed data format is invalid |
|
276 |
* @see Inflater#needsInput |
|
277 |
* @see Inflater#needsDictionary |
|
278 |
*/ |
|
279 |
public int inflate(byte[] b) throws DataFormatException { |
|
280 |
return inflate(b, 0, b.length); |
|
281 |
} |
|
282 |
||
283 |
/** |
|
284 |
* Returns the ADLER-32 value of the uncompressed data. |
|
285 |
* @return the ADLER-32 value of the uncompressed data |
|
286 |
*/ |
|
5173 | 287 |
public int getAdler() { |
288 |
synchronized (zsRef) { |
|
289 |
ensureOpen(); |
|
290 |
return getAdler(zsRef.address()); |
|
291 |
} |
|
2 | 292 |
} |
293 |
||
294 |
/** |
|
295 |
* Returns the total number of compressed bytes input so far. |
|
296 |
* |
|
297 |
* <p>Since the number of bytes may be greater than |
|
298 |
* Integer.MAX_VALUE, the {@link #getBytesRead()} method is now |
|
299 |
* the preferred means of obtaining this information.</p> |
|
300 |
* |
|
301 |
* @return the total number of compressed bytes input so far |
|
302 |
*/ |
|
303 |
public int getTotalIn() { |
|
304 |
return (int) getBytesRead(); |
|
305 |
} |
|
306 |
||
307 |
/** |
|
308 |
* Returns the total number of compressed bytes input so far.</p> |
|
309 |
* |
|
310 |
* @return the total (non-negative) number of compressed bytes input so far |
|
311 |
* @since 1.5 |
|
312 |
*/ |
|
5173 | 313 |
public long getBytesRead() { |
314 |
synchronized (zsRef) { |
|
315 |
ensureOpen(); |
|
13409
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
9035
diff
changeset
|
316 |
return bytesRead; |
5173 | 317 |
} |
2 | 318 |
} |
319 |
||
320 |
/** |
|
321 |
* Returns the total number of uncompressed bytes output so far. |
|
322 |
* |
|
323 |
* <p>Since the number of bytes may be greater than |
|
324 |
* Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now |
|
325 |
* the preferred means of obtaining this information.</p> |
|
326 |
* |
|
327 |
* @return the total number of uncompressed bytes output so far |
|
328 |
*/ |
|
329 |
public int getTotalOut() { |
|
330 |
return (int) getBytesWritten(); |
|
331 |
} |
|
332 |
||
333 |
/** |
|
334 |
* Returns the total number of uncompressed bytes output so far.</p> |
|
335 |
* |
|
336 |
* @return the total (non-negative) number of uncompressed bytes output so far |
|
337 |
* @since 1.5 |
|
338 |
*/ |
|
5173 | 339 |
public long getBytesWritten() { |
340 |
synchronized (zsRef) { |
|
341 |
ensureOpen(); |
|
13409
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
9035
diff
changeset
|
342 |
return bytesWritten; |
5173 | 343 |
} |
2 | 344 |
} |
345 |
||
346 |
/** |
|
347 |
* Resets inflater so that a new set of input data can be processed. |
|
348 |
*/ |
|
5173 | 349 |
public void reset() { |
350 |
synchronized (zsRef) { |
|
351 |
ensureOpen(); |
|
352 |
reset(zsRef.address()); |
|
353 |
buf = defaultBuf; |
|
354 |
finished = false; |
|
355 |
needDict = false; |
|
356 |
off = len = 0; |
|
13409
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
9035
diff
changeset
|
357 |
bytesRead = bytesWritten = 0; |
5173 | 358 |
} |
2 | 359 |
} |
360 |
||
361 |
/** |
|
362 |
* Closes the decompressor and discards any unprocessed input. |
|
363 |
* This method should be called when the decompressor is no longer |
|
364 |
* being used, but will also be called automatically by the finalize() |
|
365 |
* method. Once this method is called, the behavior of the Inflater |
|
366 |
* object is undefined. |
|
367 |
*/ |
|
5173 | 368 |
public void end() { |
369 |
synchronized (zsRef) { |
|
370 |
long addr = zsRef.address(); |
|
371 |
zsRef.clear(); |
|
372 |
if (addr != 0) { |
|
373 |
end(addr); |
|
374 |
buf = null; |
|
375 |
} |
|
2 | 376 |
} |
377 |
} |
|
378 |
||
379 |
/** |
|
380 |
* Closes the decompressor when garbage is collected. |
|
381 |
*/ |
|
382 |
protected void finalize() { |
|
383 |
end(); |
|
384 |
} |
|
385 |
||
386 |
private void ensureOpen () { |
|
5173 | 387 |
assert Thread.holdsLock(zsRef); |
388 |
if (zsRef.address() == 0) |
|
389 |
throw new NullPointerException("Inflater has been closed"); |
|
2 | 390 |
} |
391 |
||
7815
12cc7f77e459
7009618: regression test failed caused by the fix for 7003462
sherman
parents:
5506
diff
changeset
|
392 |
boolean ended() { |
12cc7f77e459
7009618: regression test failed caused by the fix for 7003462
sherman
parents:
5506
diff
changeset
|
393 |
synchronized (zsRef) { |
12cc7f77e459
7009618: regression test failed caused by the fix for 7003462
sherman
parents:
5506
diff
changeset
|
394 |
return zsRef.address() == 0; |
12cc7f77e459
7009618: regression test failed caused by the fix for 7003462
sherman
parents:
5506
diff
changeset
|
395 |
} |
12cc7f77e459
7009618: regression test failed caused by the fix for 7003462
sherman
parents:
5506
diff
changeset
|
396 |
} |
12cc7f77e459
7009618: regression test failed caused by the fix for 7003462
sherman
parents:
5506
diff
changeset
|
397 |
|
2 | 398 |
private native static void initIDs(); |
399 |
private native static long init(boolean nowrap); |
|
5173 | 400 |
private native static void setDictionary(long addr, byte[] b, int off, |
2 | 401 |
int len); |
5173 | 402 |
private native int inflateBytes(long addr, byte[] b, int off, int len) |
2 | 403 |
throws DataFormatException; |
5173 | 404 |
private native static int getAdler(long addr); |
405 |
private native static void reset(long addr); |
|
406 |
private native static void end(long addr); |
|
2 | 407 |
} |