FileDocCategorySizeDatePackage
FileCacheImageOutputStream.javaAPI DocAndroid 1.5 API5045Wed May 06 22:41:54 BST 2009javax.imageio.stream

FileCacheImageOutputStream

public class FileCacheImageOutputStream extends ImageOutputStreamImpl
The FileCacheImageOutputStream class is an implementation of ImageOutputStream that writes to its OutputStream using a temporary file as a cache.
since
Android 1.0

Fields Summary
static final String
IIO_TEMP_FILE_PREFIX
The Constant IIO_TEMP_FILE_PREFIX.
static final int
MAX_BUFFER_LEN
The Constant MAX_BUFFER_LEN.
private OutputStream
os
The os.
private File
file
The file.
private RandomAccessFile
raf
The raf.
Constructors Summary
public FileCacheImageOutputStream(OutputStream stream, File cacheDir)
Instantiates a FileCacheImageOutputStream.

param
stream the OutputStream for writing.
param
cacheDir the cache directory where the cache file will be created.
throws
IOException if an I/O exception has occurred.


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

        if (cacheDir == null || cacheDir.isDirectory()) {
            file = File.createTempFile(IIO_TEMP_FILE_PREFIX, null, cacheDir);
            file.deleteOnExit();
        } else {
            throw new IllegalArgumentException("Not a directory!");
        }

        raf = new RandomAccessFile(file, "rw");
    
Methods Summary
public voidclose()

        flushBefore(raf.length());
        super.close();
        raf.close();
        file.delete();
    
public voidflushBefore(long pos)

        long readFromPos = flushedPos;
        super.flushBefore(pos);

        long bytesToRead = pos - readFromPos;
        raf.seek(readFromPos);

        if (bytesToRead < MAX_BUFFER_LEN) {
            byte buffer[] = new byte[(int)bytesToRead];
            raf.readFully(buffer);
            os.write(buffer);
        } else {
            byte buffer[] = new byte[MAX_BUFFER_LEN];
            while (bytesToRead > 0) {
                int count = (int)Math.min(MAX_BUFFER_LEN, bytesToRead);
                raf.readFully(buffer, 0, count);
                os.write(buffer, 0, count);
                bytesToRead -= count;
            }
        }

        os.flush();

        if (pos != streamPos) {
            raf.seek(streamPos); // Reset the position
        }
    
public booleanisCached()

        return true;
    
public booleanisCachedFile()

        return true;
    
public booleanisCachedMemory()

        return false;
    
public longlength()

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

        bitOffset = 0; // Should reset

        int res = raf.read();
        if (res >= 0) {
            streamPos++;
        }

        return res;
    
public intread(byte[] b, int off, int len)

        bitOffset = 0;

        int numRead = raf.read(b, off, len);
        if (numRead > 0) {
            streamPos += numRead;
        }

        return numRead;
    
public voidseek(long pos)

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

        raf.seek(pos);
        streamPos = raf.getFilePointer();
        bitOffset = 0;
    
public voidwrite(int b)

        flushBits(); // See the flushBits method description

        raf.write(b);
        streamPos++;
    
public voidwrite(byte[] b, int off, int len)

        flushBits(); // See the flushBits method description

        raf.write(b, off, len);
        streamPos += len;