Skip to content

Commit

Permalink
Close streams
Browse files Browse the repository at this point in the history
  • Loading branch information
crschnick committed Nov 3, 2024
1 parent 6e1d300 commit 2b6f11d
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ private void createRecursiveWatchers(Path dir) {

try {
dir.register(FileWatchManager.this.watchService, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);
Files.list(dir).filter(Files::isDirectory).forEach(d -> createRecursiveWatchers(d));
try (var s = Files.list(dir)) {
s.filter(Files::isDirectory).forEach(this::createRecursiveWatchers);
}
} catch (IOException e) {
ErrorHandler.handleException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,15 @@ private static String calc(Path ioPackage, Path modelPackage, Path infoPackage,
}

private static void update(MessageDigest d, Path pack) throws IOException {
Files.list(pack).filter(Files::isRegularFile).forEach(p -> {
try {
d.update(Files.readAllBytes(p));
} catch (IOException e) {
ErrorHandler.handleException(e);
}
});
try (var s = Files.list(pack)) {
s.filter(Files::isRegularFile).forEach(p -> {
try {
d.update(Files.readAllBytes(p));
} catch (IOException e) {
ErrorHandler.handleException(e);
}
});
}
}

private static String checksum(MessageDigest d) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,8 @@ public static void addStylesheets(Scene scene) {
if (PdxuInstallation.getInstance() == null) {
return;
}

try {
Files.list(PdxuInstallation.getInstance().getResourceDir().resolve("style"))
.map(p -> p.toUri().toString())
.forEach(s -> scene.getStylesheets().add(s));
try (var s = Files.list(PdxuInstallation.getInstance().getResourceDir().resolve("style"))) {
s.map(p -> p.toUri().toString()).forEach(style -> scene.getStylesheets().add(style));
} catch (IOException e) {
ErrorHandler.handleException(e, "Pdx-Unlimiter installation files could not be found at " + PdxuInstallation.getInstance().getResourceDir() + ". Were they deleted somehow?");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,10 @@ private List<Path> getRemoteDataPaths(int appId) {
}

Path userData = p.get().resolve("userdata");
try {
return Files.list(userData)
.map(d -> d.resolve(String.valueOf(appId)).resolve("remote"))
.filter(Files::exists)
.collect(Collectors.toList());
try (var s = Files.list(userData)) {
return s.map(d -> d.resolve(String.valueOf(appId)).resolve("remote"))
.filter(Files::exists)
.toList();
} catch (IOException e) {
return List.of();
}
Expand Down
10 changes: 5 additions & 5 deletions pdxu-app/src/main/java/com/crschnick/pdxu/app/lang/PdxuI18n.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public final class PdxuI18n {
private static final Map<String, String> map = new HashMap<>();

public static void initDefault() {
try {
Files.list(PdxuInstallation.getInstance().getLanguageLocation()).forEach(p -> {
try (var s = Files.list(PdxuInstallation.getInstance().getLanguageLocation())) {
s.forEach(p -> {
if (LocalisationHelper.isLanguage(p, LanguageManager.DEFAULT)) {
defaultMap.putAll(LocalisationHelper.loadTranslations(p));
}
Expand All @@ -26,9 +26,9 @@ public static void initDefault() {
}

public static void init() {
try {
map.clear();
Files.list(PdxuInstallation.getInstance().getLanguageLocation()).forEach(p -> {
map.clear();
try (var s = Files.list(PdxuInstallation.getInstance().getLanguageLocation())) {
s.forEach(p -> {
if (LocalisationHelper.isLanguage(p, LanguageManager.getInstance().getActiveLanguage())) {
map.putAll(LocalisationHelper.loadTranslations(p));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public static List<StandardImportTarget> createStandardImportsTargets(String toI

if (Files.isDirectory(p)) {
List<StandardImportTarget> targets = new ArrayList<>();
try {
Files.list(p).forEach(f -> targets.addAll(
try (var s = Files.list(p)) {
s.forEach(f -> targets.addAll(
FileImportTarget.createStandardImportsTargets(f.toString())));
} catch (IOException e) {
ErrorHandler.handleException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public static void init() throws IOException {
importFromQueue(p);
};

Files.list(path).forEach(FileImporter::importFromQueue);
try (var s = Files.list(path)) {
s.forEach(FileImporter::importFromQueue);
}
FileWatchManager.getInstance().startWatchersInDirectories(List.of(path), importFunc);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,18 @@ public void convert(SavegameEntry<?, ?> entry) {
});

if (returnCode == 0) {
var latestDir = Files.list(getBackendDir().resolve("output"))
.filter(Files::isDirectory)
.max(Comparator.comparingLong(f -> f.toFile().lastModified()));
var latestFile = Files.list(getBackendDir().resolve("output"))
.filter(Files::isRegularFile)
.max(Comparator.comparingLong(f -> f.toFile().lastModified()));
Optional<Path> latestDir;
try (var s = Files.list(getBackendDir().resolve("output"))) {
latestDir = s.filter(Files::isDirectory)
.max(Comparator.comparingLong(f -> f.toFile().lastModified()));
}

Optional<Path> latestFile;
try (var s = Files.list(getBackendDir().resolve("output"))) {
latestFile = s.filter(Files::isRegularFile)
.max(Comparator.comparingLong(f -> f.toFile().lastModified()));
}

if (latestDir.isPresent() && (!hasModFile || latestFile.isPresent())) {
var outDir = Path.of(getTargetModDir()).resolve(latestDir.get().getFileName());
if (Files.exists(outDir)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public void browseExternalFile() {

public void openExternalFileIfNoSavegame(Path file) {
if (Files.isDirectory(file)) {
try {
Files.list(file).forEach(this::openExternalFileIfNoSavegame);
try (var s = Files.list(file)) {
s.forEach(this::openExternalFileIfNoSavegame);
} catch (IOException e) {
ErrorHandler.handleException(e);
}
Expand Down

0 comments on commit 2b6f11d

Please sign in to comment.