Always close the InputStreams returned from Jar.getResource (#46)

* Always close the InputStreams returned from Jar.getResource

* Tentative change to not open inputstream when checking if a resource exists
This commit is contained in:
Ross Allan 2017-09-09 11:58:53 +01:00 committed by md-5
parent 7c09caa222
commit 908f24e8ec
3 changed files with 40 additions and 29 deletions

View File

@ -75,12 +75,26 @@ public class Jar implements Closeable {
* @return
*/
public boolean containsClass(String clazz) {
try {
return contains.contains(clazz) ? true : getClass(clazz) != null;
} catch (IOException ex) {
// IO error - regardless we do not have access to the class, so we can ignore it and move on
return false;
if (contains.contains(clazz)) {
return true;
}
if (containsResource(clazz + ".class")) {
contains.add(clazz);
return true;
}
return false;
}
/**
* Check if this jar contains the given resource.
*
* @param name The name of the resource
* @return true Whether a resource with the given name exists
*/
public boolean containsResource(String name) {
JarFile jarFile = jarForResource.get(name);
return jarFile != null && jarFile.getEntry(name) != null;
}
/**
@ -128,8 +142,13 @@ public class Jar implements Closeable {
try {
InputStream is = getClass(clazz);
if (is != null) {
ClassReader cr;
// Process it
ClassReader cr = new ClassReader(is);
try {
cr = new ClassReader(is);
} finally {
is.close();
}
ClassNode node = new ClassNode();
cr.accept(node, 0);

View File

@ -299,8 +299,20 @@ public class SpecialSource {
JarComparer visitor1 = visitors.first;
JarComparer visitor2 = visitors.second;
ClassReader clazz1 = new ClassReader(jars.first.getClass(classes.first));
ClassReader clazz2 = new ClassReader(jars.second.getClass(classes.second));
ClassReader clazz1, clazz2;
InputStream first = null;
InputStream second = null;
try {
clazz1 = new ClassReader(first = jars.second.getClass(classes.first));
clazz2 = new ClassReader(second = jars.first.getClass(classes.second));
} finally {
if (first != null) {
first.close();
}
if (second != null) {
second.close();
}
}
clazz1.accept(visitor1, 0);
clazz2.accept(visitor2, 0);

View File

@ -28,11 +28,8 @@
*/
package net.md_5.specialsource.repo;
import java.io.IOException;
import java.io.InputStream;
import lombok.RequiredArgsConstructor;
import net.md_5.specialsource.Jar;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.ClassNode;
@RequiredArgsConstructor
@ -42,23 +39,6 @@ public class JarRepo extends CachingRepo {
@Override
protected ClassNode findClass0(String internalName) {
ClassNode node = null;
try {
InputStream is = jar.getClass(internalName);
if (is != null) {
ClassReader reader = new ClassReader(is);
ClassNode node0 = new ClassNode();
reader.accept(node0, 0); // TODO
is.close();
node = node0;
}
} catch (IOException ex) {
ex.printStackTrace();
}
return node;
return jar.getNode(internalName);
}
}