- creationMillis = raf.readLong();
- 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()));
- final String end = raf.readUTF();
- if (!end.equals(END_OF_DICTIONARY)) {
- throw new IOException("Dictionary seems corrupt: " + end);
+
+ public Dictionary(final FileChannel ch) throws IOException {
+ DataInput raf = new DataInputStream(Channels.newInputStream(ch));
+ dictFileVersion = raf.readInt();
+ if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
+ throw new IOException("Invalid dictionary version: " + dictFileVersion);
+ }
+ creationMillis = raf.readLong();
+ dictInfo = raf.readUTF();
+
+ // Load the sources, then seek past them, because reading them later
+ // disrupts the offset.
+ try {
+ final RAFList<EntrySource> rafSources = RAFList.create(ch, new EntrySource.Serializer(
+ this), ch.position(), dictFileVersion, dictInfo + " sources: ");
+ sources = new ArrayList<EntrySource>(rafSources);
+ ch.position(rafSources.getEndOffset());
+
+ pairEntries = CachingList.create(
+ RAFList.create(ch, new PairEntry.Serializer(this), ch.position(), dictFileVersion, dictInfo + " pairs: "),
+ CACHE_SIZE);
+ textEntries = CachingList.create(
+ RAFList.create(ch, new TextEntry.Serializer(this), ch.position(), dictFileVersion, dictInfo + " text: "),
+ CACHE_SIZE);
+ if (dictFileVersion >= 5) {
+ htmlEntries = CachingList.create(
+ RAFList.create(ch, new HtmlEntry.Serializer(this, ch), ch.position(), dictFileVersion, dictInfo + " html: "),
+ CACHE_SIZE);
+ } else {
+ htmlEntries = Collections.emptyList();
+ }
+ if (dictFileVersion >= 7) {
+ htmlData = RAFList.create(ch, new HtmlEntry.DataDeserializer(), ch.position(), dictFileVersion, dictInfo + " html: ");
+ } else {
+ htmlData = null;
+ }
+ indices = CachingList.createFullyCached(RAFList.create(ch, new IndexSerializer(ch),
+ ch.position(), dictFileVersion, dictInfo + " index: "));
+ } 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);
+ }