Added in API level 1

MemoryFile


open class MemoryFile
kotlin.Any
   ↳ android.os.MemoryFile

MemoryFile is a wrapper for SharedMemory which can optionally be set to purgeable. Applications should generally prefer to use SharedMemory which offers more flexible access & control over the shared memory region than MemoryFile does. 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.

Summary

Public constructors
MemoryFile(name: String!, length: Int)

Allocates a new ashmem region.

Public methods
open Boolean
allowPurging(allowPurging: Boolean)

Enables or disables purging of the memory file.

open Unit

Closes the memory file.

open InputStream!

Creates a new InputStream for reading from the memory file.

open OutputStream!

Creates a new OutputStream for writing to the memory file.

open Boolean

Is memory file purging enabled?

open Int

Returns the length of the memory file.

open Int
readBytes(buffer: ByteArray!, srcOffset: Int, destOffset: Int, count: Int)

Reads bytes from the memory file.

open Unit
writeBytes(buffer: ByteArray!, srcOffset: Int, destOffset: Int, count: Int)

Write bytes to the memory file.

Public constructors

MemoryFile

Added in API level 1
MemoryFile(
    name: String!,
    length: Int)

Allocates a new ashmem region. The region is initially not purgable.

Parameters
name String!: optional name for the file (can be null).
length Int: of the memory file in bytes, must be positive.
Exceptions
java.io.IOException if the memory file could not be created.

Public methods

allowPurging

Added in API level 1
Deprecated in API level 27
open fun allowPurging(allowPurging: Boolean): Boolean

Deprecated: Purgable is considered generally fragile and hard to use safely. Applications are recommend to instead use android.content.ComponentCallbacks2#onTrimMemory(int) to react to memory events and release shared memory regions as appropriate.

Enables or disables purging of the memory file.

Parameters
allowPurging Boolean: true if the operating system can purge the contents of the file in low memory situations
Return
Boolean previous value of allowPurging

close

Added in API level 1
open fun close(): Unit

Closes the memory file. If there are no other open references to the memory file, it will be deleted.

getInputStream

Added in API level 1
open fun getInputStream(): InputStream!

Creates a new InputStream for reading from the memory file.

Return
InputStream! InputStream

getOutputStream

Added in API level 1
open fun getOutputStream(): OutputStream!

Creates a new OutputStream for writing to the memory file.

Return
OutputStream! OutputStream

isPurgingAllowed

Added in API level 1
Deprecated in API level 27
open fun isPurgingAllowed(): Boolean

Deprecated: Purgable is considered generally fragile and hard to use safely. Applications are recommend to instead use android.content.ComponentCallbacks2#onTrimMemory(int) to react to memory events and release shared memory regions as appropriate.

Is memory file purging enabled?

Return
Boolean true if the file may be purged.

length

Added in API level 1
open fun length(): Int

Returns the length of the memory file.

Return
Int file length.

readBytes

Added in API level 1
open fun readBytes(
    buffer: ByteArray!,
    srcOffset: Int,
    destOffset: Int,
    count: Int
): Int

Reads bytes from the memory file. Will throw an IOException if the file has been purged.

Parameters
buffer ByteArray!: 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.
Return
Int number of bytes read.
Exceptions
java.io.IOException if the memory file has been purged or deactivated.

writeBytes

Added in API level 1
open fun writeBytes(
    buffer: ByteArray!,
    srcOffset: Int,
    destOffset: Int,
    count: Int
): Unit

Write bytes to the memory file. Will throw an IOException if the file has been purged.

Parameters
buffer ByteArray!: 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.
Exceptions
java.io.IOException if the memory file has been purged or deactivated.