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

MemoryCacheImageInputStream

public class MemoryCacheImageInputStream extends ImageInputStreamImpl
An implementation of ImageInputStream that gets its input from a regular InputStream. A memory buffer is used to cache at least the data between the discard position and the current read position.

In general, it is preferable to use a FileCacheImageInputStream when reading from a regular InputStream. This class is provided for cases where it is not possible to create a writable temporary file.

version
0.5

Fields Summary
private InputStream
stream
private MemoryCache
cache
private final Object
disposerReferent
The referent to be registered with the Disposer.
private final DisposerRecord
disposerRecord
The DisposerRecord that resets the underlying MemoryCache.
Constructors Summary
public MemoryCacheImageInputStream(InputStream stream)
Constructs a MemoryCacheImageInputStream that will read from a given InputStream.

param
stream an InputStream to read from.
exception
IllegalArgumentException if stream is null.


                                
       
        if (stream == null) {
            throw new IllegalArgumentException("stream == null!");
        }        
        this.stream = stream;

        disposerRecord = new StreamDisposerRecord(cache);
        if (getClass() == MemoryCacheImageInputStream.class) {
            disposerReferent = new Object();
            Disposer.addRecord(disposerReferent, disposerRecord);
        } else {
            disposerReferent = new StreamFinalizer(this);
        }
    
Methods Summary
public voidclose()
Closes this MemoryCacheImageInputStream, freeing the cache. The source InputStream is not closed.

        super.close();
        disposerRecord.dispose(); // this resets the MemoryCache
        stream = null;
        cache = null;
    
protected voidfinalize()
{@inheritDoc}

        // Empty finalizer: for performance reasons we instead use the
        // Disposer mechanism for ensuring that the underlying
        // MemoryCache is reset prior to garbage collection
    
public voidflushBefore(long pos)

        super.flushBefore(pos); // this will call checkClosed() for us
        cache.disposeBefore(pos);
    
public booleanisCached()
Returns true since this ImageInputStream caches data in order to allow seeking backwards.

return
true.
see
#isCachedMemory
see
#isCachedFile

        return true;
    
public booleanisCachedFile()
Returns false since this ImageInputStream does not maintain a file cache.

return
false.
see
#isCached
see
#isCachedMemory

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

return
true.
see
#isCached
see
#isCachedFile

        return true;
    
public intread()

        checkClosed();
        bitOffset = 0;
        long pos = cache.loadFromStream(stream, streamPos+1);
        if (pos >= streamPos+1) {
            return cache.read(streamPos++);
        } else {
            return -1;
        }
    
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;
        }

        long pos = cache.loadFromStream(stream, streamPos+len);

        len = (int)(pos - streamPos);  // In case stream ended early

        if (len > 0) {
            cache.read(b, off, len, streamPos);
            streamPos += len;
            return len;
        } else {
            return -1;
        }