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.channels.Channels;
26 import java.nio.channels.FileChannel;
27 import java.nio.charset.StandardCharsets;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
32 import com.hughes.android.dictionary.DictionaryInfo;
33 import com.hughes.util.CachingList;
34 import com.hughes.util.raf.RAFList;
35 import com.hughes.util.raf.RAFListSerializer;
36 import com.hughes.util.raf.RAFSerializable;
38 public class Dictionary implements RAFSerializable<Dictionary> {
40 private static final int CACHE_SIZE = 5000;
42 private static final int CURRENT_DICT_VERSION = 7;
43 private static final String END_OF_DICTIONARY = "END OF DICTIONARY";
46 final int dictFileVersion;
47 public final long creationMillis;
48 public final String dictInfo;
49 public final List<PairEntry> pairEntries;
50 public final List<TextEntry> textEntries;
51 public final List<HtmlEntry> htmlEntries;
52 public final List<byte[]> htmlData;
53 public final List<EntrySource> sources;
54 public final List<Index> indices;
57 * dictFileVersion 1 adds: <li>links to sources? dictFileVersion 2 adds: <li>
58 * counts of tokens in indices.
61 public Dictionary(final String dictInfo) {
62 this.dictFileVersion = CURRENT_DICT_VERSION;
63 this.creationMillis = System.currentTimeMillis();
64 this.dictInfo = dictInfo;
65 pairEntries = new ArrayList<>();
66 textEntries = new ArrayList<>();
67 htmlEntries = new ArrayList<>();
69 sources = new ArrayList<>();
70 indices = new ArrayList<>();
73 public Dictionary(final FileChannel ch) throws IOException {
74 DataInput raf = new DataInputStream(Channels.newInputStream(ch));
75 dictFileVersion = raf.readInt();
76 if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
77 throw new IOException("Invalid dictionary version: " + dictFileVersion);
79 creationMillis = raf.readLong();
80 dictInfo = raf.readUTF();
82 // Load the sources, then seek past them, because reading them later
83 // disrupts the offset.
85 final RAFList<EntrySource> rafSources = RAFList.create(ch, new EntrySource.Serializer(
86 this), ch.position(), dictFileVersion, dictInfo + " sources: ");
87 sources = new ArrayList<>(rafSources);
88 ch.position(rafSources.getEndOffset());
90 pairEntries = CachingList.create(
91 RAFList.create(ch, new PairEntry.Serializer(this), ch.position(), dictFileVersion, dictInfo + " pairs: "),
93 textEntries = CachingList.create(
94 RAFList.create(ch, new TextEntry.Serializer(this), ch.position(), dictFileVersion, dictInfo + " text: "),
96 if (dictFileVersion >= 5) {
97 htmlEntries = CachingList.create(
98 RAFList.create(ch, new HtmlEntry.Serializer(this, ch), ch.position(), dictFileVersion, dictInfo + " html: "),
101 htmlEntries = Collections.emptyList();
103 if (dictFileVersion >= 7) {
104 htmlData = RAFList.create(ch, new HtmlEntry.DataDeserializer(), ch.position(), dictFileVersion, dictInfo + " html: ");
108 indices = CachingList.createFullyCached(RAFList.create(ch, new IndexSerializer(ch),
109 ch.position(), dictFileVersion, dictInfo + " index: "));
110 } catch (RuntimeException e) {
111 throw new IOException("RuntimeException loading dictionary", e);
113 final String end = raf.readUTF();
114 if (!end.equals(END_OF_DICTIONARY)) {
115 throw new IOException("Dictionary seems corrupt: " + end);
120 public void write(DataOutput out) throws IOException {
121 RandomAccessFile raf = (RandomAccessFile)out;
122 if (dictFileVersion < 7) throw new RuntimeException("write function cannot write formats older than v7!");
123 raf.writeInt(dictFileVersion);
124 raf.writeLong(creationMillis);
125 raf.writeUTF(dictInfo);
126 System.out.println("sources start: " + raf.getFilePointer());
127 RAFList.write(raf, sources, new EntrySource.Serializer(this));
128 System.out.println("pair start: " + raf.getFilePointer());
129 RAFList.write(raf, pairEntries, new PairEntry.Serializer(this), 64, true);
130 System.out.println("text start: " + raf.getFilePointer());
131 RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
132 System.out.println("html index start: " + raf.getFilePointer());
133 RAFList.write(raf, htmlEntries, new HtmlEntry.Serializer(this, null), 64, true);
134 System.out.println("html data start: " + raf.getFilePointer());
135 assert htmlData == null;
136 RAFList.write(raf, htmlEntries, new HtmlEntry.DataSerializer(), 128, true);
137 System.out.println("indices start: " + raf.getFilePointer());
138 RAFList.write(raf, indices, new IndexSerializer(null));
139 System.out.println("end: " + raf.getFilePointer());
140 raf.writeUTF(END_OF_DICTIONARY);
143 private final class IndexSerializer implements RAFListSerializer<Index> {
144 private final FileChannel ch;
146 IndexSerializer(FileChannel ch) {
151 public Index read(DataInput raf, final int readIndex) throws IOException {
152 return new Index(Dictionary.this, ch, raf);
156 public void write(DataOutput raf, Index t) throws IOException {
161 final RAFListSerializer<HtmlEntry> htmlEntryIndexSerializer = new RAFListSerializer<HtmlEntry>() {
163 public void write(DataOutput raf, HtmlEntry t) {
168 public HtmlEntry read(DataInput raf, int readIndex) throws IOException {
169 return htmlEntries.get(raf.readInt());
173 public void print(final PrintStream out) {
174 out.println("dictInfo=" + dictInfo);
175 for (final EntrySource entrySource : sources) {
176 out.printf("EntrySource: %s %d\n", entrySource.name, entrySource.numEntries);
179 for (final Index index : indices) {
180 out.printf("Index: %s %s\n", index.shortName, index.longName);
186 public DictionaryInfo getDictionaryInfo() {
187 final DictionaryInfo result = new DictionaryInfo();
188 result.creationMillis = this.creationMillis;
189 result.dictInfo = this.dictInfo;
190 for (final Index index : indices) {
191 result.indexInfos.add(index.getIndexInfo());
196 public static DictionaryInfo getDictionaryInfo(final File file) {
197 RandomAccessFile raf = null;
199 raf = new RandomAccessFile(file, "r");
200 final Dictionary dict = new Dictionary(raf.getChannel());
201 final DictionaryInfo dictionaryInfo = dict.getDictionaryInfo();
202 dictionaryInfo.uncompressedFilename = file.getName();
203 dictionaryInfo.uncompressedBytes = file.length();
205 return dictionaryInfo;
206 } catch (IOException e) {
207 final DictionaryInfo dictionaryInfo = new DictionaryInfo();
208 dictionaryInfo.uncompressedFilename = file.getName();
209 dictionaryInfo.uncompressedBytes = file.length();
210 return dictionaryInfo;
215 } catch (IOException e) {