1
20 package org.crosswire.jsword.book.sword.state;
21
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.RandomAccessFile;
25 import java.net.URI;
26
27 import org.crosswire.common.util.FileUtil;
28 import org.crosswire.common.util.IOUtil;
29 import org.crosswire.common.util.NetUtil;
30 import org.crosswire.jsword.book.BookException;
31 import org.crosswire.jsword.book.BookMetaData;
32 import org.crosswire.jsword.book.sword.BlockType;
33 import org.crosswire.jsword.book.sword.SwordConstants;
34 import org.crosswire.jsword.book.sword.SwordUtil;
35 import org.crosswire.jsword.versification.Testament;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
48 public class ZVerseBackendState extends AbstractOpenFileState {
49
58 ZVerseBackendState(BookMetaData bookMetaData, BlockType blockType) throws BookException {
59 super(bookMetaData);
60 URI path = SwordUtil.getExpandedDataPath(bookMetaData);
61 String otAllButLast = NetUtil.lengthenURI(path, File.separator + SwordConstants.FILE_OT + '.' + blockType.getIndicator() + SUFFIX_PART1).getPath();
62 File otIdxFile = new File(otAllButLast + SUFFIX_INDEX);
63 File otTextFile = new File(otAllButLast + SUFFIX_TEXT);
64 File otCompFile = new File(otAllButLast + SUFFIX_COMP);
65
66 String ntAllButLast = NetUtil.lengthenURI(path, File.separator + SwordConstants.FILE_NT + '.' + blockType.getIndicator() + SUFFIX_PART1).getPath();
67 File ntIdxFile = new File(ntAllButLast + SUFFIX_INDEX);
68 File ntTextFile = new File(ntAllButLast + SUFFIX_TEXT);
69 File ntCompFile = new File(ntAllButLast + SUFFIX_COMP);
70
71 if (otIdxFile.canRead()) {
73 try {
74 otCompRaf = new RandomAccessFile(otIdxFile, FileUtil.MODE_READ);
75 otTextRaf = new RandomAccessFile(otTextFile, FileUtil.MODE_READ);
76 otIdxRaf = new RandomAccessFile(otCompFile, FileUtil.MODE_READ);
77 } catch (FileNotFoundException ex) {
78 IOUtil.close(otCompRaf);
80 IOUtil.close(otTextRaf);
81 IOUtil.close(otIdxRaf);
82
83 assert false : ex;
84 LOGGER.error("Could not open OT", ex);
85 }
86 }
87
88 if (ntIdxFile.canRead()) {
91 try {
92 ntCompRaf = new RandomAccessFile(ntIdxFile, FileUtil.MODE_READ);
93 ntTextRaf = new RandomAccessFile(ntTextFile, FileUtil.MODE_READ);
94 ntIdxRaf = new RandomAccessFile(ntCompFile, FileUtil.MODE_READ);
95 } catch (FileNotFoundException ex) {
96 IOUtil.close(ntCompRaf);
98 IOUtil.close(ntTextRaf);
99 IOUtil.close(ntIdxRaf);
100
101 assert false : ex;
102 LOGGER.error("Could not open OT", ex);
103 }
104 }
105 }
106
107 public void releaseResources() {
108 IOUtil.close(ntCompRaf);
109 IOUtil.close(ntTextRaf);
110 IOUtil.close(ntIdxRaf);
111 IOUtil.close(otCompRaf);
112 IOUtil.close(otTextRaf);
113 IOUtil.close(otIdxRaf);
114 ntCompRaf = null;
115 ntTextRaf = null;
116 ntIdxRaf = null;
117 otCompRaf = null;
118 otTextRaf = null;
119 otIdxRaf = null;
120 }
121
122
128 public RandomAccessFile getCompRaf(Testament testament) {
129 return testament == Testament.NEW ? ntCompRaf : otCompRaf;
130 }
131
132
138 public RandomAccessFile getTextRaf(Testament testament) {
139 return testament == Testament.NEW ? ntTextRaf : otTextRaf;
140 }
141
142
148 public RandomAccessFile getIdxRaf(Testament testament) {
149 return testament == Testament.NEW ? ntIdxRaf : otIdxRaf;
150 }
151
152
155 public Testament getLastTestament() {
156 return lastTestament;
157 }
158
159
162 public long getLastBlockNum() {
163 return lastBlockNum;
164 }
165
166
169 public byte[] getLastUncompressed() {
170 return lastUncompressed;
171 }
172
173
177 public void setLastTestament(Testament lastTestament) {
178 this.lastTestament = lastTestament;
179 }
180
181
185 public void setLastBlockNum(long lastBlockNum) {
186 this.lastBlockNum = lastBlockNum;
187 }
188
189
193 public void setLastUncompressed(byte[] lastUncompressed) {
194 this.lastUncompressed = lastUncompressed;
195 }
196
197 private static final String SUFFIX_COMP = "v";
198 private static final String SUFFIX_INDEX = "s";
199 private static final String SUFFIX_PART1 = "z";
200 private static final String SUFFIX_TEXT = "z";
201
202
205 private RandomAccessFile otCompRaf;
206 private RandomAccessFile ntCompRaf;
207
208
211 private RandomAccessFile otTextRaf;
212 private RandomAccessFile ntTextRaf;
213
214
217 private RandomAccessFile otIdxRaf;
218 private RandomAccessFile ntIdxRaf;
219 private Testament lastTestament;
220 private long lastBlockNum = -1;
221 private byte[] lastUncompressed;
222
223
226 private static final Logger LOGGER = LoggerFactory.getLogger(ZVerseBackendState.class);
227 }
228