AssetFileDescriptor


public class AssetFileDescriptor
extends Object implements Closeable, Parcelable

java.lang.Object
   ↳ android.content.res.AssetFileDescriptor


File descriptor of an entry in the AssetManager. This provides your own opened FileDescriptor that can be used to read the data, as well as the offset and length of that entry's data in the file.

Summary

Nested classes

class AssetFileDescriptor.AutoCloseInputStream

An InputStream you can create on a ParcelFileDescriptor, which will take care of calling ParcelFileDescriptor.close() for you when the stream is closed. 

class AssetFileDescriptor.AutoCloseOutputStream

An OutputStream you can create on a ParcelFileDescriptor, which will take care of calling ParcelFileDescriptor.close() for you when the stream is closed. 

Constants

long UNKNOWN_LENGTH

Length used with AssetFileDescriptor(android.os.ParcelFileDescriptor, long, long) and getDeclaredLength() when a length has not been declared.

Inherited constants

int CONTENTS_FILE_DESCRIPTOR

Descriptor bit used with describeContents(): indicates that the Parcelable object's flattened representation includes a file descriptor.

int PARCELABLE_WRITE_RETURN_VALUE

Flag for use with writeToParcel(Parcel, int): the object being written is a return value, that is the result of a function such as "Parcelable someFunction()", "void someFunction(out Parcelable)", or "void someFunction(inout Parcelable)".

Fields

public static final Creator<AssetFileDescriptor> CREATOR

Public constructors

AssetFileDescriptor(ParcelFileDescriptor fd, long startOffset, long length)

Create a new AssetFileDescriptor from the given values.

AssetFileDescriptor(ParcelFileDescriptor fd, long startOffset, long length, Bundle extras)

Create a new AssetFileDescriptor from the given values.

Public methods

void close()

Convenience for calling getParcelFileDescriptor().close().

FileInputStream createInputStream()

Create and return a new auto-close input stream for this asset.

FileOutputStream createOutputStream()

Create and return a new auto-close output stream for this asset.

int describeContents()

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation.

long getDeclaredLength()

Return the actual number of bytes that were declared when the AssetFileDescriptor was constructed.

Bundle getExtras()

Returns any additional details that can be used to interpret the underlying file descriptor.

FileDescriptor getFileDescriptor()

Returns the FileDescriptor that can be used to read the data in the file.

long getLength()

Returns the total number of bytes of this asset entry's data.

ParcelFileDescriptor getParcelFileDescriptor()

The AssetFileDescriptor contains its own ParcelFileDescriptor, which in addition to the normal FileDescriptor object also allows you to close the descriptor when you are done with it.

long getStartOffset()

Returns the byte offset where this asset entry's data starts.

String toString()

Returns a string representation of the object.

void writeToParcel(Parcel out, int flags)

Flatten this object in to a Parcel.

Inherited methods

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeoutMillis, int nanos)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait(long timeoutMillis)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

abstract void close()

Closes this stream and releases any system resources associated with it.

abstract int describeContents()

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation.

abstract void writeToParcel(Parcel dest, int flags)

Flatten this object in to a Parcel.

abstract void close()

Closes this resource, relinquishing any underlying resources.

Constants

UNKNOWN_LENGTH

Added in API level 3
public static final long UNKNOWN_LENGTH

Length used with AssetFileDescriptor(android.os.ParcelFileDescriptor, long, long) and getDeclaredLength() when a length has not been declared. This means the data extends to the end of the file.

Constant Value: -1 (0xffffffffffffffff)

Fields

CREATOR

Added in API level 3
public static final Creator<AssetFileDescriptor> CREATOR

Public constructors

AssetFileDescriptor

Added in API level 1
public AssetFileDescriptor (ParcelFileDescriptor fd, 
                long startOffset, 
                long length)

Create a new AssetFileDescriptor from the given values.

Parameters
fd ParcelFileDescriptor: The underlying file descriptor.

startOffset long: The location within the file that the asset starts. This must be 0 if length is UNKNOWN_LENGTH.

