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 {
return readTextFile(f).trim();
} catch (IOException e) {
throw new CmdLineException(owner, e.getMessage(), e);
throw new CmdLineException(owner, "Failed to read @file", e);
}
}
return v;

View File

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

View File

@ -195,10 +195,16 @@ public final class FileUtilities {
}
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);
// check BOM
BOM bom = BOM.detect(bytes);
BOM bom = BOM.detect(bytes); // check BOM
if (bom != null) {
return new String(bytes, bom.size(), bytes.length - bom.size(), bom.getCharset());