Fix RemappingURLClassLoader zip file closed (#336)

This commit is contained in:
IzzelAliz 2021-08-04 12:55:37 +08:00
parent 2fdfa68fac
commit 1abde6a24a

View File

@ -15,6 +15,7 @@ import java.net.URLConnection;
import java.net.URLStreamHandlerFactory;
import java.security.CodeSource;
import java.util.concurrent.Callable;
import java.util.jar.Manifest;
public class RemappingURLClassLoader extends URLClassLoader implements RemappingClassLoader {
@ -40,40 +41,42 @@ public class RemappingURLClassLoader extends URLClassLoader implements Remapping
String path = name.replace('.', '/').concat(".class");
URL resource = this.getResource(path);
if (resource != null) {
URLConnection connection;
Callable<byte[]> byteSource;
Manifest manifest;
try {
URLConnection connection;
Callable<byte[]> byteSource;
try {
connection = resource.openConnection();
connection.connect();
byteSource = () -> {
try (InputStream is = connection.getInputStream()) {
byte[] classBytes = ByteStreams.toByteArray(is);
classBytes = ArclightRemapper.SWITCH_TABLE_FIXER.apply(classBytes);
return classBytes;
}
};
} catch (IOException e) {
throw new ClassNotFoundException(name, e);
connection = resource.openConnection();
connection.connect();
if (connection instanceof JarURLConnection && ((JarURLConnection) connection).getManifest() != null) {
manifest = ((JarURLConnection) connection).getManifest();
} else {
manifest = null;
}
Product2<byte[], CodeSource> classBytes = this.getRemapper().remapClass(name, byteSource, connection);
int i = name.lastIndexOf('.');
if (i != -1) {
String pkgName = name.substring(0, i);
if (getPackage(pkgName) == null) {
if (connection instanceof JarURLConnection && ((JarURLConnection) connection).getManifest() != null) {
this.definePackage(pkgName, ((JarURLConnection) connection).getManifest(), ((JarURLConnection) connection).getJarFileURL());
} else {
this.definePackage(pkgName, null, null, null, null, null, null, null);
}
byteSource = () -> {
try (InputStream is = connection.getInputStream()) {
byte[] classBytes = ByteStreams.toByteArray(is);
classBytes = ArclightRemapper.SWITCH_TABLE_FIXER.apply(classBytes);
return classBytes;
}
}
result = this.defineClass(name, classBytes._1, 0, classBytes._1.length, classBytes._2);
};
} catch (IOException e) {
throw new ClassNotFoundException(name, e);
}
Product2<byte[], CodeSource> classBytes = this.getRemapper().remapClass(name, byteSource, connection);
int i = name.lastIndexOf('.');
if (i != -1) {
String pkgName = name.substring(0, i);
if (getPackage(pkgName) == null) {
if (manifest != null) {
this.definePackage(pkgName, manifest, ((JarURLConnection) connection).getJarFileURL());
} else {
this.definePackage(pkgName, null, null, null, null, null, null, null);
}
}
}
result = this.defineClass(name, classBytes._1, 0, classBytes._1.length, classBytes._2);
}
if (result == null) {
throw new ClassNotFoundException(name);