]> gitweb.fperrin.net Git - Dictionary.git/commitdiff
More robust generation of installed dictionary list.
authorReimar Döffinger <Reimar.Doeffinger@gmx.de>
Sat, 2 May 2020 20:22:49 +0000 (22:22 +0200)
committerReimar Döffinger <Reimar.Doeffinger@gmx.de>
Sat, 2 May 2020 20:22:49 +0000 (22:22 +0200)
With memory-mapped IO truncated files would generate
a IllegalArgumentException instead of IOError, so
handle them both in the same way.

src/com/hughes/android/dictionary/engine/Dictionary.java

index cb7371442e3bd380cfafe31d32690d02e70546ca..5b209b037da3095ebc398683331746062b237f92 100644 (file)
@@ -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 {