package com.hughes.android.dictionary.engine;
+import com.hughes.android.dictionary.DictionaryInfo;
+import com.hughes.util.CachingList;
+import com.hughes.util.raf.RAFList;
+import com.hughes.util.raf.RAFListSerializer;
+import com.hughes.util.raf.RAFSerializable;
+
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
-import com.hughes.android.dictionary.DictionaryInfo;
-import com.hughes.util.CachingList;
-import com.hughes.util.raf.RAFList;
-import com.hughes.util.raf.RAFListSerializer;
-import com.hughes.util.raf.RAFSerializable;
-
public class Dictionary implements RAFSerializable<Dictionary> {
static final int CACHE_SIZE = 5000;
- static final int CURRENT_DICT_VERSION = 3;
+ static final int CURRENT_DICT_VERSION = 6;
static final String END_OF_DICTIONARY = "END OF DICTIONARY";
// persisted
public final String dictInfo;
public final List<PairEntry> pairEntries;
public final List<TextEntry> textEntries;
+ public final List<HtmlEntry> htmlEntries;
public final List<EntrySource> sources;
public final List<Index> indices;
this.dictInfo = dictInfo;
pairEntries = new ArrayList<PairEntry>();
textEntries = new ArrayList<TextEntry>();
+ htmlEntries = new ArrayList<HtmlEntry>();
sources = new ArrayList<EntrySource>();
indices = new ArrayList<Index>();
}
dictInfo = raf.readUTF();
// Load the sources, then seek past them, because reading them later disrupts the offset.
- final RAFList<EntrySource> rafSources = RAFList.create(raf, new EntrySource.Serializer(this), raf.getFilePointer());
- sources = new ArrayList<EntrySource>(rafSources);
- raf.seek(rafSources.getEndOffset());
-
- pairEntries = CachingList.create(RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
- textEntries = CachingList.create(RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
- indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer, raf.getFilePointer()));
+ try {
+ final RAFList<EntrySource> rafSources = RAFList.create(raf, new EntrySource.Serializer(this), raf.getFilePointer());
+ sources = new ArrayList<EntrySource>(rafSources);
+ raf.seek(rafSources.getEndOffset());
+
+ pairEntries = CachingList.create(RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
+ textEntries = CachingList.create(RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
+ if (dictFileVersion >= 5) {
+ htmlEntries = CachingList.create(RAFList.create(raf, new HtmlEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
+ } else {
+ htmlEntries = Collections.emptyList();
+ }
+ indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer, raf.getFilePointer()));
+ } catch (RuntimeException e) {
+ final IOException ioe = new IOException("RuntimeException loading dictionary");
+ ioe.initCause(e);
+ throw ioe;
+ }
final String end = raf.readUTF();
if (!end.equals(END_OF_DICTIONARY)) {
throw new IOException("Dictionary seems corrupt: " + end);
RAFList.write(raf, sources, new EntrySource.Serializer(this));
RAFList.write(raf, pairEntries, new PairEntry.Serializer(this));
RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
+ RAFList.write(raf, htmlEntries, new HtmlEntry.Serializer(this));
RAFList.write(raf, indices, indexSerializer);
raf.writeUTF(END_OF_DICTIONARY);
}
t.write(raf);
}};
+ final RAFListSerializer<HtmlEntry> htmlEntryIndexSerializer = new RAFListSerializer<HtmlEntry>() {
+ @Override
+ public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
+ if (t.index() == -1) throw new IndexOutOfBoundsException();
+ raf.writeInt(t.index());
+ }
+ @Override
+ public HtmlEntry read(RandomAccessFile raf, int readIndex) throws IOException {
+ return htmlEntries.get(raf.readInt());
+ }};
+
public void print(final PrintStream out) {
out.println("dictInfo=" + dictInfo);
for (final EntrySource entrySource : sources) {
raf = new RandomAccessFile(file, "r");
final Dictionary dict = new Dictionary(raf);
final DictionaryInfo dictionaryInfo = dict.getDictionaryInfo();
+ dictionaryInfo.uncompressedFilename = file.getName();
+ dictionaryInfo.uncompressedBytes = file.length();
raf.close();
return dictionaryInfo;
} catch (IOException e) {