author | redestad |
Thu, 14 Dec 2017 16:05:08 +0100 | |
changeset 48337 | 0ee20aad71c4 |
parent 48238 | 9f225d4387e2 |
child 49834 | 99644c75eaed |
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 |
||
48337
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
28 |
import java.lang.ref.Cleaner.Cleanable; |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
29 |
import jdk.internal.ref.CleanerFactory; |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
30 |
|
2 | 31 |
/** |
32 |
* This class provides support for general purpose compression using the |
|
33 |
* popular ZLIB compression library. The ZLIB compression library was |
|
34 |
* initially developed as part of the PNG graphics standard and is not |
|
35 |
* protected by patents. It is fully described in the specifications at |
|
47004 | 36 |
* the <a href="package-summary.html#package.description">java.util.zip |
2 | 37 |
* package description</a>. |
38 |
* |
|
39 |
* <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
|
40 |
* 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
|
41 |
* {@code Inflater}. |
2 | 42 |
* |
43 |
* <blockquote><pre> |
|
44 |
* try { |
|
45 |
* // Encode a String into bytes |
|
5469 | 46 |
* String inputString = "blahblahblah"; |
2 | 47 |
* byte[] input = inputString.getBytes("UTF-8"); |
48 |
* |
|
49 |
* // Compress the bytes |
|
50 |
* byte[] output = new byte[100]; |
|
51 |
* Deflater compresser = new Deflater(); |
|
52 |
* compresser.setInput(input); |
|
53 |
* compresser.finish(); |
|
54 |
* int compressedDataLength = compresser.deflate(output); |
|
2940 | 55 |
* compresser.end(); |
2 | 56 |
* |
57 |
* // Decompress the bytes |
|
58 |
* Inflater decompresser = new Inflater(); |
|
59 |
* decompresser.setInput(output, 0, compressedDataLength); |
|
60 |
* byte[] result = new byte[100]; |
|
61 |
* int resultLength = decompresser.inflate(result); |
|
62 |
* decompresser.end(); |
|
63 |
* |
|
64 |
* // Decode the bytes into a String |
|
65 |
* String outputString = new String(result, 0, resultLength, "UTF-8"); |
|
66 |
* } catch(java.io.UnsupportedEncodingException ex) { |
|
67 |
* // handle |
|
68 |
* } catch (java.util.zip.DataFormatException ex) { |
|
69 |
* // handle |
|
70 |
* } |
|
71 |
* </pre></blockquote> |
|
72 |
* |
|
48238
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
73 |
* @apiNote |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
74 |
* To release resources used by this {@code Deflater}, the {@link #end()} method |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
75 |
* should be called explicitly. Subclasses are responsible for the cleanup of resources |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
76 |
* acquired by the subclass. Subclasses that override {@link #finalize()} in order |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
77 |
* to perform cleanup should be modified to use alternative cleanup mechanisms such |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
78 |
* as {@link java.lang.ref.Cleaner} and remove the overriding {@code finalize} method. |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
79 |
* |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
80 |
* @implSpec |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
81 |
* If this {@code Deflater} has been subclassed and the {@code end} method has been |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
82 |
* overridden, the {@code end} method will be called by the finalization when the |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
83 |
* deflater is unreachable. But the subclasses should not depend on this specific |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
84 |
* implementation; the finalization is not reliable and the {@code finalize} method |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
85 |
* is deprecated to be removed. |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
86 |
* |
2 | 87 |
* @see Inflater |
88 |
* @author David Connelly |
|
45434
4582657c7260
8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents:
44534
diff
changeset
|
89 |
* @since 1.1 |
2 | 90 |
*/ |
48238
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
91 |
|
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
92 |
public class Deflater { |
5173 | 93 |
|
48337
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
94 |
private final DeflaterZStreamRef zsRef; |
2 | 95 |
private byte[] buf = new byte[0]; |
96 |
private int off, len; |
|
97 |
private int level, strategy; |
|
98 |
private boolean setParams; |
|
99 |
private boolean finish, finished; |
|
13409
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
100 |
private long bytesRead; |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
101 |
private long bytesWritten; |
2 | 102 |
|
103 |
/** |
|
104 |
* Compression method for the deflate algorithm (the only one currently |
|
105 |
* supported). |
|
106 |
*/ |
|
107 |
public static final int DEFLATED = 8; |
|
108 |
||
109 |
/** |
|
110 |
* Compression level for no compression. |
|
111 |
*/ |
|
112 |
public static final int NO_COMPRESSION = 0; |
|
113 |
||
114 |
/** |
|
115 |
* Compression level for fastest compression. |
|
116 |
*/ |
|
117 |
public static final int BEST_SPEED = 1; |
|
118 |
||
119 |
/** |
|
120 |
* Compression level for best compression. |
|
121 |
*/ |
|
122 |
public static final int BEST_COMPRESSION = 9; |
|
123 |
||
124 |
/** |
|
125 |
* Default compression level. |
|
126 |
*/ |
|
127 |
public static final int DEFAULT_COMPRESSION = -1; |
|
128 |
||
129 |
/** |
|
130 |
* Compression strategy best used for data consisting mostly of small |
|
131 |
* values with a somewhat random distribution. Forces more Huffman coding |
|
132 |
* and less string matching. |
|
133 |
*/ |
|
134 |
public static final int FILTERED = 1; |
|
135 |
||
136 |
/** |
|
137 |
* Compression strategy for Huffman coding only. |
|
138 |
*/ |
|
139 |
public static final int HUFFMAN_ONLY = 2; |
|
140 |
||
141 |
/** |
|
142 |
* Default compression strategy. |
|
143 |
*/ |
|
144 |
public static final int DEFAULT_STRATEGY = 0; |
|
145 |
||
4162
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 achieve best compression result. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
148 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
149 |
* @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
|
150 |
* @since 1.7 |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
151 |
*/ |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
152 |
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
|
153 |
|
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
154 |
/** |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
155 |
* 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
|
156 |
* 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
|
157 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
158 |
* @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
|
159 |
* @since 1.7 |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
160 |
*/ |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
161 |
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
|
162 |
|
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
163 |
/** |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
164 |
* 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
|
165 |
* 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
|
166 |
* compression. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
167 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
168 |
* @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
|
169 |
* @since 1.7 |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
170 |
*/ |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
171 |
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
|
172 |
|
2 | 173 |
static { |
45924
8bbd04c0791e
8184917: System.initPhase1 does not need to pre-load libzip
alanb
parents:
45434
diff
changeset
|
174 |
ZipUtils.loadLibrary(); |
2 | 175 |
initIDs(); |
176 |
} |
|
177 |
||
178 |
/** |
|
179 |
* Creates a new compressor using the specified compression level. |
|
180 |
* If 'nowrap' is true then the ZLIB header and checksum fields will |
|
181 |
* not be used in order to support the compression format used in |
|
182 |
* both GZIP and PKZIP. |
|
183 |
* @param level the compression level (0-9) |
|
184 |
* @param nowrap if true then use GZIP compatible compression |
|
185 |
*/ |
|
186 |
public Deflater(int level, boolean nowrap) { |
|
187 |
this.level = level; |
|
188 |
this.strategy = DEFAULT_STRATEGY; |
|
48337
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
189 |
this.zsRef = DeflaterZStreamRef.get(this, |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
190 |
init(level, DEFAULT_STRATEGY, nowrap)); |
2 | 191 |
} |
192 |
||
193 |
/** |
|
194 |
* Creates a new compressor using the specified compression level. |
|
195 |
* Compressed data will be generated in ZLIB format. |
|
196 |
* @param level the compression level (0-9) |
|
197 |
*/ |
|
198 |
public Deflater(int level) { |
|
199 |
this(level, false); |
|
200 |
} |
|
201 |
||
202 |
/** |
|
203 |
* Creates a new compressor with the default compression level. |
|
204 |
* Compressed data will be generated in ZLIB format. |
|
205 |
*/ |
|
206 |
public Deflater() { |
|
207 |
this(DEFAULT_COMPRESSION, false); |
|
208 |
} |
|
209 |
||
210 |
/** |
|
211 |
* Sets input data for compression. This should be called whenever |
|
212 |
* needsInput() returns true indicating that more input data is required. |
|
213 |
* @param b the input data bytes |
|
214 |
* @param off the start offset of the data |
|
215 |
* @param len the length of the data |
|
216 |
* @see Deflater#needsInput |
|
217 |
*/ |
|
5173 | 218 |
public void setInput(byte[] b, int off, int len) { |
2 | 219 |
if (b== null) { |
220 |
throw new NullPointerException(); |
|
221 |
} |
|
222 |
if (off < 0 || len < 0 || off > b.length - len) { |
|
223 |
throw new ArrayIndexOutOfBoundsException(); |
|
224 |
} |
|
5173 | 225 |
synchronized (zsRef) { |
226 |
this.buf = b; |
|
227 |
this.off = off; |
|
228 |
this.len = len; |
|
229 |
} |
|
2 | 230 |
} |
231 |
||
232 |
/** |
|
233 |
* Sets input data for compression. This should be called whenever |
|
234 |
* needsInput() returns true indicating that more input data is required. |
|
235 |
* @param b the input data bytes |
|
236 |
* @see Deflater#needsInput |
|
237 |
*/ |
|
238 |
public void setInput(byte[] b) { |
|
239 |
setInput(b, 0, b.length); |
|
240 |
} |
|
241 |
||
242 |
/** |
|
243 |
* Sets preset dictionary for compression. A preset dictionary is used |
|
244 |
* when the history buffer can be predetermined. When the data is later |
|
245 |
* uncompressed with Inflater.inflate(), Inflater.getAdler() can be called |
|
246 |
* in order to get the Adler-32 value of the dictionary required for |
|
247 |
* decompression. |
|
248 |
* @param b the dictionary data bytes |
|
249 |
* @param off the start offset of the data |
|
250 |
* @param len the length of the data |
|
251 |
* @see Inflater#inflate |
|
252 |
* @see Inflater#getAdler |
|
253 |
*/ |
|
5173 | 254 |
public void setDictionary(byte[] b, int off, int len) { |
255 |
if (b == null) { |
|
2 | 256 |
throw new NullPointerException(); |
257 |
} |
|
258 |
if (off < 0 || len < 0 || off > b.length - len) { |
|
259 |
throw new ArrayIndexOutOfBoundsException(); |
|
260 |
} |
|
5173 | 261 |
synchronized (zsRef) { |
262 |
ensureOpen(); |
|
263 |
setDictionary(zsRef.address(), b, off, len); |
|
264 |
} |
|
2 | 265 |
} |
266 |
||
267 |
/** |
|
268 |
* Sets preset dictionary for compression. A preset dictionary is used |
|
269 |
* when the history buffer can be predetermined. When the data is later |
|
270 |
* uncompressed with Inflater.inflate(), Inflater.getAdler() can be called |
|
271 |
* in order to get the Adler-32 value of the dictionary required for |
|
272 |
* decompression. |
|
273 |
* @param b the dictionary data bytes |
|
274 |
* @see Inflater#inflate |
|
275 |
* @see Inflater#getAdler |
|
276 |
*/ |
|
277 |
public void setDictionary(byte[] b) { |
|
278 |
setDictionary(b, 0, b.length); |
|
279 |
} |
|
280 |
||
281 |
/** |
|
282 |
* Sets the compression strategy to the specified value. |
|
19865
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
283 |
* |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
284 |
* <p> If the compression strategy is changed, the next invocation |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
285 |
* 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
|
286 |
* 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
|
287 |
* effect only after that invocation. |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
288 |
* |
2 | 289 |
* @param strategy the new compression strategy |
290 |
* @exception IllegalArgumentException if the compression strategy is |
|
291 |
* invalid |
|
292 |
*/ |
|
5173 | 293 |
public void setStrategy(int strategy) { |
2 | 294 |
switch (strategy) { |
295 |
case DEFAULT_STRATEGY: |
|
296 |
case FILTERED: |
|
297 |
case HUFFMAN_ONLY: |
|
298 |
break; |
|
299 |
default: |
|
300 |
throw new IllegalArgumentException(); |
|
301 |
} |
|
5173 | 302 |
synchronized (zsRef) { |
303 |
if (this.strategy != strategy) { |
|
304 |
this.strategy = strategy; |
|
305 |
setParams = true; |
|
306 |
} |
|
2 | 307 |
} |
308 |
} |
|
309 |
||
310 |
/** |
|
19865
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
311 |
* Sets the compression level to the specified value. |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
312 |
* |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
313 |
* <p> If the compression level is changed, the next invocation |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
314 |
* of {@code deflate} will compress the input available so far |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
315 |
* 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
|
316 |
* take effect only after that invocation. |
b68185846a71
8020687: Deflater.setLevel does not work as expected
sherman
parents:
18560
diff
changeset
|
317 |
* |
2 | 318 |
* @param level the new compression level (0-9) |
319 |
* @exception IllegalArgumentException if the compression level is invalid |
|
320 |
*/ |
|
5173 | 321 |
public void setLevel(int level) { |
2 | 322 |
if ((level < 0 || level > 9) && level != DEFAULT_COMPRESSION) { |
323 |
throw new IllegalArgumentException("invalid compression level"); |
|
324 |
} |
|
5173 | 325 |
synchronized (zsRef) { |
326 |
if (this.level != level) { |
|
327 |
this.level = level; |
|
328 |
setParams = true; |
|
329 |
} |
|
2 | 330 |
} |
331 |
} |
|
332 |
||
333 |
/** |
|
334 |
* Returns true if the input data buffer is empty and setInput() |
|
335 |
* should be called in order to provide more input. |
|
336 |
* @return true if the input data buffer is empty and setInput() |
|
337 |
* should be called in order to provide more input |
|
338 |
*/ |
|
339 |
public boolean needsInput() { |
|
32995
a62c89adce3d
8038502: Deflater.needsInput() should use synchronization
coffeys
parents:
32649
diff
changeset
|
340 |
synchronized (zsRef) { |
a62c89adce3d
8038502: Deflater.needsInput() should use synchronization
coffeys
parents:
32649
diff
changeset
|
341 |
return len <= 0; |
a62c89adce3d
8038502: Deflater.needsInput() should use synchronization
coffeys
parents:
32649
diff
changeset
|
342 |
} |
2 | 343 |
} |
344 |
||
345 |
/** |
|
346 |
* When called, indicates that compression should end with the current |
|
347 |
* contents of the input buffer. |
|
348 |
*/ |
|
5173 | 349 |
public void finish() { |
350 |
synchronized (zsRef) { |
|
351 |
finish = true; |
|
352 |
} |
|
2 | 353 |
} |
354 |
||
355 |
/** |
|
356 |
* Returns true if the end of the compressed data output stream has |
|
357 |
* been reached. |
|
358 |
* @return true if the end of the compressed data output stream has |
|
359 |
* been reached |
|
360 |
*/ |
|
5173 | 361 |
public boolean finished() { |
362 |
synchronized (zsRef) { |
|
363 |
return finished; |
|
364 |
} |
|
2 | 365 |
} |
366 |
||
367 |
/** |
|
4162
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, off, len)} |
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, off, len, Deflater.NO_FLUSH)}. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
377 |
* |
2 | 378 |
* @param b the buffer for the compressed data |
379 |
* @param off the start offset of the data |
|
380 |
* @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
|
381 |
* @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
|
382 |
* output buffer |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
383 |
*/ |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
384 |
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
|
385 |
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
|
386 |
} |
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 |
* 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
|
390 |
* 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
|
391 |
* 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
|
392 |
* 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
|
393 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
394 |
* <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
|
395 |
* 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
|
396 |
* 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
|
397 |
* {@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
|
398 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
399 |
* @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
|
400 |
* @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
|
401 |
* output buffer |
2 | 402 |
*/ |
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
403 |
public int deflate(byte[] b) { |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
404 |
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
|
405 |
} |
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 |
/** |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
408 |
* 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
|
409 |
* 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
|
410 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
411 |
* <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
|
412 |
* |
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 |
* <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
|
415 |
* 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
|
416 |
* 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
|
417 |
* 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
|
418 |
* 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
|
419 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
420 |
* <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
|
421 |
* 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
|
422 |
* 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
|
423 |
* 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
|
424 |
* 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
|
425 |
* 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
|
426 |
* 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
|
427 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
428 |
* <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
|
429 |
* {@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
|
430 |
* 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
|
431 |
* 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
|
432 |
* 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
|
433 |
* compression. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
434 |
* </ul> |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
435 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
436 |
* <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
|
437 |
* 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
|
438 |
* 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
|
439 |
* {@code flush} parameter and more output space. Make sure that |
7701abdf2fb4
8133170: Deflater.deflate with small output buffers fails
sherman
parents:
32995
diff
changeset
|
440 |
* {@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
|
441 |
* 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
|
442 |
* invoked. |
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
443 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
444 |
* @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
|
445 |
* @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
|
446 |
* @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
|
447 |
* @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
|
448 |
* @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
|
449 |
* the output buffer |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
450 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
451 |
* @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
|
452 |
* @since 1.7 |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2940
diff
changeset
|
453 |
*/ |
5173 | 454 |
public int deflate(byte[] b, int off, int len, int flush) { |
2 | 455 |
if (b == null) { |
456 |
throw new NullPointerException(); |
|
457 |
} |
|
458 |
if (off < 0 || len < 0 || off > b.length - len) { |
|
459 |
throw new ArrayIndexOutOfBoundsException(); |
|
460 |
} |
|
5173 | 461 |
synchronized (zsRef) { |
462 |
ensureOpen(); |
|
463 |
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
|
464 |
flush == FULL_FLUSH) { |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
465 |
int thisLen = this.len; |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
466 |
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
|
467 |
bytesWritten += n; |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
468 |
bytesRead += (thisLen - this.len); |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
469 |
return n; |
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
470 |
} |
5173 | 471 |
throw new IllegalArgumentException(); |
472 |
} |
|
2 | 473 |
} |
474 |
||
475 |
/** |
|
476 |
* Returns the ADLER-32 value of the uncompressed data. |
|
477 |
* @return the ADLER-32 value of the uncompressed data |
|
478 |
*/ |
|
5173 | 479 |
public int getAdler() { |
480 |
synchronized (zsRef) { |
|
481 |
ensureOpen(); |
|
482 |
return getAdler(zsRef.address()); |
|
483 |
} |
|
2 | 484 |
} |
485 |
||
486 |
/** |
|
487 |
* Returns the total number of uncompressed bytes input so far. |
|
488 |
* |
|
489 |
* <p>Since the number of bytes may be greater than |
|
490 |
* Integer.MAX_VALUE, the {@link #getBytesRead()} method is now |
|
491 |
* the preferred means of obtaining this information.</p> |
|
492 |
* |
|
493 |
* @return the total number of uncompressed bytes input so far |
|
494 |
*/ |
|
495 |
public int getTotalIn() { |
|
496 |
return (int) getBytesRead(); |
|
497 |
} |
|
498 |
||
499 |
/** |
|
18560 | 500 |
* Returns the total number of uncompressed bytes input so far. |
2 | 501 |
* |
502 |
* @return the total (non-negative) number of uncompressed bytes input so far |
|
503 |
* @since 1.5 |
|
504 |
*/ |
|
5173 | 505 |
public long getBytesRead() { |
506 |
synchronized (zsRef) { |
|
507 |
ensureOpen(); |
|
13409
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
508 |
return bytesRead; |
5173 | 509 |
} |
2 | 510 |
} |
511 |
||
512 |
/** |
|
513 |
* Returns the total number of compressed bytes output so far. |
|
514 |
* |
|
515 |
* <p>Since the number of bytes may be greater than |
|
516 |
* Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now |
|
517 |
* the preferred means of obtaining this information.</p> |
|
518 |
* |
|
519 |
* @return the total number of compressed bytes output so far |
|
520 |
*/ |
|
521 |
public int getTotalOut() { |
|
522 |
return (int) getBytesWritten(); |
|
523 |
} |
|
524 |
||
525 |
/** |
|
18560 | 526 |
* Returns the total number of compressed bytes output so far. |
2 | 527 |
* |
528 |
* @return the total (non-negative) number of compressed bytes output so far |
|
529 |
* @since 1.5 |
|
530 |
*/ |
|
5173 | 531 |
public long getBytesWritten() { |
532 |
synchronized (zsRef) { |
|
533 |
ensureOpen(); |
|
13409
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
534 |
return bytesWritten; |
5173 | 535 |
} |
2 | 536 |
} |
537 |
||
538 |
/** |
|
539 |
* Resets deflater so that a new set of input data can be processed. |
|
540 |
* Keeps current compression level and strategy settings. |
|
541 |
*/ |
|
5173 | 542 |
public void reset() { |
543 |
synchronized (zsRef) { |
|
544 |
ensureOpen(); |
|
545 |
reset(zsRef.address()); |
|
546 |
finish = false; |
|
547 |
finished = false; |
|
548 |
off = len = 0; |
|
13409
fe3ab27ef1a6
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents:
7668
diff
changeset
|
549 |
bytesRead = bytesWritten = 0; |
5173 | 550 |
} |
2 | 551 |
} |
552 |
||
553 |
/** |
|
554 |
* Closes the compressor and discards any unprocessed input. |
|
48238
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
555 |
* |
2 | 556 |
* This method should be called when the compressor is no longer |
48238
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
557 |
* being used. Once this method is called, the behavior of the |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
558 |
* Deflater object is undefined. |
2 | 559 |
*/ |
5173 | 560 |
public void end() { |
561 |
synchronized (zsRef) { |
|
48238
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
562 |
zsRef.clean(); |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
563 |
buf = null; |
2 | 564 |
} |
565 |
} |
|
566 |
||
567 |
/** |
|
568 |
* Closes the compressor when garbage is collected. |
|
44534 | 569 |
* |
48238
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
570 |
* @deprecated The {@code finalize} method has been deprecated and will be |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
571 |
* removed. It is implemented as a no-op. Subclasses that override |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
572 |
* {@code finalize} in order to perform cleanup should be modified to use |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
573 |
* alternative cleanup mechanisms and to remove the overriding {@code finalize} |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
574 |
* method. The recommended cleanup for compressor is to explicitly call |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
575 |
* {@code end} method when it is no longer in use. If the {@code end} is |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
576 |
* not invoked explicitly the resource of the compressor will be released |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
577 |
* when the instance becomes unreachable. |
2 | 578 |
*/ |
48238
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
579 |
@Deprecated(since="9", forRemoval=true) |
9f225d4387e2
8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents:
47216
diff
changeset
|
580 |
protected void finalize() {} |
2 | 581 |
|
582 |
private void ensureOpen() { |
|
5173 | 583 |
assert Thread.holdsLock(zsRef); |
584 |
if (zsRef.address() == 0) |
|
585 |
throw new NullPointerException("Deflater has been closed"); |
|
2 | 586 |
} |
587 |
||
588 |
private static native void initIDs(); |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
32037
diff
changeset
|
589 |
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
|
590 |
private static native void setDictionary(long addr, byte[] b, int off, int len); |
5173 | 591 |
private native int deflateBytes(long addr, byte[] b, int off, int len, |
592 |
int flush); |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
32037
diff
changeset
|
593 |
private static native int getAdler(long addr); |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
32037
diff
changeset
|
594 |
private static native void reset(long addr); |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
32037
diff
changeset
|
595 |
private static native void end(long addr); |
48337
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
596 |
|
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
597 |
/** |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
598 |
* A reference to the native zlib's z_stream structure. It also |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
599 |
* serves as the "cleaner" to clean up the native resource when |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
600 |
* the Deflater is ended, closed or cleaned. |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
601 |
*/ |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
602 |
static class DeflaterZStreamRef implements Runnable { |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
603 |
|
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
604 |
private long address; |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
605 |
private final Cleanable cleanable; |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
606 |
|
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
607 |
private DeflaterZStreamRef(Deflater owner, long addr) { |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
608 |
this.cleanable = (owner != null) ? CleanerFactory.cleaner().register(owner, this) : null; |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
609 |
this.address = addr; |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
610 |
} |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
611 |
|
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
612 |
long address() { |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
613 |
return address; |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
614 |
} |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
615 |
|
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
616 |
void clean() { |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
617 |
cleanable.clean(); |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
618 |
} |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
619 |
|
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
620 |
public synchronized void run() { |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
621 |
long addr = address; |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
622 |
address = 0; |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
623 |
if (addr != 0) { |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
624 |
end(addr); |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
625 |
} |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
626 |
} |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
627 |
|
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
628 |
/* |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
629 |
* If {@code Deflater} has been subclassed and the {@code end} method is |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
630 |
* overridden, uses {@code finalizer} mechanism for resource cleanup. So |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
631 |
* {@code end} method can be called when the {@code Deflater} is unreachable. |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
632 |
* This mechanism will be removed when the {@code finalize} method is |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
633 |
* removed from {@code Deflater}. |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
634 |
*/ |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
635 |
static DeflaterZStreamRef get(Deflater owner, long addr) { |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
636 |
Class<?> clz = owner.getClass(); |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
637 |
while (clz != Deflater.class) { |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
638 |
try { |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
639 |
clz.getDeclaredMethod("end"); |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
640 |
return new FinalizableZStreamRef(owner, addr); |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
641 |
} catch (NoSuchMethodException nsme) {} |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
642 |
clz = clz.getSuperclass(); |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
643 |
} |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
644 |
return new DeflaterZStreamRef(owner, addr); |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
645 |
} |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
646 |
|
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
647 |
private static class FinalizableZStreamRef extends DeflaterZStreamRef { |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
648 |
final Deflater owner; |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
649 |
|
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
650 |
FinalizableZStreamRef (Deflater owner, long addr) { |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
651 |
super(null, addr); |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
652 |
this.owner = owner; |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
653 |
} |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
654 |
|
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
655 |
@Override |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
656 |
void clean() { |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
657 |
run(); |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
658 |
} |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
659 |
|
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
660 |
@Override |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
661 |
@SuppressWarnings("deprecation") |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
662 |
protected void finalize() { |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
663 |
owner.end(); |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
664 |
} |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
665 |
} |
0ee20aad71c4
8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents:
48238
diff
changeset
|
666 |
} |
2 | 667 |
} |