From eb69d59697e395db49e8e9398d6a80fd1657892f Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Fri, 10 May 2024 13:49:00 +0100 Subject: [PATCH 1/2] Improve javadoc and robustness of IoUtil utility --- .../io/quarkus/deployment/util/IoUtil.java | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/core/deployment/src/main/java/io/quarkus/deployment/util/IoUtil.java b/core/deployment/src/main/java/io/quarkus/deployment/util/IoUtil.java index 9c592245b4441..132efb7da3d52 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/util/IoUtil.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/util/IoUtil.java @@ -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(); } + } From 57739135bafc60e0473578402475c9e3192f3f47 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Fri, 10 May 2024 14:49:16 +0100 Subject: [PATCH 2/2] Stop using deprecated JarClassPathElement#readStreamContents --- .../quarkus/bootstrap/classloading/JarClassPathElement.java | 4 ++-- .../io/quarkus/bootstrap/classloading/QuarkusClassLoader.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/classloading/JarClassPathElement.java b/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/classloading/JarClassPathElement.java index 1c2aad6399a03..6029f23a659bd 100644 --- a/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/classloading/JarClassPathElement.java +++ b/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/classloading/JarClassPathElement.java @@ -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; } diff --git a/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/classloading/QuarkusClassLoader.java b/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/classloading/QuarkusClassLoader.java index bd9c962588950..fdac726f9b609 100644 --- a/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/classloading/QuarkusClassLoader.java +++ b/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/classloading/QuarkusClassLoader.java @@ -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();