| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class BufferedImgFileReader implements ImgFileReader { |
| 34 |
private static final Logger log = Logger.getLogger(BufferedImgFileReader.class); |
| 35 |
|
| 36 |
|
| 37 |
private static final int BUF_SIZE = 0x1000; |
| 38 |
|
| 39 |
private final ImgChannel chan; |
| 40 |
|
| 41 |
|
| 42 |
private final ByteBuffer buf = ByteBuffer.allocate(BUF_SIZE); |
| 43 |
private long bufStart; |
| 44 |
private int bufSize = -1; |
| 45 |
|
| 46 |
|
| 47 |
private long position; |
| 48 |
|
| 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 |
|
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public int getGMPOffset() { |
| 64 |
return gmpOffset; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public void close() throws IOException { |
| 73 |
chan.close(); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public long position() { |
| 83 |
return position; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public void position(long pos) { |
| 92 |
position = pos; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public byte get() throws ReadFailedException { |
| 101 |
|
| 102 |
fillBuffer(); |
| 103 |
|
| 104 |
int pos = (int) (position - bufStart); |
| 105 |
if (pos >= bufSize) |
| 106 |
return 0; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
position++; |
| 111 |
return buf.get(pos); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public int get1s() throws ReadFailedException { |
| 119 |
return get(); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 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 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public int get3s() throws ReadFailedException { |
| 140 |
|
| 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 |
|
| 156 |
|
| 157 |
|
| 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 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public int get3u() throws ReadFailedException { |
| 173 |
|
| 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: |
| 191 |
throw new MapFailedException("bad integer size " + nBytes); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 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 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public byte[] get(int len) throws ReadFailedException { |
| 217 |
byte[] bytes = new byte[len]; |
| 218 |
|
| 219 |
|
| 220 |
for (int i = 0; i < len; i++) { |
| 221 |
bytes[i] = get(); |
| 222 |
} |
| 223 |
return bytes; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public byte[] getZString() throws ReadFailedException { |
| 233 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 234 |
|
| 235 |
|
| 236 |
for (byte b = get(); b != 0; b = get()) { |
| 237 |
out.write(b); |
| 238 |
} |
| 239 |
return out.toByteArray(); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public String getBase11str(byte firstChar, char delimiter) { |
| 251 |
|
| 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 |
|
| 267 |
while (str11.length() > 0 && str11.charAt(str11.length()-1) == 'A') |
| 268 |
str11.setLength(str11.length()-1); |
| 269 |
|
| 270 |
|
| 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 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
private void fillBuffer() throws ReadFailedException { |
| 308 |
|
| 309 |
if (position < bufStart || position >= bufStart + bufSize) { |
| 310 |
|
| 311 |
|
| 312 |
bufStart = position & ~(BUF_SIZE - 1); |
| 313 |
chan.position(bufStart); |
| 314 |
log.debug("reading in a buffer start=", bufStart); |
| 315 |
|
| 316 |
|
| 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 |
} |