]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/Dictionary.java
Switch from RandomAccessFile to DataInput/DataOutput.
[Dictionary.git] / src / com / hughes / android / dictionary / engine / Dictionary.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
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
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package com.hughes.android.dictionary.engine;
16
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;
22
23 import java.io.DataInput;
24 import java.io.DataOutput;
25 import java.io.File;
26 import java.io.IOException;
27 import java.io.PrintStream;
28 import java.io.RandomAccessFile;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32
33 public class Dictionary implements RAFSerializable<Dictionary> {
34
35     static final int CACHE_SIZE = 5000;
36
37     static final int CURRENT_DICT_VERSION = 6;
38     static final String END_OF_DICTIONARY = "END OF DICTIONARY";
39
40     // persisted
41     final int dictFileVersion;
42     final long creationMillis;
43     public final String dictInfo;
44     public final List<PairEntry> pairEntries;
45     public final List<TextEntry> textEntries;
46     public final List<HtmlEntry> htmlEntries;
47     public final List<EntrySource> sources;
48     public final List<Index> indices;
49
50     /**
51      * dictFileVersion 1 adds: <li>links to sources? dictFileVersion 2 adds: <li>
52      * counts of tokens in indices.
53      */
54
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         htmlEntries = new ArrayList<HtmlEntry>();
62         sources = new ArrayList<EntrySource>();
63         indices = new ArrayList<Index>();
64     }
65
66     public Dictionary(final RandomAccessFile raf) throws IOException {
67         dictFileVersion = raf.readInt();
68         if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
69             throw new IOException("Invalid dictionary version: " + dictFileVersion);
70         }
71         creationMillis = raf.readLong();
72         dictInfo = raf.readUTF();
73
74         // Load the sources, then seek past them, because reading them later
75         // disrupts the offset.
76         try {
77             final RAFList<EntrySource> rafSources = RAFList.create(raf, new EntrySource.Serializer(
78                     this), raf.getFilePointer());
79             sources = new ArrayList<EntrySource>(rafSources);
80             raf.seek(rafSources.getEndOffset());
81
82             pairEntries = CachingList.create(
83                     RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer()),
84                     CACHE_SIZE);
85             textEntries = CachingList.create(
86                     RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer()),
87                     CACHE_SIZE);
88             if (dictFileVersion >= 5) {
89                 htmlEntries = CachingList.create(
90                         RAFList.create(raf, new HtmlEntry.Serializer(this), raf.getFilePointer()),
91                         CACHE_SIZE);
92             } else {
93                 htmlEntries = Collections.emptyList();
94             }
95             indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer,
96                     raf.getFilePointer()));
97         } catch (RuntimeException e) {
98             final IOException ioe = new IOException("RuntimeException loading dictionary");
99             ioe.initCause(e);
100             throw ioe;
101         }
102         final String end = raf.readUTF();
103         if (!end.equals(END_OF_DICTIONARY)) {
104             throw new IOException("Dictionary seems corrupt: " + end);
105         }
106     }
107
108     @Override
109     public void write(DataOutput out) throws IOException {
110         RandomAccessFile raf = (RandomAccessFile)out;
111         raf.writeInt(dictFileVersion);
112         raf.writeLong(creationMillis);
113         raf.writeUTF(dictInfo);
114         RAFList.write(raf, sources, new EntrySource.Serializer(this));
115         RAFList.write(raf, pairEntries, new PairEntry.Serializer(this));
116         RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
117         RAFList.write(raf, htmlEntries, new HtmlEntry.Serializer(this));
118         RAFList.write(raf, indices, indexSerializer);
119         raf.writeUTF(END_OF_DICTIONARY);
120     }
121
122     private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
123         @Override
124         public Index read(DataInput raf, final int readIndex) throws IOException {
125             return new Index(Dictionary.this, raf);
126         }
127
128         @Override
129         public void write(DataOutput raf, Index t) throws IOException {
130             t.write(raf);
131         }
132     };
133
134     final RAFListSerializer<HtmlEntry> htmlEntryIndexSerializer = new RAFListSerializer<HtmlEntry>() {
135         @Override
136         public void write(DataOutput raf, HtmlEntry t) throws IOException {
137             if (t.index() == -1)
138                 throw new IndexOutOfBoundsException();
139             raf.writeInt(t.index());
140         }
141
142         @Override
143         public HtmlEntry read(DataInput raf, int readIndex) throws IOException {
144             return htmlEntries.get(raf.readInt());
145         }
146     };
147
148     public void print(final PrintStream out) {
149         out.println("dictInfo=" + dictInfo);
150         for (final EntrySource entrySource : sources) {
151             out.printf("EntrySource: %s %d\n", entrySource.name, entrySource.numEntries);
152         }
153         out.println();
154         for (final Index index : indices) {
155             out.printf("Index: %s %s\n", index.shortName, index.longName);
156             index.print(out);
157             out.println();
158         }
159     }
160
161     public DictionaryInfo getDictionaryInfo() {
162         final DictionaryInfo result = new DictionaryInfo();
163         result.creationMillis = this.creationMillis;
164         result.dictInfo = this.dictInfo;
165         for (final Index index : indices) {
166             result.indexInfos.add(index.getIndexInfo());
167         }
168         return result;
169     }
170
171     public static DictionaryInfo getDictionaryInfo(final File file) {
172         RandomAccessFile raf = null;
173         try {
174             raf = new RandomAccessFile(file, "r");
175             final Dictionary dict = new Dictionary(raf);
176             final DictionaryInfo dictionaryInfo = dict.getDictionaryInfo();
177             dictionaryInfo.uncompressedFilename = file.getName();
178             dictionaryInfo.uncompressedBytes = file.length();
179             raf.close();
180             return dictionaryInfo;
181         } catch (IOException e) {
182             return null;
183         } finally {
184             if (raf != null) {
185                 try {
186                     raf.close();
187                 } catch (IOException e) {
188                     e.printStackTrace();
189                 }
190             }
191         }
192     }
193
194 }