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

More classloaders cleanup #40557

Merged
merged 2 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,40 @@
import java.io.InputStream;

public class IoUtil {

/**
* Returns an input stream for reading the specified resource from the specified ClassLoader.
* This might return {@code null}, in the case there is no matching resource.
*
* @param classLoader
* @param className
* @return
*/
public static InputStream readClass(ClassLoader classLoader, String className) {
return classLoader.getResourceAsStream(fromClassNameToResourceName(className));
}

/**
* Returns an byte array representing the content of the specified resource as loaded
* from the specified ClassLoader.
* This might return {@code null}, in the case there is no matching resource.
*
* @param classLoader
* @param className
* @return
*/
public static byte[] readClassAsBytes(ClassLoader classLoader, String className) throws IOException {
try (InputStream stream = readClass(classLoader, className)) {
return readBytes(stream);
if (stream == null) {
return null;
} else {
return readBytes(stream);
}
}
}

public static byte[] readBytes(InputStream is) throws IOException {
return is.readAllBytes();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ public byte[] getData() {
public byte[] apply(JarFile jarFile) {
try {
try {
return readStreamContents(jarFile.getInputStream(res));
return jarFile.getInputStream(res).readAllBytes();
} catch (InterruptedIOException e) {
//if we are interrupted reading data we finish the op, then just re-interrupt the thread state
byte[] bytes = readStreamContents(jarFile.getInputStream(res));
byte[] bytes = jarFile.getInputStream(res).readAllBytes();
Thread.currentThread().interrupt();
return bytes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ public void close() {
//DriverManager only lets you remove drivers with the same CL as the caller
//so we need do define the cleaner in this class loader
try (InputStream is = getClass().getResourceAsStream("DriverRemover.class")) {
byte[] data = JarClassPathElement.readStreamContents(is);
byte[] data = is.readAllBytes();
Runnable r = (Runnable) defineClass(DriverRemover.class.getName(), data, 0, data.length)
.getConstructor(ClassLoader.class).newInstance(this);
r.run();
Expand Down
Loading