Example usage for android.os Parcel createByteArray

List of usage examples for android.os Parcel createByteArray

Introduction

In this page you can find the example usage for android.os Parcel createByteArray.

Prototype

public final byte[] createByteArray() 

Source Link

Document

Read and return a byte[] object from the parcel.

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");
            }/*from ww w  .  j a v a  2s.c o  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);
}