1 7 8 package javax.imageio.stream; 9 10 import java.io.DataInput ; 11 import java.io.File ; 12 import java.io.FileNotFoundException ; 13 import java.io.IOException ; 14 import java.io.RandomAccessFile ; 15 16 24 public class FileImageInputStream extends ImageInputStreamImpl { 25 26 private RandomAccessFile raf; 27 28 46 public FileImageInputStream(File f) 47 throws FileNotFoundException , IOException { 48 if (f == null) { 49 throw new IllegalArgumentException ("f == null!"); 50 } 51 this.raf = new RandomAccessFile (f, "r"); 52 } 53 54 67 public FileImageInputStream(RandomAccessFile raf) { 68 if (raf == null) { 69 throw new IllegalArgumentException ("raf == null!"); 70 } 71 this.raf = raf; 72 } 73 74 public int read() throws IOException { 75 checkClosed(); 76 bitOffset = 0; 77 int val = raf.read(); 78 if (val != -1) { 79 ++streamPos; 80 } 81 return val; 82 } 83 84 public int read(byte[] b, int off, int len) throws IOException { 85 checkClosed(); 86 bitOffset = 0; 87 int nbytes = raf.read(b, off, len); 88 if (nbytes != -1) { 89 streamPos += nbytes; 90 } 91 return nbytes; 92 } 93 94 101 public long length() { 102 try { 103 checkClosed(); 104 return raf.length(); 105 } catch (IOException e) { 106 return -1L; 107 } 108 } 109 110 public void seek(long pos) throws IOException { 111 checkClosed(); 112 if (pos < flushedPos) { 113 throw new IndexOutOfBoundsException ("pos < flushedPos!"); 114 } 115 bitOffset = 0; 116 raf.seek(pos); 117 streamPos = raf.getFilePointer(); 118 } 119 120 public void close() throws IOException { 121 super.close(); 122 raf.close(); 123 } 124 } 125 | Popular Tags |