Skip to content

Commit

Permalink
Close Files streams using try-with-resources (#840)
Browse files Browse the repository at this point in the history
Co-authored-by: Guillaume Nodet <gnodet@gmail.com>
  • Loading branch information
mattirn and gnodet authored Oct 4, 2023
1 parent ceda3c4 commit 2f02183
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 77 deletions.
13 changes: 6 additions & 7 deletions builtins/src/main/java/org/jline/builtins/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ protected static List<Path> 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<Path> pathStream = Files.walk(searchRoot)) {
return pathStream.filter(pathMatcher::matches).collect(Collectors.toList());
}
}

public static void history(LineReader reader, PrintStream out, PrintStream err, Path currentDir, String[] argv)
Expand Down Expand Up @@ -1677,11 +1678,9 @@ public static void highlighter(
String parameter = replaceFileName(currentTheme, "*" + TYPE_NANORCTHEME);
out.println(currentTheme.getParent() + ":");
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + parameter);
Files.find(
Paths.get(new File(parameter).getParent()),
Integer.MAX_VALUE,
(path, f) -> pathMatcher.matches(path))
.forEach(p -> out.println(p.getFileName()));
try (Stream<Path> pathStream = Files.walk(Paths.get(new File(parameter).getParent()))) {
pathStream.filter(pathMatcher::matches).forEach(p -> out.println(p.getFileName()));
}
} else {
File themeFile;
if (opt.isSet("view")) {
Expand Down
6 changes: 3 additions & 3 deletions builtins/src/main/java/org/jline/builtins/Less.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -155,9 +156,8 @@ 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<Path> pathStream = Files.walk(Paths.get("/usr/share/nano"))) {
pathStream.filter(pathMatcher::matches).forEach(syntaxFiles::add);
nanorcIgnoreErrors = true;
} catch (IOException e) {
errorMessage = "Encountered error while reading nanorc files";
Expand Down
6 changes: 3 additions & 3 deletions builtins/src/main/java/org/jline/builtins/Nano.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.jline.keymap.BindingReader;
import org.jline.keymap.KeyMap;
Expand Down Expand Up @@ -1575,9 +1576,8 @@ public Nano(Terminal terminal, Path root, Options opts, ConfigurationPath config
}
} 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<Path> pathStream = Files.walk(Paths.get("/usr/share/nano"))) {
pathStream.filter(pathMatcher::matches).forEach(syntaxFiles::add);
nanorcIgnoreErrors = true;
} catch (IOException e) {
errorMessage = "Encountered error while reading nanorc files";
Expand Down
13 changes: 7 additions & 6 deletions builtins/src/main/java/org/jline/builtins/Source.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.stream.Stream;

public interface Source {

Expand Down Expand Up @@ -47,9 +48,9 @@ public InputStream read() throws IOException {
@Override
public Long lines() {
Long out = null;
try {
out = Files.lines(new File(url.toURI()).toPath()).count();
} catch (Exception e) {
try (Stream<String> lines = Files.lines(new File(url.toURI()).toPath())) {
out = lines.count();
} catch (Exception ignore) {
}
return out;
}
Expand Down Expand Up @@ -81,9 +82,9 @@ public InputStream read() throws IOException {
@Override
public Long lines() {
Long out = null;
try {
out = Files.lines(path).count();
} catch (Exception e) {
try (Stream<String> lines = Files.lines(path)) {
out = lines.count();
} catch (Exception ignore) {
}
return out;
}
Expand Down
28 changes: 11 additions & 17 deletions builtins/src/main/java/org/jline/builtins/SyntaxHighlighter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.*;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Stream;

import org.jline.utils.*;

Expand Down Expand Up @@ -149,29 +151,21 @@ public static SyntaxHighlighter build(Path nanorc, String syntaxName) {
}

protected static void nanorcInclude(String parameter, List<Path> syntaxFiles) throws IOException {
if (parameter.contains("*") || parameter.contains("?")) {
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + parameter);
Files.find(
Paths.get(new File(parameter).getParent()),
Integer.MAX_VALUE,
(path, f) -> pathMatcher.matches(path))
.forEach(syntaxFiles::add);
} else {
syntaxFiles.add(Paths.get(parameter));
}
addFiles(parameter, s -> s.forEach(syntaxFiles::add));
}

protected static void nanorcTheme(String parameter, List<Path> syntaxFiles) throws IOException {
addFiles(parameter, s -> s.findFirst().ifPresent(p -> syntaxFiles.add(0, p)));
}

protected static void addFiles(String parameter, Consumer<Stream<Path>> consumer) throws IOException {
if (parameter.contains("*") || parameter.contains("?")) {
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + parameter);
Files.find(
Paths.get(new File(parameter).getParent()),
Integer.MAX_VALUE,
(path, f) -> pathMatcher.matches(path))
.findFirst()
.ifPresent(path -> syntaxFiles.add(0, path));
try (Stream<Path> pathStream = Files.walk(Paths.get(new File(parameter).getParent()))) {
consumer.accept(pathStream.filter(pathMatcher::matches));
}
} else {
syntaxFiles.add(0, Paths.get(parameter));
consumer.accept(Stream.of(Paths.get(parameter)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -237,11 +238,10 @@ public Map<String, Boolean> 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<Path> pathStream =
Files.walk(new File(regex).getParentFile().toPath())) {
pathStream.filter(pathMatcher::matches).forEach(scripts::add);
}
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions demo/src/main/java/org/apache/felix/gogo/jline/Posix.java
Original file line number Diff line number Diff line change
Expand Up @@ -1025,8 +1025,11 @@ 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<Path> pathStream = Files.walk(session.currentDir())) {
pathStream
.filter(pathMatcher::matches)
.forEach(p -> sources.add(doUrlSource(session.currentDir(), p)));
}
} else {
sources.add(new PathSource(session.currentDir().resolve(arg), arg));
}
Expand Down Expand Up @@ -1432,10 +1435,12 @@ else if (optCol) {
if (expanded.size() > 1) {
out.println(currentDir.relativize(path).toString() + ":");
}
display.accept(Stream.concat(Stream.of(".", "..").map(path::resolve), Files.list(path))
.filter(filter)
.map(p -> new PathEntry(p, path))
.sorted());
try (Stream<Path> pathStream = Files.list(path)) {
display.accept(Stream.concat(Stream.of(".", "..").map(path::resolve), pathStream)
.filter(filter)
.map(p -> new PathEntry(p, path))
.sorted());
}
}
}

Expand Down
11 changes: 7 additions & 4 deletions groovy/src/main/java/org/jline/script/GroovyCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -289,10 +290,12 @@ 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<Path> pathStream = Files.walk(Paths.get(arg))) {
pathStream
.filter(matcher::matches)
.map(Path::toString)
.forEach(engine.classLoader::addClasspath);
}
} else {
engine.classLoader.addClasspath(arg);
}
Expand Down
44 changes: 20 additions & 24 deletions groovy/src/main/java/org/jline/script/GroovyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -809,39 +810,34 @@ private static Set<String> sourcesForPackage(String domain) {
if (separator.equals("\\")) {
separator += separator;
}
String dom;
boolean onlyPackage = domain != null && domain.endsWith("*");
if (onlyPackage) {
domain = domain.substring(0, domain.lastIndexOf("."));
}
if (domain != null) {
dom = domain.isEmpty() ? ".*" + separator : separator + domain.replace(".", separator) + "(|.*)";
} else {
String pkg = domain;
String dom;
if (domain == null) {
dom = separator + "(|.*)";
} else if (domain.isEmpty()) {
dom = ".*" + separator;
} else {
dom = separator + domain.replace(".", separator) + "(|.*)";
}
PathMatcher matcher =
FileSystems.getDefault().getPathMatcher("regex:\\." + dom + "[A-Z]+[a-zA-Z]*\\.groovy");
dom = "regex:\\." + dom + "[A-Z]+[a-zA-Z]*\\.groovy";
PathMatcher matcher = FileSystems.getDefault().getPathMatcher(dom);
Set<String> out = new HashSet<>();
try {
List<Path> paths =
Files.walk(Paths.get(".")).filter(matcher::matches).collect(Collectors.toList());
for (Path p : paths) {
if (!p.getFileName().toString().matches("[A-Z]+[a-zA-Z]*\\.groovy")) {
continue;
}
String source = p.toString();
String className = source.substring(2, source.lastIndexOf("."))
.replace(FileSystems.getDefault().getSeparator(), ".");
if (onlyPackage) {
if (Character.isUpperCase(className.charAt(domain.length() + 1))) {
out.add(className);
}
} else {
out.add(className);
}
try (Stream<Path> pathStream = Files.walk(Paths.get("."))) {
Stream<String> classes = pathStream
.filter(matcher::matches)
.filter(p -> p.getFileName().toString().matches("[A-Z]+[a-zA-Z]*\\.groovy"))
.map(Path::toString)
.map(source -> source.substring(2, source.lastIndexOf("."))
.replace(FileSystems.getDefault().getSeparator(), "."));
if (onlyPackage) {
classes = classes.filter(cl -> Character.isUpperCase(cl.charAt(pkg.length() + 1)));
}
classes.forEach(out::add);
} catch (Exception ignore) {

}
return out;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,8 +72,8 @@ public void complete(LineReader reader, ParsedLine commandLine, final List<Candi
curBuf = "";
current = getUserDir();
}
try {
Files.newDirectoryStream(current, this::accept).forEach(p -> {
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(current, this::accept)) {
directoryStream.forEach(p -> {
String value = curBuf + p.getFileName().toString();
if (Files.isDirectory(p)) {
candidates.add(new Candidate(
Expand Down

0 comments on commit 2f02183

Please sign in to comment.