Merge pull request #105 from spring-cloud/fix-unclosed-streams

Fix unclosed streams
This commit is contained in:
Anthony Dahanne
2023-08-15 10:49:32 -04:00
committed by GitHub
4 changed files with 14 additions and 19 deletions

View File

@@ -32,7 +32,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

@@ -136,7 +136,7 @@
<version>${maven-javadoc-plugin.version}</version>
<configuration>
<quiet>true</quiet>
<source>1.8</source>
<source>17</source>
<tags>
<tag>
<name>NonNullApi</name>

View File

@@ -18,11 +18,11 @@ package org.springframework.cloud.bindings;
import org.springframework.lang.Nullable;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
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
@@ -79,15 +79,10 @@ public final class Binding {
String provider = null;
for (Map.Entry<String, String> entry : secret.entrySet()) {
switch (entry.getKey()) {
case TYPE:
case KIND: // TODO: Remove as CNB_BINDINGS ages out
type = entry.getValue();
break;
case PROVIDER:
provider = entry.getValue();
break;
default:
this.secret.put(entry.getKey(), entry.getValue());
case TYPE, KIND -> // TODO: Remove as CNB_BINDINGS ages out
type = entry.getValue();
case PROVIDER -> provider = entry.getValue();
default -> this.secret.put(entry.getKey(), entry.getValue());
}
}
@@ -114,13 +109,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))
@@ -128,7 +122,7 @@ public final class Binding {
p -> p.getFileName().toString(),
p -> {
try {
return new String(Files.readAllBytes(p), StandardCharsets.UTF_8).trim();
return Files.readString(p).trim();
} catch (IOException e) {
throw new IllegalStateException(String.format("unable to read file '%s'", p), e);
}

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) {