Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add workaround for resource management issue in URLClassloader #412

Merged
merged 1 commit into from May 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
}
}
}