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.FileInputStream; |
24 | |
import java.io.FileNotFoundException; |
25 | |
import java.io.IOException; |
26 | |
|
27 | |
import org.crosswire.common.util.IOUtil; |
28 | |
import org.crosswire.jsword.book.BookException; |
29 | |
import org.crosswire.jsword.book.BookMetaData; |
30 | |
import org.crosswire.jsword.book.sword.SwordUtil; |
31 | |
import org.slf4j.Logger; |
32 | |
import org.slf4j.LoggerFactory; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public class RawFileBackendState extends RawBackendState { |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
RawFileBackendState(BookMetaData bookMetaData) throws BookException { |
55 | 0 | super(bookMetaData); |
56 | 0 | incfileValue = -1; |
57 | 0 | } |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
@Override |
63 | |
public boolean isWritable() { |
64 | 0 | File incFile = getIncfile(); |
65 | |
|
66 | 0 | if (existsAndCanReadAndWrite(otTextFile) |
67 | |
&& existsAndCanReadAndWrite(ntTextFile) |
68 | |
&& existsAndCanReadAndWrite(otIdxFile) |
69 | |
&& existsAndCanReadAndWrite(otTextFile) |
70 | |
&& (incFile == null || existsAndCanReadAndWrite(incFile))) |
71 | |
{ |
72 | 0 | return true; |
73 | |
} |
74 | 0 | return false; |
75 | |
} |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
private boolean existsAndCanReadAndWrite(File file) { |
84 | 0 | return file.exists() && file.canRead() && file.canWrite(); |
85 | |
} |
86 | |
|
87 | |
private int readIncfile() throws IOException { |
88 | 0 | int ret = -1; |
89 | |
|
90 | 0 | if (incfile == null) { |
91 | |
|
92 | 0 | initIncFile(); |
93 | |
} |
94 | |
|
95 | 0 | if (this.incfile != null) { |
96 | 0 | FileInputStream fis = null; |
97 | |
try { |
98 | 0 | fis = new FileInputStream(this.incfile); |
99 | 0 | byte[] buffer = new byte[4]; |
100 | 0 | if (fis.read(buffer) != 4) { |
101 | 0 | LOGGER.error("Read data is not of appropriate size of 4 bytes!"); |
102 | 0 | throw new IOException("Incfile is not 4 bytes long"); |
103 | |
} |
104 | 0 | ret = SwordUtil.decodeLittleEndian32(buffer, 0); |
105 | |
|
106 | |
|
107 | 0 | this.incfileValue = ret; |
108 | 0 | } catch (FileNotFoundException e) { |
109 | 0 | LOGGER.error("Error on writing to incfile, file should exist already!: {}", e.getMessage(), e); |
110 | |
} finally { |
111 | 0 | IOUtil.close(fis); |
112 | 0 | } |
113 | |
} |
114 | |
|
115 | 0 | return ret; |
116 | |
} |
117 | |
|
118 | |
private void initIncFile() { |
119 | |
try { |
120 | 0 | File tempIncfile = new File(SwordUtil.getExpandedDataPath(getBookMetaData()).getPath() + File.separator + INCFILE); |
121 | 0 | if (tempIncfile.exists()) { |
122 | 0 | this.incfile = tempIncfile; |
123 | |
} |
124 | 0 | } catch (BookException e) { |
125 | 0 | LOGGER.error("Error on checking incfile: {}", e.getMessage(), e); |
126 | 0 | this.incfile = null; |
127 | 0 | } |
128 | 0 | } |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
public int getIncfileValue() { |
134 | 0 | if (incfileValue == -1) { |
135 | |
try { |
136 | 0 | readIncfile(); |
137 | 0 | } catch (IOException e) { |
138 | 0 | LOGGER.error("IO Error: {}", e.getMessage(), e); |
139 | 0 | } |
140 | |
} |
141 | |
|
142 | 0 | return incfileValue; |
143 | |
} |
144 | |
|
145 | |
public void setIncfileValue(int incValue) { |
146 | 0 | this.incfileValue = incValue; |
147 | 0 | } |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
public File getIncfile() { |
153 | 0 | if (incfile == null) { |
154 | 0 | initIncFile(); |
155 | |
} |
156 | 0 | return incfile; |
157 | |
} |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
public void setIncfile(File incfile) { |
164 | 0 | this.incfile = incfile; |
165 | 0 | } |
166 | |
|
167 | |
public static final String INCFILE = "incfile"; |
168 | |
private File incfile; |
169 | |
private int incfileValue; |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | 0 | private static final Logger LOGGER = LoggerFactory.getLogger(RawFileBackendState.class); |
175 | |
} |