X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=src%2Fcom%2Fhughes%2Fandroid%2Fdictionary%2Fengine%2FDictionary.java;h=03984315afa7ef2921390fbbfcd14c3a91b22b30;hb=1ef7686713e8930fcb03e45c28805390cb8e6b5a;hp=cb7371442e3bd380cfafe31d32690d02e70546ca;hpb=5f7b259669237dad4cbfdec8536537815846979b;p=Dictionary.git diff --git a/src/com/hughes/android/dictionary/engine/Dictionary.java b/src/com/hughes/android/dictionary/engine/Dictionary.java index cb73714..0398431 100644 --- a/src/com/hughes/android/dictionary/engine/Dictionary.java +++ b/src/com/hughes/android/dictionary/engine/Dictionary.java @@ -20,6 +20,7 @@ import java.io.File; import java.io.IOException; import java.io.PrintStream; import java.io.RandomAccessFile; +import java.nio.BufferUnderflowException; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.util.ArrayList; @@ -188,6 +189,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 +208,15 @@ 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); + } catch (BufferUnderflowException e) { + // Most likely due to a read beyond the buffer limit set, + // do not crash just because of a truncated or corrupt dictionary file + return getErrorDictionaryInfo(file); } finally { if (raf != null) { try {