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