public class MemoryFile
extends Object
| java.lang.Object | |
| android.os.MemoryFile | |
MemoryFile是Linux ashmem驱动程序的封装。 MemoryFiles由共享内存支持,可以选择将其设置为可清除。 可清除文件可能会在内存不足的情况下由内核回收其内容(仅当allowPurging设置为true时)。 清除文件后,尝试读取或写入文件将导致抛出IOException。
Public constructors |
|
|---|---|
MemoryFile(String name, int length) 分配一个新的ashmem区域。 |
|
公共方法(Public methods) |
|
|---|---|
boolean |
allowPurging(boolean allowPurging) 启用或禁用清除内存文件。 |
void |
close() 关闭内存文件。 |
InputStream |
getInputStream() 创建一个用于从内存文件中读取的新InputStream。 |
OutputStream |
getOutputStream() 创建一个新的OutputStream以写入内存文件。 |
boolean |
isPurgingAllowed() 是否启用内存文件清除? |
int |
length() 返回内存文件的长度。 |
int |
readBytes(byte[] buffer, int srcOffset, int destOffset, int count) 从内存文件中读取字节。 |
void |
writeBytes(byte[] buffer, int srcOffset, int destOffset, int count) 将字节写入存储器文件。 |
Protected methods |
|
|---|---|
void |
finalize() 当垃圾收集确定没有更多对该对象的引用时,由对象上的垃圾回收器调用。 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
MemoryFile (String name, int length)
分配一个新的ashmem区域。 该地区最初是不可清除的。
| 参数(Parameters) | |
|---|---|
name |
String: optional name for the file (can be null). |
length |
int: of the memory file in bytes, must be non-negative. |
| 抛出异常(Throws) | |
|---|---|
IOException |
if the memory file could not be created. |
boolean allowPurging (boolean allowPurging)
启用或禁用清除内存文件。
| 参数(Parameters) | |
|---|---|
allowPurging |
boolean: true if the operating system can purge the contents of the file in low memory situations |
| 返回(Returns) | |
|---|---|
boolean |
previous value of allowPurging |
| 抛出异常(Throws) | |
|---|---|
IOException |
|
InputStream getInputStream ()
创建一个用于从内存文件中读取的新InputStream。
| 返回(Returns) | |
|---|---|
InputStream |
InputStream |
OutputStream getOutputStream ()
创建一个新的OutputStream以写入内存文件。
| 返回(Returns) | |
|---|---|
OutputStream |
OutputStream |
boolean isPurgingAllowed ()
是否启用内存文件清除?
| 返回(Returns) | |
|---|---|
boolean |
true if the file may be purged. |
int readBytes (byte[] buffer,
int srcOffset,
int destOffset,
int count)
从内存文件中读取字节。 如果文件已被清除,将抛出IOException。
| 参数(Parameters) | |
|---|---|
buffer |
byte: byte array to read bytes into. |
srcOffset |
int: offset into the memory file to read from. |
destOffset |
int: offset into the byte array buffer to read into. |
count |
int: number of bytes to read. |
| 返回(Returns) | |
|---|---|
int |
number of bytes read. |
| 抛出异常(Throws) | |
|---|---|
IOException |
if the memory file has been purged or deactivated. |
void writeBytes (byte[] buffer,
int srcOffset,
int destOffset,
int count)
将字节写入存储器文件。 如果文件已被清除,将抛出IOException。
| 参数(Parameters) | |
|---|---|
buffer |
byte: byte array to write bytes from. |
srcOffset |
int: offset into the byte array buffer to write from. |
destOffset |
int: offset into the memory file to write to. |
count |
int: number of bytes to write. |
| 抛出异常(Throws) | |
|---|---|
IOException |
if the memory file has been purged or deactivated. |
void finalize ()
当垃圾收集确定没有更多对该对象的引用时,由对象上的垃圾回收器调用。 一个子类重写了finalize方法来处置系统资源或执行其他清理。
的常规协定finalize是,它被调用,如果当在Java TM虚拟机已确定不再有由该目的可以通过还没有死亡,除了作为一个动作的结果的任何线程访问的任何手段取决于某些其他可以完成的对象或类别的最终定稿。 finalize方法可以采取任何行动,包括使这个对象再次可用于其他线程; 然而, finalize的通常目的是在对象被不可撤销地丢弃之前执行清理操作。 例如,表示输入/输出连接的对象的finalize方法可能会执行显式I / O事务,以在永久丢弃该对象之前中断连接。
类Object的finalize方法Object执行特殊操作; 它只是正常返回。 Object子类可能会覆盖此定义。
Java编程语言不保证哪个线程将为任何给定对象调用finalize方法。 但是,保证调用finalize的线程在调用finalize时不会保留任何用户可见的同步锁。 如果finalize方法引发未捕获的异常,则忽略该异常,并终止该对象的终止。
在针对某个对象调用 finalize方法之后,在Java虚拟机再次确定不再有任何途径可以通过任何尚未死亡的线程访问此对象,包括可能的操作通过准备完成的其他对象或类别,此时该对象可能被丢弃。
对于任何给定的对象,Java虚拟机永远不会多次调用 finalize方法。
finalize方法抛出的任何异常 finalize导致终止此对象的终止,但会被忽略。