8290316: Ensure that all directory streams are closed in java.base

Reviewed-by: chegar
This commit is contained in:
Ryan Ernst 2022-07-21 06:19:00 +00:00 committed by Chris Hegarty
parent db1e44c2dd
commit 53fc495e3a
5 changed files with 35 additions and 30 deletions

View file

@ -86,6 +86,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.stream.Stream;
import sun.util.logging.PlatformLogger; import sun.util.logging.PlatformLogger;
@ -1035,9 +1036,8 @@ public final class HijrahChronology extends AbstractChronology implements Serial
AccessController.doPrivileged( AccessController.doPrivileged(
(PrivilegedAction<Void>)() -> { (PrivilegedAction<Void>)() -> {
if (Files.isDirectory(CONF_PATH)) { if (Files.isDirectory(CONF_PATH)) {
try { try (Stream<Path> stream = Files.list(CONF_PATH)) {
Files.list(CONF_PATH) stream.map(p -> p.getFileName().toString())
.map(p -> p.getFileName().toString())
.filter(fn -> fn.matches("hijrah-config-[^\\.]+\\.properties")) .filter(fn -> fn.matches("hijrah-config-[^\\.]+\\.properties"))
.map(fn -> fn.replaceAll("(hijrah-config-|\\.properties)", "")) .map(fn -> fn.replaceAll("(hijrah-config-|\\.properties)", ""))
.forEach(idtype -> { .forEach(idtype -> {

View file

@ -38,6 +38,7 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Stream;
import jdk.internal.jimage.ImageReader.Node; import jdk.internal.jimage.ImageReader.Node;
@ -254,19 +255,21 @@ class ExplodedImage extends SystemImage {
String moduleName = module.getFileName().toString(); String moduleName = module.getFileName().toString();
// make sure "/modules/<moduleName>" is created // make sure "/modules/<moduleName>" is created
findModulesNode(MODULES + moduleName); findModulesNode(MODULES + moduleName);
Files.walk(module).filter(Files::isDirectory).forEach((p) -> { try (Stream<Path> contentsStream = Files.walk(module)) {
p = module.relativize(p); contentsStream.filter(Files::isDirectory).forEach((p) -> {
String pkgName = slashesToDots(p.toString()); p = module.relativize(p);
// skip META-INFO and empty strings String pkgName = slashesToDots(p.toString());
if (!pkgName.isEmpty() && !pkgName.startsWith("META-INF")) { // skip META-INF and empty strings
List<String> moduleNames = packageToModules.get(pkgName); if (!pkgName.isEmpty() && !pkgName.startsWith("META-INF")) {
if (moduleNames == null) { List<String> moduleNames = packageToModules.get(pkgName);
moduleNames = new ArrayList<>(); if (moduleNames == null) {
packageToModules.put(pkgName, moduleNames); moduleNames = new ArrayList<>();
packageToModules.put(pkgName, moduleNames);
}
moduleNames.add(moduleName);
} }
moduleNames.add(moduleName); });
} }
});
} }
} }
} }

View file

@ -41,6 +41,7 @@ import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Stream;
/** /**
* The result of hashing the contents of a number of module artifacts. * The result of hashing the contents of a number of module artifacts.
@ -114,9 +115,9 @@ public final class ModuleHashes {
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
} }
try { byte[] buf = new byte[32*1024];
byte[] buf = new byte[32*1024]; try (Stream<String> stream = reader.list()) {
reader.list().sorted().forEach(rn -> { stream.sorted().forEach(rn -> {
md.update(rn.getBytes(StandardCharsets.UTF_8)); md.update(rn.getBytes(StandardCharsets.UTF_8));
try (InputStream in = reader.open(rn).orElseThrow()) { try (InputStream in = reader.open(rn).orElseThrow()) {
int n; int n;

View file

@ -131,14 +131,15 @@ public final class ModulePatcher {
// exploded directory without following sym links // exploded directory without following sym links
Path top = file; Path top = file;
Files.find(top, Integer.MAX_VALUE, try (Stream<Path> stream = Files.find(top, Integer.MAX_VALUE,
((path, attrs) -> attrs.isRegularFile())) ((path, attrs) -> attrs.isRegularFile()))) {
.filter(path -> (!isAutomatic stream.filter(path -> (!isAutomatic
|| path.toString().endsWith(".class")) || path.toString().endsWith(".class"))
&& !isHidden(path)) && !isHidden(path))
.map(path -> toPackageName(top, path)) .map(path -> toPackageName(top, path))
.filter(Checks::isPackageName) .filter(Checks::isPackageName)
.forEach(packages::add); .forEach(packages::add);
}
} }
} }

View file

@ -58,6 +58,7 @@ import java.util.jar.Manifest;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipException; import java.util.zip.ZipException;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
@ -663,13 +664,12 @@ public class ModulePath implements ModuleFinder {
private Set<String> explodedPackages(Path dir) { private Set<String> explodedPackages(Path dir) {
String separator = dir.getFileSystem().getSeparator(); String separator = dir.getFileSystem().getSeparator();
try { try (Stream<Path> stream = Files.find(dir, Integer.MAX_VALUE,
return Files.find(dir, Integer.MAX_VALUE, (path, attrs) -> attrs.isRegularFile() && !isHidden(path))) {
((path, attrs) -> attrs.isRegularFile() && !isHidden(path))) return stream.map(dir::relativize)
.map(path -> dir.relativize(path)) .map(path -> toPackageName(path, separator))
.map(path -> toPackageName(path, separator)) .flatMap(Optional::stream)
.flatMap(Optional::stream) .collect(Collectors.toSet());
.collect(Collectors.toSet());
} catch (IOException x) { } catch (IOException x) {
throw new UncheckedIOException(x); throw new UncheckedIOException(x);
} }