FileDocCategorySizeDatePackage
AssetFileDescriptor.javaAPI DocAndroid 5.1 API12788Thu Mar 12 22:22:10 GMT 2015android.content.res

AssetFileDescriptor

public class AssetFileDescriptor extends Object implements Closeable, android.os.Parcelable
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.

Fields Summary
public static final long
UNKNOWN_LENGTH
Length used with {@link #AssetFileDescriptor(ParcelFileDescriptor, long, long)} and {@link #getDeclaredLength} when a length has not been declared. This means the data extends to the end of the file.
private final android.os.ParcelFileDescriptor
mFd
private final long
mStartOffset
private final long
mLength
private final android.os.Bundle
mExtras
public static final Parcelable.Creator
CREATOR
Constructors Summary
public AssetFileDescriptor(android.os.ParcelFileDescriptor fd, long startOffset, long length)
Create a new AssetFileDescriptor from the given values.

param
fd The underlying file descriptor.
param
startOffset The location within the file that the asset starts. This must be 0 if length is UNKNOWN_LENGTH.
param
length The number of bytes of the asset, or {@link #UNKNOWN_LENGTH} if it extends to the end of the file.


                                                                                     
        
              
        this(fd, startOffset, length, null);
    
AssetFileDescriptor(android.os.Parcel src)

        mFd = ParcelFileDescriptor.CREATOR.createFromParcel(src);
        mStartOffset = src.readLong();
        mLength = src.readLong();
        if (src.readInt() != 0) {
            mExtras = src.readBundle();
        } else {
            mExtras = null;
        }
    
public AssetFileDescriptor(android.os.ParcelFileDescriptor fd, long startOffset, long length, android.os.Bundle extras)
Create a new AssetFileDescriptor from the given values.

param
fd The underlying file descriptor.
param
startOffset The location within the file that the asset starts. This must be 0 if length is UNKNOWN_LENGTH.
param
length The number of bytes of the asset, or {@link #UNKNOWN_LENGTH} if it extends to the end of the file.
param
extras additional details that can be used to interpret the underlying file descriptor. May be null.

        if (fd == null) {
            throw new IllegalArgumentException("fd must not be null");
        }
        if (length < 0 && startOffset != 0) {
            throw new IllegalArgumentException(
                    "startOffset must be 0 when using UNKNOWN_LENGTH");
        }
        mFd = fd;
        mStartOffset = startOffset;
        mLength = length;
        mExtras = extras;
    
Methods Summary
public voidclose()
Convenience for calling getParcelFileDescriptor().close().

        mFd.close();
    
public java.io.FileInputStreamcreateInputStream()
Create and return a new auto-close input stream for this asset. This will either return a full asset {@link AutoCloseInputStream}, or an underlying {@link ParcelFileDescriptor.AutoCloseInputStream 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.

        if (mLength < 0) {
            return new ParcelFileDescriptor.AutoCloseInputStream(mFd);
        }
        return new AutoCloseInputStream(this);
    
public java.io.FileOutputStreamcreateOutputStream()
Create and return a new auto-close output stream for this asset. This will either return a full asset {@link AutoCloseOutputStream}, or an underlying {@link ParcelFileDescriptor.AutoCloseOutputStream 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.

        if (mLength < 0) {
            return new ParcelFileDescriptor.AutoCloseOutputStream(mFd);
        }
        return new AutoCloseOutputStream(this);
    
public intdescribeContents()

        return mFd.describeContents();
    
public longgetDeclaredLength()
Return the actual number of bytes that were declared when the AssetFileDescriptor was constructed. Will be {@link #UNKNOWN_LENGTH} if the length was not declared, meaning data should be read to the end of the file.

see
#getDeclaredLength()

        return mLength;
    
public android.os.BundlegetExtras()
Returns any additional details that can be used to interpret the underlying file descriptor. May be null.

        return mExtras;
    
public java.io.FileDescriptorgetFileDescriptor()
Returns the FileDescriptor that can be used to read the data in the file.

        return mFd.getFileDescriptor();
    
public longgetLength()
Returns the total number of bytes of this asset entry's data. May be {@link #UNKNOWN_LENGTH} if the asset extends to the end of the file. If the AssetFileDescriptor was constructed with {@link #UNKNOWN_LENGTH}, this will use {@link ParcelFileDescriptor#getStatSize() ParcelFileDescriptor.getStatSize()} to find the total size of the file, returning that number if found or {@link #UNKNOWN_LENGTH} if it could not be determined.

see
#getDeclaredLength()

        if (mLength >= 0) {
            return mLength;
        }
        long len = mFd.getStatSize();
        return len >= 0 ? len : UNKNOWN_LENGTH;
    
public android.os.ParcelFileDescriptorgetParcelFileDescriptor()
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.

        return mFd;
    
public longgetStartOffset()
Returns the byte offset where this asset entry's data starts.

        return mStartOffset;
    
public java.lang.StringtoString()

        return "{AssetFileDescriptor: " + mFd
                + " start=" + mStartOffset + " len=" + mLength + "}";
    
public voidwriteToParcel(android.os.Parcel out, int flags)

        mFd.writeToParcel(out, flags);
        out.writeLong(mStartOffset);
        out.writeLong(mLength);
        if (mExtras != null) {
            out.writeInt(1);
            out.writeBundle(mExtras);
        } else {
            out.writeInt(0);
        }