author | mli |
Wed, 31 May 2017 19:54:16 -0700 | |
changeset 45434 | 4582657c7260 |
parent 44534 | a076dffbc2c1 |
child 45924 | 8bbd04c0791e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
44534 | 2 |
* Copyright (c) 1996, 2017, 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 compression 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 |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
25859
diff
changeset
|
37 |
* and decompression of a string using {@code Deflater} and |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
25859
diff
changeset
|
38 |
* {@code Inflater}. |
2 | 39 |
* |
40 |
* <blockquote><pre> |
|
41 |
* try { |
|
42 |
* // Encode a String into bytes |
|
5469 | 43 |
* String inputString = "blahblahblah"; |
2 | 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); |
|
2940 | 52 |
* compresser.end(); |
2 | 53 |
* |
54 |
* // Decompress the bytes |
|
55 |
* Inflater decompresser = new Inflater(); |
|
56 |
* decompresser.setInput(output, 0, compressedDataLength); |
|
57 |
* byte[] result = new byte[100]; |
|
58 |
* int resultLength = decompresser.inflate(result); |
|
59 |
* decompresser.end(); |
|
60 |
* |
|
61 |
* // Decode the bytes into a String |
|
62 |
* String outputString = new String(result, 0, resultLength, "UTF-8"); |
|
63 |
* } catch(java.io.UnsupportedEncodingException ex) { |
|
64 |
* // handle |
|
65 |
* } catch (java.util.zip.DataFormatException ex) { |
|
66 |
* // handle |
|
67 |
* } |
|
68 |
* </pre></blockquote> |
|
69 |
* |
|
70 |
* @see Inflater |
|
71 |
* @author David Connelly |
|
45434
4582657c7260
8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents:
44534
diff
changeset
|
72 |
* @since 1.1 |
2 | 73 |
*/ |
74 |
public |
|
75 |
class Deflater { |
|
5173 | 76 |
|
77 |
private final ZStreamRef zsRef; |
|
2 | 78 |
private byte[] buf = new byte[0]; |
79 |
private int off, len; |
|
80 |
private int level, strategy; |
|
81 |
private boolean setParams; |
|
82 |
private boolean finish, finished; |
|
13409
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
83 |
private long bytesRead; |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
84 |
private long bytesWritten; |
2 | 85 |
|
86 |
/** |
|
87 |
* Compression method for the deflate algorithm (the only one currently |
|
88 |
* supported). |
|
89 |
*/ |
|
90 |
public static final int DEFLATED = 8; |
|
91 |
||
92 |
/** |
|
93 |
* Compression level for no compression. |
|
94 |
*/ |
|
95 |
public static final int NO_COMPRESSION = 0; |
|
96 |
||
97 |
/** |
|
98 |
* Compression level for fastest compression. |
|
99 |
*/ |
|
100 |
public static final int BEST_SPEED = 1; |
|
101 |
||
102 |
/** |
|
103 |
* Compression level for best compression. |
|
104 |
*/ |
|
105 |
public static final int BEST_COMPRESSION = 9; |
|
106 |
||
107 |
/** |
|
108 |
* Default compression level. |
|
109 |
*/ |
|
110 |
public static final int DEFAULT_COMPRESSION = -1; |
|
111 |
||
112 |
/** |
|
113 |
* Compression strategy best used for data consisting mostly of small |
|
114 |
* values with a somewhat random distribution. Forces more Huffman coding |
|
115 |
* and less string matching. |
|
116 |
*/ |
|
117 |
public static final int FILTERED = 1; |
|
118 |
||
119 |
/** |
|
120 |
* Compression strategy for Huffman coding only. |
|
121 |
*/ |
|
122 |
public static final int HUFFMAN_ONLY = 2; |
|
123 |
||
124 |
/** |
|
125 |
* Default compression strategy. |
|
126 |
*/ |
|
127 |
public static final int DEFAULT_STRATEGY = 0; |
|
128 |
||
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
129 |
/** |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
130 |
* Compression flush mode used to achieve best compression result. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
131 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
132 |
* @see Deflater#deflate(byte[], int, int, int) |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
133 |
* @since 1.7 |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
134 |
*/ |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
135 |
public static final int NO_FLUSH = 0; |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
136 |
|
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
137 |
/** |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
138 |
* Compression flush mode used to flush out all pending output; may |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
139 |
* degrade compression for some compression algorithms. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
140 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
141 |
* @see Deflater#deflate(byte[], int, int, int) |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
142 |
* @since 1.7 |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
143 |
*/ |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
144 |
public static final int SYNC_FLUSH = 2; |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
145 |
|
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
146 |
/** |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
147 |
* Compression flush mode used to flush out all pending output and |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
148 |
* reset the deflater. Using this mode too often can seriously degrade |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
149 |
* compression. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
150 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
151 |
* @see Deflater#deflate(byte[], int, int, int) |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
152 |
* @since 1.7 |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
153 |
*/ |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
154 |
public static final int FULL_FLUSH = 3; |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
155 |
|
2 | 156 |
static { |
157 |
/* Zip library is loaded from System.initializeSystemClass */ |
|
158 |
initIDs(); |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* Creates a new compressor using the specified compression level. |
|
163 |
* If 'nowrap' is true then the ZLIB header and checksum fields will |
|
164 |
* not be used in order to support the compression format used in |
|
165 |
* both GZIP and PKZIP. |
|
166 |
* @param level the compression level (0-9) |
|
167 |
* @param nowrap if true then use GZIP compatible compression |
|
168 |
*/ |
|
169 |
public Deflater(int level, boolean nowrap) { |
|
170 |
this.level = level; |
|
171 |
this.strategy = DEFAULT_STRATEGY; |
|
5173 | 172 |
this.zsRef = new ZStreamRef(init(level, DEFAULT_STRATEGY, nowrap)); |
2 | 173 |
} |
174 |
||
175 |
/** |
|
176 |
* Creates a new compressor using the specified compression level. |
|
177 |
* Compressed data will be generated in ZLIB format. |
|
178 |
* @param level the compression level (0-9) |
|
179 |
*/ |
|
180 |
public Deflater(int level) { |
|
181 |
this(level, false); |
|
182 |
} |
|
183 |
||
184 |
/** |
|
185 |
* Creates a new compressor with the default compression level. |
|
186 |
* Compressed data will be generated in ZLIB format. |
|
187 |
*/ |
|
188 |
public Deflater() { |
|
189 |
this(DEFAULT_COMPRESSION, false); |
|
190 |
} |
|
191 |
||
192 |
/** |
|
193 |
* Sets input data for compression. This should be called whenever |
|
194 |
* needsInput() returns true indicating that more input data is required. |
|
195 |
* @param b the input data bytes |
|
196 |
* @param off the start offset of the data |
|
197 |
* @param len the length of the data |
|
198 |
* @see Deflater#needsInput |
|
199 |
*/ |
|
5173 | 200 |
public void setInput(byte[] b, int off, int len) { |
2 | 201 |
if (b== null) { |
202 |
throw new NullPointerException(); |
|
203 |
} |
|
204 |
if (off < 0 || len < 0 || off > b.length - len) { |
|
205 |
throw new ArrayIndexOutOfBoundsException(); |
|
206 |
} |
|
5173 | 207 |
synchronized (zsRef) { |
208 |
this.buf = b; |
|
209 |
this.off = off; |
|
210 |
this.len = len; |
|
211 |
} |
|
2 | 212 |
} |
213 |
||
214 |
/** |
|
215 |
* Sets input data for compression. This should be called whenever |
|
216 |
* needsInput() returns true indicating that more input data is required. |
|
217 |
* @param b the input data bytes |
|
218 |
* @see Deflater#needsInput |
|
219 |
*/ |
|
220 |
public void setInput(byte[] b) { |
|
221 |
setInput(b, 0, b.length); |
|
222 |
} |
|
223 |
||
224 |
/** |
|
225 |
* Sets preset dictionary for compression. A preset dictionary is used |
|
226 |
* when the history buffer can be predetermined. When the data is later |
|
227 |
* uncompressed with Inflater.inflate(), Inflater.getAdler() can be called |
|
228 |
* in order to get the Adler-32 value of the dictionary required for |
|
229 |
* decompression. |
|
230 |
* @param b the dictionary data bytes |
|
231 |
* @param off the start offset of the data |
|
232 |
* @param len the length of the data |
|
233 |
* @see Inflater#inflate |
|
234 |
* @see Inflater#getAdler |
|
235 |
*/ |
|
5173 | 236 |
public void setDictionary(byte[] b, int off, int len) { |
237 |
if (b == null) { |
|
2 | 238 |
throw new NullPointerException(); |
239 |
} |
|
240 |
if (off < 0 || len < 0 || off > b.length - len) { |
|
241 |
throw new ArrayIndexOutOfBoundsException(); |
|
242 |
} |
|
5173 | 243 |
synchronized (zsRef) { |
244 |
ensureOpen(); |
|
245 |
setDictionary(zsRef.address(), b, off, len); |
|
246 |
} |
|
2 | 247 |
} |
248 |
||
249 |
/** |
|
250 |
* Sets preset dictionary for compression. A preset dictionary is used |
|
251 |
* when the history buffer can be predetermined. When the data is later |
|
252 |
* uncompressed with Inflater.inflate(), Inflater.getAdler() can be called |
|
253 |
* in order to get the Adler-32 value of the dictionary required for |
|
254 |
* decompression. |
|
255 |
* @param b the dictionary data bytes |
|
256 |
* @see Inflater#inflate |
|
257 |
* @see Inflater#getAdler |
|
258 |
*/ |
|
259 |
public void setDictionary(byte[] b) { |
|
260 |
setDictionary(b, 0, b.length); |
|
261 |
} |
|
262 |
||
263 |
/** |
|
264 |
* Sets the compression strategy to the specified value. |
|
19865
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
265 |
* |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
266 |
* <p> If the compression strategy is changed, the next invocation |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
267 |
* of {@code deflate} will compress the input available so far with |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
268 |
* the old strategy (and may be flushed); the new strategy will take |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
269 |
* effect only after that invocation. |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
270 |
* |
2 | 271 |
* @param strategy the new compression strategy |
272 |
* @exception IllegalArgumentException if the compression strategy is |
|
273 |
* invalid |
|
274 |
*/ |
|
5173 | 275 |
public void setStrategy(int strategy) { |
2 | 276 |
switch (strategy) { |
277 |
case DEFAULT_STRATEGY: |
|
278 |
case FILTERED: |
|
279 |
case HUFFMAN_ONLY: |
|
280 |
break; |
|
281 |
default: |
|
282 |
throw new IllegalArgumentException(); |
|
283 |
} |
|
5173 | 284 |
synchronized (zsRef) { |
285 |
if (this.strategy != strategy) { |
|
286 |
this.strategy = strategy; |
|
287 |
setParams = true; |
|
288 |
} |
|
2 | 289 |
} |
290 |
} |
|
291 |
||
292 |
/** |
|
19865
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
293 |
* Sets the compression level to the specified value. |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
294 |
* |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
295 |
* <p> If the compression level is changed, the next invocation |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
296 |
* of {@code deflate} will compress the input available so far |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
297 |
* with the old level (and may be flushed); the new level will |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
298 |
* take effect only after that invocation. |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
299 |
* |
2 | 300 |
* @param level the new compression level (0-9) |
301 |
* @exception IllegalArgumentException if the compression level is invalid |
|
302 |
*/ |
|
5173 | 303 |
public void setLevel(int level) { |
2 | 304 |
if ((level < 0 || level > 9) && level != DEFAULT_COMPRESSION) { |
305 |
throw new IllegalArgumentException("invalid compression level"); |
|
306 |
} |
|
5173 | 307 |
synchronized (zsRef) { |
308 |
if (this.level != level) { |
|
309 |
this.level = level; |
|
310 |
setParams = true; |
|
311 |
} |
|
2 | 312 |
} |
313 |
} |
|
314 |
||
315 |
/** |
|
316 |
* Returns true if the input data buffer is empty and setInput() |
|
317 |
* should be called in order to provide more input. |
|
318 |
* @return true if the input data buffer is empty and setInput() |
|
319 |
* should be called in order to provide more input |
|
320 |
*/ |
|
321 |
public boolean needsInput() { |
|
32995
a62c89adce3d
8038502: Deflater.needsInput() should use synchronization
coffeys
parents:
32649
diff
changeset
|
322 |
synchronized (zsRef) { |
a62c89adce3d
8038502: Deflater.needsInput() should use synchronization
coffeys
parents:
32649
diff
changeset
|
323 |
return len <= 0; |
a62c89adce3d
8038502: Deflater.needsInput() should use synchronization
coffeys
parents:
32649
diff
changeset
|
324 |
} |
2 | 325 |
} |
326 |
||
327 |
/** |
|
328 |
* When called, indicates that compression should end with the current |
|
329 |
* contents of the input buffer. |
|
330 |
*/ |
|
5173 | 331 |
public void finish() { |
332 |
synchronized (zsRef) { |
|
333 |
finish = true; |
|
334 |
} |
|
2 | 335 |
} |
336 |
||
337 |
/** |
|
338 |
* Returns true if the end of the compressed data output stream has |
|
339 |
* been reached. |
|
340 |
* @return true if the end of the compressed data output stream has |
|
341 |
* been reached |
|
342 |
*/ |
|
5173 | 343 |
public boolean finished() { |
344 |
synchronized (zsRef) { |
|
345 |
return finished; |
|
346 |
} |
|
2 | 347 |
} |
348 |
||
349 |
/** |
|
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
350 |
* Compresses the input data and fills specified buffer with compressed |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
351 |
* data. Returns actual number of bytes of compressed data. A return value |
4354
3a70dde80b3b
6905029: Broken links in Deflater and DeflaterOutputStream javadoc
martin
parents:
4178
diff
changeset
|
352 |
* of 0 indicates that {@link #needsInput() needsInput} should be called |
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
353 |
* in order to determine if more input data is required. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
354 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
355 |
* <p>This method uses {@link #NO_FLUSH} as its compression flush mode. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
356 |
* An invocation of this method of the form {@code deflater.deflate(b, off, len)} |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
357 |
* yields the same result as the invocation of |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
358 |
* {@code deflater.deflate(b, off, len, Deflater.NO_FLUSH)}. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
359 |
* |
2 | 360 |
* @param b the buffer for the compressed data |
361 |
* @param off the start offset of the data |
|
362 |
* @param len the maximum number of bytes of compressed data |
|
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
363 |
* @return the actual number of bytes of compressed data written to the |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
364 |
* output buffer |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
365 |
*/ |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
366 |
public int deflate(byte[] b, int off, int len) { |
4178
61284fdd478f
6894950: test/java/util/zip/Bounds.java fails with OoutOfMemoryError
sherman
parents:
4162
diff
changeset
|
367 |
return deflate(b, off, len, NO_FLUSH); |
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
368 |
} |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
369 |
|
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
370 |
/** |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
371 |
* Compresses the input data and fills specified buffer with compressed |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
372 |
* data. Returns actual number of bytes of compressed data. A return value |
4354
3a70dde80b3b
6905029: Broken links in Deflater and DeflaterOutputStream javadoc
martin
parents:
4178
diff
changeset
|
373 |
* of 0 indicates that {@link #needsInput() needsInput} should be called |
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
374 |
* in order to determine if more input data is required. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
375 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
376 |
* <p>This method uses {@link #NO_FLUSH} as its compression flush mode. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
377 |
* An invocation of this method of the form {@code deflater.deflate(b)} |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
378 |
* yields the same result as the invocation of |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
379 |
* {@code deflater.deflate(b, 0, b.length, Deflater.NO_FLUSH)}. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
380 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
381 |
* @param b the buffer for the compressed data |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
382 |
* @return the actual number of bytes of compressed data written to the |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
383 |
* output buffer |
2 | 384 |
*/ |
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
385 |
public int deflate(byte[] b) { |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
386 |
return deflate(b, 0, b.length, NO_FLUSH); |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
387 |
} |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
388 |
|
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
389 |
/** |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
390 |
* Compresses the input data and fills the specified buffer with compressed |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
391 |
* data. Returns actual number of bytes of data compressed. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
392 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
393 |
* <p>Compression flush mode is one of the following three modes: |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
394 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
395 |
* <ul> |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
396 |
* <li>{@link #NO_FLUSH}: allows the deflater to decide how much data |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
397 |
* to accumulate, before producing output, in order to achieve the best |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
398 |
* compression (should be used in normal use scenario). A return value |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
399 |
* of 0 in this flush mode indicates that {@link #needsInput()} should |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
400 |
* be called in order to determine if more input data is required. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
401 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
402 |
* <li>{@link #SYNC_FLUSH}: all pending output in the deflater is flushed, |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
403 |
* to the specified output buffer, so that an inflater that works on |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
404 |
* compressed data can get all input data available so far (In particular |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
405 |
* the {@link #needsInput()} returns {@code true} after this invocation |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
406 |
* if enough output space is provided). Flushing with {@link #SYNC_FLUSH} |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
407 |
* may degrade compression for some compression algorithms and so it |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
408 |
* should be used only when necessary. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
409 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
410 |
* <li>{@link #FULL_FLUSH}: all pending output is flushed out as with |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
411 |
* {@link #SYNC_FLUSH}. The compression state is reset so that the inflater |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
412 |
* that works on the compressed output data can restart from this point |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
413 |
* if previous compressed data has been damaged or if random access is |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
414 |
* desired. Using {@link #FULL_FLUSH} too often can seriously degrade |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
415 |
* compression. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
416 |
* </ul> |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
417 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
418 |
* <p>In the case of {@link #FULL_FLUSH} or {@link #SYNC_FLUSH}, if |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
419 |
* the return value is {@code len}, the space available in output |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
420 |
* buffer {@code b}, this method should be invoked again with the same |
39497
7701abdf2fb4
8133170: Deflater.deflate with small output buffers fails
sherman
parents:
32995
diff
changeset
|
421 |
* {@code flush} parameter and more output space. Make sure that |
7701abdf2fb4
8133170: Deflater.deflate with small output buffers fails
sherman
parents:
32995
diff
changeset
|
422 |
* {@code len} is greater than 6 to avoid flush marker (5 bytes) being |
7701abdf2fb4
8133170: Deflater.deflate with small output buffers fails
sherman
parents:
32995
diff
changeset
|
423 |
* repeatedly output to the output buffer every time this method is |
7701abdf2fb4
8133170: Deflater.deflate with small output buffers fails
sherman
parents:
32995
diff
changeset
|
424 |
* invoked. |
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
425 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
426 |
* @param b the buffer for the compressed data |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
427 |
* @param off the start offset of the data |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
428 |
* @param len the maximum number of bytes of compressed data |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
429 |
* @param flush the compression flush mode |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
430 |
* @return the actual number of bytes of compressed data written to |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
431 |
* the output buffer |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
432 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
433 |
* @throws IllegalArgumentException if the flush mode is invalid |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
434 |
* @since 1.7 |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
435 |
*/ |
5173 | 436 |
public int deflate(byte[] b, int off, int len, int flush) { |
2 | 437 |
if (b == null) { |
438 |
throw new NullPointerException(); |
|
439 |
} |
|
440 |
if (off < 0 || len < 0 || off > b.length - len) { |
|
441 |
throw new ArrayIndexOutOfBoundsException(); |
|
442 |
} |
|
5173 | 443 |
synchronized (zsRef) { |
444 |
ensureOpen(); |
|
445 |
if (flush == NO_FLUSH || flush == SYNC_FLUSH || |
|
13409
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
446 |
flush == FULL_FLUSH) { |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
447 |
int thisLen = this.len; |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
448 |
int n = deflateBytes(zsRef.address(), b, off, len, flush); |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
449 |
bytesWritten += n; |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
450 |
bytesRead += (thisLen - this.len); |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
451 |
return n; |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
452 |
} |
5173 | 453 |
throw new IllegalArgumentException(); |
454 |
} |
|
2 | 455 |
} |
456 |
||
457 |
/** |
|
458 |
* Returns the ADLER-32 value of the uncompressed data. |
|
459 |
* @return the ADLER-32 value of the uncompressed data |
|
460 |
*/ |
|
5173 | 461 |
public int getAdler() { |
462 |
synchronized (zsRef) { |
|
463 |
ensureOpen(); |
|
464 |
return getAdler(zsRef.address()); |
|
465 |
} |
|
2 | 466 |
} |
467 |
||
468 |
/** |
|
469 |
* Returns the total number of uncompressed bytes input so far. |
|
470 |
* |
|
471 |
* <p>Since the number of bytes may be greater than |
|
472 |
* Integer.MAX_VALUE, the {@link #getBytesRead()} method is now |
|
473 |
* the preferred means of obtaining this information.</p> |
|
474 |
* |
|
475 |
* @return the total number of uncompressed bytes input so far |
|
476 |
*/ |
|
477 |
public int getTotalIn() { |
|
478 |
return (int) getBytesRead(); |
|
479 |
} |
|
480 |
||
481 |
/** |
|
18560 | 482 |
* Returns the total number of uncompressed bytes input so far. |
2 | 483 |
* |
484 |
* @return the total (non-negative) number of uncompressed bytes input so far |
|
485 |
* @since 1.5 |
|
486 |
*/ |
|
5173 | 487 |
public long getBytesRead() { |
488 |
synchronized (zsRef) { |
|
489 |
ensureOpen(); |
|
13409
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
490 |
return bytesRead; |
5173 | 491 |
} |
2 | 492 |
} |
493 |
||
494 |
/** |
|
495 |
* Returns the total number of compressed bytes output so far. |
|
496 |
* |
|
497 |
* <p>Since the number of bytes may be greater than |
|
498 |
* Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now |
|
499 |
* the preferred means of obtaining this information.</p> |
|
500 |
* |
|
501 |
* @return the total number of compressed bytes output so far |
|
502 |
*/ |
|
503 |
public int getTotalOut() { |
|
504 |
return (int) getBytesWritten(); |
|
505 |
} |
|
506 |
||
507 |
/** |
|
18560 | 508 |
* Returns the total number of compressed bytes output so far. |
2 | 509 |
* |
510 |
* @return the total (non-negative) number of compressed bytes output so far |
|
511 |
* @since 1.5 |
|
512 |
*/ |
|
5173 | 513 |
public long getBytesWritten() { |
514 |
synchronized (zsRef) { |
|
515 |
ensureOpen(); |
|
13409
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
516 |
return bytesWritten; |
5173 | 517 |
} |
2 | 518 |
} |
519 |
||
520 |
/** |
|
521 |
* Resets deflater so that a new set of input data can be processed. |
|
522 |
* Keeps current compression level and strategy settings. |
|
523 |
*/ |
|
5173 | 524 |
public void reset() { |
525 |
synchronized (zsRef) { |
|
526 |
ensureOpen(); |
|
527 |
reset(zsRef.address()); |
|
528 |
finish = false; |
|
529 |
finished = false; |
|
530 |
off = len = 0; |
|
13409
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
531 |
bytesRead = bytesWritten = 0; |
5173 | 532 |
} |
2 | 533 |
} |
534 |
||
535 |
/** |
|
536 |
* Closes the compressor and discards any unprocessed input. |
|
537 |
* This method should be called when the compressor is no longer |
|
538 |
* being used, but will also be called automatically by the |
|
539 |
* finalize() method. Once this method is called, the behavior |
|
540 |
* of the Deflater object is undefined. |
|
541 |
*/ |
|
5173 | 542 |
public void end() { |
543 |
synchronized (zsRef) { |
|
544 |
long addr = zsRef.address(); |
|
545 |
zsRef.clear(); |
|
546 |
if (addr != 0) { |
|
547 |
end(addr); |
|
548 |
buf = null; |
|
549 |
} |
|
2 | 550 |
} |
551 |
} |
|
552 |
||
553 |
/** |
|
554 |
* Closes the compressor when garbage is collected. |
|
44534 | 555 |
* |
556 |
* @deprecated The {@code finalize} method has been deprecated. |
|
557 |
* Subclasses that override {@code finalize} in order to perform cleanup |
|
558 |
* should be modified to use alternative cleanup mechanisms and |
|
559 |
* to remove the overriding {@code finalize} method. |
|
560 |
* When overriding the {@code finalize} method, its implementation must explicitly |
|
561 |
* ensure that {@code super.finalize()} is invoked as described in {@link Object#finalize}. |
|
562 |
* See the specification for {@link Object#finalize()} for further |
|
563 |
* information about migration options. |
|
2 | 564 |
*/ |
44534 | 565 |
@Deprecated(since="9") |
2 | 566 |
protected void finalize() { |
567 |
end(); |
|
568 |
} |
|
569 |
||
570 |
private void ensureOpen() { |
|
5173 | 571 |
assert Thread.holdsLock(zsRef); |
572 |
if (zsRef.address() == 0) |
|
573 |
throw new NullPointerException("Deflater has been closed"); |
|
2 | 574 |
} |
575 |
||
576 |
private static native void initIDs(); |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
32037
diff
changeset
|
577 |
private static native long init(int level, int strategy, boolean nowrap); |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
32037
diff
changeset
|
578 |
private static native void setDictionary(long addr, byte[] b, int off, int len); |
5173 | 579 |
private native int deflateBytes(long addr, byte[] b, int off, int len, |
580 |
int flush); |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
32037
diff
changeset
|
581 |
private static native int getAdler(long addr); |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
32037
diff
changeset
|
582 |
private static native void reset(long addr); |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
32037
diff
changeset
|
583 |
private static native void end(long addr); |
2 | 584 |
} |