1 // Copyright 2011 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 package com.hughes.android.dictionary.engine;
17 import com.hughes.android.dictionary.DictionaryInfo;
18 import com.hughes.util.CachingList;
19 import com.hughes.util.raf.RAFList;
20 import com.hughes.util.raf.RAFListSerializer;
21 import com.hughes.util.raf.RAFSerializable;
23 import java.io.DataInput;
24 import java.io.DataInputStream;
25 import java.io.DataOutput;
27 import java.io.IOException;
28 import java.io.PrintStream;
29 import java.io.RandomAccessFile;
30 import java.nio.channels.Channels;
31 import java.nio.channels.FileChannel;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
36 public class Dictionary implements RAFSerializable<Dictionary> {
38 private static final int CACHE_SIZE = 5000;
40 private static final int CURRENT_DICT_VERSION = 7;
41 private static final String END_OF_DICTIONARY = "END OF DICTIONARY";
44 final int dictFileVersion;
45 private final long creationMillis;
46 public final String dictInfo;
47 public final List<PairEntry> pairEntries;
48 public final List<TextEntry> textEntries;
49 public final List<HtmlEntry> htmlEntries;
50 public final List<byte[]> htmlData;
51 public final List<EntrySource> sources;
52 public final List<Index> indices;
55 * dictFileVersion 1 adds: <li>links to sources? dictFileVersion 2 adds: <li>
56 * counts of tokens in indices.
59 public Dictionary(final String dictInfo) {
60 this.dictFileVersion = CURRENT_DICT_VERSION;
61 this.creationMillis = System.currentTimeMillis();
62 this.dictInfo = dictInfo;
63 pairEntries = new ArrayList<>();
64 textEntries = new ArrayList<>();
65 htmlEntries = new ArrayList<>();
67 sources = new ArrayList<>();
68 indices = new ArrayList<>();
71 public Dictionary(final FileChannel ch) throws IOException {
72 DataInput raf = new DataInputStream(Channels.newInputStream(ch));
73 dictFileVersion = raf.readInt();
74 if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
75 throw new IOException("Invalid dictionary version: " + dictFileVersion);
77 creationMillis = raf.readLong();
78 dictInfo = raf.readUTF();
80 // Load the sources, then seek past them, because reading them later
81 // disrupts the offset.
83 final RAFList<EntrySource> rafSources = RAFList.create(ch, new EntrySource.Serializer(
84 this), ch.position(), dictFileVersion, dictInfo + " sources: ");
85 sources = new ArrayList<>(rafSources);
86 ch.position(rafSources.getEndOffset());
88 pairEntries = CachingList.create(
89 RAFList.create(ch, new PairEntry.Serializer(this), ch.position(), dictFileVersion, dictInfo + " pairs: "),
91 textEntries = CachingList.create(
92 RAFList.create(ch, new TextEntry.Serializer(this), ch.position(), dictFileVersion, dictInfo + " text: "),
94 if (dictFileVersion >= 5) {
95 htmlEntries = CachingList.create(
96 RAFList.create(ch, new HtmlEntry.Serializer(this, ch), ch.position(), dictFileVersion, dictInfo + " html: "),
99 htmlEntries = Collections.emptyList();
101 if (dictFileVersion >= 7) {
102 htmlData = RAFList.create(ch, new HtmlEntry.DataDeserializer(), ch.position(), dictFileVersion, dictInfo + " html: ");
106 indices = CachingList.createFullyCached(RAFList.create(ch, new IndexSerializer(ch),
107 ch.position(), dictFileVersion, dictInfo + " index: "));
108 } catch (RuntimeException e) {
109 throw new IOException("RuntimeException loading dictionary", e);
111 final String end = raf.readUTF();
112 if (!end.equals(END_OF_DICTIONARY)) {
113 throw new IOException("Dictionary seems corrupt: " + end);
118 public void write(DataOutput out) throws IOException {
119 RandomAccessFile raf = (RandomAccessFile)out;
120 raf.writeInt(dictFileVersion);
121 raf.writeLong(creationMillis);
122 raf.writeUTF(dictInfo);
123 System.out.println("sources start: " + raf.getFilePointer());
124 RAFList.write(raf, sources, new EntrySource.Serializer(this));
125 System.out.println("pair start: " + raf.getFilePointer());
126 RAFList.write(raf, pairEntries, new PairEntry.Serializer(this), 64, true);
127 System.out.println("text start: " + raf.getFilePointer());
128 RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
129 System.out.println("html index start: " + raf.getFilePointer());
130 RAFList.write(raf, htmlEntries, new HtmlEntry.Serializer(this, null), 64, true);
131 System.out.println("html data start: " + raf.getFilePointer());
132 assert htmlData == null;
133 RAFList.write(raf, htmlEntries, new HtmlEntry.DataSerializer(), 128, true);
134 System.out.println("indices start: " + raf.getFilePointer());
135 RAFList.write(raf, indices, new IndexSerializer(null));
136 System.out.println("end: " + raf.getFilePointer());
137 raf.writeUTF(END_OF_DICTIONARY);
140 private final class IndexSerializer implements RAFListSerializer<Index> {
141 private final FileChannel ch;
143 IndexSerializer(FileChannel ch) {
148 public Index read(DataInput raf, final int readIndex) throws IOException {
149 return new Index(Dictionary.this, ch, raf);
153 public void write(DataOutput raf, Index t) throws IOException {
158 final RAFListSerializer<HtmlEntry> htmlEntryIndexSerializer = new RAFListSerializer<HtmlEntry>() {
160 public void write(DataOutput raf, HtmlEntry t) {
165 public HtmlEntry read(DataInput raf, int readIndex) throws IOException {
166 return htmlEntries.get(raf.readInt());
170 public void print(final PrintStream out) {
171 out.println("dictInfo=" + dictInfo);
172 for (final EntrySource entrySource : sources) {
173 out.printf("EntrySource: %s %d\n", entrySource.name, entrySource.numEntries);
176 for (final Index index : indices) {
177 out.printf("Index: %s %s\n", index.shortName, index.longName);
183 public DictionaryInfo getDictionaryInfo() {
184 final DictionaryInfo result = new DictionaryInfo();
185 result.creationMillis = this.creationMillis;
186 result.dictInfo = this.dictInfo;
187 for (final Index index : indices) {
188 result.indexInfos.add(index.getIndexInfo());
193 public static DictionaryInfo getDictionaryInfo(final File file) {
194 RandomAccessFile raf = null;
196 raf = new RandomAccessFile(file, "r");
197 final Dictionary dict = new Dictionary(raf.getChannel());
198 final DictionaryInfo dictionaryInfo = dict.getDictionaryInfo();
199 dictionaryInfo.uncompressedFilename = file.getName();
200 dictionaryInfo.uncompressedBytes = file.length();
202 return dictionaryInfo;
203 } catch (IOException e) {
204 final DictionaryInfo dictionaryInfo = new DictionaryInfo();
205 dictionaryInfo.uncompressedFilename = file.getName();
206 dictionaryInfo.uncompressedBytes = file.length();
207 return dictionaryInfo;
212 } catch (IOException e) {