Skip to content

Commit 53fc495

Browse files
rjernstChrisHegarty
authored andcommitted
8290316: Ensure that all directory streams are closed in java.base
Reviewed-by: chegar
1 parent db1e44c commit 53fc495

File tree

5 files changed

+35
-30
lines changed

5 files changed

+35
-30
lines changed

src/java.base/share/classes/java/time/chrono/HijrahChronology.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import java.util.List;
8787
import java.util.Map;
8888
import java.util.Properties;
89+
import java.util.stream.Stream;
8990

9091
import sun.util.logging.PlatformLogger;
9192

@@ -1035,9 +1036,8 @@ private static void registerCustomChrono() {
10351036
AccessController.doPrivileged(
10361037
(PrivilegedAction<Void>)() -> {
10371038
if (Files.isDirectory(CONF_PATH)) {
1038-
try {
1039-
Files.list(CONF_PATH)
1040-
.map(p -> p.getFileName().toString())
1039+
try (Stream<Path> stream = Files.list(CONF_PATH)) {
1040+
stream.map(p -> p.getFileName().toString())
10411041
.filter(fn -> fn.matches("hijrah-config-[^\\.]+\\.properties"))
10421042
.map(fn -> fn.replaceAll("(hijrah-config-|\\.properties)", ""))
10431043
.forEach(idtype -> {

src/java.base/share/classes/jdk/internal/jrtfs/ExplodedImage.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.HashMap;
3939
import java.util.List;
4040
import java.util.Map;
41+
import java.util.stream.Stream;
4142

4243
import jdk.internal.jimage.ImageReader.Node;
4344

@@ -254,19 +255,21 @@ private void initNodes() throws IOException {
254255
String moduleName = module.getFileName().toString();
255256
// make sure "/modules/<moduleName>" is created
256257
findModulesNode(MODULES + moduleName);
257-
Files.walk(module).filter(Files::isDirectory).forEach((p) -> {
258-
p = module.relativize(p);
259-
String pkgName = slashesToDots(p.toString());
260-
// skip META-INFO and empty strings
261-
if (!pkgName.isEmpty() && !pkgName.startsWith("META-INF")) {
262-
List<String> moduleNames = packageToModules.get(pkgName);
263-
if (moduleNames == null) {
264-
moduleNames = new ArrayList<>();
265-
packageToModules.put(pkgName, moduleNames);
258+
try (Stream<Path> contentsStream = Files.walk(module)) {
259+
contentsStream.filter(Files::isDirectory).forEach((p) -> {
260+
p = module.relativize(p);
261+
String pkgName = slashesToDots(p.toString());
262+
// skip META-INF and empty strings
263+
if (!pkgName.isEmpty() && !pkgName.startsWith("META-INF")) {
264+
List<String> moduleNames = packageToModules.get(pkgName);
265+
if (moduleNames == null) {
266+
moduleNames = new ArrayList<>();
267+
packageToModules.put(pkgName, moduleNames);
268+
}
269+
moduleNames.add(moduleName);
266270
}
267-
moduleNames.add(moduleName);
268-
}
269-
});
271+
});
272+
}
270273
}
271274
}
272275
}

src/java.base/share/classes/jdk/internal/module/ModuleHashes.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.Set;
4242
import java.util.TreeMap;
4343
import java.util.function.Supplier;
44+
import java.util.stream.Stream;
4445

4546
/**
4647
* The result of hashing the contents of a number of module artifacts.
@@ -114,9 +115,9 @@ private static byte[] computeHash(ModuleReader reader, String algorithm) {
114115
} catch (NoSuchAlgorithmException e) {
115116
throw new IllegalArgumentException(e);
116117
}
117-
try {
118-
byte[] buf = new byte[32*1024];
119-
reader.list().sorted().forEach(rn -> {
118+
byte[] buf = new byte[32*1024];
119+
try (Stream<String> stream = reader.list()) {
120+
stream.sorted().forEach(rn -> {
120121
md.update(rn.getBytes(StandardCharsets.UTF_8));
121122
try (InputStream in = reader.open(rn).orElseThrow()) {
122123
int n;

src/java.base/share/classes/jdk/internal/module/ModulePatcher.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,15 @@ public ModuleReference patchIfNeeded(ModuleReference mref) {
131131

132132
// exploded directory without following sym links
133133
Path top = file;
134-
Files.find(top, Integer.MAX_VALUE,
135-
((path, attrs) -> attrs.isRegularFile()))
136-
.filter(path -> (!isAutomatic
137-
|| path.toString().endsWith(".class"))
138-
&& !isHidden(path))
134+
try (Stream<Path> stream = Files.find(top, Integer.MAX_VALUE,
135+
((path, attrs) -> attrs.isRegularFile()))) {
136+
stream.filter(path -> (!isAutomatic
137+
|| path.toString().endsWith(".class"))
138+
&& !isHidden(path))
139139
.map(path -> toPackageName(top, path))
140140
.filter(Checks::isPackageName)
141141
.forEach(packages::add);
142+
}
142143

143144
}
144145
}

src/java.base/share/classes/jdk/internal/module/ModulePath.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import java.util.regex.Matcher;
5959
import java.util.regex.Pattern;
6060
import java.util.stream.Collectors;
61+
import java.util.stream.Stream;
6162
import java.util.zip.ZipException;
6263
import java.util.zip.ZipFile;
6364

@@ -663,13 +664,12 @@ private ModuleReference readJar(Path file) throws IOException {
663664

664665
private Set<String> explodedPackages(Path dir) {
665666
String separator = dir.getFileSystem().getSeparator();
666-
try {
667-
return Files.find(dir, Integer.MAX_VALUE,
668-
((path, attrs) -> attrs.isRegularFile() && !isHidden(path)))
669-
.map(path -> dir.relativize(path))
670-
.map(path -> toPackageName(path, separator))
671-
.flatMap(Optional::stream)
672-
.collect(Collectors.toSet());
667+
try (Stream<Path> stream = Files.find(dir, Integer.MAX_VALUE,
668+
(path, attrs) -> attrs.isRegularFile() && !isHidden(path))) {
669+
return stream.map(dir::relativize)
670+
.map(path -> toPackageName(path, separator))
671+
.flatMap(Optional::stream)
672+
.collect(Collectors.toSet());
673673
} catch (IOException x) {
674674
throw new UncheckedIOException(x);
675675
}

0 commit comments

Comments
 (0)