Android Guides | Samples

Android.OS.Parcel Class

Container for a message (data and object references) that can be sent through an IBinder.

See Also: Parcel

Syntax

[Android.Runtime.Register("android/os/Parcel", DoNotGenerateAcw=true)]
public sealed class Parcel : Object

Remarks

Container for a message (data and object references) that can be sent through an IBinder. A Parcel can contain both flattened data that will be unflattened on the other side of the IPC (using the various methods here for writing specific types, or the general Parcelable interface), and references to live BinderConsts objects that will result in the other side receiving a proxy IBinder connected with the original IBinder in the Parcel.

Parcel is not a general-purpose serialization mechanism. This class (and the corresponding Parcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.

The bulk of the Parcel API revolves around reading and writing data of various types. There are six major classes of such functions available.

Primitives

The most basic data functions are for writing and reading primitive data types: Parcel.WriteByte(SByte), Parcel.ReadByte, Parcel.WriteDouble(Double), Parcel.ReadDouble, Parcel.WriteFloat(Single), Parcel.ReadFloat, Parcel.WriteInt(Int32), Parcel.ReadInt, Parcel.WriteLong(Int64), Parcel.ReadLong, Parcel.WriteString(String), Parcel.ReadString. Most other data operations are built on top of these. The given data is written and read using the endianess of the host CPU.

Primitive Arrays

There are a variety of methods for reading and writing raw arrays of primitive objects, which generally result in writing a 4-byte length followed by the primitive data items. The methods for reading can either read the data into an existing array, or create and return a new array. These available types are:

Parcelables

The Parcelable protocol provides an extremely efficient (but low-level) protocol for objects to write and read themselves from Parcels. You can use the direct methods Parcel.WriteParcelable(IParcelable,ParcelableWriteFlags) and Parcel.ReadParcelable(ClassLoader) or Parcel.writeParcelableArray(T[], int) and Parcel.ReadParcelableArray(ClassLoader) to write or read. These methods write both the class type and its data to the Parcel, allowing that class to be reconstructed from the appropriate class loader when later reading.

There are also some methods that provide a more efficient way to work with Parcelables: Parcel.writeTypedObject(T, int), Parcel.writeTypedArray(T[], int), Parcel.writeTypedList(java.util.List<T>), Parcel.readTypedObject(android.os.Parcelable.Creator<T>), Parcel.createTypedArray(android.os.Parcelable.Creator<T>) and Parcel.createTypedArrayList(android.os.Parcelable.Creator<T>). These methods do not write the class information of the original object: instead, the caller of the read function must know what type to expect and pass in the appropriate IParcelableCreator instead to properly construct the new object and read its data. (To more efficient write and read a single Parceable object that is not null, you can directly call . and IParcelableCreator.CreateFromParcel(Parcel) yourself.)

Bundles

A special type-safe container, called Bundle, is available for key/value maps of heterogeneous values. This has many optimizations for improved performance when reading and writing data, and its type-safe API avoids difficult to debug type errors when finally marshalling the data contents into a Parcel. The methods to use are Parcel.WriteBundle(Bundle), Parcel.ReadBundle, and Parcel.ReadBundle(ClassLoader).

Active Objects

An unusual feature of Parcel is the ability to read and write active objects. For these objects the actual contents of the object is not written, rather a special token referencing the object is written. When reading the object back from the Parcel, you do not get a new instance of the object, but rather a handle that operates on the exact same object that was originally written. There are two forms of active objects available.

Binder objects are a core facility of Android's general cross-process communication system. The BinderConsts interface describes an abstract protocol with a Binder object. Any such interface can be written in to a Parcel, and upon reading you will receive either the original object implementing that interface or a special proxy implementation that communicates calls back to the original object. The methods to use are Parcel.WriteStrongBinder(IBinder), Parcel.WriteStrongInterface(IInterface), Parcel.ReadStrongBinder, Parcel.WriteBinderArray(IBinder[]), Parcel.ReadBinderArray(IBinder[]), Parcel.CreateBinderArray, Parcel.writeBinderList(java.util.List<android.os.IBinder>), Parcel.readBinderList(java.util.List<android.os.IBinder>), Parcel.CreateBinderArrayList.

FileDescriptor objects, representing raw Linux file descriptor identifiers, can be written and ParcelFileDescriptor objects returned to operate on the original file descriptor. The returned file descriptor is a dup of the original file descriptor: the object and fd is different, but operating on the same underlying file stream, with the same position, etc. The methods to use are Parcel.WriteFileDescriptor(FileDescriptor), Parcel.ReadFileDescriptor.

Untyped Containers

A final class of methods are for writing and reading standard Java containers of arbitrary types. These all revolve around the Parcel.WriteValue(Object) and Parcel.ReadValue(ClassLoader) methods which define the types of objects allowed. The container methods are Parcel.WriteArray(Object[]), Parcel.ReadArray(ClassLoader), Parcel.WriteList(IList), Parcel.ReadList(IList,ClassLoader), Parcel.ReadArrayList(ClassLoader), Parcel.WriteMap(IDictionary), Parcel.ReadMap(IDictionary,ClassLoader), Parcel.writeSparseArray(android.util.SparseArray<java.lang.Object>), Parcel.ReadSparseArray(ClassLoader).

[Android Documentation]

Requirements

Namespace: Android.OS
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 1

The members of Android.OS.Parcel are listed below.

See Also: Object

Public Properties

[read-only]
HasFileDescriptorsBoolean. Report whether the parcel contains any marshalled file descriptors.
[read-only]
static
StringCreatorIParcelableCreator.

Protected Properties

[read-only]
override
ThresholdClassIntPtr. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
[read-only]
override
ThresholdTypeType. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Public Methods

AppendFrom(Parcel, Int32, Int32)
CreateBinderArray() : IBinder[]
CreateBinderArrayList() : IList<IBinder>
Read and return a new ArrayList containing IBinder objects from the parcel that was written with Parcel.writeBinderList(java.util.List<android.os.IBinder>) at the current dataPosition().
CreateBooleanArray() : Boolean[]
CreateByteArray() : Byte[]
Read and return a byte[] object from the parcel.
CreateCharArray() : Char[]
CreateDoubleArray() : Double[]
CreateFloatArray() : Single[]
CreateIntArray() : Int32[]
CreateLongArray() : Int64[]
CreateStringArray() : String[]
CreateStringArrayList() : IList<string>
Read and return a new ArrayList containing String objects from the parcel that was written with Parcel.writeStringList(java.util.List<java.lang.String>) at the current dataPosition().
CreateTypedArray(IParcelableCreator) : Object[]
CreateTypedArrayList(IParcelableCreator) : IList
DataAvail() : Int32
Returns the amount of data remaining to be read from the parcel.
DataCapacity() : Int32
Returns the total amount of space in the parcel.
DataPosition() : Int32
Returns the current position in the parcel data.
DataSize() : Int32
Returns the total amount of data contained in the parcel.
EnforceInterface(String)
Marshall() : Byte[]
Returns the raw bytes of the parcel.
static
Obtain() : Parcel
Retrieve a new Parcel object from the pool.
ReadArray(ClassLoader) : Object[]
Read and return a new Object array from the parcel at the current dataPosition().
ReadArrayList(ClassLoader) : IList
Read and return a new ArrayList object from the parcel at the current dataPosition().
ReadBinderArray(IBinder[])
ReadBinderList(IList<IBinder>)
ReadBooleanArray(Boolean[])
ReadBundle() : Bundle
Read and return a new Bundle object from the parcel at the current dataPosition().
ReadBundle(ClassLoader) : Bundle
Read and return a new Bundle object from the parcel at the current dataPosition(), using the given class loader to initialize the class loader of the Bundle for later retrieval of Parcelable objects.
ReadByte() : SByte
Read a byte value from the parcel at the current dataPosition().
ReadByteArray(Byte[])
Read a byte[] object from the parcel and copy it into the given byte array.
ReadCharArray(Char[])
ReadDouble() : Double
Read a double precision floating point value from the parcel at the current dataPosition().
ReadDoubleArray(Double[])
ReadException()
Special function for reading an exception result from the header of a parcel, to be used after receiving the result of a transaction.
ReadException(Int32, String)
Throw an exception with the given message.
ReadFileDescriptor() : ParcelFileDescriptor
Read a FileDescriptor from the parcel at the current dataPosition().
ReadFloat() : Single
Read a floating point value from the parcel at the current dataPosition().
ReadFloatArray(Single[])
ReadHashMap(ClassLoader) : IDictionary
Please use Parcel.ReadBundle(ClassLoader) instead (whose data must have been written with Parcel.WriteBundle(Bundle).
ReadInt() : Int32
Read an integer value from the parcel at the current dataPosition().
ReadIntArray(Int32[])
ReadList(IList, ClassLoader)
Read into an existing List object from the parcel at the current dataPosition(), using the given class loader to load any enclosed Parcelables.
ReadLong() : Int64
Read a long integer value from the parcel at the current dataPosition().
ReadLongArray(Int64[])
ReadMap(IDictionary, ClassLoader)
Please use Parcel.ReadBundle(ClassLoader) instead (whose data must have been written with Parcel.WriteBundle(Bundle).
ReadParcelable(ClassLoader) : Object
Read and return a new Parcelable from the parcel.
ReadParcelableArray(ClassLoader) : IParcelable[]
Read and return a new Parcelable array from the parcel.
ReadPersistableBundle() : PersistableBundle
Read and return a new Bundle object from the parcel at the current dataPosition().
ReadPersistableBundle(ClassLoader) : PersistableBundle
Read and return a new Bundle object from the parcel at the current dataPosition(), using the given class loader to initialize the class loader of the Bundle for later retrieval of Parcelable objects.
ReadSerializable() : ISerializable
Read and return a new Serializable object from the parcel.
ReadSize() : Size
Read a Size from the parcel at the current dataPosition().
ReadSizeF() : SizeF
Read a SizeF from the parcel at the current dataPosition().
ReadSparseArray(ClassLoader) : SparseArray
Read and return a new SparseArray object from the parcel at the current dataPosition().
ReadSparseBooleanArray() : SparseBooleanArray
Read and return a new SparseBooleanArray object from the parcel at the current dataPosition().
ReadString() : String
Read a string value from the parcel at the current dataPosition().
ReadStringArray(String[])
ReadStringList(IList<string>)
ReadStrongBinder() : IBinder
Read an object from the parcel at the current dataPosition().
ReadTypedArray(Object[], IParcelableCreator)
ReadTypedList(IList, IParcelableCreator)
ReadValue(ClassLoader) : Object
Read a typed object from a parcel.
Recycle()
Put a Parcel object back into the pool.
SetDataCapacity(Int32)
Change the capacity (current available space) of the parcel.
SetDataPosition(Int32)
Move the current read/write position in the parcel.
SetDataSize(Int32)
Change the amount of data in the parcel.
Unmarshall(Byte[], Int32, Int32)
Set the bytes in data to be the raw bytes of this Parcel.
WriteArray(Object[])
Flatten an Object array into the parcel at the current dataPosition(), growing dataCapacity() if needed.
WriteBinderArray(IBinder[])
WriteBinderList(IList<IBinder>)
WriteBooleanArray(Boolean[])
WriteBundle(Bundle)
Flatten a Bundle into the parcel at the current dataPosition(), growing dataCapacity() if needed.
WriteByte(SByte)
Write a byte value into the parcel at the current dataPosition(), growing dataCapacity() if needed.
WriteByteArray(Byte[])
Write a byte array into the parcel at the current Parcel.DataPosition, growing Parcel.DataCapacity if needed.
WriteByteArray(Byte[], Int32, Int32)
Write a byte array into the parcel at the current Parcel.DataPosition, growing Parcel.DataCapacity if needed.
WriteCharArray(Char[])
WriteDouble(Double)
Write a double precision floating point value into the parcel at the current dataPosition(), growing dataCapacity() if needed.
WriteDoubleArray(Double[])
WriteException(Exception)
Special function for writing an exception result at the header of a parcel, to be used when returning an exception from a transaction.
WriteFileDescriptor(FileDescriptor)
Write a FileDescriptor into the parcel at the current dataPosition(), growing dataCapacity() if needed.
WriteFloat(Single)
Write a floating point value into the parcel at the current dataPosition(), growing dataCapacity() if needed.
WriteFloatArray(Single[])
WriteInt(Int32)
Write an integer value into the parcel at the current dataPosition(), growing dataCapacity() if needed.
WriteIntArray(Int32[])
WriteInterfaceToken(String)
Store or read an IBinder interface token in the parcel at the current Parcel.DataPosition.
WriteList(IList)
Flatten a List into the parcel at the current dataPosition(), growing dataCapacity() if needed.
WriteLong(Int64)
Write a long integer value into the parcel at the current dataPosition(), growing dataCapacity() if needed.
WriteLongArray(Int64[])
WriteMap(IDictionary)
Please use Parcel.WriteBundle(Bundle) instead.
WriteNoException()
Special function for writing information at the front of the Parcel indicating that no exception occurred.
WriteParcelable(IParcelable, ParcelableWriteFlags)
Flatten the name of the class of the Parcelable and its contents into the parcel.
WriteParcelableArray(Object[], ParcelableWriteFlags)
WritePersistableBundle(PersistableBundle)
Flatten a PersistableBundle into the parcel at the current dataPosition(), growing dataCapacity() if needed.
WriteSerializable(ISerializable)
Write a generic serializable object in to a Parcel.
WriteSize(Size)
Flatten a Size into the parcel at the current dataPosition(), growing dataCapacity() if needed.
WriteSizeF(SizeF)
Flatten a SizeF into the parcel at the current dataPosition(), growing dataCapacity() if needed.
WriteSparseArray(SparseArray)
WriteSparseBooleanArray(SparseBooleanArray)
WriteString(String)
Write a string value into the parcel at the current dataPosition(), growing dataCapacity() if needed.
WriteStringArray(String[])
WriteStringList(IList<string>)
WriteStrongBinder(IBinder)
Write an object into the parcel at the current dataPosition(), growing dataCapacity() if needed.
WriteStrongInterface(IInterface)
Write an object into the parcel at the current dataPosition(), growing dataCapacity() if needed.
WriteTypedArray(Object[], ParcelableWriteFlags)
WriteTypedList(IList)
WriteValue(Object)
Flatten a generic object in to a parcel.

Protected Methods

static
Obtain(Int32) : Parcel