1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | public class ZVerseBackendState extends AbstractOpenFileState { |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
ZVerseBackendState(BookMetaData bookMetaData, BlockType blockType) throws BookException { |
59 | 0 | super(bookMetaData); |
60 | 0 | URI path = SwordUtil.getExpandedDataPath(bookMetaData); |
61 | 0 | String otAllButLast = NetUtil.lengthenURI(path, File.separator + SwordConstants.FILE_OT + '.' + blockType.getIndicator() + SUFFIX_PART1).getPath(); |
62 | 0 | File otIdxFile = new File(otAllButLast + SUFFIX_INDEX); |
63 | 0 | File otTextFile = new File(otAllButLast + SUFFIX_TEXT); |
64 | 0 | File otCompFile = new File(otAllButLast + SUFFIX_COMP); |
65 | |
|
66 | 0 | String ntAllButLast = NetUtil.lengthenURI(path, File.separator + SwordConstants.FILE_NT + '.' + blockType.getIndicator() + SUFFIX_PART1).getPath(); |
67 | 0 | File ntIdxFile = new File(ntAllButLast + SUFFIX_INDEX); |
68 | 0 | File ntTextFile = new File(ntAllButLast + SUFFIX_TEXT); |
69 | 0 | File ntCompFile = new File(ntAllButLast + SUFFIX_COMP); |
70 | |
|
71 | |
|
72 | 0 | if (otIdxFile.canRead()) { |
73 | |
try { |
74 | 0 | otCompRaf = new RandomAccessFile(otIdxFile, FileUtil.MODE_READ); |
75 | 0 | otTextRaf = new RandomAccessFile(otTextFile, FileUtil.MODE_READ); |
76 | 0 | otIdxRaf = new RandomAccessFile(otCompFile, FileUtil.MODE_READ); |
77 | 0 | } catch (FileNotFoundException ex) { |
78 | |
|
79 | 0 | IOUtil.close(otCompRaf); |
80 | 0 | IOUtil.close(otTextRaf); |
81 | 0 | IOUtil.close(otIdxRaf); |
82 | |
|
83 | 0 | assert false : ex; |
84 | 0 | LOGGER.error("Could not open OT", ex); |
85 | 0 | } |
86 | |
} |
87 | |
|
88 | |
|
89 | |
|
90 | 0 | if (ntIdxFile.canRead()) { |
91 | |
try { |
92 | 0 | ntCompRaf = new RandomAccessFile(ntIdxFile, FileUtil.MODE_READ); |
93 | 0 | ntTextRaf = new RandomAccessFile(ntTextFile, FileUtil.MODE_READ); |
94 | 0 | ntIdxRaf = new RandomAccessFile(ntCompFile, FileUtil.MODE_READ); |
95 | 0 | } catch (FileNotFoundException ex) { |
96 | |
|
97 | 0 | IOUtil.close(ntCompRaf); |
98 | 0 | IOUtil.close(ntTextRaf); |
99 | 0 | IOUtil.close(ntIdxRaf); |
100 | |
|
101 | 0 | assert false : ex; |
102 | 0 | LOGGER.error("Could not open OT", ex); |
103 | 0 | } |
104 | |
} |
105 | 0 | } |
106 | |
|
107 | |
public void releaseResources() { |
108 | 0 | IOUtil.close(ntCompRaf); |
109 | 0 | IOUtil.close(ntTextRaf); |
110 | 0 | IOUtil.close(ntIdxRaf); |
111 | 0 | IOUtil.close(otCompRaf); |
112 | 0 | IOUtil.close(otTextRaf); |
113 | 0 | IOUtil.close(otIdxRaf); |
114 | 0 | ntCompRaf = null; |
115 | 0 | ntTextRaf = null; |
116 | 0 | ntIdxRaf = null; |
117 | 0 | otCompRaf = null; |
118 | 0 | otTextRaf = null; |
119 | 0 | otIdxRaf = null; |
120 | 0 | } |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public RandomAccessFile getCompRaf(Testament testament) { |
129 | 0 | return testament == Testament.NEW ? ntCompRaf : otCompRaf; |
130 | |
} |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public RandomAccessFile getTextRaf(Testament testament) { |
139 | 0 | return testament == Testament.NEW ? ntTextRaf : otTextRaf; |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
public RandomAccessFile getIdxRaf(Testament testament) { |
149 | 0 | return testament == Testament.NEW ? ntIdxRaf : otIdxRaf; |
150 | |
} |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
public Testament getLastTestament() { |
156 | 0 | return lastTestament; |
157 | |
} |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
public long getLastBlockNum() { |
163 | 0 | return lastBlockNum; |
164 | |
} |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
public byte[] getLastUncompressed() { |
170 | 0 | return lastUncompressed; |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
public void setLastTestament(Testament lastTestament) { |
178 | 0 | this.lastTestament = lastTestament; |
179 | 0 | } |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
public void setLastBlockNum(long lastBlockNum) { |
186 | 0 | this.lastBlockNum = lastBlockNum; |
187 | 0 | } |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
public void setLastUncompressed(byte[] lastUncompressed) { |
194 | 0 | this.lastUncompressed = lastUncompressed; |
195 | 0 | } |
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 | |
|
203 | |
|
204 | |
|
205 | |
private RandomAccessFile otCompRaf; |
206 | |
private RandomAccessFile ntCompRaf; |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
private RandomAccessFile otTextRaf; |
212 | |
private RandomAccessFile ntTextRaf; |
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
private RandomAccessFile otIdxRaf; |
218 | |
private RandomAccessFile ntIdxRaf; |
219 | |
private Testament lastTestament; |
220 | 0 | private long lastBlockNum = -1; |
221 | |
private byte[] lastUncompressed; |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | 0 | private static final Logger LOGGER = LoggerFactory.getLogger(ZVerseBackendState.class); |
227 | |
} |