--- old/src/java.base/share/classes/java/util/zip/InflaterInputStream.java 2017-10-31 23:27:35.854577904 +0100 +++ new/src/java.base/share/classes/java/util/zip/InflaterInputStream.java 2017-10-31 23:27:35.734579948 +0100 @@ -25,10 +25,15 @@ package java.util.zip; +import jdk.internal.ref.CleanerFactory; + +import java.io.EOFException; import java.io.FilterInputStream; -import java.io.InputStream; import java.io.IOException; -import java.io.EOFException; +import java.io.InputStream; +import java.lang.ref.Cleaner; +import java.util.function.Consumer; +import java.util.function.Supplier; /** * This class implements a stream filter for uncompressing data in the @@ -60,6 +65,9 @@ // this flag is set to true after EOF has reached private boolean reachEOF = false; + // in case this stream manages own Inflater, else null + private final Cleaner.CleanableResource infRes; + /** * Check to make sure that this stream has not been closed */ @@ -85,8 +93,9 @@ } else if (size <= 0) { throw new IllegalArgumentException("buffer size <= 0"); } + this.buf = new byte[size]; + this.infRes = null; // uses specified/default decompressor this.inf = inf; - buf = new byte[size]; } /** @@ -110,6 +119,31 @@ usesDefaultInflater = true; } + /** + * Creates a new input stream with a decompressor allocated by inflaterAllocator + * and deallocated by inflaterDeallocator and buffer size. + * @param in the input stream + * @param inflaterAllocator the inflater allocator function + * @param inflaterDeallocator the inflater de-allocator function + * @param size the input buffer size + */ + InflaterInputStream(InputStream in, + Supplier inflaterAllocator, + Consumer inflaterDeallocator, + int size) { + super(in); + if (in == null) { + throw new NullPointerException(); + } else if (size <= 0) { + throw new IllegalArgumentException("buffer size <= 0"); + } + this.buf = new byte[size]; + this.infRes = CleanerFactory + .cleaner() + .createResource(this, inflaterAllocator, inflaterDeallocator); + this.inf = infRes.value(); + } + private byte[] singleByteBuf = new byte[1]; /** @@ -227,7 +261,9 @@ */ public void close() throws IOException { if (!closed) { - if (usesDefaultInflater) + if (infRes != null) + infRes.clean(); + else if (usesDefaultInflater) inf.end(); in.close(); closed = true;