Fix unclosed streams

* make sure Files.path() properly closes its directory, using try-with-resources
* missing one, found by @violetagg
* cherry pick from 1abd516456 and ec894cb82f
This commit is contained in:
Anthony Dahanne
2023-08-11 23:07:44 -04:00
parent 874b54b4b6
commit 8c0fe7682b
3 changed files with 8 additions and 7 deletions

View File

@@ -26,7 +26,7 @@
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bindings</artifactId>
<version>${version}</version>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>

View File

@@ -23,6 +23,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* A representation of a binding as defined by the
@@ -114,13 +115,12 @@ public final class Binding {
return Collections.emptyMap();
}
try {
return Files.list(path)
.filter(p -> {
try (Stream<Path> paths = Files.list(path)) {
return paths.filter(p -> {
try {
return !Files.isHidden(p);
} catch (IOException e) {
throw new IllegalStateException(String.format("unable to determine id file '%s' is hidden", p), e);
throw new IllegalStateException(String.format("unable to determine if file '%s' is hidden", p), e);
}
})
.filter(p -> !Files.isDirectory(p))

View File

@@ -26,6 +26,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
@@ -81,8 +82,8 @@ public final class Bindings {
throw new IllegalArgumentException(String.format("%s is not a directory", p));
}
try {
this.bindings = Files.list(p)
try (Stream<Path> paths = Files.list(p)) {
this.bindings = paths
.map(Binding::new)
.collect(Collectors.toList());
} catch (IOException e) {