Skip to content

Commit

Permalink
Fix: workaround io error 'stream closed' caused by bugs in URLClasslo…
Browse files Browse the repository at this point in the history
…ader (#412)
  • Loading branch information
jizhilong committed May 16, 2023
1 parent 027c0c2 commit d72a2bc
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/main/java/org/xerial/snappy/SnappyLoader.java
Expand Up @@ -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;
Expand Down Expand Up @@ -235,7 +237,7 @@ private static File extractLibraryFile(String libFolderForCurrentOS, String libr
InputStream reader = null;
FileOutputStream writer = null;
try {
reader = SnappyLoader.class.getResourceAsStream(nativeLibraryFilePath);
reader = getResourceAsInputStream(nativeLibraryFilePath);
try {
writer = new FileOutputStream(extractedLibFile);

Expand Down Expand Up @@ -273,7 +275,7 @@ private static File extractLibraryFile(String libFolderForCurrentOS, String libr
InputStream nativeIn = null;
InputStream extractedLibIn = null;
try {
nativeIn = SnappyLoader.class.getResourceAsStream(nativeLibraryFilePath);
nativeIn = getResourceAsInputStream(nativeLibraryFilePath);
extractedLibIn = new FileInputStream(extractedLibFile);

if (!contentsEquals(nativeIn, extractedLibIn)) {
Expand Down Expand Up @@ -394,4 +396,16 @@ public static String getVersion()
}
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();
}
}
}

0 comments on commit d72a2bc

Please sign in to comment.