FileDocCategorySizeDatePackage
MemoryCacheImageOutputStream.javaAPI DocJava SE 6 API5127Tue Jun 10 00:26:10 BST 2008javax.imageio.stream

MemoryCacheImageOutputStream

public class MemoryCacheImageOutputStream extends ImageOutputStreamImpl
An implementation of ImageOutputStream that writes its output to a regular OutputStream. A memory buffer is used to cache at least the data between the discard position and the current write position. The only constructor takes an OutputStream, so this class may not be used for read/modify/write operations. Reading can occur only on parts of the stream that have already been written to the cache and not yet flushed.
version
0.5

Fields Summary
private OutputStream
stream
private MemoryCache
cache
Constructors Summary
public MemoryCacheImageOutputStream(OutputStream stream)
Constructs a MemoryCacheImageOutputStream that will write to a given OutputStream.

param
stream an OutputStream to write to.
exception
IllegalArgumentException if stream is null.


                                
       
        if (stream == null) {
            throw new IllegalArgumentException("stream == null!");
        }
        this.stream = stream;
    
Methods Summary
public voidclose()
Closes this MemoryCacheImageOutputStream. All pending data is flushed to the output, and the cache is released. The destination OutputStream is not closed.

        long length = cache.getLength();
        seek(length);
        flushBefore(length);
        super.close();
        cache.reset();
        cache = null;
        stream = null;
    
public voidflushBefore(long pos)

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

        long flushBytes = flushedPos - oFlushedPos;
        cache.writeToStream(stream, oFlushedPos, flushBytes);
        cache.disposeBefore(flushedPos);
        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 false since this ImageOutputStream does not maintain a file cache.

return
false.
see
#isCached
see
#isCachedMemory

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

return
true.
see
#isCached
see
#isCachedFile

        return true;
    
public longlength()

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

        checkClosed();

        bitOffset = 0;

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

        checkClosed();
        
        if (b == null) {
            throw new NullPointerException("b == null!");
        }
        // Fix 4467608: read([B,I,I) works incorrectly if len<=0
        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;
        }

        // check if we're already at/past EOF i.e.
        // no more bytes left to read from cache
        long bytesLeftInCache = cache.getLength() - streamPos;
        if (bytesLeftInCache <= 0) {
            return -1; // EOF
        }

        // guaranteed by now that bytesLeftInCache > 0 && len > 0
        // and so the rest of the error checking is done by cache.read()
        // NOTE that alot of error checking is duplicated
        len = (int)Math.min(bytesLeftInCache, (long)len);
        cache.read(b, off, len, streamPos);
        streamPos += len;
        return len;
    
public voidwrite(int b)

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

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