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.OutputStream ; 15 import java.io.RandomAccessFile ; 16 import com.sun.imageio.stream.StreamCloser; 17 18 25 public class FileCacheImageOutputStream extends ImageOutputStreamImpl { 26 27 private OutputStream stream; 28 29 private File cacheFile; 30 31 private RandomAccessFile cache; 32 33 private long maxStreamPos = 0L; 35 36 58 public FileCacheImageOutputStream(OutputStream stream, File cacheDir) 59 throws IOException { 60 if (stream == null) { 61 throw new IllegalArgumentException ("stream == null!"); 62 } 63 if ((cacheDir != null) && !(cacheDir.isDirectory())) { 64 throw new IllegalArgumentException ("Not a directory!"); 65 } 66 this.stream = stream; 67 this.cacheFile = 68 File.createTempFile("imageio", ".tmp", cacheDir); 69 this.cache = new RandomAccessFile (cacheFile, "rw"); 70 StreamCloser.addToQueue(this); 71 } 72 73 public int read() throws IOException { 74 bitOffset = 0; 75 int val = cache.read(); 76 if (val != -1) { 77 ++streamPos; 78 } 79 return val; 80 } 81 82 public int read(byte[] b, int off, int len) throws IOException { 83 bitOffset = 0; 84 int nbytes = cache.read(b, off, len); 85 if (nbytes != -1) { 86 streamPos += nbytes; 87 } 88 return nbytes; 89 } 90 91 public void write(int b) throws IOException { 92 flushBits(); 93 cache.write(b); 94 ++streamPos; 95 maxStreamPos = Math.max(maxStreamPos, streamPos); 96 } 97 98 public void write(byte[] b, int off, int len) throws IOException { 99 flushBits(); 100 cache.write(b, off, len); 101 streamPos += len; 102 maxStreamPos = Math.max(maxStreamPos, streamPos); 103 } 104 105 public long length() { 106 try { 107 return cache.length(); 108 } catch (IOException e) { 109 return -1L; 110 } 111 } 112 113 124 public void seek(long pos) throws IOException { 125 checkClosed(); 126 127 if (pos < flushedPos) { 128 throw new IndexOutOfBoundsException (); 129 } 130 131 cache.seek(pos); 132 this.streamPos = cache.getFilePointer(); 133 maxStreamPos = Math.max(maxStreamPos, streamPos); 134 this.bitOffset = 0; 135 } 136 137 147 public boolean isCached() { 148 return true; 149 } 150 151 160 public boolean isCachedFile() { 161 return true; 162 } 163 164 174 public boolean isCachedMemory() { 175 return false; 176 } 177 178 186 public void close() throws IOException { 187 maxStreamPos = cache.length(); 188 189 seek(maxStreamPos); 190 flushBefore(maxStreamPos); 191 super.close(); 192 cache.close(); 193 cacheFile.delete(); 194 stream.flush(); 195 stream = null; 196 StreamCloser.removeFromQueue(this); 197 } 198 199 public void flushBefore(long pos) throws IOException { 200 long oFlushedPos = flushedPos; 201 super.flushBefore(pos); 202 203 long flushBytes = flushedPos - oFlushedPos; 204 if (flushBytes > 0) { 205 int bufLen = 512; 206 byte[] buf = new byte[bufLen]; 207 cache.seek(oFlushedPos); 208 while (flushBytes > 0) { 209 int len = (int)Math.min(flushBytes, bufLen); 210 cache.readFully(buf, 0, len); 211 stream.write(buf, 0, len); 212 flushBytes -= len; 213 } 214 stream.flush(); 215 } 216 } 217 } 218 | Popular Tags |