MemoryFilepublic 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. // true if our ashmem region is unpinned
mLength = length;
mFD = native_open(name, length);
mAddress = native_mmap(mFD, length);
|
Methods Summary |
---|
public synchronized boolean | allowPurging(boolean allowPurging)Enables or disables purging of the memory file.
boolean oldValue = mAllowPurging;
if (oldValue != allowPurging) {
native_pin(mFD, !allowPurging);
mAllowPurging = allowPurging;
}
return oldValue;
| public void | close()Closes and releases all resources for the memory file.
if (mFD > 0) {
native_close(mFD);
mFD = 0;
}
| protected void | finalize()
if (mFD > 0) {
Log.e(TAG, "MemoryFile.finalize() called while ashmem still open");
close();
}
| public java.io.InputStream | getInputStream()Creates a new InputStream for reading from the memory file.
return new MemoryInputStream();
| public java.io.OutputStream | getOutputStream()Creates a new OutputStream for writing to the memory file.
return new MemoryOutputStream();
| public boolean | isPurgingAllowed()Is memory file purging enabled?
return mAllowPurging;
| public int | length()Returns the length of the memory file.
return mLength;
| private native void | native_close(int fd)
| private native int | native_mmap(int fd, int length)
| private native int | native_open(java.lang.String name, int length)
| private native void | native_pin(int fd, boolean pin)
| private native int | native_read(int fd, int address, byte[] buffer, int srcOffset, int destOffset, int count, boolean isUnpinned)
| private native void | native_write(int fd, int address, byte[] buffer, int srcOffset, int destOffset, int count, boolean isUnpinned)
| public int | readBytes(byte[] buffer, int srcOffset, int destOffset, int count)Reads bytes from the memory file.
Will throw an IOException if the file has been purged.
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 void | writeBytes(byte[] buffer, int srcOffset, int destOffset, int count)Write bytes to the memory file.
Will throw an IOException if the file has been purged.
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);
|
|