From 2210fbca9d80a38d48670e840e6b16af141abb30 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Wed, 24 Aug 2016 03:27:27 +0800 Subject: [PATCH] Make sure file handles are closed immediately --- .../format/ExpressionFormatFunctions.java | 8 ++++---- source/net/filebot/util/FileSet.java | 4 ++-- source/net/filebot/util/FileUtilities.java | 17 +++++------------ 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/source/net/filebot/format/ExpressionFormatFunctions.java b/source/net/filebot/format/ExpressionFormatFunctions.java index df53f308..3fef3317 100644 --- a/source/net/filebot/format/ExpressionFormatFunctions.java +++ b/source/net/filebot/format/ExpressionFormatFunctions.java @@ -1,7 +1,6 @@ package net.filebot.format; import static java.util.stream.Collectors.*; -import static net.filebot.util.FileUtilities.*; import static net.filebot.util.RegularExpressions.*; import java.io.File; @@ -15,6 +14,7 @@ import java.util.stream.Stream; import groovy.lang.Closure; import groovy.util.XmlSlurper; +import net.filebot.util.FileUtilities; /** * Global functions available in the {@link ExpressionFormat} @@ -57,7 +57,7 @@ public class ExpressionFormatFunctions { public static Map csv(String path) throws IOException { Pattern[] delimiter = { TAB, SEMICOLON }; Map map = new LinkedHashMap(); - streamLines(new File(path)).forEach(line -> { + for (String line : readLines(path)) { for (Pattern d : delimiter) { String[] field = d.split(line, 2); if (field.length >= 2) { @@ -65,12 +65,12 @@ public class ExpressionFormatFunctions { break; } } - }); + } return map; } public static List readLines(String path) throws IOException { - return streamLines(new File(path)).collect(toList()); + return FileUtilities.readLines(new File(path)); } public static Object readXml(String path) throws Exception { diff --git a/source/net/filebot/util/FileSet.java b/source/net/filebot/util/FileSet.java index 82f408ec..7f46f049 100644 --- a/source/net/filebot/util/FileSet.java +++ b/source/net/filebot/util/FileSet.java @@ -158,13 +158,13 @@ public class FileSet extends AbstractSet { } public void load(File f) throws IOException { - streamLines(f).forEach(path -> { + for (String path : readLines(f)) { try { add(Paths.get(path)); } catch (InvalidPathException e) { debug.warning(e::toString); } - }); + } } public void append(File f, Collection... paths) throws IOException { diff --git a/source/net/filebot/util/FileUtilities.java b/source/net/filebot/util/FileUtilities.java index 2d1e9a5a..3e0337cb 100644 --- a/source/net/filebot/util/FileUtilities.java +++ b/source/net/filebot/util/FileUtilities.java @@ -43,10 +43,8 @@ import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; import java.util.TreeSet; -import java.util.logging.Level; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.stream.Stream; import org.apache.commons.io.FileUtils; @@ -193,19 +191,14 @@ public final class FileUtilities { return Files.readAllBytes(source.toPath()); } - public static Stream streamLines(File file) throws IOException { - BufferedReader reader = new BufferedReader(new UnicodeReader(new BufferedInputStream(new FileInputStream(file)), false, UTF_8), BUFFER_SIZE); - return reader.lines().onClose(() -> { - try { - reader.close(); - } catch (Exception e) { - debug.log(Level.SEVERE, "Failed to close file: " + file, e); - } - }); + public static List readLines(File file) throws IOException { + try (BufferedReader reader = new BufferedReader(new UnicodeReader(new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE), false, UTF_8), BUFFER_SIZE)) { + return reader.lines().collect(toList()); + } } public static String readTextFile(File file) throws IOException { - return streamLines(file).collect(joining(System.lineSeparator())); + return String.join(System.lineSeparator(), readLines(file)); } public static File writeFile(ByteBuffer data, File destination) throws IOException {