< prev index next >
src/java.base/share/classes/java/util/zip/Inflater.java
Print this page
@@ -23,10 +23,15 @@
* questions.
*/
package java.util.zip;
+import jdk.internal.ref.CleanerFactory;
+
+import java.lang.ref.Cleaner;
+import java.lang.ref.Reference;
+
/**
* This class provides support for general purpose decompression using the
* popular ZLIB compression library. The ZLIB compression library was
* initially developed as part of the PNG graphics standard and is not
* protected by patents. It is fully described in the specifications at
@@ -64,19 +69,33 @@
* } catch (java.util.zip.DataFormatException ex) {
* // handle
* }
* </pre></blockquote>
*
+ * <p>
+ * @apiNote
+ * In earlier versions the {@link Object#finalize} method was overridden and
+ * specified to call the {@code end} method to close the {@code inflater} and
+ * release the resource when the instance becomes unreachable.
+ * The {@code finalize} method is no longer defined. The recommended cleanup
+ * for decompressor is to explicitly call {@code end} method when it is no
+ * longer in use.
+ *
+ * @implNote
+ * The resource of the decompressor will be released when the instance becomes
+ * phantom-reachable, if the {@code end} is not invoked explicitly.
+ * <p>
+ *
* @see Deflater
* @author David Connelly
* @since 1.1
*
*/
public
class Inflater {
- private final ZStreamRef zsRef;
+ private final Cleaner.LongCleanableResource zsRef;
private byte[] buf = defaultBuf;
private int off, len;
private boolean finished;
private boolean needDict;
private long bytesRead;
@@ -99,11 +118,13 @@
* library in order to support certain optimizations.
*
* @param nowrap if true then support GZIP compatible compression
*/
public Inflater(boolean nowrap) {
- zsRef = new ZStreamRef(init(nowrap));
+ this.zsRef = CleanerFactory
+ .cleaner()
+ .createLongResource(this, () -> init(nowrap), Inflater::end);
}
/**
* Creates a new decompressor.
*/
@@ -163,11 +184,12 @@
if (off < 0 || len < 0 || off > b.length - len) {
throw new ArrayIndexOutOfBoundsException();
}
synchronized (zsRef) {
ensureOpen();
- setDictionary(zsRef.address(), b, off, len);
+ setDictionary(zsRef.value(), b, off, len);
+ Reference.reachabilityFence(this);
needDict = false;
}
}
/**
@@ -255,11 +277,12 @@
throw new ArrayIndexOutOfBoundsException();
}
synchronized (zsRef) {
ensureOpen();
int thisLen = this.len;
- int n = inflateBytes(zsRef.address(), b, off, len);
+ int n = inflateBytes(zsRef.value(), b, off, len);
+ Reference.reachabilityFence(this);
bytesWritten += n;
bytesRead += (thisLen - this.len);
return n;
}
}
@@ -286,11 +309,13 @@
* @return the ADLER-32 value of the uncompressed data
*/
public int getAdler() {
synchronized (zsRef) {
ensureOpen();
- return getAdler(zsRef.address());
+ int adler = getAdler(zsRef.value());
+ Reference.reachabilityFence(this);
+ return adler;
}
}
/**
* Returns the total number of compressed bytes input so far.
@@ -348,64 +373,41 @@
* Resets inflater so that a new set of input data can be processed.
*/
public void reset() {
synchronized (zsRef) {
ensureOpen();
- reset(zsRef.address());
+ reset(zsRef.value());
+ Reference.reachabilityFence(this);
buf = defaultBuf;
finished = false;
needDict = false;
off = len = 0;
bytesRead = bytesWritten = 0;
}
}
/**
* Closes the decompressor and discards any unprocessed input.
+ *
* This method should be called when the decompressor is no longer
- * being used, but will also be called automatically by the finalize()
- * method. Once this method is called, the behavior of the Inflater
- * object is undefined.
+ * being used. Once this method is called, the behavior of the
+ * Inflater object is undefined.
*/
public void end() {
synchronized (zsRef) {
- long addr = zsRef.address();
- zsRef.clear();
- if (addr != 0) {
- end(addr);
+ zsRef.clean();
buf = null;
}
}
- }
-
- /**
- * Closes the decompressor when garbage is collected.
- *
- * @deprecated The {@code finalize} method has been deprecated.
- * Subclasses that override {@code finalize} in order to perform cleanup
- * should be modified to use alternative cleanup mechanisms and
- * to remove the overriding {@code finalize} method.
- * When overriding the {@code finalize} method, its implementation must explicitly
- * ensure that {@code super.finalize()} is invoked as described in {@link Object#finalize}.
- * See the specification for {@link Object#finalize()} for further
- * information about migration options.
- */
- @Deprecated(since="9")
- protected void finalize() {
- end();
- }
private void ensureOpen () {
assert Thread.holdsLock(zsRef);
- if (zsRef.address() == 0)
+ try {
+ zsRef.value();
+ } catch (IllegalStateException e) {
throw new NullPointerException("Inflater has been closed");
}
-
- boolean ended() {
- synchronized (zsRef) {
- return zsRef.address() == 0;
- }
}
private static native void initIDs();
private static native long init(boolean nowrap);
private static native void setDictionary(long addr, byte[] b, int off,
< prev index next >