1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
/* * @(#)FileCacheImageInputStream.java 1.32 06/01/05 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.imageio.stream; import java.io.DataInput; import java.io.File; import java.io.InputStream; import java.io.IOException; import java.io.RandomAccessFile; import com.sun.imageio.stream.StreamCloser; import com.sun.imageio.stream.StreamFinalizer; import sun.java2d.Disposer; import sun.java2d.DisposerRecord; /** * An implementation of <code>ImageInputStream</code> that gets its * input from a regular <code>InputStream</code>. A file is used to * cache previously read data. * * @version 0.5 */ public class FileCacheImageInputStream extends ImageInputStreamImpl { private InputStream stream; private File cacheFile; private RandomAccessFile cache; private static final int BUFFER_LENGTH = 1024; private byte[] buf = new byte[BUFFER_LENGTH]; private long length = 0L; private boolean foundEOF = false; /** The referent to be registered with the Disposer. */ private final Object disposerReferent; /** The DisposerRecord that closes the underlying cache. */ private final DisposerRecord disposerRecord; /** * Constructs a <code>FileCacheImageInputStream</code> that will read * from a given <code>InputStream</code>. * * <p> A temporary file is used as a cache. If * <code>cacheDir</code>is non-<code>null</code> and is a * directory, the file will be created there. If it is * <code>null</code>, the system-dependent default temporary-file * directory will be used (see the documentation for * <code>File.createTempFile</code> for details). * * @param stream an <code>InputStream</code> to read from. * @param cacheDir a <code>File</code> indicating where the * cache file should be created, or <code>null</code> to use the * system directory. * * @exception IllegalArgumentException if <code>stream</code> is * <code>null</code>. * @exception IllegalArgumentException if <code>cacheDir</code> is * non-<code>null</code> but is not a directory. * @exception IOException if a cache file cannot be created. */ public FileCacheImageInputStream(InputStream stream, File cacheDir) throws IOException { if (stream == null) { throw new IllegalArgumentException("stream == null!"); } if ((cacheDir != null) && !(cacheDir.isDirectory())) { throw new IllegalArgumentException("Not a directory!"); } this.stream = stream; this.cacheFile = File.createTempFile("imageio", ".tmp", cacheDir); this.cache = new RandomAccessFile(cacheFile, "rw"); StreamCloser.addToQueue(this); disposerRecord = new StreamDisposerRecord(cacheFile, cache); if (getClass() == FileCacheImageInputStream.class) { disposerReferent = new Object(); Disposer.addRecord(disposerReferent, disposerRecord); } else { disposerReferent = new StreamFinalizer(this); } } /** * Ensures that at least <code>pos</code> bytes are cached, * or the end of the source is reached. The return value * is equal to the smaller of <code>pos</code> and the * length of the source file. */ private long readUntil(long pos) throws IOException { // We've already got enough data cached if (pos < length) { return pos; } // pos >= length but length isn't getting any bigger, so return it if (foundEOF) { return length; } long len = pos - length; cache.seek(length); while (len > 0) { // Copy a buffer's worth of data from the source to the cache // BUFFER_LENGTH will always fit into an int so this is safe int nbytes = stream.read(buf, 0, (int)Math.min(len, (long)BUFFER_LENGTH)); if (nbytes == -1) { foundEOF = true; return length; } cache.write(buf, 0, nbytes); len -= nbytes; length += nbytes; } return pos; } public int read() throws IOException { checkClosed(); bitOffset = 0; long next = streamPos + 1; long pos = readUntil(next); if (pos >= next) { cache.seek(streamPos++); return cache.read(); } else { return -1; } } public int read(byte[] b, int off, int len) throws IOException { checkClosed(); if (b == null) { throw new NullPointerException("b == null!"); } // Fix 4430357 - if off + len < 0, overflow occurred if (off < 0 || len < 0 || off + len > b.length || off + len < 0) { throw new IndexOutOfBoundsException ("off < 0 || len < 0 || off+len > b.length || off+len < 0!"); } bitOffset = 0; if (len == 0) { return 0; } long pos = readUntil(streamPos + len); // len will always fit into an int so this is safe len = (int)Math.min((long)len, pos - streamPos); if (len > 0) { cache.seek(streamPos); cache.readFully(b, off, len); streamPos += len; return len; } else { return -1; } } /** * Returns <code>true</code> since this * <code>ImageInputStream</code> caches data in order to allow * seeking backwards. * * @return <code>true</code>. * * @see #isCachedMemory * @see #isCachedFile */ public boolean isCached() { return true; } /** * Returns <code>true</code> since this * <code>ImageInputStream</code> maintains a file cache. * * @return <code>true</code>. * * @see #isCached * @see #isCachedMemory */ public boolean isCachedFile() { return true; } /** * Returns <code>false</code> since this * <code>ImageInputStream</code> does not maintain a main memory * cache. * * @return <code>false</code>. * * @see #isCached * @see #isCachedFile */ public boolean isCachedMemory() { return false; } /** * Closes this <code>FileCacheImageInputStream</code>, closing * and removing the cache file. The source <code>InputStream</code> * is not closed. * * @exception IOException if an error occurs. */ public void close() throws IOException { super.close(); disposerRecord.dispose(); // this will close/delete the cache file stream = null; cache = null; cacheFile = null; StreamCloser.removeFromQueue(this); } /** * {@inheritDoc} */ protected void finalize() throws Throwable { // Empty finalizer: for performance reasons we instead use the // Disposer mechanism for ensuring that the underlying // RandomAccessFile is closed/deleted prior to garbage collection } private static class StreamDisposerRecord implements DisposerRecord { private File cacheFile; private RandomAccessFile cache; public StreamDisposerRecord(File cacheFile, RandomAccessFile cache) { this.cacheFile = cacheFile; this.cache = cache; } public synchronized void dispose() { if (cache != null) { try { cache.close(); } catch (IOException e) { } finally { cache = null; } } if (cacheFile != null) { cacheFile.delete(); cacheFile = null; } // Note: Explicit removal of the stream from the StreamCloser // queue is not mandatory in this case, as it will be removed // automatically by GC shortly after this method is called. } } }