IOException if text file is insanely huge (>1GB)

@see https://www.filebot.net/forums/viewtopic.php?f=10&t=4364
This commit is contained in:
Reinhard Pointner 2016-11-21 02:25:46 +08:00
parent 8932eb0b2a
commit 28fa511fbe
3 changed files with 22 additions and 15 deletions

View File

@ -70,7 +70,7 @@ public class BindingsHandler extends MapOptionHandler {
try { try {
return readTextFile(f).trim(); return readTextFile(f).trim();
} catch (IOException e) { } catch (IOException e) {
throw new CmdLineException(owner, e.getMessage(), e); throw new CmdLineException(owner, "Failed to read @file", e);
} }
} }
return v; return v;

View File

@ -583,11 +583,15 @@ public class MediaDetection {
} }
// try to grep imdb id from nfo files // try to grep imdb id from nfo files
for (int imdbid : grepImdbIdFor(movieFile)) { try {
Movie movie = service.getMovieDescriptor(new Movie(imdbid), locale); for (int imdbid : grepImdbIdFor(movieFile)) {
if (movie != null) { Movie movie = service.getMovieDescriptor(new Movie(imdbid), locale);
options.add(movie); if (movie != null) {
options.add(movie);
}
} }
} catch (Exception e) {
debug.warning("Failed to lookup info by id: " + e);
} }
} }
@ -1210,20 +1214,16 @@ public class MediaDetection {
public static Set<Integer> grepImdbIdFor(File file) throws Exception { public static Set<Integer> grepImdbIdFor(File file) throws Exception {
Set<Integer> collection = new LinkedHashSet<Integer>(); Set<Integer> collection = new LinkedHashSet<Integer>();
List<File> nfoFiles = new ArrayList<File>(); List<File> nfoFiles = new ArrayList<File>();
if (file.isDirectory()) { if (file.isDirectory()) {
nfoFiles.addAll(listFiles(file, NFO_FILES)); nfoFiles.addAll(listFiles(file, NFO_FILES));
} else if (file.getParentFile() != null && file.getParentFile().isDirectory()) { } else if (file.getParentFile() != null && file.getParentFile().isDirectory()) {
nfoFiles.addAll(getChildren(file.getParentFile(), NFO_FILES)); nfoFiles.addAll(getChildren(file.getParentFile(), NFO_FILES));
} }
// parse ids from nfo files // parse IMDb IDs from NFO files
for (File nfo : nfoFiles) { for (File nfo : nfoFiles) {
try { collection.addAll(grepImdbId(readTextFile(nfo)));
String text = readTextFile(nfo);
collection.addAll(grepImdbId(text));
} catch (Exception e) {
debug.warning("Failed to read nfo: " + e.getMessage());
}
} }
return collection; return collection;
@ -1242,8 +1242,9 @@ public class MediaDetection {
// search for id in sibling nfo files // search for id in sibling nfo files
for (File folder : folders) { for (File folder : folders) {
if (!folder.exists()) if (!folder.exists()) {
continue; continue;
}
for (File nfo : getChildren(folder, NFO_FILES)) { for (File nfo : getChildren(folder, NFO_FILES)) {
String text = readTextFile(nfo); String text = readTextFile(nfo);

View File

@ -195,10 +195,16 @@ public final class FileUtilities {
} }
public static String readTextFile(File file) throws IOException { public static String readTextFile(File file) throws IOException {
long size = file.length();
// ignore absurdly large text files that might cause OutOfMemoryError issues
if (size > ONE_GIGABYTE) {
throw new IOException(String.format("Text file is too large: %s (%s)", file, formatSize(size)));
}
byte[] bytes = readFile(file); byte[] bytes = readFile(file);
// check BOM BOM bom = BOM.detect(bytes); // check BOM
BOM bom = BOM.detect(bytes);
if (bom != null) { if (bom != null) {
return new String(bytes, bom.size(), bytes.length - bom.size(), bom.getCharset()); return new String(bytes, bom.size(), bytes.length - bom.size(), bom.getCharset());