Bläddra i källkod

prevent dealing with unclosed directory streams

Armin Schrenk 2 år sedan
förälder
incheckning
b1a3ef9023

+ 7 - 6
src/main/java/org/cryptomator/common/locationpresets/DropboxLinuxLocationPresetsProvider.java

@@ -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();
 		}
 	}