FileDocCategorySizeDatePackage
MemoryFile.javaAPI DocAndroid 1.5 API7972Wed May 06 22:41:56 BST 2009android.os

MemoryFile

public class MemoryFile extends Object
MemoryFile is a wrapper for the Linux ashmem driver. MemoryFiles are backed by shared memory, which can be optionally set to be purgeable. Purgeable files may have their contents reclaimed by the kernel in low memory conditions (only if allowPurging is set to true). After a file is purged, attempts to read or write the file will cause an IOException to be thrown.

Fields Summary
private static String
TAG
private int
mFD
private int
mAddress
private int
mLength
private boolean
mAllowPurging
Constructors Summary
public MemoryFile(String name, int length)
MemoryFile constructor.

param
name optional name for the file (can be null).
param
length of the memory file in bytes.

  // true if our ashmem region is unpinned

                             
         
        mLength = length;
        mFD = native_open(name, length);
        mAddress = native_mmap(mFD, length);
    
Methods Summary
public synchronized booleanallowPurging(boolean allowPurging)
Enables or disables purging of the memory file.

param
allowPurging true if the operating system can purge the contents of the file in low memory situations
return
previous value of allowPurging

        boolean oldValue = mAllowPurging;
        if (oldValue != allowPurging) {
            native_pin(mFD, !allowPurging);
            mAllowPurging = allowPurging;
        }
        return oldValue;
    
public voidclose()
Closes and releases all resources for the memory file.

        if (mFD > 0) {
            native_close(mFD);
            mFD = 0;
        }
    
protected voidfinalize()

        if (mFD > 0) {
            Log.e(TAG, "MemoryFile.finalize() called while ashmem still open");
            close();
        }
    
public java.io.InputStreamgetInputStream()
Creates a new InputStream for reading from the memory file.

return
InputStream

        return new MemoryInputStream();
    
public java.io.OutputStreamgetOutputStream()
Creates a new OutputStream for writing to the memory file.

return
OutputStream


        return new MemoryOutputStream();
    
public booleanisPurgingAllowed()
Is memory file purging enabled?

return
true if the file may be purged.

        return mAllowPurging;
    
public intlength()
Returns the length of the memory file.

return
file length.

        return mLength;
    
private native voidnative_close(int fd)

private native intnative_mmap(int fd, int length)

private native intnative_open(java.lang.String name, int length)

private native voidnative_pin(int fd, boolean pin)

private native intnative_read(int fd, int address, byte[] buffer, int srcOffset, int destOffset, int count, boolean isUnpinned)

private native voidnative_write(int fd, int address, byte[] buffer, int srcOffset, int destOffset, int count, boolean isUnpinned)

public intreadBytes(byte[] buffer, int srcOffset, int destOffset, int count)
Reads bytes from the memory file. Will throw an IOException if the file has been purged.

param
buffer byte array to read bytes into.
param
srcOffset offset into the memory file to read from.
param
destOffset offset into the byte array buffer to read into.
param
count number of bytes to read.
return
number of bytes read.

        if (destOffset < 0 || destOffset > buffer.length || count < 0
                || count > buffer.length - destOffset
                || srcOffset < 0 || srcOffset > mLength
                || count > mLength - srcOffset) {
            throw new IndexOutOfBoundsException();
        }
        return native_read(mFD, mAddress, buffer, srcOffset, destOffset, count, mAllowPurging);
    
public voidwriteBytes(byte[] buffer, int srcOffset, int destOffset, int count)
Write bytes to the memory file. Will throw an IOException if the file has been purged.

param
buffer byte array to write bytes from.
param
srcOffset offset into the byte array buffer to write from.
param
destOffset offset into the memory file to write to.
param
count number of bytes to write.

        if (srcOffset < 0 || srcOffset > buffer.length || count < 0
                || count > buffer.length - srcOffset
                || destOffset < 0 || destOffset > mLength
                || count > mLength - destOffset) {
            throw new IndexOutOfBoundsException();
        }
        native_write(mFD, mAddress, buffer, srcOffset, destOffset, count, mAllowPurging);