java.lang.Object | |
↳ | android.os.Parcel |
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 IBinder 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.
The most basic data functions are for writing and reading primitive data types: writeByte(byte), readByte(), writeDouble(double), readDouble(), writeFloat(float), readFloat(), writeInt(int), readInt(), writeLong(long), readLong(), writeString(String), 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.
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:
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 writeParcelable(Parcelable, int) and readParcelable(ClassLoader) or writeParcelableArray(T[], int) and 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: writeTypedArray(T[], int),
writeTypedList(List),
readTypedArray(T[], Parcelable.Creator
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 writeBundle(Bundle), readBundle(), and readBundle(ClassLoader).
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 IBinder 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 writeStrongBinder(IBinder), writeStrongInterface(IInterface), readStrongBinder(), writeBinderArray(IBinder[]), readBinderArray(IBinder[]), createBinderArray(), writeBinderList(List), readBinderList(List), 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 writeFileDescriptor(FileDescriptor), readFileDescriptor().
A final class of methods are for writing and reading standard Java containers of arbitrary types. These all revolve around the writeValue(Object) and readValue(ClassLoader) methods which define the types of objects allowed. The container methods are writeArray(Object[]), readArray(ClassLoader), writeList(List), readList(List, ClassLoader), readArrayList(ClassLoader), writeMap(Map), readMap(Map, ClassLoader), writeSparseArray(SparseArray), readSparseArray(ClassLoader).
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Creator<String> | STRING_CREATOR |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Read and return a new ArrayList containing IBinder objects from
the parcel that was written with writeBinderList(List
| |||||||||||
Read and return a byte[] object from the parcel.
| |||||||||||
Read and return a new ArrayList containing String objects from
the parcel that was written with writeStringList(List
| |||||||||||
Read and return a new array containing a particular object type from
the parcel at the current dataPosition().
| |||||||||||
Read and return a new ArrayList containing a particular object type from
the parcel that was written with writeTypedList(List
| |||||||||||
Returns the amount of data remaining to be read from the
parcel.
| |||||||||||
Returns the total amount of space in the parcel.
| |||||||||||
Returns the current position in the parcel data.
| |||||||||||
Returns the total amount of data contained in the parcel.
| |||||||||||
Report whether the parcel contains any marshalled file descriptors.
| |||||||||||
Returns the raw bytes of the parcel.
| |||||||||||
Retrieve a new Parcel object from the pool.
| |||||||||||
Read and return a new Object array from the parcel at the current
dataPosition().
| |||||||||||
Read and return a new ArrayList object from the parcel at the current
dataPosition().
| |||||||||||
Read into the given List items IBinder objects that were written with
writeBinderList(List
| |||||||||||
Read and return a new Bundle object from the parcel at the current
dataPosition().
| |||||||||||
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.
| |||||||||||
Read a byte value from the parcel at the current dataPosition().
| |||||||||||
Read a byte[] object from the parcel and copy it into the
given byte array.
| |||||||||||
Read a double precision floating point value from the parcel at the
current dataPosition().
| |||||||||||
Special function for reading an exception result from the header of
a parcel, to be used after receiving the result of a transaction.
| |||||||||||
Use this function for customized exception handling.
| |||||||||||
Read a FileDescriptor from the parcel at the current dataPosition().
| |||||||||||
Read a floating point value from the parcel at the current
dataPosition().
| |||||||||||
Please use readBundle(ClassLoader) instead (whose data must have
been written with writeBundle(Bundle).
| |||||||||||
Read an integer value from the parcel at the current dataPosition().
| |||||||||||
Read into an existing List object from the parcel at the current
dataPosition(), using the given class loader to load any enclosed
Parcelables.
| |||||||||||
Read a long integer value from the parcel at the current dataPosition().
| |||||||||||
Please use readBundle(ClassLoader) instead (whose data must have
been written with writeBundle(Bundle).
| |||||||||||
Read and return a new Parcelable from the parcel.
| |||||||||||
Read and return a new Parcelable array from the parcel.
| |||||||||||
Read and return a new Serializable object from the parcel.
| |||||||||||
Read and return a new SparseArray object from the parcel at the current
dataPosition().
| |||||||||||
Read and return a new SparseBooleanArray object from the parcel at the current
dataPosition().
| |||||||||||
Read a string value from the parcel at the current dataPosition().
| |||||||||||
Read into the given List items String objects that were written with
writeStringList(List
| |||||||||||
Read an object from the parcel at the current dataPosition().
| |||||||||||
Read into the given List items containing a particular object type
that were written with writeTypedList(List
| |||||||||||
Read a typed object from a parcel.
| |||||||||||
Put a Parcel object back into the pool.
| |||||||||||
Change the capacity (current available space) of the parcel.
| |||||||||||
Move the current read/write position in the parcel.
| |||||||||||
Change the amount of data in the parcel.
| |||||||||||
Set the bytes in data to be the raw bytes of this Parcel.
| |||||||||||
Flatten an Object array into the parcel at the current dataPosition(),
growing dataCapacity() if needed.
| |||||||||||
Flatten a List containing IBinder objects into the parcel, at
the current dataPosition() and growing dataCapacity() if needed.
| |||||||||||
Flatten a Bundle into the parcel at the current dataPosition(),
growing dataCapacity() if needed.
| |||||||||||
Write an byte value into the parcel at the current dataPosition(),
growing dataCapacity() if needed.
| |||||||||||
Write a byte array into the parcel at the current {#link #dataPosition},
growing dataCapacity() if needed.
| |||||||||||
Write an byte array into the parcel at the current {#link #dataPosition},
growing dataCapacity() if needed.
| |||||||||||
Write a double precision floating point value into the parcel at the
current dataPosition(), growing dataCapacity() if needed.
| |||||||||||
Special function for writing an exception result at the header of
a parcel, to be used when returning an exception from a transaction.
| |||||||||||
Write a FileDescriptor into the parcel at the current dataPosition(),
growing dataCapacity() if needed.
| |||||||||||
Write a floating point value into the parcel at the current
dataPosition(), growing dataCapacity() if needed.
| |||||||||||
Write an integer value into the parcel at the current dataPosition(),
growing dataCapacity() if needed.
| |||||||||||
Store or read an IBinder interface token in the parcel at the current
dataPosition().
| |||||||||||
Flatten a List into the parcel at the current dataPosition(), growing
dataCapacity() if needed.
| |||||||||||
Write a long integer value into the parcel at the current dataPosition(),
growing dataCapacity() if needed.
| |||||||||||
Please use writeBundle(Bundle) instead.
| |||||||||||
Special function for writing information at the front of the Parcel
indicating that no exception occurred.
| |||||||||||
Flatten the name of the class of the Parcelable and its contents
into the parcel.
| |||||||||||
Write a heterogeneous array of Parcelable objects into the Parcel.
| |||||||||||
Write a generic serializable object in to a Parcel.
| |||||||||||
Flatten a generic SparseArray into the parcel at the current
dataPosition(), growing dataCapacity() if needed.
| |||||||||||
Write a string value into the parcel at the current dataPosition(),
growing dataCapacity() if needed.
| |||||||||||
Flatten a List containing String objects into the parcel, at
the current dataPosition() and growing dataCapacity() if needed.
| |||||||||||
Write an object into the parcel at the current dataPosition(),
growing dataCapacity() if needed.
| |||||||||||
Write an object into the parcel at the current dataPosition(),
growing dataCapacity() if needed.
| |||||||||||
Flatten a heterogeneous array containing a particular object type into
the parcel, at
the current dataPosition() and growing dataCapacity() if needed.
| |||||||||||
Flatten a List containing a particular object type into the parcel, at
the current dataPosition() and growing dataCapacity() if needed.
| |||||||||||
Flatten a generic object in to a parcel.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Is called before the object's memory is being reclaimed by the VM.
| |||||||||||
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class java.lang.Object
|
Read and return a new ArrayList containing IBinder objects from
the parcel that was written with writeBinderList(List
Read and return a byte[] object from the parcel.
Read and return a new ArrayList containing String objects from
the parcel that was written with writeStringList(List
Read and return a new array containing a particular object type from the parcel at the current dataPosition(). Returns null if the previously written array was null. The array must have previously been written via writeTypedArray(T[], int) with the same object type.
Read and return a new ArrayList containing a particular object type from
the parcel that was written with writeTypedList(List
Returns the amount of data remaining to be read from the parcel. That is, dataSize()-dataPosition().
Returns the total amount of space in the parcel. This is always >= dataSize(). The difference between it and dataSize() is the amount of room left until the parcel needs to re-allocate its data buffer.
Returns the current position in the parcel data. Never more than dataSize().
Returns the total amount of data contained in the parcel.
Report whether the parcel contains any marshalled file descriptors.
Returns the raw bytes of the parcel.
The data you retrieve here must not be placed in any kind of persistent storage (on local disk, across a network, etc). For that, you should use standard serialization or another kind of general serialization mechanism. The Parcel marshalled representation is highly optimized for local IPC, and as such does not attempt to maintain compatibility with data created in different versions of the platform.
Retrieve a new Parcel object from the pool.
Read and return a new Object array from the parcel at the current dataPosition(). Returns null if the previously written array was null. The given class loader will be used to load any enclosed Parcelables.
Read and return a new ArrayList object from the parcel at the current dataPosition(). Returns null if the previously written list object was null. The given class loader will be used to load any enclosed Parcelables.
Read into the given List items IBinder objects that were written with
writeBinderList(List
Read and return a new Bundle object from the parcel at the current dataPosition(). Returns null if the previously written Bundle object was null.
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. Returns null if the previously written Bundle object was null.
Read a byte value from the parcel at the current dataPosition().
Read a byte[] object from the parcel and copy it into the given byte array.
Read a double precision floating point value from the parcel at the current dataPosition().
Special function for reading an exception result from the header of a parcel, to be used after receiving the result of a transaction. This will throw the exception for you if it had been written to the Parcel, otherwise return and let you read the normal result data from the Parcel.
Use this function for customized exception handling. customized method call this method for all unknown case
code | exception code |
---|---|
msg | exception message |
Read a FileDescriptor from the parcel at the current dataPosition().
Read a floating point value from the parcel at the current dataPosition().
Please use readBundle(ClassLoader) instead (whose data must have been written with writeBundle(Bundle). Read and return a new HashMap object from the parcel at the current dataPosition(), using the given class loader to load any enclosed Parcelables. Returns null if the previously written map object was null.
Read an integer value from the parcel at the current dataPosition().
Read into an existing List object from the parcel at the current dataPosition(), using the given class loader to load any enclosed Parcelables. If it is null, the default class loader is used.
Read a long integer value from the parcel at the current dataPosition().
Please use readBundle(ClassLoader) instead (whose data must have been written with writeBundle(Bundle). Read into an existing Map object from the parcel at the current dataPosition().
Read and return a new Parcelable from the parcel. The given class loader will be used to load any enclosed Parcelables. If it is null, the default class loader will be used.
loader | A ClassLoader from which to instantiate the Parcelable object, or null for the default class loader. |
---|
BadParcelableException | Throws BadParcelableException if there was an error trying to instantiate the Parcelable. |
---|
Read and return a new Parcelable array from the parcel. The given class loader will be used to load any enclosed Parcelables.
Read and return a new Serializable object from the parcel.
Read and return a new SparseArray object from the parcel at the current dataPosition(). Returns null if the previously written list object was null. The given class loader will be used to load any enclosed Parcelables.
Read and return a new SparseBooleanArray object from the parcel at the current dataPosition(). Returns null if the previously written list object was null.
Read a string value from the parcel at the current dataPosition().
Read into the given List items String objects that were written with
writeStringList(List
Read an object from the parcel at the current dataPosition().
Read into the given List items containing a particular object type
that were written with writeTypedList(List
Read a typed object from a parcel. The given class loader will be used to load any enclosed Parcelables. If it is null, the default class loader will be used.
Put a Parcel object back into the pool. You must not touch the object after this call.
Change the capacity (current available space) of the parcel.
size | The new capacity of the parcel, in bytes. Can not be less than dataSize() -- that is, you can not drop existing data with this method. |
---|
Move the current read/write position in the parcel.
pos | New offset in the parcel; must be between 0 and dataSize(). |
---|
Change the amount of data in the parcel. Can be either smaller or larger than the current size. If larger than the current capacity, more memory will be allocated.
size | The new number of bytes in the Parcel. |
---|
Set the bytes in data to be the raw bytes of this Parcel.
Flatten an Object array into the parcel at the current dataPosition(), growing dataCapacity() if needed. The array values are written using writeValue(Object) and must follow the specification there.
Flatten a List containing IBinder objects into the parcel, at
the current dataPosition() and growing dataCapacity() if needed. They
can later be retrieved with createBinderArrayList() or
readBinderList(List
val | The list of strings to be written. |
---|
Flatten a Bundle into the parcel at the current dataPosition(), growing dataCapacity() if needed.
Write an byte value into the parcel at the current dataPosition(), growing dataCapacity() if needed.
Write a byte array into the parcel at the current {#link #dataPosition}, growing dataCapacity() if needed.
b | Bytes to place into the parcel. |
---|
Write an byte array into the parcel at the current {#link #dataPosition}, growing dataCapacity() if needed.
b | Bytes to place into the parcel. |
---|---|
offset | Index of first byte to be written. |
len | Number of bytes to write. |
Write a double precision floating point value into the parcel at the current dataPosition(), growing dataCapacity() if needed.
Special function for writing an exception result at the header of a parcel, to be used when returning an exception from a transaction. Note that this currently only supports a few exception types; any other exception will be re-thrown by this function as a RuntimeException (to be caught by the system's last-resort exception handling when dispatching a transaction).
The supported exception types are:
e | The Exception to be written. |
---|
Write a FileDescriptor into the parcel at the current dataPosition(), growing dataCapacity() if needed.
Write a floating point value into the parcel at the current dataPosition(), growing dataCapacity() if needed.
Write an integer value into the parcel at the current dataPosition(), growing dataCapacity() if needed.
Store or read an IBinder interface token in the parcel at the current dataPosition(). This is used to validate that the marshalled transaction is intended for the target interface.
Flatten a List into the parcel at the current dataPosition(), growing dataCapacity() if needed. The List values are written using writeValue(Object) and must follow the specification there.
Write a long integer value into the parcel at the current dataPosition(), growing dataCapacity() if needed.
Please use writeBundle(Bundle) instead. Flattens a Map into the parcel at the current dataPosition(), growing dataCapacity() if needed. The Map keys must be String objects. The Map values are written using writeValue(Object) and must follow the specification there.
It is strongly recommended to use writeBundle(Bundle) instead of this method, since the Bundle class provides a type-safe API that allows you to avoid mysterious type errors at the point of marshalling.
Special function for writing information at the front of the Parcel indicating that no exception occurred.
Flatten the name of the class of the Parcelable and its contents into the parcel.
p | The Parcelable object to be written. |
---|---|
parcelableFlags | Contextual flags as per Parcelable.writeToParcel(). |
Write a heterogeneous array of Parcelable objects into the Parcel. Each object in the array is written along with its class name, so that the correct class can later be instantiated. As a result, this has significantly more overhead than writeTypedArray(T[], int), but will correctly handle an array containing more than one type of object.
value | The array of objects to be written. |
---|---|
parcelableFlags | Contextual flags as per Parcelable.writeToParcel(). |
Write a generic serializable object in to a Parcel. It is strongly recommended that this method be avoided, since the serialization overhead is extremely large, and this approach will be much slower than using the other approaches to writing data in to a Parcel.
Flatten a generic SparseArray into the parcel at the current dataPosition(), growing dataCapacity() if needed. The SparseArray values are written using writeValue(Object) and must follow the specification there.
Write a string value into the parcel at the current dataPosition(), growing dataCapacity() if needed.
Flatten a List containing String objects into the parcel, at
the current dataPosition() and growing dataCapacity() if needed. They
can later be retrieved with createStringArrayList() or
readStringList(List
val | The list of strings to be written. |
---|
Write an object into the parcel at the current dataPosition(), growing dataCapacity() if needed.
Write an object into the parcel at the current dataPosition(), growing dataCapacity() if needed.
Flatten a heterogeneous array containing a particular object type into
the parcel, at
the current dataPosition() and growing dataCapacity() if needed. The
type of the objects in the array must be one that implements Parcelable.
Unlike the writeParcelableArray(T[], int) method, however, only the
raw data of the objects is written and not their type, so you must use
readTypedArray(T[], Parcelable.Creator
val | The array of objects to be written. |
---|---|
parcelableFlags | Contextual flags as per Parcelable.writeToParcel(). |
Flatten a List containing a particular object type into the parcel, at the current dataPosition() and growing dataCapacity() if needed. The type of the objects in the list must be one that implements Parcelable. Unlike the generic writeList() method, however, only the raw data of the objects is written and not their type, so you must use the corresponding readTypedList() to unmarshall them.
val | The list of objects to be written. |
---|
Flatten a generic object in to a parcel. The given Object value may currently be one of the following types:
Is called before the object's memory is being reclaimed by the VM. This can only happen once the VM has detected, during a run of the garbage collector, that the object is no longer reachable by any thread of the running application.
The method can be used to free system resources or perform other cleanup
before the object is garbage collected. The default implementation of the
method is empty, which is also expected by the VM, but subclasses can
override finalize()
as required. Uncaught exceptions which are
thrown during the execution of this method cause it to terminate
immediately but are otherwise ignored.
Note that the VM does guarantee that finalize()
is called at most
once for any object, but it doesn't guarantee when (if at all) finalize()
will be called. For example, object B's finalize()
can delay the execution of object A's finalize()
method and
therefore it can delay the reclamation of A's memory. To be safe, use a
ReferenceQueue, because it provides more control
over the way the VM deals with references during garbage collection.
Throwable |
---|