author | mchung |
Fri, 22 May 2015 16:43:39 -0700 | |
changeset 30789 | 9eca83469588 |
parent 25859 | 3317bb8137f4 |
child 32037 | ab4526f4ac10 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
18560 | 2 |
* Copyright (c) 1996, 2013, 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 |
|
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 |
|
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 |
|
72 |
*/ |
|
73 |
public |
|
74 |
class Deflater { |
|
5173 | 75 |
|
76 |
private final ZStreamRef zsRef; |
|
2 | 77 |
private byte[] buf = new byte[0]; |
78 |
private int off, len; |
|
79 |
private int level, strategy; |
|
80 |
private boolean setParams; |
|
81 |
private boolean finish, finished; |
|
13409
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
82 |
private long bytesRead; |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
83 |
private long bytesWritten; |
2 | 84 |
|
85 |
/** |
|
86 |
* Compression method for the deflate algorithm (the only one currently |
|
87 |
* supported). |
|
88 |
*/ |
|
89 |
public static final int DEFLATED = 8; |
|
90 |
||
91 |
/** |
|
92 |
* Compression level for no compression. |
|
93 |
*/ |
|
94 |
public static final int NO_COMPRESSION = 0; |
|
95 |
||
96 |
/** |
|
97 |
* Compression level for fastest compression. |
|
98 |
*/ |
|
99 |
public static final int BEST_SPEED = 1; |
|
100 |
||
101 |
/** |
|
102 |
* Compression level for best compression. |
|
103 |
*/ |
|
104 |
public static final int BEST_COMPRESSION = 9; |
|
105 |
||
106 |
/** |
|
107 |
* Default compression level. |
|
108 |
*/ |
|
109 |
public static final int DEFAULT_COMPRESSION = -1; |
|
110 |
||
111 |
/** |
|
112 |
* Compression strategy best used for data consisting mostly of small |
|
113 |
* values with a somewhat random distribution. Forces more Huffman coding |
|
114 |
* and less string matching. |
|
115 |
*/ |
|
116 |
public static final int FILTERED = 1; |
|
117 |
||
118 |
/** |
|
119 |
* Compression strategy for Huffman coding only. |
|
120 |
*/ |
|
121 |
public static final int HUFFMAN_ONLY = 2; |
|
122 |
||
123 |
/** |
|
124 |
* Default compression strategy. |
|
125 |
*/ |
|
126 |
public static final int DEFAULT_STRATEGY = 0; |
|
127 |
||
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
128 |
/** |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
129 |
* 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
|
130 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
131 |
* @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
|
132 |
* @since 1.7 |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
133 |
*/ |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
134 |
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
|
135 |
|
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 |
* 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
|
138 |
* 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
|
139 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
140 |
* @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
|
141 |
* @since 1.7 |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
142 |
*/ |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
143 |
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
|
144 |
|
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 |
* 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
|
147 |
* 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
|
148 |
* compression. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
149 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
150 |
* @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
|
151 |
* @since 1.7 |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
152 |
*/ |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
153 |
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
|
154 |
|
2 | 155 |
static { |
156 |
/* Zip library is loaded from System.initializeSystemClass */ |
|
157 |
initIDs(); |
|
158 |
} |
|
159 |
||
160 |
/** |
|
161 |
* Creates a new compressor using the specified compression level. |
|
162 |
* If 'nowrap' is true then the ZLIB header and checksum fields will |
|
163 |
* not be used in order to support the compression format used in |
|
164 |
* both GZIP and PKZIP. |
|
165 |
* @param level the compression level (0-9) |
|
166 |
* @param nowrap if true then use GZIP compatible compression |
|
167 |
*/ |
|
168 |
public Deflater(int level, boolean nowrap) { |
|
169 |
this.level = level; |
|
170 |
this.strategy = DEFAULT_STRATEGY; |
|
5173 | 171 |
this.zsRef = new ZStreamRef(init(level, DEFAULT_STRATEGY, nowrap)); |
2 | 172 |
} |
173 |
||
174 |
/** |
|
175 |
* Creates a new compressor using the specified compression level. |
|
176 |
* Compressed data will be generated in ZLIB format. |
|
177 |
* @param level the compression level (0-9) |
|
178 |
*/ |
|
179 |
public Deflater(int level) { |
|
180 |
this(level, false); |
|
181 |
} |
|
182 |
||
183 |
/** |
|
184 |
* Creates a new compressor with the default compression level. |
|
185 |
* Compressed data will be generated in ZLIB format. |
|
186 |
*/ |
|
187 |
public Deflater() { |
|
188 |
this(DEFAULT_COMPRESSION, false); |
|
189 |
} |
|
190 |
||
191 |
/** |
|
192 |
* Sets input data for compression. This should be called whenever |
|
193 |
* needsInput() returns true indicating that more input data is required. |
|
194 |
* @param b the input data bytes |
|
195 |
* @param off the start offset of the data |
|
196 |
* @param len the length of the data |
|
197 |
* @see Deflater#needsInput |
|
198 |
*/ |
|
5173 | 199 |
public void setInput(byte[] b, int off, int len) { |
2 | 200 |
if (b== null) { |
201 |
throw new NullPointerException(); |
|
202 |
} |
|
203 |
if (off < 0 || len < 0 || off > b.length - len) { |
|
204 |
throw new ArrayIndexOutOfBoundsException(); |
|
205 |
} |
|
5173 | 206 |
synchronized (zsRef) { |
207 |
this.buf = b; |
|
208 |
this.off = off; |
|
209 |
this.len = len; |
|
210 |
} |
|
2 | 211 |
} |
212 |
||
213 |
/** |
|
214 |
* Sets input data for compression. This should be called whenever |
|
215 |
* needsInput() returns true indicating that more input data is required. |
|
216 |
* @param b the input data bytes |
|
217 |
* @see Deflater#needsInput |
|
218 |
*/ |
|
219 |
public void setInput(byte[] b) { |
|
220 |
setInput(b, 0, b.length); |
|
221 |
} |
|
222 |
||
223 |
/** |
|
224 |
* Sets preset dictionary for compression. A preset dictionary is used |
|
225 |
* when the history buffer can be predetermined. When the data is later |
|
226 |
* uncompressed with Inflater.inflate(), Inflater.getAdler() can be called |
|
227 |
* in order to get the Adler-32 value of the dictionary required for |
|
228 |
* decompression. |
|
229 |
* @param b the dictionary data bytes |
|
230 |
* @param off the start offset of the data |
|
231 |
* @param len the length of the data |
|
232 |
* @see Inflater#inflate |
|
233 |
* @see Inflater#getAdler |
|
234 |
*/ |
|
5173 | 235 |
public void setDictionary(byte[] b, int off, int len) { |
236 |
if (b == null) { |
|
2 | 237 |
throw new NullPointerException(); |
238 |
} |
|
239 |
if (off < 0 || len < 0 || off > b.length - len) { |
|
240 |
throw new ArrayIndexOutOfBoundsException(); |
|
241 |
} |
|
5173 | 242 |
synchronized (zsRef) { |
243 |
ensureOpen(); |
|
244 |
setDictionary(zsRef.address(), b, off, len); |
|
245 |
} |
|
2 | 246 |
} |
247 |
||
248 |
/** |
|
249 |
* Sets preset dictionary for compression. A preset dictionary is used |
|
250 |
* when the history buffer can be predetermined. When the data is later |
|
251 |
* uncompressed with Inflater.inflate(), Inflater.getAdler() can be called |
|
252 |
* in order to get the Adler-32 value of the dictionary required for |
|
253 |
* decompression. |
|
254 |
* @param b the dictionary data bytes |
|
255 |
* @see Inflater#inflate |
|
256 |
* @see Inflater#getAdler |
|
257 |
*/ |
|
258 |
public void setDictionary(byte[] b) { |
|
259 |
setDictionary(b, 0, b.length); |
|
260 |
} |
|
261 |
||
262 |
/** |
|
263 |
* Sets the compression strategy to the specified value. |
|
19865
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
264 |
* |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
265 |
* <p> If the compression strategy is changed, the next invocation |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
266 |
* 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
|
267 |
* 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
|
268 |
* effect only after that invocation. |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
269 |
* |
2 | 270 |
* @param strategy the new compression strategy |
271 |
* @exception IllegalArgumentException if the compression strategy is |
|
272 |
* invalid |
|
273 |
*/ |
|
5173 | 274 |
public void setStrategy(int strategy) { |
2 | 275 |
switch (strategy) { |
276 |
case DEFAULT_STRATEGY: |
|
277 |
case FILTERED: |
|
278 |
case HUFFMAN_ONLY: |
|
279 |
break; |
|
280 |
default: |
|
281 |
throw new IllegalArgumentException(); |
|
282 |
} |
|
5173 | 283 |
synchronized (zsRef) { |
284 |
if (this.strategy != strategy) { |
|
285 |
this.strategy = strategy; |
|
286 |
setParams = true; |
|
287 |
} |
|
2 | 288 |
} |
289 |
} |
|
290 |
||
291 |
/** |
|
19865
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
292 |
* Sets the compression level to the specified value. |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
293 |
* |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
294 |
* <p> If the compression level is changed, the next invocation |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
295 |
* of {@code deflate} will compress the input available so far |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
296 |
* 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
|
297 |
* take effect only after that invocation. |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
298 |
* |
2 | 299 |
* @param level the new compression level (0-9) |
300 |
* @exception IllegalArgumentException if the compression level is invalid |
|
301 |
*/ |
|
5173 | 302 |
public void setLevel(int level) { |
2 | 303 |
if ((level < 0 || level > 9) && level != DEFAULT_COMPRESSION) { |
304 |
throw new IllegalArgumentException("invalid compression level"); |
|
305 |
} |
|
5173 | 306 |
synchronized (zsRef) { |
307 |
if (this.level != level) { |
|
308 |
this.level = level; |
|
309 |
setParams = true; |
|
310 |
} |
|
2 | 311 |
} |
312 |
} |
|
313 |
||
314 |
/** |
|
315 |
* Returns true if the input data buffer is empty and setInput() |
|
316 |
* should be called in order to provide more input. |
|
317 |
* @return true if the input data buffer is empty and setInput() |
|
318 |
* should be called in order to provide more input |
|
319 |
*/ |
|
320 |
public boolean needsInput() { |
|
321 |
return len <= 0; |
|
322 |
} |
|
323 |
||
324 |
/** |
|
325 |
* When called, indicates that compression should end with the current |
|
326 |
* contents of the input buffer. |
|
327 |
*/ |
|
5173 | 328 |
public void finish() { |
329 |
synchronized (zsRef) { |
|
330 |
finish = true; |
|
331 |
} |
|
2 | 332 |
} |
333 |
||
334 |
/** |
|
335 |
* Returns true if the end of the compressed data output stream has |
|
336 |
* been reached. |
|
337 |
* @return true if the end of the compressed data output stream has |
|
338 |
* been reached |
|
339 |
*/ |
|
5173 | 340 |
public boolean finished() { |
341 |
synchronized (zsRef) { |
|
342 |
return finished; |
|
343 |
} |
|
2 | 344 |
} |
345 |
||
346 |
/** |
|
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
347 |
* 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
|
348 |
* 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
|
349 |
* 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
|
350 |
* 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
|
351 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
352 |
* <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
|
353 |
* 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
|
354 |
* 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
|
355 |
* {@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
|
356 |
* |
2 | 357 |
* @param b the buffer for the compressed data |
358 |
* @param off the start offset of the data |
|
359 |
* @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
|
360 |
* @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
|
361 |
* output buffer |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
362 |
*/ |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
363 |
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
|
364 |
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
|
365 |
} |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
366 |
|
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
367 |
/** |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
368 |
* 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
|
369 |
* 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
|
370 |
* 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
|
371 |
* 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
|
372 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
373 |
* <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
|
374 |
* 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
|
375 |
* 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
|
376 |
* {@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
|
377 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
378 |
* @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
|
379 |
* @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
|
380 |
* output buffer |
2 | 381 |
*/ |
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
382 |
public int deflate(byte[] b) { |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
383 |
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
|
384 |
} |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
385 |
|
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
386 |
/** |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
387 |
* 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
|
388 |
* 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
|
389 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
390 |
* <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
|
391 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
392 |
* <ul> |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
393 |
* <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
|
394 |
* 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
|
395 |
* 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
|
396 |
* 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
|
397 |
* 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
|
398 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
399 |
* <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
|
400 |
* 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
|
401 |
* 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
|
402 |
* 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
|
403 |
* 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
|
404 |
* 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
|
405 |
* 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
|
406 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
407 |
* <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
|
408 |
* {@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
|
409 |
* 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
|
410 |
* 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
|
411 |
* 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
|
412 |
* compression. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
413 |
* </ul> |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
414 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
415 |
* <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
|
416 |
* 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
|
417 |
* buffer {@code b}, this method should be invoked again with the same |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
418 |
* {@code flush} parameter and more output space. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
419 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
420 |
* @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
|
421 |
* @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
|
422 |
* @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
|
423 |
* @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
|
424 |
* @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
|
425 |
* the output buffer |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
426 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
427 |
* @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
|
428 |
* @since 1.7 |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
429 |
*/ |
5173 | 430 |
public int deflate(byte[] b, int off, int len, int flush) { |
2 | 431 |
if (b == null) { |
432 |
throw new NullPointerException(); |
|
433 |
} |
|
434 |
if (off < 0 || len < 0 || off > b.length - len) { |
|
435 |
throw new ArrayIndexOutOfBoundsException(); |
|
436 |
} |
|
5173 | 437 |
synchronized (zsRef) { |
438 |
ensureOpen(); |
|
439 |
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
|
440 |
flush == FULL_FLUSH) { |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
441 |
int thisLen = this.len; |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
442 |
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
|
443 |
bytesWritten += n; |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
444 |
bytesRead += (thisLen - this.len); |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
445 |
return n; |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
446 |
} |
5173 | 447 |
throw new IllegalArgumentException(); |
448 |
} |
|
2 | 449 |
} |
450 |
||
451 |
/** |
|
452 |
* Returns the ADLER-32 value of the uncompressed data. |
|
453 |
* @return the ADLER-32 value of the uncompressed data |
|
454 |
*/ |
|
5173 | 455 |
public int getAdler() { |
456 |
synchronized (zsRef) { |
|
457 |
ensureOpen(); |
|
458 |
return getAdler(zsRef.address()); |
|
459 |
} |
|
2 | 460 |
} |
461 |
||
462 |
/** |
|
463 |
* Returns the total number of uncompressed bytes input so far. |
|
464 |
* |
|
465 |
* <p>Since the number of bytes may be greater than |
|
466 |
* Integer.MAX_VALUE, the {@link #getBytesRead()} method is now |
|
467 |
* the preferred means of obtaining this information.</p> |
|
468 |
* |
|
469 |
* @return the total number of uncompressed bytes input so far |
|
470 |
*/ |
|
471 |
public int getTotalIn() { |
|
472 |
return (int) getBytesRead(); |
|
473 |
} |
|
474 |
||
475 |
/** |
|
18560 | 476 |
* Returns the total number of uncompressed bytes input so far. |
2 | 477 |
* |
478 |
* @return the total (non-negative) number of uncompressed bytes input so far |
|
479 |
* @since 1.5 |
|
480 |
*/ |
|
5173 | 481 |
public long getBytesRead() { |
482 |
synchronized (zsRef) { |
|
483 |
ensureOpen(); |
|
13409
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
484 |
return bytesRead; |
5173 | 485 |
} |
2 | 486 |
} |
487 |
||
488 |
/** |
|
489 |
* Returns the total number of compressed bytes output so far. |
|
490 |
* |
|
491 |
* <p>Since the number of bytes may be greater than |
|
492 |
* Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now |
|
493 |
* the preferred means of obtaining this information.</p> |
|
494 |
* |
|
495 |
* @return the total number of compressed bytes output so far |
|
496 |
*/ |
|
497 |
public int getTotalOut() { |
|
498 |
return (int) getBytesWritten(); |
|
499 |
} |
|
500 |
||
501 |
/** |
|
18560 | 502 |
* Returns the total number of compressed bytes output so far. |
2 | 503 |
* |
504 |
* @return the total (non-negative) number of compressed bytes output so far |
|
505 |
* @since 1.5 |
|
506 |
*/ |
|
5173 | 507 |
public long getBytesWritten() { |
508 |
synchronized (zsRef) { |
|
509 |
ensureOpen(); |
|
13409
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
510 |
return bytesWritten; |
5173 | 511 |
} |
2 | 512 |
} |
513 |
||
514 |
/** |
|
515 |
* Resets deflater so that a new set of input data can be processed. |
|
516 |
* Keeps current compression level and strategy settings. |
|
517 |
*/ |
|
5173 | 518 |
public void reset() { |
519 |
synchronized (zsRef) { |
|
520 |
ensureOpen(); |
|
521 |
reset(zsRef.address()); |
|
522 |
finish = false; |
|
523 |
finished = false; |
|
524 |
off = len = 0; |
|
13409
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
525 |
bytesRead = bytesWritten = 0; |
5173 | 526 |
} |
2 | 527 |
} |
528 |
||
529 |
/** |
|
530 |
* Closes the compressor and discards any unprocessed input. |
|
531 |
* This method should be called when the compressor is no longer |
|
532 |
* being used, but will also be called automatically by the |
|
533 |
* finalize() method. Once this method is called, the behavior |
|
534 |
* of the Deflater object is undefined. |
|
535 |
*/ |
|
5173 | 536 |
public void end() { |
537 |
synchronized (zsRef) { |
|
538 |
long addr = zsRef.address(); |
|
539 |
zsRef.clear(); |
|
540 |
if (addr != 0) { |
|
541 |
end(addr); |
|
542 |
buf = null; |
|
543 |
} |
|
2 | 544 |
} |
545 |
} |
|
546 |
||
547 |
/** |
|
548 |
* Closes the compressor when garbage is collected. |
|
549 |
*/ |
|
550 |
protected void finalize() { |
|
551 |
end(); |
|
552 |
} |
|
553 |
||
554 |
private void ensureOpen() { |
|
5173 | 555 |
assert Thread.holdsLock(zsRef); |
556 |
if (zsRef.address() == 0) |
|
557 |
throw new NullPointerException("Deflater has been closed"); |
|
2 | 558 |
} |
559 |
||
560 |
private static native void initIDs(); |
|
561 |
private native static long init(int level, int strategy, boolean nowrap); |
|
5173 | 562 |
private native static void setDictionary(long addr, byte[] b, int off, int len); |
563 |
private native int deflateBytes(long addr, byte[] b, int off, int len, |
|
564 |
int flush); |
|
565 |
private native static int getAdler(long addr); |
|
566 |
private native static void reset(long addr); |
|
567 |
private native static void end(long addr); |
|
2 | 568 |
} |