prevent dealing with unclosed directory streams

This commit is contained in:
Armin Schrenk
2023-05-23 12:35:14 +02:00
parent 32436f779f
commit b1a3ef9023

View File

@@ -3,12 +3,12 @@ package org.cryptomator.common.locationpresets;
import org.cryptomator.integrations.common.OperatingSystem;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static org.cryptomator.integrations.common.OperatingSystem.Value.LINUX;
@@ -20,11 +20,12 @@ public final class DropboxLinuxLocationPresetsProvider implements LocationPreset
@Override
public Stream<LocationPreset> getLocations() {
try (var dirStream = Files.newDirectoryStream(USER_HOME, "Dropbox*")) {
return StreamSupport.stream(dirStream.spliterator(), false) //
.filter(p -> Files.isDirectory(p) && PATTERN.test(p.getFileName().toString())) //
.map(p -> new LocationPreset(p.getFileName().toString(), p));
} catch (IOException e) {
try (var dirStream = Files.list(USER_HOME)) {
var presets = dirStream.filter(p -> Files.isDirectory(p) && PATTERN.test(p.getFileName().toString())) //
.map(p -> new LocationPreset(p.getFileName().toString(), p)) //
.toList();
return presets.stream(); //workaround to ensure that the directory stream is always closed
} catch (IOException | UncheckedIOException e) { //UncheckedIOException thrown by the stream of Files.list()
return Stream.of();
}
}