Make sure file handles are closed immediately

This commit is contained in:
Reinhard Pointner 2016-08-24 03:27:27 +08:00
parent d5cf630fe5
commit 2210fbca9d
3 changed files with 11 additions and 18 deletions

View File

@ -1,7 +1,6 @@
package net.filebot.format; package net.filebot.format;
import static java.util.stream.Collectors.*; import static java.util.stream.Collectors.*;
import static net.filebot.util.FileUtilities.*;
import static net.filebot.util.RegularExpressions.*; import static net.filebot.util.RegularExpressions.*;
import java.io.File; import java.io.File;
@ -15,6 +14,7 @@ import java.util.stream.Stream;
import groovy.lang.Closure; import groovy.lang.Closure;
import groovy.util.XmlSlurper; import groovy.util.XmlSlurper;
import net.filebot.util.FileUtilities;
/** /**
* Global functions available in the {@link ExpressionFormat} * Global functions available in the {@link ExpressionFormat}
@ -57,7 +57,7 @@ public class ExpressionFormatFunctions {
public static Map<String, String> csv(String path) throws IOException { public static Map<String, String> csv(String path) throws IOException {
Pattern[] delimiter = { TAB, SEMICOLON }; Pattern[] delimiter = { TAB, SEMICOLON };
Map<String, String> map = new LinkedHashMap<String, String>(); Map<String, String> map = new LinkedHashMap<String, String>();
streamLines(new File(path)).forEach(line -> { for (String line : readLines(path)) {
for (Pattern d : delimiter) { for (Pattern d : delimiter) {
String[] field = d.split(line, 2); String[] field = d.split(line, 2);
if (field.length >= 2) { if (field.length >= 2) {
@ -65,12 +65,12 @@ public class ExpressionFormatFunctions {
break; break;
} }
} }
}); }
return map; return map;
} }
public static List<String> readLines(String path) throws IOException { public static List<String> 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 { public static Object readXml(String path) throws Exception {

View File

@ -158,13 +158,13 @@ public class FileSet extends AbstractSet<Path> {
} }
public void load(File f) throws IOException { public void load(File f) throws IOException {
streamLines(f).forEach(path -> { for (String path : readLines(f)) {
try { try {
add(Paths.get(path)); add(Paths.get(path));
} catch (InvalidPathException e) { } catch (InvalidPathException e) {
debug.warning(e::toString); debug.warning(e::toString);
} }
}); }
} }
public void append(File f, Collection<?>... paths) throws IOException { public void append(File f, Collection<?>... paths) throws IOException {

View File

@ -43,10 +43,8 @@ import java.util.Map;
import java.util.SortedMap; import java.util.SortedMap;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.logging.Level;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Stream;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
@ -193,19 +191,14 @@ public final class FileUtilities {
return Files.readAllBytes(source.toPath()); return Files.readAllBytes(source.toPath());
} }
public static Stream<String> streamLines(File file) throws IOException { public static List<String> readLines(File file) throws IOException {
BufferedReader reader = new BufferedReader(new UnicodeReader(new BufferedInputStream(new FileInputStream(file)), false, UTF_8), BUFFER_SIZE); try (BufferedReader reader = new BufferedReader(new UnicodeReader(new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE), false, UTF_8), BUFFER_SIZE)) {
return reader.lines().onClose(() -> { return reader.lines().collect(toList());
try {
reader.close();
} catch (Exception e) {
debug.log(Level.SEVERE, "Failed to close file: " + file, e);
} }
});
} }
public static String readTextFile(File file) throws IOException { 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 { public static File writeFile(ByteBuffer data, File destination) throws IOException {