Source code: com/eireneh/bible/book/raw/PuncItemsMem.java
1
2 package com.eireneh.bible.book.raw;
3
4 import java.io.*;
5 import java.util.*;
6
7 /**
8 * PuncItemsMem is almost identical to WordItemsMem, but the Dictionary is
9 * much smaller, there are almost certainly less than 256 different
10 * intra-word punctuation sets, so we will only need 1 byte per word
11 * instead of 2.
12 *
13 * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
14 * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
15 * Distribution Licence:<br />
16 * Project B is free software; you can redistribute it
17 * and/or modify it under the terms of the GNU General Public License,
18 * version 2 as published by the Free Software Foundation.<br />
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.<br />
23 * The License is available on the internet
24 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
25 * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26 * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
27 * The copyright to this program is held by it's authors.
28 * </font></td></tr></table>
29 * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
30 * @see docs.Licence
31 * @author Joe Walker
32 * @version D0.I0.T0
33 */
34 public class PuncItemsMem extends ItemsMem
35 {
36 /**
37 * Create a PuncItemsMem from a File that contains the dictionary.
38 * @param raw Reference to the RawBible that is using us
39 * @param filename The leaf name to read/write
40 * @param create Should we start all over again
41 */
42 public PuncItemsMem(RawBible raw, boolean create) throws Exception
43 {
44 super(raw, "punc.idx", create);
45 }
46
47 /**
48 * Create a PuncItemsMem from a File that contains the dictionary.
49 * @param raw Reference to the RawBible that is using us
50 * @param filename The leaf name to read/write
51 * @param create Should we start all over again
52 * @param messages We append stuff here if something went wrong
53 */
54 public PuncItemsMem(RawBible raw, boolean create, StringBuffer messages)
55 {
56 super(raw, "punc.idx", create, messages);
57 }
58
59 /**
60 * How many items are there in this index?
61 * @return The number of items that we must remember
62 */
63 public int getMaxItems()
64 {
65 return 1000;
66 }
67
68 /**
69 * Load the Resource from a stream
70 * @param in The stream to read from
71 */
72 public void load(InputStream in) throws IOException, ClassNotFoundException
73 {
74 DataInputStream din = new DataInputStream(in);
75
76 byte[] asig = new byte[6];
77 din.readFully(asig);
78 String ssig = new String(asig);
79 if (!ssig.equals("RAW:PR"))
80 throw new IOException("This file is not a Punc file");
81
82 count = din.readInt();
83 hash = new Hashtable(count);
84 array = new String[count];
85
86 for (int i=0; i<count; i++)
87 {
88 byte wordlen = din.readByte();
89 byte[] aword = new byte[wordlen];
90 din.readFully(aword);
91 String word = new String(aword);
92
93 hash.put(word, new Integer(i));
94 array[i] = word;
95 }
96
97 din.close();
98 }
99
100 /**
101 * Ensure that all changes to the index of words are written to a
102 * stream
103 * @param out The stream to write to
104 */
105 public void save(OutputStream out) throws IOException
106 {
107 DataOutputStream dout = new DataOutputStream(out);
108
109 dout.writeBytes("RAW:PR");
110 dout.writeInt(hash.size());
111
112 for (int i=0; i<hash.size(); i++)
113 {
114 dout.writeByte(array[i].length());
115 dout.writeBytes(array[i]);
116 }
117
118 dout.close();
119 }
120 }