1
20 package org.crosswire.jsword.book.sword.state;
21
22 import java.io.File;
23 import java.io.IOException;
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.Reporter;
30 import org.crosswire.jsword.JSMsg;
31 import org.crosswire.jsword.book.BookException;
32 import org.crosswire.jsword.book.BookMetaData;
33 import org.crosswire.jsword.book.sword.RawLDBackend;
34 import org.crosswire.jsword.book.sword.SwordConstants;
35 import org.crosswire.jsword.book.sword.SwordUtil;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
46 public class RawLDBackendState extends AbstractOpenFileState {
47
56 RawLDBackendState(BookMetaData bookMetaData) throws BookException {
57 super(bookMetaData);
58 URI path = null;
59 try {
60 path = SwordUtil.getExpandedDataPath(bookMetaData);
61 } catch (BookException e) {
62 Reporter.informUser(this, e);
63 throw e;
64 }
65
66 try {
67 idxFile = new File(path.getPath() + SwordConstants.EXTENSION_INDEX);
68 datFile = new File(path.getPath() + SwordConstants.EXTENSION_DATA);
69
70 if (!idxFile.canRead()) {
71 Reporter.informUser(this, new BookException(JSMsg.gettext("Error reading {0}", idxFile.getAbsolutePath())));
75
76 return;
78 }
79
80 if (!datFile.canRead()) {
81
83 BookException prob = new BookException(JSMsg.gettext("Error reading {0}", datFile.getAbsolutePath()));
87 Reporter.informUser(this, prob);
88 throw prob;
89 }
90
91 idxRaf = new RandomAccessFile(idxFile, FileUtil.MODE_READ);
93 datRaf = new RandomAccessFile(datFile, FileUtil.MODE_READ);
94 } catch (IOException ex) {
95 IOUtil.close(idxRaf);
97 IOUtil.close(datRaf);
98
99 LOGGER.error("failed to open files", ex);
100 idxRaf = null;
101 datRaf = null;
102 throw new BookException(JSMsg.gettext("Error reading {0}", datFile.getAbsolutePath()), ex);
106 }
107 }
108
109 public void releaseResources() {
110 size = -1;
111 IOUtil.close(idxRaf);
112 IOUtil.close(datRaf);
113 idxRaf = null;
114 datRaf = null;
115 }
116
117
120 public int getSize() {
121 return size;
122 }
123
124
127 public File getIdxFile() {
128 return idxFile;
129 }
130
131
134 public RandomAccessFile getIdxRaf() {
135 return idxRaf;
136 }
137
138
141 public RandomAccessFile getDatRaf() {
142 return datRaf;
143 }
144
145
149 public void setSize(int size) {
150 this.size = size;
151 }
152
153
156 private int size = -1;
157
158
161 private File idxFile;
162
163
166 private RandomAccessFile idxRaf;
167
168
171 private File datFile;
172
173
176 private RandomAccessFile datRaf;
177
178
181 private static final Logger LOGGER = LoggerFactory.getLogger(RawLDBackend.class);
182 }
183