Skip to content

Commit

Permalink
8193507: [REDO] Startup regression due to JDK-8185582
Browse files Browse the repository at this point in the history
Co-authored-by: Xueming Shen <[email protected]>
Reviewed-by: alanb, rriggs
  • Loading branch information
cl4es and Xueming Shen committed Dec 14, 2017
1 parent 8374bf4 commit a9a2711
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 121 deletions.
81 changes: 77 additions & 4 deletions src/java.base/share/classes/java/util/zip/Deflater.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

package java.util.zip;

import java.lang.ref.Cleaner.Cleanable;
import jdk.internal.ref.CleanerFactory;

/**
* This class provides support for general purpose compression using the
* popular ZLIB compression library. The ZLIB compression library was
Expand Down Expand Up @@ -88,7 +91,7 @@

public class Deflater {

private final ZStreamRef zsRef;
private final DeflaterZStreamRef zsRef;
private byte[] buf = new byte[0];
private int off, len;
private int level, strategy;
Expand Down Expand Up @@ -183,9 +186,8 @@ public class Deflater {
public Deflater(int level, boolean nowrap) {
this.level = level;
this.strategy = DEFAULT_STRATEGY;
this.zsRef = ZStreamRef.get(this,
() -> init(level, DEFAULT_STRATEGY, nowrap),
Deflater::end);
this.zsRef = DeflaterZStreamRef.get(this,
init(level, DEFAULT_STRATEGY, nowrap));
}

/**
Expand Down Expand Up @@ -591,4 +593,75 @@ private native int deflateBytes(long addr, byte[] b, int off, int len,
private static native int getAdler(long addr);
private static native void reset(long addr);
private static native void end(long addr);

/**
* A reference to the native zlib's z_stream structure. It also
* serves as the "cleaner" to clean up the native resource when
* the Deflater is ended, closed or cleaned.
*/
static class DeflaterZStreamRef implements Runnable {

private long address;
private final Cleanable cleanable;

private DeflaterZStreamRef(Deflater owner, long addr) {
this.cleanable = (owner != null) ? CleanerFactory.cleaner().register(owner, this) : null;
this.address = addr;
}

long address() {
return address;
}

void clean() {
cleanable.clean();
}

public synchronized void run() {
long addr = address;
address = 0;
if (addr != 0) {
end(addr);
}
}

/*
* If {@code Deflater} has been subclassed and the {@code end} method is
* overridden, uses {@code finalizer} mechanism for resource cleanup. So
* {@code end} method can be called when the {@code Deflater} is unreachable.
* This mechanism will be removed when the {@code finalize} method is
* removed from {@code Deflater}.
*/
static DeflaterZStreamRef get(Deflater owner, long addr) {
Class<?> clz = owner.getClass();
while (clz != Deflater.class) {
try {
clz.getDeclaredMethod("end");
return new FinalizableZStreamRef(owner, addr);
} catch (NoSuchMethodException nsme) {}
clz = clz.getSuperclass();
}
return new DeflaterZStreamRef(owner, addr);
}

private static class FinalizableZStreamRef extends DeflaterZStreamRef {
final Deflater owner;

FinalizableZStreamRef (Deflater owner, long addr) {
super(null, addr);
this.owner = owner;
}

@Override
void clean() {
run();
}

@Override
@SuppressWarnings("deprecation")
protected void finalize() {
owner.end();
}
}
}
}
78 changes: 76 additions & 2 deletions src/java.base/share/classes/java/util/zip/Inflater.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

package java.util.zip;

import java.lang.ref.Cleaner.Cleanable;
import jdk.internal.ref.CleanerFactory;

/**
* This class provides support for general purpose decompression using the
* popular ZLIB compression library. The ZLIB compression library was
Expand Down Expand Up @@ -88,7 +91,7 @@

public class Inflater {

private final ZStreamRef zsRef;
private final InflaterZStreamRef zsRef;
private byte[] buf = defaultBuf;
private int off, len;
private boolean finished;
Expand All @@ -115,7 +118,7 @@ public class Inflater {
* @param nowrap if true then support GZIP compatible compression
*/
public Inflater(boolean nowrap) {
this.zsRef = ZStreamRef.get(this, () -> init(nowrap), Inflater::end);
this.zsRef = InflaterZStreamRef.get(this, init(nowrap));
}

/**
Expand Down Expand Up @@ -428,4 +431,75 @@ private native int inflateBytes(long addr, byte[] b, int off, int len)
private static native int getAdler(long addr);
private static native void reset(long addr);
private static native void end(long addr);

/**
* A reference to the native zlib's z_stream structure. It also
* serves as the "cleaner" to clean up the native resource when
* the Inflater is ended, closed or cleaned.
*/
static class InflaterZStreamRef implements Runnable {

private long address;
private final Cleanable cleanable;

private InflaterZStreamRef(Inflater owner, long addr) {
this.cleanable = (owner != null) ? CleanerFactory.cleaner().register(owner, this) : null;
this.address = addr;
}

long address() {
return address;
}

void clean() {
cleanable.clean();
}

public synchronized void run() {
long addr = address;
address = 0;
if (addr != 0) {
end(addr);
}
}

/*
* If {@code Inflater} has been subclassed and the {@code end} method is
* overridden, uses {@code finalizer} mechanism for resource cleanup. So
* {@code end} method can be called when the {@code Inflater} is unreachable.
* This mechanism will be removed when the {@code finalize} method is
* removed from {@code Inflater}.
*/
static InflaterZStreamRef get(Inflater owner, long addr) {
Class<?> clz = owner.getClass();
while (clz != Inflater.class) {
try {
clz.getDeclaredMethod("end");
return new FinalizableZStreamRef(owner, addr);
} catch (NoSuchMethodException nsme) {}
clz = clz.getSuperclass();
}
return new InflaterZStreamRef(owner, addr);
}

private static class FinalizableZStreamRef extends InflaterZStreamRef {
final Inflater owner;

FinalizableZStreamRef(Inflater owner, long addr) {
super(null, addr);
this.owner = owner;
}

@Override
void clean() {
run();
}

@Override
@SuppressWarnings("deprecation")
protected void finalize() {
owner.end();
}
}
}
}
113 changes: 0 additions & 113 deletions src/java.base/share/classes/java/util/zip/ZStreamRef.java

This file was deleted.

19 changes: 17 additions & 2 deletions src/java.base/share/classes/java/util/zip/ZipFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,21 @@ public InputStream getInputStream(ZipEntry entry) throws IOException {
}
}

private static class InflaterCleanupAction implements Runnable {
private final Inflater inf;
private final CleanableResource res;

InflaterCleanupAction(Inflater inf, CleanableResource res) {
this.inf = inf;
this.res = res;
}

@Override
public void run() {
res.releaseInflater(inf);
}
}

private class ZipFileInflaterInputStream extends InflaterInputStream {
private volatile boolean closeRequested;
private boolean eof = false;
Expand All @@ -427,8 +442,8 @@ private ZipFileInflaterInputStream(ZipFileInputStream zfin,
Inflater inf, int size) {
super(zfin, inf, size);
this.cleanable = CleanerFactory.cleaner().register(this,
() -> res.releaseInflater(inf));
}
new InflaterCleanupAction(inf, res));
}

public void close() throws IOException {
if (closeRequested)
Expand Down

0 comments on commit a9a2711

Please sign in to comment.