Skip to content

Commit

Permalink
Close file handles
Browse files Browse the repository at this point in the history
  • Loading branch information
nea89o committed Sep 1, 2024
1 parent 533eae7 commit f7391de
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/main/java/io/github/moulberry/repo/NEURepoFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -26,8 +27,8 @@ public String getPath() {
private Path fsPath;

public <T> T json(TypeToken<T> type) throws NEURepositoryException {
try {
return repository.gson.fromJson(Files.newBufferedReader(fsPath, StandardCharsets.UTF_8), type.getType());
try (Reader reader = Files.newBufferedReader(fsPath, StandardCharsets.UTF_8)) {
return repository.gson.fromJson(reader, type.getType());
} catch (IOException | SecurityException e) {
throw new NEURepositoryException(getPath(), "Could not read file", e);
} catch (JsonSyntaxException e) {
Expand All @@ -36,8 +37,8 @@ public <T> T json(TypeToken<T> type) throws NEURepositoryException {
}

public <T> T json(Class<T> $class) throws NEURepositoryException {
try {
return repository.gson.fromJson(Files.newBufferedReader(fsPath, StandardCharsets.UTF_8), $class);
try (Reader reader = Files.newBufferedReader(fsPath, StandardCharsets.UTF_8)) {
return repository.gson.fromJson(reader, $class);
} catch (IOException | SecurityException e) {
throw new NEURepositoryException(getPath(), "Could not read file", e);
} catch (JsonSyntaxException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ public class NEURepositoryVersion {
public static Properties VERSION_PROPERTIES = new Properties();

static {
InputStream resourceAsStream = NEURepositoryVersion.class.getClassLoader().getResourceAsStream("neurepoparser.properties");
try {
try (InputStream resourceAsStream = NEURepositoryVersion.class.getClassLoader().getResourceAsStream("neurepoparser.properties");) {
VERSION_PROPERTIES.load(resourceAsStream);
} catch (IOException | NullPointerException e) {
new RuntimeException("NEURepositoryVersion could not load neurepoparser.properties.", e).printStackTrace();
Expand Down

0 comments on commit f7391de

Please sign in to comment.