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;
18 import java.io.IOException;
19 import java.io.PrintStream;
20 import java.io.RandomAccessFile;
21 import java.util.ArrayList;
22 import java.util.List;
24 import com.hughes.android.dictionary.DictionaryInfo;
25 import com.hughes.util.CachingList;
26 import com.hughes.util.raf.RAFList;
27 import com.hughes.util.raf.RAFListSerializer;
28 import com.hughes.util.raf.RAFSerializable;
31 public class Dictionary implements RAFSerializable<Dictionary> {
33 static final int CACHE_SIZE = 5000;
35 static final int CURRENT_DICT_VERSION = 4;
36 static final String END_OF_DICTIONARY = "END OF DICTIONARY";
39 final int dictFileVersion;
40 final long creationMillis;
41 public final String dictInfo;
42 public final List<PairEntry> pairEntries;
43 public final List<TextEntry> textEntries;
44 public final List<EntrySource> sources;
45 public final List<Index> indices;
48 * dictFileVersion 1 adds:
49 * <li> links to sources?
51 * dictFileVersion 2 adds:
52 * <li> counts of tokens in indices.
55 public Dictionary(final String dictInfo) {
56 this.dictFileVersion = CURRENT_DICT_VERSION;
57 this.creationMillis = System.currentTimeMillis();
58 this.dictInfo = dictInfo;
59 pairEntries = new ArrayList<PairEntry>();
60 textEntries = new ArrayList<TextEntry>();
61 sources = new ArrayList<EntrySource>();
62 indices = new ArrayList<Index>();
65 public Dictionary(final RandomAccessFile raf) throws IOException {
66 dictFileVersion = raf.readInt();
67 if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
68 throw new IOException("Invalid dictionary version: " + dictFileVersion);
70 creationMillis = raf.readLong();
71 dictInfo = raf.readUTF();
73 // Load the sources, then seek past them, because reading them later disrupts the offset.
75 final RAFList<EntrySource> rafSources = RAFList.create(raf, new EntrySource.Serializer(this), raf.getFilePointer());
76 sources = new ArrayList<EntrySource>(rafSources);
77 raf.seek(rafSources.getEndOffset());
79 pairEntries = CachingList.create(RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
80 textEntries = CachingList.create(RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
81 indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer, raf.getFilePointer()));
82 } catch (RuntimeException e) {
83 final IOException ioe = new IOException("RuntimeException loading dictionary");
87 final String end = raf.readUTF();
88 if (!end.equals(END_OF_DICTIONARY)) {
89 throw new IOException("Dictionary seems corrupt: " + end);
94 public void write(RandomAccessFile raf) throws IOException {
95 raf.writeInt(dictFileVersion);
96 raf.writeLong(creationMillis);
97 raf.writeUTF(dictInfo);
98 RAFList.write(raf, sources, new EntrySource.Serializer(this));
99 RAFList.write(raf, pairEntries, new PairEntry.Serializer(this));
100 RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
101 RAFList.write(raf, indices, indexSerializer);
102 raf.writeUTF(END_OF_DICTIONARY);
105 private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
107 public Index read(RandomAccessFile raf, final int readIndex) throws IOException {
108 return new Index(Dictionary.this, raf);
111 public void write(RandomAccessFile raf, Index t) throws IOException {
115 public void print(final PrintStream out) {
116 out.println("dictInfo=" + dictInfo);
117 for (final EntrySource entrySource : sources) {
118 out.printf("EntrySource: %s %d\n", entrySource.name, entrySource.numEntries);
121 for (final Index index : indices) {
122 out.printf("Index: %s %s\n", index.shortName, index.longName);
128 public DictionaryInfo getDictionaryInfo() {
129 final DictionaryInfo result = new DictionaryInfo();
130 result.creationMillis = this.creationMillis;
131 result.dictInfo = this.dictInfo;
132 for (final Index index : indices) {
133 result.indexInfos.add(index.getIndexInfo());
138 public static DictionaryInfo getDictionaryInfo(final File file) {
139 RandomAccessFile raf = null;
141 raf = new RandomAccessFile(file, "r");
142 final Dictionary dict = new Dictionary(raf);
143 final DictionaryInfo dictionaryInfo = dict.getDictionaryInfo();
144 dictionaryInfo.uncompressedFilename = file.getName();
145 dictionaryInfo.uncompressedBytes = file.length();
147 return dictionaryInfo;
148 } catch (IOException e) {
154 } catch (IOException e) {