From 42c4bcc977f8d07fdf00071e2a6c440ca8012a68 Mon Sep 17 00:00:00 2001 From: Lee Hinman Date: Thu, 7 Nov 2024 12:38:13 -0700 Subject: [PATCH] Ensure class resource stream is closed in ResourceUtils This ensures that `ResourceUtils.loadResource` closes the resource stream when reading all bytes. --- .../elasticsearch/xpack/core/template/ResourceUtils.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/ResourceUtils.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/ResourceUtils.java index f52013ea5aa89..50f144a0e0899 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/ResourceUtils.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/ResourceUtils.java @@ -35,11 +35,12 @@ static byte[] loadVersionedResourceUTF8( } public static String loadResource(Class clazz, String name) throws IOException { - InputStream is = clazz.getResourceAsStream(name); - if (is == null) { - throw new IOException("Resource [" + name + "] not found in classpath."); + try (InputStream is = clazz.getResourceAsStream(name)) { + if (is == null) { + throw new IOException("Resource [" + name + "] not found in classpath."); + } + return new String(is.readAllBytes(), java.nio.charset.StandardCharsets.UTF_8); } - return new String(is.readAllBytes(), java.nio.charset.StandardCharsets.UTF_8); } }