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