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;
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<String, String> csv(String path) throws IOException {
Pattern[] delimiter = { TAB, SEMICOLON };
Map<String, String> map = new LinkedHashMap<String, String>();
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<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 {

View File

@ -158,13 +158,13 @@ public class FileSet extends AbstractSet<Path> {
}
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 {

View File

@ -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<String> 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<String> 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 {