length long: The number of bytes of the asset, or UNKNOWN_LENGTH if it extends to the end of the file.

AssetFileDescriptor

Added in API level 19
public AssetFileDescriptor (ParcelFileDescriptor fd, 
                long startOffset, 
                long length, 
                Bundle extras)

Create a new AssetFileDescriptor from the given values.

Parameters
fd ParcelFileDescriptor: The underlying file descriptor.

startOffset long: The location within the file that the asset starts. This must be 0 if length is UNKNOWN_LENGTH.

length long: The number of bytes of the asset, or UNKNOWN_LENGTH if it extends to the end of the file.

extras Bundle: additional details that can be used to interpret the underlying file descriptor. May be null.

Public methods

close

Added in API level 1
public void close ()

Convenience for calling getParcelFileDescriptor().close().

Throws
IOException

createInputStream

Added in API level 3
public FileInputStream createInputStream ()

Create and return a new auto-close input stream for this asset. This will either return a full asset AutoCloseInputStream, or an underlying ParcelFileDescriptor.AutoCloseInputStream depending on whether the the object represents a complete file or sub-section of a file. You should only call this once for a particular asset.

Returns
FileInputStream

Throws
IOException

createOutputStream

Added in API level 3
public FileOutputStream createOutputStream ()

Create and return a new auto-close output stream for this asset. This will either return a full asset AutoCloseOutputStream, or an underlying ParcelFileDescriptor.AutoCloseOutputStream depending on whether the the object represents a complete file or sub-section of a file. You should only call this once for a particular asset.

Throws
IOException

describeContents

Added in API level 3
public int describeContents ()

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. For example, if the object will include a file descriptor in the output of writeToParcel(android.os.Parcel, int), the return value of this method must include the CONTENTS_FILE_DESCRIPTOR bit.

Returns
int a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either 0 or CONTENTS_FILE_DESCRIPTOR

getDeclaredLength

Added in API level 3
public long getDeclaredLength ()

Return the actual number of bytes that were declared when the AssetFileDescriptor was constructed. Will be UNKNOWN_LENGTH if the length was not declared, meaning data should be read to the end of the file.

Returns
long

getExtras

Added in API level 19
public Bundle getExtras ()

Returns any additional details that can be used to interpret the underlying file descriptor. May be null.

Returns
Bundle

getFileDescriptor

Added in API level 1
public FileDescriptor getFileDescriptor ()

Returns the FileDescriptor that can be used to read the data in the file.

Returns
FileDescriptor

getLength

Added in API level 1
public long getLength ()

Returns the total number of bytes of this asset entry's data. May be UNKNOWN_LENGTH if the asset extends to the end of the file. If the AssetFileDescriptor was constructed with UNKNOWN_LENGTH, this will use ParcelFileDescriptor.getStatSize() to find the total size of the file, returning that number if found or UNKNOWN_LENGTH if it could not be determined.

Returns
long

getParcelFileDescriptor

Added in API level 1
public ParcelFileDescriptor getParcelFileDescriptor ()

The AssetFileDescriptor contains its own ParcelFileDescriptor, which in addition to the normal FileDescriptor object also allows you to close the descriptor when you are done with it.

getStartOffset

Added in API level 1
public long getStartOffset ()

Returns the byte offset where this asset entry's data starts.

Returns
long

toString

Added in API level 1
public String toString ()

Returns a string representation of the object.

Returns
String a string representation of the object.

writeToParcel

Added in API level 3
public void writeToParcel (Parcel out, 
                int flags)

Flatten this object in to a Parcel.

Parameters
out Parcel: The Parcel in which the object should be written. This value cannot be null.

flags int: Additional flags about how the object should be written. May be 0 or Parcelable.PARCELABLE_WRITE_RETURN_VALUE. Value is either 0 or a combination of Parcelable.PARCELABLE_WRITE_RETURN_VALUE, and android.os.Parcelable.PARCELABLE_ELIDE_DUPLICATES