Skip to content

Commit 00d1041

Browse files
authored
Ensure class resource stream is closed in ResourceUtils (#116437)
* Ensure class resource stream is closed in ResourceUtils This ensures that `ResourceUtils.loadResource` closes the resource stream when reading all bytes. * Update docs/changelog/116437.yaml
1 parent e27c909 commit 00d1041

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

docs/changelog/116437.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 116437
2+
summary: Ensure class resource stream is closed in `ResourceUtils`
3+
area: Indices APIs
4+
type: enhancement
5+
issues: []

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/ResourceUtils.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ static byte[] loadVersionedResourceUTF8(
3535
}
3636

3737
public static String loadResource(Class<?> clazz, String name) throws IOException {
38-
InputStream is = clazz.getResourceAsStream(name);
39-
if (is == null) {
40-
throw new IOException("Resource [" + name + "] not found in classpath.");
38+
try (InputStream is = clazz.getResourceAsStream(name)) {
39+
if (is == null) {
40+
throw new IOException("Resource [" + name + "] not found in classpath.");
41+
}
42+
return new String(is.readAllBytes(), java.nio.charset.StandardCharsets.UTF_8);
4143
}
42-
return new String(is.readAllBytes(), java.nio.charset.StandardCharsets.UTF_8);
4344
}
4445

4546
}

0 commit comments

Comments
 (0)