Skip to content

Commit

Permalink
Merge pull request #602 from chrisr3/chrisr3-simplify
Browse files Browse the repository at this point in the history
Only invoke Resource's super.close() if the resource is open.
  • Loading branch information
lukehutch authored Nov 22, 2021
2 parents 3d3d3a8 + 53f01da commit c7dc311
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,7 @@ private Resource newResource(final String pathRelativeToPackageRoot, final File
private final Runnable onClose = new Runnable() {
@Override
public void run() {
if (isOpen.get()) {
close();
}
close();
}
};

Expand Down Expand Up @@ -298,8 +296,10 @@ public void close() {
nestedJarHandler.markSliceAsClosed(fileSlice);
fileSlice = null;
}

// Close inputStream
super.close();
}
super.close(); // Close inputStream
}
};
}
Expand Down
32 changes: 17 additions & 15 deletions src/main/java/io/github/classgraph/ClasspathElementModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ private Resource newResource(final String resourcePath) {
private final Runnable onClose = new Runnable() {
@Override
public void run() {
if (isOpen.get()) {
close();
}
close();
}
};

Expand Down Expand Up @@ -243,20 +241,24 @@ public byte[] load() throws IOException {

@Override
public void close() {
if (isOpen.getAndSet(false) && moduleReaderProxy != null) {
if (byteBuffer != null) {
// Release any open ByteBuffer
moduleReaderProxy.release(byteBuffer);
byteBuffer = null;
if (isOpen.getAndSet(false)) {
if (moduleReaderProxy != null) {
if (byteBuffer != null) {
// Release any open ByteBuffer
moduleReaderProxy.release(byteBuffer);
byteBuffer = null;
}
// Recycle the (open) ModuleReaderProxy instance.
moduleReaderProxyRecycler.recycle(moduleReaderProxy);
// Don't call ModuleReaderProxy#close(), leave the ModuleReaderProxy open in the recycler.
// Just set the ref to null here. The ModuleReaderProxy will be closed by
// ClasspathElementModule#close().
moduleReaderProxy = null;
}
// Recycle the (open) ModuleReaderProxy instance.
moduleReaderProxyRecycler.recycle(moduleReaderProxy);
// Don't call ModuleReaderProxy#close(), leave the ModuleReaderProxy open in the recycler.
// Just set the ref to null here. The ModuleReaderProxy will be closed by
// ClasspathElementModule#close().
moduleReaderProxy = null;

// Close inputStream
super.close();
}
super.close(); // Close inputStream
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,7 @@ private Resource newResource(final Path resourcePath, final NestedJarHandler nes
private final Runnable onClose = new Runnable() {
@Override
public void run() {
if (isOpen.get()) {
close();
}
close();
}
};

Expand Down Expand Up @@ -304,8 +302,10 @@ public void close() {
nestedJarHandler.markSliceAsClosed(pathSlice);
pathSlice = null;
}

// Close inputStream
super.close();
}
super.close(); // Close inputStream
}
};
}
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/io/github/classgraph/ClasspathElementZip.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,7 @@ private Resource newResource(final FastZipEntry zipEntry, final String pathRelat
private final Runnable onClose = new Runnable() {
@Override
public void run() {
if (isOpen.get()) {
close();
}
close();
}
};

Expand Down Expand Up @@ -455,12 +453,16 @@ public byte[] load() throws IOException {

@Override
public void close() {
if (isOpen.getAndSet(false) && byteBuffer != null) {
// ByteBuffer should be a duplicate or slice, or should wrap an array, so it doesn't
// need to be unmapped
byteBuffer = null;
if (isOpen.getAndSet(false)) {
if (byteBuffer != null) {
// ByteBuffer should be a duplicate or slice, or should wrap an array, so it doesn't
// need to be unmapped
byteBuffer = null;
}

// Close inputStream
super.close();
}
super.close(); // Close inputStream
}
};
}
Expand Down

0 comments on commit c7dc311

Please sign in to comment.