Example usage for android.os MemoryFile getSize

List of usage examples for android.os MemoryFile getSize

Introduction

In this page you can find the example usage for android.os MemoryFile getSize.

Prototype

@UnsupportedAppUsage
public static int getSize(FileDescriptor fd) throws IOException 

Source Link

Document

Returns the size of the memory file that the file descriptor refers to, or -1 if the file descriptor does not refer to a memory file.

Usage

From source file:edu.umich.flowfence.common.ParceledPayload.java

public static ParceledPayload fromParcel(Parcel p) {
    byte[] data = p.createByteArray();
    if (data == null) {
        // Null data = data's stored in an ashmem region.
        try (ParcelFileDescriptor pfd = p.readFileDescriptor()) {
            FileDescriptor fd = pfd.getFileDescriptor();
            int size = MemoryFile.getSize(fd);
            if (size == -1) {
                throw new ParcelFormatException("ParceledPayload blob is not ashmem");
            }/*w  ww .  ja  v  a 2  s.co m*/
            data = new byte[size];
            FileInputStream fis = new FileInputStream(fd);
            FileChannel chan = fis.getChannel();
            MappedByteBuffer mapping = chan.map(FileChannel.MapMode.READ_ONLY, 0, size);
            mapping.get(data);
        } catch (IOException e) {
            Log.e(TAG, "Couldn't unparcel - not an ashmem region?", e);
            ParcelFormatException pfe = new ParcelFormatException("Exception reading blob for ParceledPayload");
            pfe.initCause(e);
            throw pfe;
        }
    }
    return new ParceledPayload(data);
}