Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/eireneh/bible/book/raw/CaseInstsMem.java


1   
2   package com.eireneh.bible.book.raw;
3   
4   import java.io.*;
5   import java.util.Vector;
6   
7   import com.eireneh.bible.passage.*;
8   
9   /**
10  * A CaseInstsMem provides access to the list of case ids that
11  * make up a Passage. The central interface is an array of cases
12  * of words in the given verse. 
13  * <p>This is different from WordInsts and PuncInsts
14  * in that there is no CaseInsts. This is because there are only
15  * 4 cases worthy of note, and they are all well defined in
16  * PassageUtil.
17  * <p>Storing these 4 cases takes 2 bits per word, 4 words per byte.
18  * 
19  * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
20  * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
21  * Distribution Licence:<br />
22  * Project B is free software; you can redistribute it
23  * and/or modify it under the terms of the GNU General Public License,
24  * version 2 as published by the Free Software Foundation.<br />
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28  * General Public License for more details.<br />
29  * The License is available on the internet
30  * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
31  * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32  * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
33  * The copyright to this program is held by it's authors.
34  * </font></td></tr></table>
35  * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
36  * @see docs.Licence
37  * @see com.eireneh.bible.passage.PassageUtil#getCase(String)
38  * @see com.eireneh.bible.passage.PassageUtil#setCase(String,int)
39  * @author Joe Walker
40  * @version D0.I0.T0
41  */
42  public class CaseInstsMem extends InstsMem
43  {
44      /**
45      * Basic constructor
46      * @param raw Reference to the RawBible that is using us
47      * @param filename The leaf name to read/write
48      * @param create Should we start all over again
49      */
50      public CaseInstsMem(RawBible raw, boolean create) throws Exception
51      {
52          super(raw, "caseinst.idx", create);
53      }
54  
55      /**
56      * Basic constructor
57      * @param raw Reference to the RawBible that is using us
58      * @param filename The leaf name to read/write
59      * @param create Should we start all over again
60      * @param messages We append stuff here if something went wrong
61      */
62      public CaseInstsMem(RawBible raw, boolean create, StringBuffer messages)
63      {
64          super(raw, "caseinst.idx", create, messages);
65      }
66  
67      /**
68      * Load the Resource from a stream
69      * @param in The stream to read from
70      */
71      public void load(InputStream in) throws IOException, ClassNotFoundException
72      {
73          DataInputStream din = new DataInputStream(in);
74  
75          byte[] asig = new byte[6];
76          din.readFully(asig);
77          String ssig = new String(asig);
78          if (!ssig.equals("RAW:CI"))
79              throw new IOException("This file is not a CaseInst file");
80  
81          for (int i=0; i<Books.versesInBible(); i++)
82          {
83              int insts = din.readByte();
84              array[i] = new int[insts];
85              for (int j=0; j<insts; j+=4)
86              {
87                  byte b = din.readByte();
88  
89                  for (int k=0; k<4; k++)
90                  {
91                      if (j+k < array[i].length)
92                          array[i][j+k] = (b >> (6-(2*k))) & 3;
93                  }
94              }
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:CI");
110 
111         for (int i=0; i<Books.versesInBible(); i++)
112         {
113             if (array[i] == null)
114             {
115                 dout.writeByte(0);
116             }
117             else
118             {
119                 dout.writeByte(array[i].length);
120                 for (int j=0; j<array[i].length; j+=4)
121                 {
122                     byte b = 0;
123 
124                     for (int k=0; k<4; k++)
125                     {
126                         if (j+k < array[i].length)
127                             b += array[i][j+k] << (6-(2*k));
128                     }
129 
130                     dout.writeByte(b);
131                 }
132             }
133         }
134 
135         dout.close();
136     }
137 }
138