X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=src%2Fcom%2Fhughes%2Fandroid%2Fdictionary%2Fengine%2FDictionary.java;h=5b209b037da3095ebc398683331746062b237f92;hb=9cab1701d42d237791c816e8593b36abf515973a;hp=c8e6cb197d6e51c5fe5a06e0d209762c1697ccd9;hpb=35b7b7dc537441278934398a6b81009c1ec42bbf;p=Dictionary.git diff --git a/src/com/hughes/android/dictionary/engine/Dictionary.java b/src/com/hughes/android/dictionary/engine/Dictionary.java index c8e6cb1..5b209b0 100644 --- a/src/com/hughes/android/dictionary/engine/Dictionary.java +++ b/src/com/hughes/android/dictionary/engine/Dictionary.java @@ -15,17 +15,13 @@ package com.hughes.android.dictionary.engine; import java.io.DataInput; -import java.io.DataInputStream; import java.io.DataOutput; import java.io.File; import java.io.IOException; -import java.io.ObjectOutputStream; import java.io.PrintStream; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; -import java.nio.channels.Channels; import java.nio.channels.FileChannel; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -35,9 +31,8 @@ import com.hughes.util.CachingList; import com.hughes.util.DataInputBuffer; import com.hughes.util.raf.RAFList; import com.hughes.util.raf.RAFListSerializer; -import com.hughes.util.raf.RAFSerializable; -public class Dictionary implements RAFSerializable { +public class Dictionary { private static final int CACHE_SIZE = 5000; @@ -54,6 +49,9 @@ public class Dictionary implements RAFSerializable { public final List htmlData; public final List sources; public final List indices; + // Could be a local variable in constructor, but + // this way avoids a native-image VM bug. + private final MappedByteBuffer wholefile; /** * dictFileVersion 1 adds:
  • links to sources? dictFileVersion 2 adds:
  • @@ -70,10 +68,11 @@ public class Dictionary implements RAFSerializable { htmlData = null; sources = new ArrayList<>(); indices = new ArrayList<>(); + wholefile = null; } public Dictionary(final FileChannel ch) throws IOException { - MappedByteBuffer wholefile = ch.map(FileChannel.MapMode.READ_ONLY, 0, ch.size()); + wholefile = ch.map(FileChannel.MapMode.READ_ONLY, 0, ch.size()); DataInputBuffer in = new DataInputBuffer(wholefile, 0); dictFileVersion = in.readInt(); if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) { @@ -119,7 +118,6 @@ public class Dictionary implements RAFSerializable { } } - @Override public void write(DataOutput out) throws IOException { RandomAccessFile raf = (RandomAccessFile)out; if (dictFileVersion < 7) throw new RuntimeException("write function cannot write formats older than v7!"); @@ -190,6 +188,14 @@ public class Dictionary implements RAFSerializable { 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 { @@ -201,10 +207,11 @@ public class Dictionary implements RAFSerializable { 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 {