diff --git a/builtins/src/main/java/org/jline/builtins/Commands.java b/builtins/src/main/java/org/jline/builtins/Commands.java index e1406a172..295777e60 100644 --- a/builtins/src/main/java/org/jline/builtins/Commands.java +++ b/builtins/src/main/java/org/jline/builtins/Commands.java @@ -190,8 +190,10 @@ protected static List findFiles(Path root, String files) throws IOExceptio searchRoot = root; } PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + regex); - return Files.find(searchRoot, Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path)) - .collect(Collectors.toList()); + try (Stream pathStream = + Files.find(searchRoot, Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path))) { + return pathStream.collect(Collectors.toList()); + } } public static void history(LineReader reader, PrintStream out, PrintStream err, Path currentDir, String[] argv) diff --git a/builtins/src/main/java/org/jline/builtins/Less.java b/builtins/src/main/java/org/jline/builtins/Less.java index 02e1db5de..4e14c02cc 100644 --- a/builtins/src/main/java/org/jline/builtins/Less.java +++ b/builtins/src/main/java/org/jline/builtins/Less.java @@ -25,6 +25,7 @@ import java.util.*; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; +import java.util.stream.Stream; import org.jline.builtins.Nano.PatternHistory; import org.jline.builtins.Source.ResourceSource; @@ -155,9 +156,9 @@ public Less(Terminal terminal, Path currentDir, Options opts, ConfigurationPath } } else if (new File("/usr/share/nano").exists() && !ignorercfiles) { PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:/usr/share/nano/*.nanorc"); - try { - Files.find(Paths.get("/usr/share/nano"), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path)) - .forEach(syntaxFiles::add); + try (Stream pathStream = Files.find( + Paths.get("/usr/share/nano"), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path))) { + pathStream.forEach(syntaxFiles::add); nanorcIgnoreErrors = true; } catch (IOException e) { errorMessage = "Encountered error while reading nanorc files"; diff --git a/console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java b/console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java index e0641a192..13a106b49 100644 --- a/console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java +++ b/console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java @@ -25,6 +25,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.jline.builtins.Completers.FilesCompleter; import org.jline.builtins.Completers.OptDesc; @@ -237,11 +238,12 @@ public Map scripts() { for (String e : scriptExtensions()) { String regex = pp + "/*." + e; PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + regex); - Files.find( - Paths.get(new File(regex).getParent()), - Integer.MAX_VALUE, - (path, f) -> pathMatcher.matches(path)) - .forEach(scripts::add); + try (Stream pathStream = Files.find( + Paths.get(new File(regex).getParent()), + Integer.MAX_VALUE, + (path, f) -> pathMatcher.matches(path))) { + pathStream.forEach(scripts::add); + } } } } diff --git a/demo/src/main/java/org/apache/felix/gogo/jline/Posix.java b/demo/src/main/java/org/apache/felix/gogo/jline/Posix.java index 964269c10..204d0d717 100644 --- a/demo/src/main/java/org/apache/felix/gogo/jline/Posix.java +++ b/demo/src/main/java/org/apache/felix/gogo/jline/Posix.java @@ -1025,8 +1025,10 @@ protected void less(CommandSession session, Process process, String[] argv) thro sources.add(new StdInSource(process)); } else if (arg.contains("*") || arg.contains("?")) { PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + arg); - Files.find(session.currentDir(), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path)) - .forEach(p -> sources.add(doUrlSource(session.currentDir(), p))); + try (Stream pathStream = + Files.find(session.currentDir(), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path))) { + pathStream.forEach(p -> sources.add(doUrlSource(session.currentDir(), p))); + } } else { sources.add(new PathSource(session.currentDir().resolve(arg), arg)); } diff --git a/groovy/src/main/java/org/jline/script/GroovyCommand.java b/groovy/src/main/java/org/jline/script/GroovyCommand.java index cb7f52334..f6c5a52aa 100644 --- a/groovy/src/main/java/org/jline/script/GroovyCommand.java +++ b/groovy/src/main/java/org/jline/script/GroovyCommand.java @@ -11,6 +11,7 @@ import java.io.File; import java.nio.file.*; import java.util.*; +import java.util.stream.Stream; import org.jline.builtins.Completers; import org.jline.builtins.Completers.OptDesc; @@ -288,10 +289,10 @@ private Object classLoader(CommandInput input) { .getPathMatcher("regex:" + arg.replace("\\", "\\\\").replace(".", "\\.") + separator.replace("\\", "\\\\") + ".*\\.jar"); - Files.walk(Paths.get(arg)) - .filter(matcher::matches) - .map(Path::toString) - .forEach(engine.classLoader::addClasspath); + try (Stream pathStream = + Files.walk(Paths.get(arg)).filter(matcher::matches)) { + pathStream.map(Path::toString).forEach(engine.classLoader::addClasspath); + } } else { engine.classLoader.addClasspath(arg); } diff --git a/groovy/src/main/java/org/jline/script/GroovyEngine.java b/groovy/src/main/java/org/jline/script/GroovyEngine.java index 68c0e657b..7ee117c9c 100644 --- a/groovy/src/main/java/org/jline/script/GroovyEngine.java +++ b/groovy/src/main/java/org/jline/script/GroovyEngine.java @@ -18,6 +18,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.groovy.ast.tools.ImmutablePropertyUtils; import org.codehaus.groovy.control.messages.SyntaxErrorMessage; @@ -822,10 +823,8 @@ private static Set sourcesForPackage(String domain) { PathMatcher matcher = FileSystems.getDefault().getPathMatcher("regex:\\." + dom + "[A-Z]+[a-zA-Z]*\\.groovy"); Set out = new HashSet<>(); - try { - List paths = - Files.walk(Paths.get(".")).filter(matcher::matches).collect(Collectors.toList()); - for (Path p : paths) { + try (Stream pathStream = Files.walk(Paths.get(".")).filter(matcher::matches)) { + for (Path p : pathStream.collect(Collectors.toList())) { if (!p.getFileName().toString().matches("[A-Z]+[a-zA-Z]*\\.groovy")) { continue; } diff --git a/reader/src/main/java/org/jline/reader/impl/completer/FileNameCompleter.java b/reader/src/main/java/org/jline/reader/impl/completer/FileNameCompleter.java index 9ab927db1..3b4bd6816 100644 --- a/reader/src/main/java/org/jline/reader/impl/completer/FileNameCompleter.java +++ b/reader/src/main/java/org/jline/reader/impl/completer/FileNameCompleter.java @@ -9,6 +9,7 @@ package org.jline.reader.impl.completer; import java.io.IOException; +import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -71,8 +72,8 @@ public void complete(LineReader reader, ParsedLine commandLine, final List { + try (DirectoryStream directoryStream = Files.newDirectoryStream(current, this::accept)) { + directoryStream.forEach(p -> { String value = curBuf + p.getFileName().toString(); if (Files.isDirectory(p)) { candidates.add(new Candidate(