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/Mem.java


1   
2   package com.eireneh.bible.book.raw;
3   
4   import java.io.*;
5   import java.util.*;
6   import java.util.zip.*;
7   import java.net.*;
8   
9   import com.eireneh.util.*;
10  
11  /**
12  * Mem is the root of all the data sources that load their data fully into
13  * memory at init time. This is fairly fast but very memory hungry.
14  * <p>There is code here to implememt compressed data files, however this
15  * makes load time very very slow, instead of just slow, so it is all
16  * commented out.</p>
17  *
18  * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
19  * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
20  * Distribution Licence:<br />
21  * Project B is free software; you can redistribute it
22  * and/or modify it under the terms of the GNU General Public License,
23  * version 2 as published by the Free Software Foundation.<br />
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27  * General Public License for more details.<br />
28  * The License is available on the internet
29  * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
30  * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
31  * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
32  * The copyright to this program is held by it's authors.
33  * </font></td></tr></table>
34  * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
35  * @see docs.Licence
36  * @author Joe Walker
37  * @version D0.I0.T0
38  */
39  public abstract class Mem
40  {
41      /**
42      * Create a WordResource from a File that contains the dictionary.
43      * @param raw Reference to the RawBible that is using us
44      * @param leafname The leaf name to read/write
45      * @param create Should we start all over again
46      */
47      public Mem(RawBible raw, String leafname, boolean create) throws Exception
48      {
49          ctor(raw, leafname, create);
50      }
51  
52      /**
53      * Create a WordResource from a File that contains the dictionary.
54      * @param raw Reference to the RawBible that is using us
55      * @param leafname The leaf name to read/write
56      * @param create Should we start all over again
57      * @param messages We append stuff here if something went wrong
58      */
59      public Mem(RawBible raw, String leafname, boolean create, StringBuffer messages)
60      {
61          try
62          {
63              ctor(raw, leafname, create);
64          }
65          catch (Exception ex)
66          {
67              messages.append(""+ex);
68          }
69      }
70  
71      /**
72      * This really should be a constructor, however the StringBuffer ctor
73      * wants to trap and muffle exceptions. and I can't do this:
74      * <code>try { this(...) } ...</code>
75      * @param raw Reference to the RawBible that is using us
76      * @param leafname The leaf name to read/write
77      * @param create Should we start all over again
78      */
79      private void ctor(RawBible raw, String leafname, boolean create) throws Exception
80      {
81          this.raw = raw;
82          this.leafname = leafname;
83          this.create = create;
84  
85          init();
86          if (create)
87          {
88              save();
89          }
90          else
91          {
92              load();
93          }
94      }
95  
96      /**
97      * Start all over again and clear the decks for more data.
98      */
99      public abstract void init();
100 
101     /**
102     * Load the Resource from a stream
103     * @param in The stream to read from
104     */
105     public abstract void load(InputStream in) throws IOException, ClassNotFoundException;
106 
107     /**
108     * Load the Resource from a named file
109     */
110     public void load() throws IOException, ClassNotFoundException
111     {
112         URL url = NetUtil.lengthenURL(raw.getBaseURL(), leafname);
113 
114         // For the pkzip version
115         //String filename = raw.getDir()+leafname+".zip";
116 
117         // For the gzip version
118         //String filename = raw.getDir()+leafname+".gz";
119 
120         InputStream in = url.openStream();
121 
122         // For the pkzip version
123         //ZipInputStream in = new ZipInputStream(new FileInputStream(filename));
124         //ZipEntry entry = in.getNextEntry();
125         //if (entry == null) throw new IOException("Empty ZIP file");
126 
127         // For the gzip version
128         //GZIPInputStream in = new GZIPInputStream(new FileInputStream(filename));
129 
130         load(in);
131 
132         in.close();
133     }
134 
135     /**
136     * Ensure that all changes to the index of words are written to a
137     * stream
138     * @param out The stream to write to
139     */
140     public abstract void save(OutputStream out) throws IOException;
141 
142     /**
143     * Ensure that all changes to the index of words are written to disk
144     */
145     public void save() throws IOException
146     {
147         URL url = NetUtil.lengthenURL(raw.getBaseURL(), leafname);
148 
149         // For the pkzip version
150         //String filename = raw.getDir()+leafname+".zip";
151 
152         // For the gzip version
153         //String filename = raw.getDir()+leafname+".gz";
154 
155         OutputStream out = NetUtil.getOutputStream(url);
156 
157         // For the pkzip version
158         //ZipOutputStream out = new ZipOutputStream(new FileOutputStream(filename));
159         //out.putNextEntry(new ZipEntry(leafname));
160 
161         // For the gzip version
162         //GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(filename));
163 
164         save(out);
165 
166         // For both zip versions
167         //out.finish();
168 
169         // For the pkzip version
170         //out.closeEntry();
171 
172         out.close();
173     }
174 
175     /** Are we allowed to create new indexes */
176     protected boolean create;
177 
178     /** The leafname of the file read */
179     protected String leafname;
180 
181     /** The RawBible co-ordinated the various classes that cache the files */
182     protected RawBible raw;
183 }