Fix: workaround io error 'stream closed' caused by bugs in URLClassloader (#412)

This commit is contained in:
jzl 2023-05-17 02:06:13 +08:00 committed by GitHub
parent 027c0c2703
commit d72a2bc805
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -25,7 +25,9 @@
package org.xerial.snappy;
import java.io.*;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.Properties;
import java.util.UUID;
@ -235,7 +237,7 @@ public class SnappyLoader
InputStream reader = null;
FileOutputStream writer = null;
try {
reader = SnappyLoader.class.getResourceAsStream(nativeLibraryFilePath);
reader = getResourceAsInputStream(nativeLibraryFilePath);
try {
writer = new FileOutputStream(extractedLibFile);
@ -273,7 +275,7 @@ public class SnappyLoader
InputStream nativeIn = null;
InputStream extractedLibIn = null;
try {
nativeIn = SnappyLoader.class.getResourceAsStream(nativeLibraryFilePath);
nativeIn = getResourceAsInputStream(nativeLibraryFilePath);
extractedLibIn = new FileInputStream(extractedLibFile);
if (!contentsEquals(nativeIn, extractedLibIn)) {
@ -394,4 +396,16 @@ public class SnappyLoader
}
return version;
}
private static InputStream getResourceAsInputStream(String resourcePath) throws IOException {
URL url = SnappyLoader.class.getResource(resourcePath);
URLConnection connection = url.openConnection();
if (connection instanceof JarURLConnection) {
JarURLConnection jarConnection = (JarURLConnection) connection;
jarConnection.setUseCaches(false); // workaround for https://bugs.openjdk.org/browse/JDK-8205976
return jarConnection.getInputStream();
} else {
return connection.getInputStream();
}
}
}