FileDocCategorySizeDatePackage
FileCacheImageOutputStream.javaAPI DocJava SE 6 API7044Tue Jun 10 00:26:08 BST 2008javax.imageio.stream

FileCacheImageOutputStream

public class FileCacheImageOutputStream extends ImageOutputStreamImpl
An implementation of ImageOutputStream that writes its output to a regular OutputStream. A file is used to cache data until it is flushed to the output stream.
version
0.5

Fields Summary
private OutputStream
stream
private File
cacheFile
private RandomAccessFile
cache
private long
maxStreamPos
Constructors Summary
public FileCacheImageOutputStream(OutputStream stream, File cacheDir)
Constructs a FileCacheImageOutputStream that will write to a given outputStream.

A temporary file is used as a cache. If cacheDiris non-null and is a directory, the file will be created there. If it is null, the system-dependent default temporary-file directory will be used (see the documentation for File.createTempFile for details).

param
stream an OutputStream to write to.
param
cacheDir a File indicating where the cache file should be created, or null to use the system directory.
exception
IllegalArgumentException if stream is null.
exception
IllegalArgumentException if cacheDir is non-null but is not a directory.
exception
IOException if a cache file cannot be created.


                                                                                                                   
        
          
        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);
    
Methods Summary
public voidclose()
Closes this FileCacheImageOututStream. All pending data is flushed to the output, and the cache file is closed and removed. The destination OutputStream is not closed.

exception
IOException if an error occurs.

        maxStreamPos = cache.length();

        seek(maxStreamPos);
        flushBefore(maxStreamPos);
        super.close();
        cache.close();
        cache = null;
        cacheFile.delete();
        cacheFile = null;
        stream.flush();
        stream = null;
        StreamCloser.removeFromQueue(this);
    
public voidflushBefore(long pos)

        long oFlushedPos = flushedPos;
        super.flushBefore(pos); // this will call checkClosed() for us

        long flushBytes = flushedPos - oFlushedPos;
        if (flushBytes > 0) {
            int bufLen = 512;
            byte[] buf = new byte[bufLen];
            cache.seek(oFlushedPos);
            while (flushBytes > 0) {
                int len = (int)Math.min(flushBytes, bufLen);
                cache.readFully(buf, 0, len);
                stream.write(buf, 0, len);
                flushBytes -= len;
            }
            stream.flush();
        }
    
public booleanisCached()
Returns true since this ImageOutputStream caches data in order to allow seeking backwards.

return
true.
see
#isCachedMemory
see
#isCachedFile

        return true;
    
public booleanisCachedFile()
Returns true since this ImageOutputStream maintains a file cache.

return
true.
see
#isCached
see
#isCachedMemory

        return true;
    
public booleanisCachedMemory()
Returns false since this ImageOutputStream does not maintain a main memory cache.

return
false.
see
#isCached
see
#isCachedFile

        return false;
    
public longlength()

        try {
            checkClosed();
            return cache.length();
        } catch (IOException e) {
            return -1L;
        }
    
public intread()

        checkClosed();
        bitOffset = 0;
        int val =  cache.read();
        if (val != -1) {
            ++streamPos;
        }
        return val;
    
public intread(byte[] b, int off, int len)

        checkClosed();

        if (b == null) {
            throw new NullPointerException("b == null!");
        }
        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;
        }

        int nbytes = cache.read(b, off, len);
        if (nbytes != -1) {
            streamPos += nbytes;
        }
        return nbytes;
    
public voidseek(long pos)
Sets the current stream position and resets the bit offset to 0. It is legal to seek past the end of the file; an EOFException will be thrown only if a read is performed. The file length will not be increased until a write is performed.

exception
IndexOutOfBoundsException if pos is smaller than the flushed position.
exception
IOException if any other I/O error occurs.

        checkClosed();

        if (pos < flushedPos) {
            throw new IndexOutOfBoundsException();
        }

        cache.seek(pos);
        this.streamPos = cache.getFilePointer();
        maxStreamPos = Math.max(maxStreamPos, streamPos);
        this.bitOffset = 0;
    
public voidwrite(int b)

        flushBits(); // this will call checkClosed() for us
        cache.write(b);
        ++streamPos;
        maxStreamPos = Math.max(maxStreamPos, streamPos);
    
public voidwrite(byte[] b, int off, int len)

        flushBits(); // this will call checkClosed() for us
        cache.write(b, off, len);
        streamPos += len;
        maxStreamPos = Math.max(maxStreamPos, streamPos);