Debian Codegraph @

src/uk/me/parabola/imgfmt/app/BufferedImgFileReader.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2007 Steve Ratcliffe
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 *
14 * Author: Steve Ratcliffe
15 * Create date: Dec 14, 2007
16 */
17 package uk.me.parabola.imgfmt.app;
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.nio.ByteBuffer;
22
23 import uk.me.parabola.imgfmt.MapFailedException;
24 import uk.me.parabola.imgfmt.ReadFailedException;
25 import uk.me.parabola.imgfmt.fs.ImgChannel;
26 import uk.me.parabola.log.Logger;
27
28 /**
29 * Read from an img file via a buffer.
30 *
31 * @author Steve Ratcliffe
32 */
33 public class BufferedImgFileReader implements ImgFileReader {
34 private static final Logger log = Logger.getLogger(BufferedImgFileReader.class);
35
36 // Buffer size, must be a power of 2
37 private static final int BUF_SIZE = 0x1000;
38
39 private final ImgChannel chan;
40
41 // The buffer that we read out of
42 private final ByteBuffer buf = ByteBuffer.allocate(BUF_SIZE);
43 private long bufStart;
44 private int bufSize = -1;
45
46 // We keep our own idea of the file position.
47 private long position;
48 // the position of the header, normally 0, but not in GMP format
49 private final int gmpOffset;
50
51 public BufferedImgFileReader(ImgChannel chan) {
52 this(chan, 0);
53 }
54
55 public BufferedImgFileReader(ImgChannel chan, int gmpOffset) {
56 this.chan = chan;
57 this.gmpOffset = gmpOffset;
58 // buf.order(ByteOrder.LITTLE_ENDIAN);
59 // could use getShort/getChar/getInt if sure buffer loaded
60 }
61
62 @Override
63 public int getGMPOffset() {
64 return gmpOffset;
65 }
66
67 /**
68 * Called when the stream is closed. Any resources can be freed.
69 *
70 * @throws IOException When there is an error in closing.
71 */
72 public void close() throws IOException {
73 chan.close();
74 }
75
76 /**
77 * Get the position. Needed because may not be reflected in the underlying
78 * file if being buffered.
79 *
80 * @return The logical position within the file.
81 */
82 public long position() {
83 return position;
84 }
85
86 /**
87 * Set the position of the file.
88 *
89 * @param pos The new position in the file.
90 */
91 public void position(long pos) {
92 position = pos;
93 }
94
95 /**
96 * Read in a single byte from the current position.
97 * Should not be used for reading numbers, use get1s/u instead.
98 * @return The byte that was read.
99 */
100 public byte get() throws ReadFailedException {
101 // Check if the current position is within the buffer
102 fillBuffer();
103
104 int pos = (int) (position - bufStart);
105 if (pos >= bufSize)
106 return 0; // XXX do something else
107 // doing following makes test/func/files/GmapsuppTest.java test fail
108 // throw new MapFailedException("buffered input unavailable");
109
110 position++;
111 return buf.get(pos);
112 }
113
114 /**
115 * Read in a single byte from the current position.
116 * @return int sign-extended value that was read.
117 */
118 public int get1s() throws ReadFailedException {
119 return get();
120 }
121
122 /**
123 * Read in two bytes. Done in the correct byte order.
124 * @return int sign-extended value that was read.
125 */
126 public int get2s() throws ReadFailedException {
127 byte b1 = get();
128 byte b2 = get();
129 return (b1 & 0xff)
130 | (b2 << 8)
131 ;
132 }
133
134 /**
135 * Read a three byte signed quantity.
136 * @return int sign-extended value that was read.
137 * @throws ReadFailedException
138 */
139 public int get3s() throws ReadFailedException {
140 // Slow but sure implementation
141 byte b1 = get();
142 byte b2 = get();
143 byte b3 = get();
144 return (b1 & 0xff)
145 | ((b2 & 0xff) << 8)
146 | (b3 << 16)
147 ;
148 }
149
150 public int get1u() throws ReadFailedException {
151 return get() & 0xff;
152 }
153
154 /**
155 * Read in two bytes. Done in the correct byte order.
156 *
157 * @return The 2 byte integer that was read.
158 */
159 public int get2u() throws ReadFailedException {
160 byte b1 = get();
161 byte b2 = get();
162 return (b1 & 0xff)
163 | ((b2 & 0xff) << 8)
164 ;
165 }
166
167 /**
168 * Read a three byte signed quantity.
169 * @return The read value.
170 * @throws ReadFailedException
171 */
172 public int get3u() throws ReadFailedException {
173 // Slow but sure implementation
174 byte b1 = get();
175 byte b2 = get();
176 byte b3 = get();
177 return (b1 & 0xff)
178 | ((b2 & 0xff) << 8)
179 | ((b3 & 0xff) << 16)
180 ;
181 }
182
183
184 public int getNu(int nBytes) throws ReadFailedException {
185 switch (nBytes) {
186 case 1: return get1u();
187 case 2: return get2u();
188 case 3: return get3u();
189 case 4: return get4();
190 default: // this is a programming error so exit
191 throw new MapFailedException("bad integer size " + nBytes);
192 }
193 }
194
195 /**
196 * Read in a 4 byte signed value.
197 *
198 * @return A 4 byte integer.
199 */
200 public int get4() throws ReadFailedException {
201 byte b1 = get();
202 byte b2 = get();
203 byte b3 = get();
204 byte b4 = get();
205 return (b1 & 0xff)
206 | ((b2 & 0xff) << 8)
207 | ((b3 & 0xff) << 16)
208 | (b4 << 24)
209 ;
210 }
211 /**
212 * Read in an arbitrary length sequence of bytes.
213 *
214 * @param len The number of bytes to read.
215 */
216 public byte[] get(int len) throws ReadFailedException {
217 byte[] bytes = new byte[len];
218
219 // Slow but sure implementation.
220 for (int i = 0; i < len; i++) {
221 bytes[i] = get();
222 }
223 return bytes;
224 }
225
226 /**
227 * Read a zero terminated string from the file, still as raw bytes.
228 *
229 * @return A byte array containing the encoded representation of the string.
230 * @throws ReadFailedException For failures.
231 */
232 public byte[] getZString() throws ReadFailedException {
233 ByteArrayOutputStream out = new ByteArrayOutputStream();
234
235 // Slow but sure implementation.
236 for (byte b = get(); b != 0; b = get()) {
237 out.write(b);
238 }
239 return out.toByteArray();
240 }
241
242 /**
243 * Read in a string of digits in the compressed base 11 format that is used
244 * for phone numbers in the POI section.
245 * @param delimiter This will replace all digit 11 characters. Usually a
246 * '-' to separate numbers in a telephone. No doubt there is a different
247 * standard in each country.
248 * @return A phone number possibly containing the delimiter character.
249 */
250 public String getBase11str(byte firstChar, char delimiter) {
251 // NB totally untested.
252 StringBuilder str11 = new StringBuilder();
253 int term = 2;
254
255 int ch = firstChar & 0xff;
256 do {
257 assert !(str11.length() == 0 && (ch & 0x80) == 0);
258
259 if ((ch & 0x80) != 0)
260 --term;
261 str11.append(base(ch & 0x7F, 11, 2));
262 if (term != 0)
263 ch = get();
264 } while (term != 0);
265
266 // Remove any trailing delimiters
267 while (str11.length() > 0 && str11.charAt(str11.length()-1) == 'A')
268 str11.setLength(str11.length()-1);
269
270 // Convert in-line delimiters to the char delimiter
271 int len = str11.length();
272 for (int i = 0; i < len; i++) {
273 if (str11.charAt(i) == 'A')
274 str11.setCharAt(i, delimiter);
275 }
276
277 return str11.toString();
278 }
279
280 private String base(int inNum, int base, int width) {
281 int num = inNum;
282 StringBuilder val = new StringBuilder();
283
284 if (base < 2 || base > 36 || width < 1)
285 return "";
286
287 String digit = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
288 while (num != 0) {
289 val.append(digit.charAt(num % base));
290 num /= base;
291 }
292
293 while (val.length() < width)
294 val.append('0');
295
296 val.reverse();
297 return val.toString();
298 }
299
300 /**
301 * Check to see if the buffer contains the byte at the current position.
302 * If not then it is re-read so that it does.
303 *
304 * @throws ReadFailedException If the buffer needs filling and the file cannot be
305 * read.
306 */
307 private void fillBuffer() throws ReadFailedException {
308 // If we are no longer inside the buffer, then re-read it.
309 if (position < bufStart || position >= bufStart + bufSize) {
310
311 // Get channel position on a block boundary.
312 bufStart = position & ~(BUF_SIZE - 1);
313 chan.position(bufStart);
314 log.debug("reading in a buffer start=", bufStart);
315
316 // Fill buffer
317 buf.clear();
318 bufSize = 0;
319 try {
320 bufSize = chan.read(buf);
321 } catch (IOException e) {
322 throw new ReadFailedException("failed to fill buffer", e);
323 }
324
325 log.debug("there were", bufSize, "bytes read");
326 }
327 }
328 }