1 18 package org.apache.batik.ext.awt.image.codec; 19 20 import java.io.DataInput ; 21 import java.io.DataInputStream ; 22 import java.io.EOFException ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 26 84 public abstract class SeekableStream extends InputStream implements DataInput { 85 86 99 public static SeekableStream wrapInputStream(InputStream is, 100 boolean canSeekBackwards) { 101 SeekableStream stream = null; 102 103 if (canSeekBackwards) { 104 try { 105 stream = new FileCacheSeekableStream(is); 106 } catch (Exception e) { 107 stream = new MemoryCacheSeekableStream(is); 108 } 109 } else { 110 stream = new ForwardSeekableStream(is); 111 } 112 return stream; 113 } 114 115 117 131 public abstract int read() throws IOException ; 132 133 183 public abstract int read(byte[] b, int off, int len) throws IOException ; 184 185 192 193 protected long markPos = -1L; 194 195 199 public synchronized void mark(int readLimit) { 200 try { 201 markPos = getFilePointer(); 202 } catch (IOException e) { 203 markPos = -1L; 204 } 205 } 206 207 212 public synchronized void reset() throws IOException { 213 if (markPos != -1) { 214 seek(markPos); 215 } 216 } 217 218 225 public boolean markSupported() { 226 return canSeekBackwards(); 227 } 228 229 234 public boolean canSeekBackwards() { 235 return false; 236 } 237 238 245 public abstract long getFilePointer() throws IOException ; 246 247 262 public abstract void seek(long pos) throws IOException ; 263 264 266 278 public final void readFully(byte[] b) throws IOException { 279 readFully(b, 0, b.length); 280 } 281 282 296 public final void readFully(byte[] b, int off, int len) 297 throws IOException { 298 int n = 0; 299 do { 300 int count = this.read(b, off + n, len - n); 301 if (count < 0) 302 throw new EOFException (); 303 n += count; 304 } while (n < len); 305 } 306 307 309 325 public int skipBytes(int n) throws IOException { 326 if (n <= 0) { 327 return 0; 328 } 329 return (int)skip(n); 330 } 331 332 344 public final boolean readBoolean() throws IOException { 345 int ch = this.read(); 346 if (ch < 0) 347 throw new EOFException (); 348 return (ch != 0); 349 } 350 351 369 public final byte readByte() throws IOException { 370 int ch = this.read(); 371 if (ch < 0) 372 throw new EOFException (); 373 return (byte)(ch); 374 } 375 376 389 public final int readUnsignedByte() throws IOException { 390 int ch = this.read(); 391 if (ch < 0) 392 throw new EOFException (); 393 return ch; 394 } 395 396 417 public final short readShort() throws IOException { 418 int ch1 = this.read(); 419 int ch2 = this.read(); 420 if ((ch1 | ch2) < 0) 421 throw new EOFException (); 422 return (short)((ch1 << 8) + (ch2 << 0)); 423 } 424 425 446 public final short readShortLE() throws IOException { 447 int ch1 = this.read(); 448 int ch2 = this.read(); 449 if ((ch1 | ch2) < 0) 450 throw new EOFException (); 451 return (short)((ch2 << 8) + (ch1 << 0)); 452 } 453 454 474 public final int readUnsignedShort() throws IOException { 475 int ch1 = this.read(); 476 int ch2 = this.read(); 477 if ((ch1 | ch2) < 0) 478 throw new EOFException (); 479 return (ch1 << 8) + (ch2 << 0); 480 } 481 482 503 public final int readUnsignedShortLE() throws IOException { 504 int ch1 = this.read(); 505 int ch2 = this.read(); 506 if ((ch1 | ch2) < 0) 507 throw new EOFException (); 508 return (ch2 << 8) + (ch1 << 0); 509 } 510 511 530 public final char readChar() throws IOException { 531 int ch1 = this.read(); 532 int ch2 = this.read(); 533 if ((ch1 | ch2) < 0) 534 throw new EOFException (); 535 return (char)((ch1 << 8) + (ch2 << 0)); 536 } 537 538 558 public final char readCharLE() throws IOException { 559 int ch1 = this.read(); 560 int ch2 = this.read(); 561 if ((ch1 | ch2) < 0) 562 throw new EOFException (); 563 return (char)((ch2 << 8) + (ch1 << 0)); 564 } 565 566 586 public final int readInt() throws IOException { 587 int ch1 = this.read(); 588 int ch2 = this.read(); 589 int ch3 = this.read(); 590 int ch4 = this.read(); 591 if ((ch1 | ch2 | ch3 | ch4) < 0) 592 throw new EOFException (); 593 return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); 594 } 595 596 617 public final int readIntLE() throws IOException { 618 int ch1 = this.read(); 619 int ch2 = this.read(); 620 int ch3 = this.read(); 621 int ch4 = this.read(); 622 if ((ch1 | ch2 | ch3 | ch4) < 0) 623 throw new EOFException (); 624 return ((ch4 << 24) + (ch3 << 16) + (ch2 << 8) + (ch1 << 0)); 625 } 626 627 647 public final long readUnsignedInt() throws IOException { 648 long ch1 = this.read(); 649 long ch2 = this.read(); 650 long ch3 = this.read(); 651 long ch4 = this.read(); 652 if ((ch1 | ch2 | ch3 | ch4) < 0) 653 throw new EOFException (); 654 return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); 655 } 656 657 private byte[] ruileBuf = new byte[4]; 658 659 680 public final long readUnsignedIntLE() throws IOException { 681 this.readFully(ruileBuf); 682 long ch1 = (ruileBuf[0] & 0xff); 683 long ch2 = (ruileBuf[1] & 0xff); 684 long ch3 = (ruileBuf[2] & 0xff); 685 long ch4 = (ruileBuf[3] & 0xff); 686 687 return ((ch4 << 24) + (ch3 << 16) + (ch2 << 8) + (ch1 << 0)); 688 } 689 690 718 public final long readLong() throws IOException { 719 return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL); 720 } 721 722 751 public final long readLongLE() throws IOException { 752 int i1 = readIntLE(); 753 int i2 = readIntLE(); 754 return ((long)i2 << 32) + (i1 & 0xFFFFFFFFL); 755 } 756 757 774 public final float readFloat() throws IOException { 775 return Float.intBitsToFloat(readInt()); 776 } 777 778 796 public final float readFloatLE() throws IOException { 797 return Float.intBitsToFloat(readIntLE()); 798 } 799 800 817 public final double readDouble() throws IOException { 818 return Double.longBitsToDouble(readLong()); 819 } 820 821 839 public final double readDoubleLE() throws IOException { 840 return Double.longBitsToDouble(readLongLE()); 841 } 842 843 866 public final String readLine() throws IOException { 867 StringBuffer input = new StringBuffer (); 868 int c = -1; 869 boolean eol = false; 870 871 while (!eol) { 872 switch (c = read()) { 873 case -1: 874 case '\n': 875 eol = true; 876 break; 877 case '\r': 878 eol = true; 879 long cur = getFilePointer(); 880 if ((read()) != '\n') { 881 seek(cur); 882 } 883 break; 884 default: 885 input.append((char)c); 886 break; 887 } 888 } 889 890 if ((c == -1) && (input.length() == 0)) { 891 return null; 892 } 893 return input.toString(); 894 } 895 896 918 public final String readUTF() throws IOException { 919 return DataInputStream.readUTF(this); 920 } 921 922 926 protected void finalize() throws Throwable { 927 super.finalize(); 928 close(); 929 } 930 } 931 | Popular Tags |