From: Reimar Döffinger Date: Sat, 2 May 2020 20:22:49 +0000 (+0200) Subject: More robust generation of installed dictionary list. X-Git-Url: http://gitweb.fperrin.net/?p=Dictionary.git;a=commitdiff_plain;h=9cab1701d42d237791c816e8593b36abf515973a More robust generation of installed dictionary list. With memory-mapped IO truncated files would generate a IllegalArgumentException instead of IOError, so handle them both in the same way. --- diff --git a/src/com/hughes/android/dictionary/engine/Dictionary.java b/src/com/hughes/android/dictionary/engine/Dictionary.java index cb73714..5b209b0 100644 --- a/src/com/hughes/android/dictionary/engine/Dictionary.java +++ b/src/com/hughes/android/dictionary/engine/Dictionary.java @@ -188,6 +188,14 @@ public class Dictionary { return result; } + // get DictionaryInfo for case when Dictionary cannot be opened + private static DictionaryInfo getErrorDictionaryInfo(final File file) { + final DictionaryInfo dictionaryInfo = new DictionaryInfo(); + dictionaryInfo.uncompressedFilename = file.getName(); + dictionaryInfo.uncompressedBytes = file.length(); + return dictionaryInfo; + } + public static DictionaryInfo getDictionaryInfo(final File file) { RandomAccessFile raf = null; try { @@ -199,10 +207,11 @@ public class Dictionary { raf.close(); return dictionaryInfo; } catch (IOException e) { - final DictionaryInfo dictionaryInfo = new DictionaryInfo(); - dictionaryInfo.uncompressedFilename = file.getName(); - dictionaryInfo.uncompressedBytes = file.length(); - return dictionaryInfo; + return getErrorDictionaryInfo(file); + } catch (IllegalArgumentException e) { + // Most likely due to a Buffer.limit beyond size of file, + // do not crash just because of a truncated dictionary file + return getErrorDictionaryInfo(file); } finally { if (raf != null) { try {