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 23 public class FileImageOutputStream extends ImageOutputStreamImpl { 24 25 private RandomAccessFile raf; 26 27 42 public FileImageOutputStream(File f) 43 throws FileNotFoundException , IOException { 44 this(f == null ? null : new RandomAccessFile (f, "rw")); 45 } 46 47 56 public FileImageOutputStream(RandomAccessFile raf) { 57 if (raf == null) { 58 throw new IllegalArgumentException ("raf == null!"); 59 } 60 this.raf = raf; 61 } 62 63 public int read() throws IOException { 64 checkClosed(); 65 bitOffset = 0; 66 int val = raf.read(); 67 if (val != -1) { 68 ++streamPos; 69 } 70 return val; 71 } 72 73 public int read(byte[] b, int off, int len) throws IOException { 74 checkClosed(); 75 bitOffset = 0; 76 int nbytes = raf.read(b, off, len); 77 if (nbytes != -1) { 78 streamPos += nbytes; 79 } 80 return nbytes; 81 } 82 83 public void write(int b) throws IOException { 84 checkClosed(); 85 flushBits(); 86 raf.write(b); 87 ++streamPos; 88 } 89 90 public void write(byte[] b, int off, int len) throws IOException { 91 checkClosed(); 92 flushBits(); 93 raf.write(b, off, len); 94 streamPos += len; 95 } 96 97 public long length() { 98 try { 99 checkClosed(); 100 return raf.length(); 101 } catch (IOException e) { 102 return -1L; 103 } 104 } 105 106 117 public void seek(long pos) throws IOException { 118 checkClosed(); 119 if (pos < flushedPos) { 120 throw new IndexOutOfBoundsException ("pos < flushedPos!"); 121 } 122 bitOffset = 0; 123 raf.seek(pos); 124 streamPos = raf.getFilePointer(); 125 } 126 127 public void close() throws IOException { 128 super.close(); 129 raf.close(); 130 } 131 } 132 | Popular Tags |