Skip to content

Commit 57754ed

Browse files
committed
fix(api): use try-with-resource for JarFile
1 parent e1f4ca9 commit 57754ed

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

api/src/main/java/com/github/siroshun09/configapi/api/util/ResourceUtils.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ public static void copyFromJar(@NotNull Path jarPath,
112112
return;
113113
}
114114

115-
var jar = new JarFile(jarPath.toFile(), false);
116-
copyFromJar(jar, name, target);
115+
try (var jar = new JarFile(jarPath.toFile(), false)) {
116+
copyFromJar(jar, name, target);
117+
}
117118
}
118119

119120
/**
@@ -194,7 +195,9 @@ public static void copyFromJarIfNotExists(@NotNull Path jarPath,
194195
public static @NotNull InputStream getInputStreamFromJar(@NotNull Path jarPath,
195196
@NotNull String name) throws IOException {
196197
Objects.requireNonNull(jarPath);
197-
return getInputStreamFromJar(new JarFile(jarPath.toFile(), false), name);
198+
try (var jar = new JarFile(jarPath.toFile(), false)) {
199+
return getInputStreamFromJar(jar, name);
200+
}
198201
}
199202

200203
private static void copy(@NotNull IOSupplier<InputStream> inputSupplier, @NotNull Path target) throws IOException {

api/src/test/java/com/github/siroshun09/configapi/api/test/util/ResourceUtilsTest.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ void testResourceCopy() throws IOException {
4646
ResourceUtils.copyFromJar(JAR_PATH, "test.txt", TEXT_PATH);
4747
Assertions.assertTrue(Files.exists(TEXT_PATH));
4848

49-
try {
50-
Files.delete(JAR_PATH);
51-
Files.delete(TEXT_PATH);
52-
} catch (Exception ignored) {
53-
}
49+
Files.delete(JAR_PATH);
50+
Files.delete(TEXT_PATH);
5451
}
5552
}

0 commit comments

Comments
 (0)