Skip to content

Commit f51c2dc

Browse files
committed
🐛 close filehandles
1 parent 1245dd9 commit f51c2dc

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

src/main/java/dev/ebullient/convert/VersionProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.ebullient.convert;
22

33
import java.io.IOException;
4+
import java.io.InputStream;
45
import java.util.Properties;
56

67
import dev.ebullient.convert.config.TtrpgConfig;
@@ -11,8 +12,8 @@ public class VersionProvider implements CommandLine.IVersionProvider {
1112
@Override
1213
public String[] getVersion() {
1314
Properties properties = new Properties();
14-
try {
15-
properties.load(TtrpgConfig.class.getResourceAsStream("/git.properties"));
15+
try (InputStream in = TtrpgConfig.class.getResourceAsStream("/git.properties")) {
16+
properties.load(in);
1617
return new String[] {
1718
"${COMMAND-FULL-NAME} version " + properties.getProperty("git.build.version"),
1819
"Git commit: " + properties.get("git.commit.id.abbrev")

src/main/java/dev/ebullient/convert/io/Tui.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,7 @@ private void copyImageResource(ImageRef image, Path targetPath) {
484484
String sourcePath = image.sourcePath().toString().replace("stream", "");
485485
targetPath.getParent().toFile().mkdirs();
486486

487-
try {
488-
InputStream in = TtrpgConfig.class.getResourceAsStream(sourcePath);
487+
try (InputStream in = TtrpgConfig.class.getResourceAsStream(sourcePath)) {
489488
Files.copy(in, targetPath, StandardCopyOption.REPLACE_EXISTING);
490489
} catch (IOException e) {
491490
errorf("Unable to copy resource. %s", e);
@@ -505,10 +504,8 @@ private void copyRemoteImage(ImageRef image, Path targetPath) {
505504
return;
506505
}
507506

508-
try {
509-
Tui.instance().debugf("copy image %s", url);
510-
511-
ReadableByteChannel readableByteChannel = Channels.newChannel(new URL(url).openStream());
507+
Tui.instance().debugf("copy image %s", url);
508+
try (ReadableByteChannel readableByteChannel = Channels.newChannel(new URL(url).openStream())) {
512509
try (FileOutputStream fileOutputStream = new FileOutputStream(targetPath.toFile())) {
513510
fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
514511
}
@@ -653,10 +650,10 @@ public <T> T readJsonValue(JsonNode node, Class<T> classTarget) {
653650
}
654651

655652
public static JsonNode readTreeFromResource(String resource) {
656-
try {
653+
try (InputStream in = TtrpgConfig.class.getResourceAsStream(resource)) {
657654
return resource.endsWith(".yaml")
658-
? Tui.yamlMapper().readTree(TtrpgConfig.class.getResourceAsStream(resource))
659-
: Tui.MAPPER.readTree(TtrpgConfig.class.getResourceAsStream(resource));
655+
? Tui.yamlMapper().readTree(in)
656+
: Tui.MAPPER.readTree(in);
660657
} catch (IOException | IllegalArgumentException e) {
661658
Tui.instance.errorf(e, "Unable to read or parse required resource (%s): %s", resource, e.toString());
662659
return null;

0 commit comments

Comments
 (0)