FileDocCategorySizeDatePackage
MemoryFile.javaAPI DocAndroid 5.1 API11314Thu Mar 12 22:22:10 GMT 2015android.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 static final int
PROT_READ
private static final int
PROT_WRITE
private FileDescriptor
mFD
private long
mAddress
private int
mLength
private boolean
mAllowPurging
Constructors Summary
public MemoryFile(String name, int length)
Allocates a new ashmem region. The region is initially not purgable.

param
name optional name for the file (can be null).
param
length of the memory file in bytes, must be non-negative.
throws
IOException if the memory file could not be created.

  // true if our ashmem region is unpinned

                                                   
           
        mLength = length;
        if (length >= 0) {
            mFD = native_open(name, length);
        } else {
            throw new IOException("Invalid length: " + length);
        }

        if (length > 0) {
            mAddress = native_mmap(mFD, length, PROT_READ | PROT_WRITE);
        } else {
            mAddress = 0;
        }
    
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 the memory file. If there are no other open references to the memory file, it will be deleted.

        deactivate();
        if (!isClosed()) {
            native_close(mFD);
        }
    
voiddeactivate()
Unmaps the memory file from the process's memory space, but does not close it. After this method has been called, read and write operations through this object will fail, but {@link #getFileDescriptor()} will still return a valid file descriptor.

hide

        if (!isDeactivated()) {
            try {
                native_munmap(mAddress, mLength);
                mAddress = 0;
            } catch (IOException ex) {
                Log.e(TAG, ex.toString());
            }
        }
    
protected voidfinalize()

        if (!isClosed()) {
            Log.e(TAG, "MemoryFile.finalize() called while ashmem still open");
            close();
        }
    
public java.io.FileDescriptorgetFileDescriptor()
Gets a FileDescriptor for the memory file. The returned file descriptor is not duplicated.

throws
IOException If the memory file has been closed.
hide

        return mFD;
    
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 static intgetSize(java.io.FileDescriptor fd)
Returns the size of the memory file that the file descriptor refers to, or -1 if the file descriptor does not refer to a memory file.

throws
IOException If fd is not a valid file descriptor.
hide

        return native_get_size(fd);
    
private booleanisClosed()
Checks whether the memory file has been closed.

        return !mFD.valid();
    
private booleanisDeactivated()
Checks whether the memory file has been deactivated.

        return mAddress == 0;
    
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 static native voidnative_close(java.io.FileDescriptor fd)

private static native intnative_get_size(java.io.FileDescriptor fd)

private static native longnative_mmap(java.io.FileDescriptor fd, int length, int mode)

private static native voidnative_munmap(long addr, int length)

private static native java.io.FileDescriptornative_open(java.lang.String name, int length)

private static native voidnative_pin(java.io.FileDescriptor fd, boolean pin)

private static native intnative_read(java.io.FileDescriptor fd, long address, byte[] buffer, int srcOffset, int destOffset, int count, boolean isUnpinned)

private static native voidnative_write(java.io.FileDescriptor fd, long 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.
throws
IOException if the memory file has been purged or deactivated.

        if (isDeactivated()) {
            throw new IOException("Can't read from deactivated memory file.");
        }
        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.
throws
IOException if the memory file has been purged or deactivated.

        if (isDeactivated()) {
            throw new IOException("Can't write to deactivated memory file.");
        }
        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);