1 50 51 package com.lowagie.text.pdf; 52 53 import com.lowagie.text.Document; 54 import java.io.ByteArrayOutputStream ; 55 import java.io.DataInput ; 56 import java.io.DataInputStream ; 57 import java.io.EOFException ; 58 import java.io.File ; 59 import java.io.FileInputStream ; 60 import java.io.IOException ; 61 import java.io.InputStream ; 62 import java.io.RandomAccessFile ; 63 import java.net.URL ; 64 69 public class RandomAccessFileOrArray implements DataInput { 70 71 MappedRandomAccessFile rf; 72 RandomAccessFile trf; 73 boolean plainRandomAccess; 74 String filename; 75 byte arrayIn[]; 76 int arrayInPtr; 77 byte back; 78 boolean isBack = false; 79 80 81 private int startOffset = 0; 82 83 public RandomAccessFileOrArray(String filename) throws IOException { 84 this(filename, false, Document.plainRandomAccess); 85 } 86 87 public RandomAccessFileOrArray(String filename, boolean forceRead, boolean plainRandomAccess) throws IOException { 88 this.plainRandomAccess = plainRandomAccess; 89 File file = new File (filename); 90 if (!file.canRead()) { 91 if (filename.startsWith("file:/") || filename.startsWith("http://") || filename.startsWith("https://") || filename.startsWith("jar:")) { 92 InputStream is = new URL (filename).openStream(); 93 try { 94 this.arrayIn = InputStreamToArray(is); 95 return; 96 } 97 finally { 98 try {is.close();}catch(IOException ioe){} 99 } 100 } 101 else { 102 InputStream is = BaseFont.getResourceStream(filename); 103 if (is == null) 104 throw new IOException (filename + " not found as file or resource."); 105 try { 106 this.arrayIn = InputStreamToArray(is); 107 return; 108 } 109 finally { 110 try {is.close();}catch(IOException ioe){} 111 } 112 } 113 } 114 else if (forceRead) { 115 InputStream s = null; 116 try { 117 s = new FileInputStream (file); 118 this.arrayIn = InputStreamToArray(s); 119 } 120 finally { 121 try {if (s != null) {s.close();}}catch(Exception e){} 122 } 123 return; 124 } 125 this.filename = filename; 126 if (plainRandomAccess) 127 trf = new RandomAccessFile (filename, "r"); 128 else 129 rf = new MappedRandomAccessFile(filename, "r"); 130 } 131 132 public RandomAccessFileOrArray(URL url) throws IOException { 133 InputStream is = url.openStream(); 134 try { 135 this.arrayIn = InputStreamToArray(is); 136 } 137 finally { 138 try {is.close();}catch(IOException ioe){} 139 } 140 } 141 142 public RandomAccessFileOrArray(InputStream is) throws IOException { 143 this.arrayIn = InputStreamToArray(is); 144 } 145 146 public static byte[] InputStreamToArray(InputStream is) throws IOException { 147 byte b[] = new byte[8192]; 148 ByteArrayOutputStream out = new ByteArrayOutputStream (); 149 while (true) { 150 int read = is.read(b); 151 if (read < 1) 152 break; 153 out.write(b, 0, read); 154 } 155 return out.toByteArray(); 156 } 157 158 public RandomAccessFileOrArray(byte arrayIn[]) { 159 this.arrayIn = arrayIn; 160 } 161 162 public RandomAccessFileOrArray(RandomAccessFileOrArray file) { 163 filename = file.filename; 164 arrayIn = file.arrayIn; 165 startOffset = file.startOffset; 166 plainRandomAccess = file.plainRandomAccess; 167 } 168 169 public void pushBack(byte b) { 170 back = b; 171 isBack = true; 172 } 173 174 public int read() throws IOException { 175 if(isBack) { 176 isBack = false; 177 return back & 0xff; 178 } 179 if (arrayIn == null) 180 return plainRandomAccess ? trf.read() : rf.read(); 181 else { 182 if (arrayInPtr >= arrayIn.length) 183 return -1; 184 return arrayIn[arrayInPtr++] & 0xff; 185 } 186 } 187 188 public int read(byte[] b, int off, int len) throws IOException { 189 if (len == 0) 190 return 0; 191 int n = 0; 192 if (isBack) { 193 isBack = false; 194 if (len == 1) { 195 b[off] = back; 196 return 1; 197 } 198 else { 199 n = 1; 200 b[off++] = back; 201 --len; 202 } 203 } 204 if (arrayIn == null) { 205 return (plainRandomAccess ? trf.read(b, off, len) : rf.read(b, off, len)) + n; 206 } 207 else { 208 if (arrayInPtr >= arrayIn.length) 209 return -1; 210 if (arrayInPtr + len > arrayIn.length) 211 len = arrayIn.length - arrayInPtr; 212 System.arraycopy(arrayIn, arrayInPtr, b, off, len); 213 arrayInPtr += len; 214 return len + n; 215 } 216 } 217 218 public int read(byte b[]) throws IOException { 219 return read(b, 0, b.length); 220 } 221 222 public void readFully(byte b[]) throws IOException { 223 readFully(b, 0, b.length); 224 } 225 226 public void readFully(byte b[], int off, int len) throws IOException { 227 int n = 0; 228 do { 229 int count = read(b, off + n, len - n); 230 if (count < 0) 231 throw new EOFException (); 232 n += count; 233 } while (n < len); 234 } 235 236 public long skip(long n) throws IOException { 237 return skipBytes((int)n); 238 } 239 240 public int skipBytes(int n) throws IOException { 241 if (n <= 0) { 242 return 0; 243 } 244 int adj = 0; 245 if (isBack) { 246 isBack = false; 247 if (n == 1) { 248 return 1; 249 } 250 else { 251 --n; 252 adj = 1; 253 } 254 } 255 int pos; 256 int len; 257 int newpos; 258 259 pos = getFilePointer(); 260 len = length(); 261 newpos = pos + n; 262 if (newpos > len) { 263 newpos = len; 264 } 265 seek(newpos); 266 267 268 return newpos - pos + adj; 269 } 270 271 public void reOpen() throws IOException { 272 if (filename != null && rf == null && trf == null) { 273 if (plainRandomAccess) 274 trf = new RandomAccessFile (filename, "r"); 275 else 276 rf = new MappedRandomAccessFile(filename, "r"); 277 } 278 seek(0); 279 } 280 281 protected void insureOpen() throws IOException { 282 if (filename != null && rf == null && trf == null) { 283 reOpen(); 284 } 285 } 286 287 public boolean isOpen() { 288 return (filename == null || rf != null || trf != null); 289 } 290 291 public void close() throws IOException { 292 isBack = false; 293 if (rf != null) { 294 rf.close(); 295 rf = null; 296 } 297 else if (trf != null) { 298 trf.close(); 299 trf = null; 300 } 301 } 302 303 public int length() throws IOException { 304 if (arrayIn == null) { 305 insureOpen(); 306 return (int)(plainRandomAccess ? trf.length() : rf.length()) - startOffset; 307 } 308 else 309 return arrayIn.length - startOffset; 310 } 311 312 public void seek(int pos) throws IOException { 313 pos += startOffset; 314 isBack = false; 315 if (arrayIn == null) { 316 insureOpen(); 317 if (plainRandomAccess) 318 trf.seek(pos); 319 else 320 rf.seek(pos); 321 } 322 else 323 arrayInPtr = pos; 324 } 325 326 public void seek(long pos) throws IOException { 327 seek((int)pos); 328 } 329 330 public int getFilePointer() throws IOException { 331 insureOpen(); 332 int n = isBack ? 1 : 0; 333 if (arrayIn == null) { 334 return (int)(plainRandomAccess ? trf.getFilePointer() : rf.getFilePointer()) - n - startOffset; 335 } 336 else 337 return arrayInPtr - n - startOffset; 338 } 339 340 public boolean readBoolean() throws IOException { 341 int ch = this.read(); 342 if (ch < 0) 343 throw new EOFException (); 344 return (ch != 0); 345 } 346 347 public byte readByte() throws IOException { 348 int ch = this.read(); 349 if (ch < 0) 350 throw new EOFException (); 351 return (byte)(ch); 352 } 353 354 public int readUnsignedByte() throws IOException { 355 int ch = this.read(); 356 if (ch < 0) 357 throw new EOFException (); 358 return ch; 359 } 360 361 public short readShort() throws IOException { 362 int ch1 = this.read(); 363 int ch2 = this.read(); 364 if ((ch1 | ch2) < 0) 365 throw new EOFException (); 366 return (short)((ch1 << 8) + ch2); 367 } 368 369 390 public final short readShortLE() throws IOException { 391 int ch1 = this.read(); 392 int ch2 = this.read(); 393 if ((ch1 | ch2) < 0) 394 throw new EOFException (); 395 return (short)((ch2 << 8) + (ch1 << 0)); 396 } 397 398 public int readUnsignedShort() throws IOException { 399 int ch1 = this.read(); 400 int ch2 = this.read(); 401 if ((ch1 | ch2) < 0) 402 throw new EOFException (); 403 return (ch1 << 8) + ch2; 404 } 405 406 427 public final int readUnsignedShortLE() throws IOException { 428 int ch1 = this.read(); 429 int ch2 = this.read(); 430 if ((ch1 | ch2) < 0) 431 throw new EOFException (); 432 return (ch2 << 8) + (ch1 << 0); 433 } 434 435 public char readChar() throws IOException { 436 int ch1 = this.read(); 437 int ch2 = this.read(); 438 if ((ch1 | ch2) < 0) 439 throw new EOFException (); 440 return (char)((ch1 << 8) + ch2); 441 } 442 443 463 public final char readCharLE() throws IOException { 464 int ch1 = this.read(); 465 int ch2 = this.read(); 466 if ((ch1 | ch2) < 0) 467 throw new EOFException (); 468 return (char)((ch2 << 8) + (ch1 << 0)); 469 } 470 471 public int readInt() throws IOException { 472 int ch1 = this.read(); 473 int ch2 = this.read(); 474 int ch3 = this.read(); 475 int ch4 = this.read(); 476 if ((ch1 | ch2 | ch3 | ch4) < 0) 477 throw new EOFException (); 478 return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + ch4); 479 } 480 481 502 public final int readIntLE() throws IOException { 503 int ch1 = this.read(); 504 int ch2 = this.read(); 505 int ch3 = this.read(); 506 int ch4 = this.read(); 507 if ((ch1 | ch2 | ch3 | ch4) < 0) 508 throw new EOFException (); 509 return ((ch4 << 24) + (ch3 << 16) + (ch2 << 8) + (ch1 << 0)); 510 } 511 512 532 public final long readUnsignedInt() throws IOException { 533 long ch1 = this.read(); 534 long ch2 = this.read(); 535 long ch3 = this.read(); 536 long ch4 = this.read(); 537 if ((ch1 | ch2 | ch3 | ch4) < 0) 538 throw new EOFException (); 539 return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); 540 } 541 542 public final long readUnsignedIntLE() throws IOException { 543 long ch1 = this.read(); 544 long ch2 = this.read(); 545 long ch3 = this.read(); 546 long ch4 = this.read(); 547 if ((ch1 | ch2 | ch3 | ch4) < 0) 548 throw new EOFException (); 549 return ((ch4 << 24) + (ch3 << 16) + (ch2 << 8) + (ch1 << 0)); 550 } 551 552 public long readLong() throws IOException { 553 return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL); 554 } 555 556 public final long readLongLE() throws IOException { 557 int i1 = readIntLE(); 558 int i2 = readIntLE(); 559 return ((long)i2 << 32) + (i1 & 0xFFFFFFFFL); 560 } 561 562 public float readFloat() throws IOException { 563 return Float.intBitsToFloat(readInt()); 564 } 565 566 public final float readFloatLE() throws IOException { 567 return Float.intBitsToFloat(readIntLE()); 568 } 569 570 public double readDouble() throws IOException { 571 return Double.longBitsToDouble(readLong()); 572 } 573 574 public final double readDoubleLE() throws IOException { 575 return Double.longBitsToDouble(readLongLE()); 576 } 577 578 public String readLine() throws IOException { 579 StringBuffer input = new StringBuffer (); 580 int c = -1; 581 boolean eol = false; 582 583 while (!eol) { 584 switch (c = read()) { 585 case -1: 586 case '\n': 587 eol = true; 588 break; 589 case '\r': 590 eol = true; 591 int cur = getFilePointer(); 592 if ((read()) != '\n') { 593 seek(cur); 594 } 595 break; 596 default: 597 input.append((char)c); 598 break; 599 } 600 } 601 602 if ((c == -1) && (input.length() == 0)) { 603 return null; 604 } 605 return input.toString(); 606 } 607 608 public String readUTF() throws IOException { 609 return DataInputStream.readUTF(this); 610 } 611 612 616 public int getStartOffset() { 617 return this.startOffset; 618 } 619 620 624 public void setStartOffset(int startOffset) { 625 this.startOffset = startOffset; 626 } 627 628 } 629 | Popular Tags |