Skip to content

Commit

Permalink
Merge pull request #40557 from Sanne/MoreClassloadersCleanup
Browse files Browse the repository at this point in the history
More classloaders cleanup
  • Loading branch information
Sanne authored May 10, 2024
2 parents 4d6425b + 5773913 commit 602e4c4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
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

0 comments on commit 602e4c4

Please sign in to comment.