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 java.io.DataInput;
18 import java.io.DataInputStream;
19 import java.io.DataOutput;
21 import java.io.IOException;
22 import java.io.ObjectOutputStream;
23 import java.io.PrintStream;
24 import java.io.RandomAccessFile;
25 import java.nio.MappedByteBuffer;
26 import java.nio.channels.Channels;
27 import java.nio.channels.FileChannel;
28 import java.nio.charset.StandardCharsets;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
33 import com.hughes.android.dictionary.DictionaryInfo;
34 import com.hughes.util.CachingList;
35 import com.hughes.util.DataInputBuffer;
36 import com.hughes.util.raf.RAFList;
37 import com.hughes.util.raf.RAFListSerializer;
38 import com.hughes.util.raf.RAFSerializable;
40 public class Dictionary implements RAFSerializable<Dictionary> {
42 private static final int CACHE_SIZE = 5000;
44 private static final int CURRENT_DICT_VERSION = 7;
45 private static final String END_OF_DICTIONARY = "END OF DICTIONARY";
48 final int dictFileVersion;
49 public final long creationMillis;
50 public final String dictInfo;
51 public final List<PairEntry> pairEntries;
52 public final List<TextEntry> textEntries;
53 public final List<HtmlEntry> htmlEntries;
54 public final List<DataInputBuffer> htmlData;
55 public final List<EntrySource> sources;
56 public final List<Index> indices;
59 * dictFileVersion 1 adds: <li>links to sources? dictFileVersion 2 adds: <li>
60 * counts of tokens in indices.
63 public Dictionary(final String dictInfo) {
64 this.dictFileVersion = CURRENT_DICT_VERSION;
65 this.creationMillis = System.currentTimeMillis();
66 this.dictInfo = dictInfo;
67 pairEntries = new ArrayList<>();
68 textEntries = new ArrayList<>();
69 htmlEntries = new ArrayList<>();
71 sources = new ArrayList<>();
72 indices = new ArrayList<>();
75 public Dictionary(final FileChannel ch) throws IOException {
76 MappedByteBuffer wholefile = ch.map(FileChannel.MapMode.READ_ONLY, 0, ch.size());
77 DataInputBuffer in = new DataInputBuffer(wholefile, 0);
78 dictFileVersion = in.readInt();
79 if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
80 throw new IOException("Invalid dictionary version: " + dictFileVersion);
82 creationMillis = in.readLong();
83 dictInfo = in.readUTF();
85 // Load the sources, then seek past them, because reading them later
86 // disrupts the offset.
88 final RAFList<EntrySource> rafSources = RAFList.create(in, new EntrySource.Serializer(
89 this), dictFileVersion, dictInfo + " sources: ");
90 sources = new ArrayList<>(rafSources);
91 ch.position(rafSources.getEndOffset());
93 pairEntries = CachingList.create(
94 RAFList.create(in, new PairEntry.Serializer(this), dictFileVersion, dictInfo + " pairs: "),
96 textEntries = CachingList.create(
97 RAFList.create(in, new TextEntry.Serializer(this), dictFileVersion, dictInfo + " text: "),
99 if (dictFileVersion >= 5) {
100 htmlEntries = CachingList.create(
101 RAFList.create(in, new HtmlEntry.Serializer(this), dictFileVersion, dictInfo + " html: "),
104 htmlEntries = Collections.emptyList();
106 if (dictFileVersion >= 7) {
107 htmlData = RAFList.create(in, new HtmlEntry.DataDeserializer(), dictFileVersion, dictInfo + " html: ");
111 indices = CachingList.createFullyCached(RAFList.create(in, new IndexSerializer(),
112 dictFileVersion, dictInfo + " index: "));
113 } catch (RuntimeException e) {
114 throw new IOException("RuntimeException loading dictionary", e);
116 final String end = in.readUTF();
117 if (!end.equals(END_OF_DICTIONARY)) {
118 throw new IOException("Dictionary seems corrupt: " + end);
123 public void write(DataOutput out) throws IOException {
124 RandomAccessFile raf = (RandomAccessFile)out;
125 if (dictFileVersion < 7) throw new RuntimeException("write function cannot write formats older than v7!");
126 raf.writeInt(dictFileVersion);
127 raf.writeLong(creationMillis);
128 raf.writeUTF(dictInfo);
129 System.out.println("sources start: " + raf.getFilePointer());
130 RAFList.write(raf, sources, new EntrySource.Serializer(this));
131 System.out.println("pair start: " + raf.getFilePointer());
132 RAFList.write(raf, pairEntries, new PairEntry.Serializer(this), 64, true);
133 System.out.println("text start: " + raf.getFilePointer());
134 RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
135 System.out.println("html index start: " + raf.getFilePointer());
136 RAFList.write(raf, htmlEntries, new HtmlEntry.Serializer(this), 64, true);
137 System.out.println("html data start: " + raf.getFilePointer());
138 assert htmlData == null;
139 RAFList.write(raf, htmlEntries, new HtmlEntry.DataSerializer(), 128, true);
140 System.out.println("indices start: " + raf.getFilePointer());
141 RAFList.write(raf, indices, new IndexSerializer());
142 System.out.println("end: " + raf.getFilePointer());
143 raf.writeUTF(END_OF_DICTIONARY);
146 private final class IndexSerializer implements RAFListSerializer<Index> {
148 public Index read(DataInput raf, final int readIndex) throws IOException {
149 return new Index(Dictionary.this, (DataInputBuffer)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) {