19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2006 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.os;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
190450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roosimport android.annotation.IntegerRes;
20ddb948896ca7059161e09d0063b3332352772c0aSvet Ganovimport android.annotation.Nullable;
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.text.TextUtils;
22b87655b3e551c6a32f34084c8533800bbd1aff7dDianne Hackbornimport android.util.ArrayMap;
23ddb948896ca7059161e09d0063b3332352772c0aSvet Ganovimport android.util.ArraySet;
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.Log;
255ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkeyimport android.util.Size;
265ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkeyimport android.util.SizeF;
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.SparseArray;
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.SparseBooleanArray;
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.ByteArrayInputStream;
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.ByteArrayOutputStream;
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.FileDescriptor;
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.FileNotFoundException;
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.IOException;
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.ObjectInputStream;
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.ObjectOutputStream;
375002b8c972967b9f0f58dcefe6e37ea79fa9a179John Spurlockimport java.io.ObjectStreamClass;
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.Serializable;
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.lang.reflect.Field;
4044e440cc7e834de7811f005998acb32716835b00Neil Fullerimport java.lang.reflect.Modifier;
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.ArrayList;
42a28b83ee04ca25100781f37a50665d6e1b05e3a2Elliott Hughesimport java.util.Arrays;
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.HashMap;
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.List;
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.Map;
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.Set;
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
480450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roosimport dalvik.system.VMRuntime;
490450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/**
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Container for a message (data and object references) that can
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * be sent through an IBinder.  A Parcel can contain both flattened data
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * that will be unflattened on the other side of the IPC (using the various
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * methods here for writing specific types, or the general
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link Parcelable} interface), and references to live {@link IBinder}
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * objects that will result in the other side receiving a proxy IBinder
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * connected with the original IBinder in the Parcel.
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p class="note">Parcel is <strong>not</strong> a general-purpose
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * serialization mechanism.  This class (and the corresponding
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link Parcelable} API for placing arbitrary objects into a Parcel) is
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * designed as a high-performance IPC transport.  As such, it is not
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * appropriate to place any Parcel data in to persistent storage: changes
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * in the underlying implementation of any of the data in the Parcel can
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * render older data unreadable.</p>
66a8036660ea522e8509e4fd5c79bd4466ac6d9179Samuel Tan *
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>The bulk of the Parcel API revolves around reading and writing data
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * of various types.  There are six major classes of such functions available.</p>
69a8036660ea522e8509e4fd5c79bd4466ac6d9179Samuel Tan *
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <h3>Primitives</h3>
71a8036660ea522e8509e4fd5c79bd4466ac6d9179Samuel Tan *
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>The most basic data functions are for writing and reading primitive
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * data types: {@link #writeByte}, {@link #readByte}, {@link #writeDouble},
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #readDouble}, {@link #writeFloat}, {@link #readFloat}, {@link #writeInt},
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #readInt}, {@link #writeLong}, {@link #readLong},
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #writeString}, {@link #readString}.  Most other
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * data operations are built on top of these.  The given data is written and
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * read using the endianess of the host CPU.</p>
79a8036660ea522e8509e4fd5c79bd4466ac6d9179Samuel Tan *
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <h3>Primitive Arrays</h3>
81a8036660ea522e8509e4fd5c79bd4466ac6d9179Samuel Tan *
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>There are a variety of methods for reading and writing raw arrays
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * of primitive objects, which generally result in writing a 4-byte length
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * followed by the primitive data items.  The methods for reading can either
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * read the data into an existing array, or create and return a new array.
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * These available types are:</p>
87a8036660ea522e8509e4fd5c79bd4466ac6d9179Samuel Tan *
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <ul>
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <li> {@link #writeBooleanArray(boolean[])},
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #readBooleanArray(boolean[])}, {@link #createBooleanArray()}
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <li> {@link #writeByteArray(byte[])},
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #writeByteArray(byte[], int, int)}, {@link #readByteArray(byte[])},
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #createByteArray()}
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <li> {@link #writeCharArray(char[])}, {@link #readCharArray(char[])},
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #createCharArray()}
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <li> {@link #writeDoubleArray(double[])}, {@link #readDoubleArray(double[])},
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #createDoubleArray()}
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <li> {@link #writeFloatArray(float[])}, {@link #readFloatArray(float[])},
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #createFloatArray()}
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <li> {@link #writeIntArray(int[])}, {@link #readIntArray(int[])},
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #createIntArray()}
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <li> {@link #writeLongArray(long[])}, {@link #readLongArray(long[])},
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #createLongArray()}
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <li> {@link #writeStringArray(String[])}, {@link #readStringArray(String[])},
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #createStringArray()}.
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <li> {@link #writeSparseBooleanArray(SparseBooleanArray)},
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #readSparseBooleanArray()}.
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * </ul>
109a8036660ea522e8509e4fd5c79bd4466ac6d9179Samuel Tan *
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <h3>Parcelables</h3>
111a8036660ea522e8509e4fd5c79bd4466ac6d9179Samuel Tan *
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>The {@link Parcelable} protocol provides an extremely efficient (but
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * low-level) protocol for objects to write and read themselves from Parcels.
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You can use the direct methods {@link #writeParcelable(Parcelable, int)}
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * and {@link #readParcelable(ClassLoader)} or
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #writeParcelableArray} and
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #readParcelableArray(ClassLoader)} to write or read.  These
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * methods write both the class type and its data to the Parcel, allowing
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * that class to be reconstructed from the appropriate class loader when
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * later reading.</p>
121a8036660ea522e8509e4fd5c79bd4466ac6d9179Samuel Tan *
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>There are also some methods that provide a more efficient way to work
1238dea8744ac12a97533e8de0f8e56a58d630eb489Jens Ole Lauridsen * with Parcelables: {@link #writeTypedObject}, {@link #writeTypedArray},
1248dea8744ac12a97533e8de0f8e56a58d630eb489Jens Ole Lauridsen * {@link #writeTypedList}, {@link #readTypedObject},
1258dea8744ac12a97533e8de0f8e56a58d630eb489Jens Ole Lauridsen * {@link #createTypedArray} and {@link #createTypedArrayList}.  These methods
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * do not write the class information of the original object: instead, the
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * caller of the read function must know what type to expect and pass in the
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * appropriate {@link Parcelable.Creator Parcelable.Creator} instead to
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * properly construct the new object and read its data.  (To more efficient
1308dea8744ac12a97533e8de0f8e56a58d630eb489Jens Ole Lauridsen * write and read a single Parceable object that is not null, you can directly
1318dea8744ac12a97533e8de0f8e56a58d630eb489Jens Ole Lauridsen * call {@link Parcelable#writeToParcel Parcelable.writeToParcel} and
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link Parcelable.Creator#createFromParcel Parcelable.Creator.createFromParcel}
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * yourself.)</p>
134a8036660ea522e8509e4fd5c79bd4466ac6d9179Samuel Tan *
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <h3>Bundles</h3>
136a8036660ea522e8509e4fd5c79bd4466ac6d9179Samuel Tan *
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>A special type-safe container, called {@link Bundle}, is available
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * for key/value maps of heterogeneous values.  This has many optimizations
1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * for improved performance when reading and writing data, and its type-safe
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * API avoids difficult to debug type errors when finally marshalling the
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * data contents into a Parcel.  The methods to use are
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #writeBundle(Bundle)}, {@link #readBundle()}, and
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #readBundle(ClassLoader)}.
144a8036660ea522e8509e4fd5c79bd4466ac6d9179Samuel Tan *
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <h3>Active Objects</h3>
146a8036660ea522e8509e4fd5c79bd4466ac6d9179Samuel Tan *
1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>An unusual feature of Parcel is the ability to read and write active
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * objects.  For these objects the actual contents of the object is not
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * written, rather a special token referencing the object is written.  When
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * reading the object back from the Parcel, you do not get a new instance of
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * the object, but rather a handle that operates on the exact same object that
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * was originally written.  There are two forms of active objects available.</p>
153a8036660ea522e8509e4fd5c79bd4466ac6d9179Samuel Tan *
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>{@link Binder} objects are a core facility of Android's general cross-process
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * communication system.  The {@link IBinder} interface describes an abstract
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * protocol with a Binder object.  Any such interface can be written in to
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * a Parcel, and upon reading you will receive either the original object
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * implementing that interface or a special proxy implementation
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * that communicates calls back to the original object.  The methods to use are
1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #writeStrongBinder(IBinder)},
1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #writeStrongInterface(IInterface)}, {@link #readStrongBinder()},
1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #writeBinderArray(IBinder[])}, {@link #readBinderArray(IBinder[])},
1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #createBinderArray()},
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #writeBinderList(List)}, {@link #readBinderList(List)},
1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #createBinderArrayList()}.</p>
166a8036660ea522e8509e4fd5c79bd4466ac6d9179Samuel Tan *
1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>FileDescriptor objects, representing raw Linux file descriptor identifiers,
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * can be written and {@link ParcelFileDescriptor} objects returned to operate
1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * on the original file descriptor.  The returned file descriptor is a dup
1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * of the original file descriptor: the object and fd is different, but
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * operating on the same underlying file stream, with the same position, etc.
1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * The methods to use are {@link #writeFileDescriptor(FileDescriptor)},
1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #readFileDescriptor()}.
174a8036660ea522e8509e4fd5c79bd4466ac6d9179Samuel Tan *
1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <h3>Untyped Containers</h3>
176a8036660ea522e8509e4fd5c79bd4466ac6d9179Samuel Tan *
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>A final class of methods are for writing and reading standard Java
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * containers of arbitrary types.  These all revolve around the
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #writeValue(Object)} and {@link #readValue(ClassLoader)} methods
1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * which define the types of objects allowed.  The container methods are
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #writeArray(Object[])}, {@link #readArray(ClassLoader)},
1829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #writeList(List)}, {@link #readList(List, ClassLoader)},
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #readArrayList(ClassLoader)},
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #writeMap(Map)}, {@link #readMap(Map, ClassLoader)},
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #writeSparseArray(SparseArray)},
1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #readSparseArray(ClassLoader)}.
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic final class Parcel {
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final boolean DEBUG_RECYCLE = false;
190e784d1e4cfeaf3a9accd7caa33c087abd8f39040Dianne Hackborn    private static final boolean DEBUG_ARRAY_MAP = false;
1915b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static final String TAG = "Parcel";
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @SuppressWarnings({"UnusedDeclaration"})
1948ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private long mNativePtr; // used by native code
195047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey
196047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    /**
197047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey     * Flag indicating if {@link #mNativePtr} was allocated by this object,
198047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey     * indicating that we're responsible for its lifecycle.
199047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey     */
200047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    private boolean mOwnsNativeParcelObject;
2010450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos    private long mNativeSize;
202047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private RuntimeException mStack;
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int POOL_SIZE = 6;
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final Parcel[] sOwnedPool = new Parcel[POOL_SIZE];
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final Parcel[] sHolderPool = new Parcel[POOL_SIZE];
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2093cefe6a991ba650375e663e7095266e2273d2372Samuel Tan    // Keep in sync with frameworks/native/libs/binder/PersistableBundle.cpp.
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_NULL = -1;
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_STRING = 0;
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_INTEGER = 1;
2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_MAP = 2;
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_BUNDLE = 3;
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_PARCELABLE = 4;
2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_SHORT = 5;
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_LONG = 6;
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_FLOAT = 7;
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_DOUBLE = 8;
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_BOOLEAN = 9;
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_CHARSEQUENCE = 10;
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_LIST  = 11;
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_SPARSEARRAY = 12;
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_BYTEARRAY = 13;
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_STRINGARRAY = 14;
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_IBINDER = 15;
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_PARCELABLEARRAY = 16;
2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_OBJECTARRAY = 17;
2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_INTARRAY = 18;
2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_LONGARRAY = 19;
2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_BYTE = 20;
2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_SERIALIZABLE = 21;
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_SPARSEBOOLEANARRAY = 22;
2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int VAL_BOOLEANARRAY = 23;
23508bbffb049c135c5dfd40d261118c90d1a6dc111Bjorn Bringert    private static final int VAL_CHARSEQUENCEARRAY = 24;
236719e6b167041ffaffc2245f692714c8de191863fCraig Mautner    private static final int VAL_PERSISTABLEBUNDLE = 25;
2375ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkey    private static final int VAL_SIZE = 26;
2385ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkey    private static final int VAL_SIZEF = 27;
239a8036660ea522e8509e4fd5c79bd4466ac6d9179Samuel Tan    private static final int VAL_DOUBLEARRAY = 28;
2409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2415b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    // The initial int32 in a Binder call's reply Parcel header:
24280fd1208b99fbb88f24faa5d55183cdbbdeb6777Christopher Wiley    // Keep these in sync with libbinder's binder/Status.h.
2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int EX_SECURITY = -1;
2449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int EX_BAD_PARCELABLE = -2;
2459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int EX_ILLEGAL_ARGUMENT = -3;
2469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int EX_NULL_POINTER = -4;
2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int EX_ILLEGAL_STATE = -5;
2487e7144218e8681fff2065a891de517bad6fe1b38Dianne Hackborn    private static final int EX_NETWORK_MAIN_THREAD = -6;
24933d738a10209d4dc882a65285d6e71cfaeabe769Dianne Hackborn    private static final int EX_UNSUPPORTED_OPERATION = -7;
25080fd1208b99fbb88f24faa5d55183cdbbdeb6777Christopher Wiley    private static final int EX_SERVICE_SPECIFIC = -8;
2515b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static final int EX_HAS_REPLY_HEADER = -128;  // special; see below
25280fd1208b99fbb88f24faa5d55183cdbbdeb6777Christopher Wiley    // EX_TRANSACTION_FAILED is used exclusively in native code.
25380fd1208b99fbb88f24faa5d55183cdbbdeb6777Christopher Wiley    // see libbinder's binder/Status.h
25480fd1208b99fbb88f24faa5d55183cdbbdeb6777Christopher Wiley    private static final int EX_TRANSACTION_FAILED = -129;
2555b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
2568ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native int nativeDataSize(long nativePtr);
2578ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native int nativeDataAvail(long nativePtr);
2588ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native int nativeDataPosition(long nativePtr);
2598ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native int nativeDataCapacity(long nativePtr);
2600450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos    private static native long nativeSetDataSize(long nativePtr, int size);
2618ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native void nativeSetDataPosition(long nativePtr, int pos);
2628ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native void nativeSetDataCapacity(long nativePtr, int size);
2638ab665dda40ab10e60fc69392022171f454af530Ashok Bhat
2648ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native boolean nativePushAllowFds(long nativePtr, boolean allowFds);
2658ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native void nativeRestoreAllowFds(long nativePtr, boolean lastValue);
2668ab665dda40ab10e60fc69392022171f454af530Ashok Bhat
2678ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native void nativeWriteByteArray(long nativePtr, byte[] b, int offset, int len);
26890d7a3e996ee3a6e33e655341c5d301fbe30cd58Sandeep Siddhartha    private static native void nativeWriteBlob(long nativePtr, byte[] b, int offset, int len);
2698ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native void nativeWriteInt(long nativePtr, int val);
2708ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native void nativeWriteLong(long nativePtr, long val);
2718ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native void nativeWriteFloat(long nativePtr, float val);
2728ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native void nativeWriteDouble(long nativePtr, double val);
2738ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native void nativeWriteString(long nativePtr, String val);
2748ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native void nativeWriteStrongBinder(long nativePtr, IBinder val);
2750450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos    private static native long nativeWriteFileDescriptor(long nativePtr, FileDescriptor val);
2768ab665dda40ab10e60fc69392022171f454af530Ashok Bhat
2778ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native byte[] nativeCreateByteArray(long nativePtr);
27890d7a3e996ee3a6e33e655341c5d301fbe30cd58Sandeep Siddhartha    private static native byte[] nativeReadBlob(long nativePtr);
2798ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native int nativeReadInt(long nativePtr);
2808ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native long nativeReadLong(long nativePtr);
2818ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native float nativeReadFloat(long nativePtr);
2828ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native double nativeReadDouble(long nativePtr);
2838ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native String nativeReadString(long nativePtr);
2848ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native IBinder nativeReadStrongBinder(long nativePtr);
2858ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native FileDescriptor nativeReadFileDescriptor(long nativePtr);
2868ab665dda40ab10e60fc69392022171f454af530Ashok Bhat
2878ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native long nativeCreate();
2880450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos    private static native long nativeFreeBuffer(long nativePtr);
2898ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native void nativeDestroy(long nativePtr);
2908ab665dda40ab10e60fc69392022171f454af530Ashok Bhat
2918ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native byte[] nativeMarshall(long nativePtr);
2920450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos    private static native long nativeUnmarshall(
293e08523684d5414117f81debd4eb14bc8eb494c29John Spurlock            long nativePtr, byte[] data, int offset, int length);
2940450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos    private static native long nativeAppendFrom(
2958ab665dda40ab10e60fc69392022171f454af530Ashok Bhat            long thisNativePtr, long otherNativePtr, int offset, int length);
2968ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native boolean nativeHasFileDescriptors(long nativePtr);
2978ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native void nativeWriteInterfaceToken(long nativePtr, String interfaceName);
2988ab665dda40ab10e60fc69392022171f454af530Ashok Bhat    private static native void nativeEnforceInterface(long nativePtr, String interfaceName);
299047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey
300aa861666ed537389e335f586bc84fe8ddb9fb026Dan Sandler    private static native long nativeGetBlobAshmemSize(long nativePtr);
3015ce0430edfc176de29c0cf503975570dfda8111fDan Sandler
3029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final static Parcelable.Creator<String> STRING_CREATOR
3039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project             = new Parcelable.Creator<String>() {
3049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public String createFromParcel(Parcel source) {
3059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return source.readString();
3069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public String[] newArray(int size) {
3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return new String[size];
3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    };
3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Retrieve a new Parcel object from the pool.
3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Parcel obtain() {
3169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        final Parcel[] pool = sOwnedPool;
3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        synchronized (pool) {
3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Parcel p;
3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int i=0; i<POOL_SIZE; i++) {
3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                p = pool[i];
3219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (p != null) {
3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    pool[i] = null;
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (DEBUG_RECYCLE) {
3249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        p.mStack = new RuntimeException();
3259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    return p;
3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return new Parcel(0);
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Put a Parcel object back into the pool.  You must not touch
3359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the object after this call.
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void recycle() {
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (DEBUG_RECYCLE) mStack = null;
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        freeBuffer();
340047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey
341047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        final Parcel[] pool;
342047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        if (mOwnsNativeParcelObject) {
343047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey            pool = sOwnedPool;
344047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        } else {
345047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey            mNativePtr = 0;
346047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey            pool = sHolderPool;
347047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        }
348047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey
3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        synchronized (pool) {
3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int i=0; i<POOL_SIZE; i++) {
3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (pool[i] == null) {
3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    pool[i] = this;
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    return;
3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
3559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
359fabb70b2efbd125f74923d19ab12bc8e1371ed1fDianne Hackborn    /** @hide */
360fabb70b2efbd125f74923d19ab12bc8e1371ed1fDianne Hackborn    public static native long getGlobalAllocSize();
361fabb70b2efbd125f74923d19ab12bc8e1371ed1fDianne Hackborn
362fabb70b2efbd125f74923d19ab12bc8e1371ed1fDianne Hackborn    /** @hide */
363fabb70b2efbd125f74923d19ab12bc8e1371ed1fDianne Hackborn    public static native long getGlobalAllocCount();
364fabb70b2efbd125f74923d19ab12bc8e1371ed1fDianne Hackborn
3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the total amount of data contained in the parcel.
3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
368047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final int dataSize() {
369047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        return nativeDataSize(mNativePtr);
370047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the amount of data remaining to be read from the
3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * parcel.  That is, {@link #dataSize}-{@link #dataPosition}.
3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
376047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final int dataAvail() {
377047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        return nativeDataAvail(mNativePtr);
378047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
3799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the current position in the parcel data.  Never
3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * more than {@link #dataSize}.
3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
384047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final int dataPosition() {
385047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        return nativeDataPosition(mNativePtr);
386047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the total amount of space in the parcel.  This is always
3909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * >= {@link #dataSize}.  The difference between it and dataSize() is the
3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * amount of room left until the parcel needs to re-allocate its
3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * data buffer.
3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
394047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final int dataCapacity() {
395047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        return nativeDataCapacity(mNativePtr);
396047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
3979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Change the amount of data in the parcel.  Can be either smaller or
4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * larger than the current size.  If larger than the current capacity,
4019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * more memory will be allocated.
4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param size The new number of bytes in the Parcel.
4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
405047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final void setDataSize(int size) {
4060450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos        updateNativeSize(nativeSetDataSize(mNativePtr, size));
407047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
4089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Move the current read/write position in the parcel.
4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param pos New offset in the parcel; must be between 0 and
4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * {@link #dataSize}.
4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
414047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final void setDataPosition(int pos) {
415047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        nativeSetDataPosition(mNativePtr, pos);
416047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
4179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Change the capacity (current available space) of the parcel.
4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param size The new capacity of the parcel, in bytes.  Can not be
4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * less than {@link #dataSize} -- that is, you can not drop existing data
4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * with this method.
4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
425047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final void setDataCapacity(int size) {
426047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        nativeSetDataCapacity(mNativePtr, size);
427047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4299ecebbfbf768fd63e9a6c9a09c86d81c7737ee2dDianne Hackborn    /** @hide */
430047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final boolean pushAllowFds(boolean allowFds) {
431047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        return nativePushAllowFds(mNativePtr, allowFds);
432047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
433c04db7e06737c5b9bae276ac462858d44002672eDianne Hackborn
434c04db7e06737c5b9bae276ac462858d44002672eDianne Hackborn    /** @hide */
435047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final void restoreAllowFds(boolean lastValue) {
436047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        nativeRestoreAllowFds(mNativePtr, lastValue);
437047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
4389ecebbfbf768fd63e9a6c9a09c86d81c7737ee2dDianne Hackborn
4399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the raw bytes of the parcel.
4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p class="note">The data you retrieve here <strong>must not</strong>
4439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * be placed in any kind of persistent storage (on local disk, across
4449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * a network, etc).  For that, you should use standard serialization
4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or another kind of general serialization mechanism.  The Parcel
4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * marshalled representation is highly optimized for local IPC, and as
4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * such does not attempt to maintain compatibility with data created
4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * in different versions of the platform.
4499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
450047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final byte[] marshall() {
451047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        return nativeMarshall(mNativePtr);
452047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Set the bytes in data to be the raw bytes of this Parcel.
4569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
457e08523684d5414117f81debd4eb14bc8eb494c29John Spurlock    public final void unmarshall(byte[] data, int offset, int length) {
4580450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos        updateNativeSize(nativeUnmarshall(mNativePtr, data, offset, length));
459047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
4609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
461047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final void appendFrom(Parcel parcel, int offset, int length) {
4620450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos        updateNativeSize(nativeAppendFrom(mNativePtr, parcel.mNativePtr, offset, length));
463047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
4649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Report whether the parcel contains any marshalled file descriptors.
4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
468047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final boolean hasFileDescriptors() {
469047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        return nativeHasFileDescriptors(mNativePtr);
470047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
4719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Store or read an IBinder interface token in the parcel at the current
4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * {@link #dataPosition}.  This is used to validate that the marshalled
4759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * transaction is intended for the target interface.
4769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
477047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final void writeInterfaceToken(String interfaceName) {
478047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        nativeWriteInterfaceToken(mNativePtr, interfaceName);
479047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
480047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey
481047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final void enforceInterface(String interfaceName) {
482047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        nativeEnforceInterface(mNativePtr, interfaceName);
483047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
486a28b83ee04ca25100781f37a50665d6e1b05e3a2Elliott Hughes     * Write a byte array into the parcel at the current {@link #dataPosition},
4879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * growing {@link #dataCapacity} if needed.
4889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param b Bytes to place into the parcel.
4899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void writeByteArray(byte[] b) {
4919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        writeByteArray(b, 0, (b != null) ? b.length : 0);
4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
495f76a50ce8fdc6aea22cabc77b2977a1a15a79630Ken Wakasa     * Write a byte array into the parcel at the current {@link #dataPosition},
4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * growing {@link #dataCapacity} if needed.
4979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param b Bytes to place into the parcel.
4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param offset Index of first byte to be written.
4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param len Number of bytes to write.
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void writeByteArray(byte[] b, int offset, int len) {
5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (b == null) {
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeInt(-1);
5049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
5059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
506a28b83ee04ca25100781f37a50665d6e1b05e3a2Elliott Hughes        Arrays.checkOffsetAndCount(b.length, offset, len);
507047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        nativeWriteByteArray(mNativePtr, b, offset, len);
5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
51190d7a3e996ee3a6e33e655341c5d301fbe30cd58Sandeep Siddhartha     * Write a blob of data into the parcel at the current {@link #dataPosition},
51290d7a3e996ee3a6e33e655341c5d301fbe30cd58Sandeep Siddhartha     * growing {@link #dataCapacity} if needed.
51390d7a3e996ee3a6e33e655341c5d301fbe30cd58Sandeep Siddhartha     * @param b Bytes to place into the parcel.
51490d7a3e996ee3a6e33e655341c5d301fbe30cd58Sandeep Siddhartha     * {@hide}
51539c12fab49075b715c253c68c84b5c10c3150197Sandeep Siddhartha     * {@SystemApi}
51690d7a3e996ee3a6e33e655341c5d301fbe30cd58Sandeep Siddhartha     */
51790d7a3e996ee3a6e33e655341c5d301fbe30cd58Sandeep Siddhartha    public final void writeBlob(byte[] b) {
518b9f7aac3488873677377b36c57338d758098f78eDan Sandler        writeBlob(b, 0, (b != null) ? b.length : 0);
519b9f7aac3488873677377b36c57338d758098f78eDan Sandler    }
520b9f7aac3488873677377b36c57338d758098f78eDan Sandler
521b9f7aac3488873677377b36c57338d758098f78eDan Sandler    /**
522b9f7aac3488873677377b36c57338d758098f78eDan Sandler     * Write a blob of data into the parcel at the current {@link #dataPosition},
523b9f7aac3488873677377b36c57338d758098f78eDan Sandler     * growing {@link #dataCapacity} if needed.
524b9f7aac3488873677377b36c57338d758098f78eDan Sandler     * @param b Bytes to place into the parcel.
525b9f7aac3488873677377b36c57338d758098f78eDan Sandler     * @param offset Index of first byte to be written.
526b9f7aac3488873677377b36c57338d758098f78eDan Sandler     * @param len Number of bytes to write.
527b9f7aac3488873677377b36c57338d758098f78eDan Sandler     * {@hide}
528b9f7aac3488873677377b36c57338d758098f78eDan Sandler     * {@SystemApi}
529b9f7aac3488873677377b36c57338d758098f78eDan Sandler     */
530b9f7aac3488873677377b36c57338d758098f78eDan Sandler    public final void writeBlob(byte[] b, int offset, int len) {
531b9f7aac3488873677377b36c57338d758098f78eDan Sandler        if (b == null) {
532b9f7aac3488873677377b36c57338d758098f78eDan Sandler            writeInt(-1);
533b9f7aac3488873677377b36c57338d758098f78eDan Sandler            return;
534b9f7aac3488873677377b36c57338d758098f78eDan Sandler        }
535b9f7aac3488873677377b36c57338d758098f78eDan Sandler        Arrays.checkOffsetAndCount(b.length, offset, len);
536b9f7aac3488873677377b36c57338d758098f78eDan Sandler        nativeWriteBlob(mNativePtr, b, offset, len);
53790d7a3e996ee3a6e33e655341c5d301fbe30cd58Sandeep Siddhartha    }
53890d7a3e996ee3a6e33e655341c5d301fbe30cd58Sandeep Siddhartha
53990d7a3e996ee3a6e33e655341c5d301fbe30cd58Sandeep Siddhartha    /**
5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Write an integer value into the parcel at the current dataPosition(),
5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * growing dataCapacity() if needed.
5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
543047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final void writeInt(int val) {
544047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        nativeWriteInt(mNativePtr, val);
545047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
5469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Write a long integer value into the parcel at the current dataPosition(),
5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * growing dataCapacity() if needed.
5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
551047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final void writeLong(long val) {
552047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        nativeWriteLong(mNativePtr, val);
553047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
5549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Write a floating point value into the parcel at the current
5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * dataPosition(), growing dataCapacity() if needed.
5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
559047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final void writeFloat(float val) {
560047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        nativeWriteFloat(mNativePtr, val);
561047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
5629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Write a double precision floating point value into the parcel at the
5659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * current dataPosition(), growing dataCapacity() if needed.
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
567047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final void writeDouble(double val) {
568047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        nativeWriteDouble(mNativePtr, val);
569047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Write a string value into the parcel at the current dataPosition(),
5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * growing dataCapacity() if needed.
5749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
575047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final void writeString(String val) {
576047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        nativeWriteString(mNativePtr, val);
577047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
58008bbffb049c135c5dfd40d261118c90d1a6dc111Bjorn Bringert     * Write a CharSequence value into the parcel at the current dataPosition(),
58108bbffb049c135c5dfd40d261118c90d1a6dc111Bjorn Bringert     * growing dataCapacity() if needed.
58208bbffb049c135c5dfd40d261118c90d1a6dc111Bjorn Bringert     * @hide
58308bbffb049c135c5dfd40d261118c90d1a6dc111Bjorn Bringert     */
58408bbffb049c135c5dfd40d261118c90d1a6dc111Bjorn Bringert    public final void writeCharSequence(CharSequence val) {
58508bbffb049c135c5dfd40d261118c90d1a6dc111Bjorn Bringert        TextUtils.writeToParcel(val, this, 0);
58608bbffb049c135c5dfd40d261118c90d1a6dc111Bjorn Bringert    }
58708bbffb049c135c5dfd40d261118c90d1a6dc111Bjorn Bringert
58808bbffb049c135c5dfd40d261118c90d1a6dc111Bjorn Bringert    /**
5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Write an object into the parcel at the current dataPosition(),
5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * growing dataCapacity() if needed.
5919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
592047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final void writeStrongBinder(IBinder val) {
593047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey        nativeWriteStrongBinder(mNativePtr, val);
594047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
5959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Write an object into the parcel at the current dataPosition(),
5989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * growing dataCapacity() if needed.
5999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void writeStrongInterface(IInterface val) {
6019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        writeStrongBinder(val == null ? null : val.asBinder());
6029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
6059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Write a FileDescriptor into the parcel at the current dataPosition(),
6069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * growing dataCapacity() if needed.
607b3e4ef37021a9e5518fdbc7d0cbb0a1709d5301bDan Egnor     *
608b3e4ef37021a9e5518fdbc7d0cbb0a1709d5301bDan Egnor     * <p class="caution">The file descriptor will not be closed, which may
609b3e4ef37021a9e5518fdbc7d0cbb0a1709d5301bDan Egnor     * result in file descriptor leaks when objects are returned from Binder
610b3e4ef37021a9e5518fdbc7d0cbb0a1709d5301bDan Egnor     * calls.  Use {@link ParcelFileDescriptor#writeToParcel} instead, which
611b3e4ef37021a9e5518fdbc7d0cbb0a1709d5301bDan Egnor     * accepts contextual flags and will close the original file descriptor
612b3e4ef37021a9e5518fdbc7d0cbb0a1709d5301bDan Egnor     * if {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} is set.</p>
6139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
614047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    public final void writeFileDescriptor(FileDescriptor val) {
6150450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos        updateNativeSize(nativeWriteFileDescriptor(mNativePtr, val));
6160450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos    }
6170450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos
6180450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos    private void updateNativeSize(long newNativeSize) {
6190450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos        if (mOwnsNativeParcelObject) {
6200450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos            if (newNativeSize > Integer.MAX_VALUE) {
6210450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos                newNativeSize = Integer.MAX_VALUE;
6220450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos            }
6230450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos            if (newNativeSize != mNativeSize) {
6240450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos                int delta = (int) (newNativeSize - mNativeSize);
6250450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos                if (delta > 0) {
6260450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos                    VMRuntime.getRuntime().registerNativeAllocation(delta);
6270450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos                } else {
6280450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos                    VMRuntime.getRuntime().registerNativeFree(-delta);
6290450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos                }
6300450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos                mNativeSize = newNativeSize;
6310450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos            }
6320450565d9a6de0f0828c9ad8ce16c8645c6f394cAdrian Roos        }
633047238ced42eea812de9d39a9f32e94d002bfa5cJeff Sharkey    }
6349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
6362f974b252f819405a54798c9736f448f979a095fCasey Dahlin     * {@hide}
6372f974b252f819405a54798c9736f448f979a095fCasey Dahlin     * This will be the new name for writeFileDescriptor, for consistency.
6382f974b252f819405a54798c9736f448f979a095fCasey Dahlin     **/
6392f974b252f819405a54798c9736f448f979a095fCasey Dahlin    public final void writeRawFileDescriptor(FileDescriptor val) {
6402f974b252f819405a54798c9736f448f979a095fCasey Dahlin        nativeWriteFileDescriptor(mNativePtr, val);
6412f974b252f819405a54798c9736f448f979a095fCasey Dahlin    }
6422f974b252f819405a54798c9736f448f979a095fCasey Dahlin
6432f974b252f819405a54798c9736f448f979a095fCasey Dahlin    /**
6442f974b252f819405a54798c9736f448f979a095fCasey Dahlin     * {@hide}
6452f974b252f819405a54798c9736f448f979a095fCasey Dahlin     * Write an array of FileDescriptor objects into the Parcel.
6462f974b252f819405a54798c9736f448f979a095fCasey Dahlin     *
6472f974b252f819405a54798c9736f448f979a095fCasey Dahlin     * @param value The array of objects to be written.
6482f974b252f819405a54798c9736f448f979a095fCasey Dahlin     */
6492f974b252f819405a54798c9736f448f979a095fCasey Dahlin    public final void writeRawFileDescriptorArray(FileDescriptor[] value) {
6502f974b252f819405a54798c9736f448f979a095fCasey Dahlin        if (value != null) {
6512f974b252f819405a54798c9736f448f979a095fCasey Dahlin            int N = value.length;
6522f974b252f819405a54798c9736f448f979a095fCasey Dahlin            writeInt(N);
6532f974b252f819405a54798c9736f448f979a095fCasey Dahlin            for (int i=0; i<N; i++) {
6542f974b252f819405a54798c9736f448f979a095fCasey Dahlin                writeRawFileDescriptor(value[i]);
6552f974b252f819405a54798c9736f448f979a095fCasey Dahlin            }
6562f974b252f819405a54798c9736f448f979a095fCasey Dahlin        } else {
6572f974b252f819405a54798c9736f448f979a095fCasey Dahlin            writeInt(-1);
6582f974b252f819405a54798c9736f448f979a095fCasey Dahlin        }
6592f974b252f819405a54798c9736f448f979a095fCasey Dahlin    }
6602f974b252f819405a54798c9736f448f979a095fCasey Dahlin
6612f974b252f819405a54798c9736f448f979a095fCasey Dahlin    /**
662f76a50ce8fdc6aea22cabc77b2977a1a15a79630Ken Wakasa     * Write a byte value into the parcel at the current dataPosition(),
6639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * growing dataCapacity() if needed.
6649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void writeByte(byte val) {
6669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        writeInt(val);
6679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
6709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Please use {@link #writeBundle} instead.  Flattens a Map into the parcel
6719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * at the current dataPosition(),
6729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * growing dataCapacity() if needed.  The Map keys must be String objects.
6739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * The Map values are written using {@link #writeValue} and must follow
6749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the specification there.
675a8036660ea522e8509e4fd5c79bd4466ac6d9179Samuel Tan     *
6769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p>It is strongly recommended to use {@link #writeBundle} instead of
6779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * this method, since the Bundle class provides a type-safe API that
6789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * allows you to avoid mysterious type errors at the point of marshalling.
6799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void writeMap(Map val) {
681b87655b3e551c6a32f34084c8533800bbd1aff7dDianne Hackborn        writeMapInternal((Map<String, Object>) val);
6829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
6859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Flatten a Map into the parcel at the current dataPosition(),
6869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * growing dataCapacity() if needed.  The Map keys must be String objects.
6879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6886aff905048ba3b03724f17e2aba9089872e14cd2Dianne Hackborn    /* package */ void writeMapInternal(Map<String,Object> val) {
6899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (val == null) {
6909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeInt(-1);
6919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
6929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Set<Map.Entry<String,Object>> entries = val.entrySet();
6949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        writeInt(entries.size());
6959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        for (Map.Entry<String,Object> e : entries) {
6969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeValue(e.getKey());
6979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeValue(e.getValue());
6989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
702b87655b3e551c6a32f34084c8533800bbd1aff7dDianne Hackborn     * Flatten an ArrayMap into the parcel at the current dataPosition(),
703b87655b3e551c6a32f34084c8533800bbd1aff7dDianne Hackborn     * growing dataCapacity() if needed.  The Map keys must be String objects.
704b87655b3e551c6a32f34084c8533800bbd1aff7dDianne Hackborn     */
7059c3e74f1f77d3b29ad47d2c74b0a0061e67c76f1Dianne Hackborn    /* package */ void writeArrayMapInternal(ArrayMap<String, Object> val) {
706b87655b3e551c6a32f34084c8533800bbd1aff7dDianne Hackborn        if (val == null) {
707b87655b3e551c6a32f34084c8533800bbd1aff7dDianne Hackborn            writeInt(-1);
708b87655b3e551c6a32f34084c8533800bbd1aff7dDianne Hackborn            return;
709b87655b3e551c6a32f34084c8533800bbd1aff7dDianne Hackborn        }
7103cefe6a991ba650375e663e7095266e2273d2372Samuel Tan        // Keep the format of this Parcel in sync with writeToParcelInner() in
7113cefe6a991ba650375e663e7095266e2273d2372Samuel Tan        // frameworks/native/libs/binder/PersistableBundle.cpp.
712b87655b3e551c6a32f34084c8533800bbd1aff7dDianne Hackborn        final int N = val.size();
713b87655b3e551c6a32f34084c8533800bbd1aff7dDianne Hackborn        writeInt(N);
714e784d1e4cfeaf3a9accd7caa33c087abd8f39040Dianne Hackborn        if (DEBUG_ARRAY_MAP) {
715e784d1e4cfeaf3a9accd7caa33c087abd8f39040Dianne Hackborn            RuntimeException here =  new RuntimeException("here");
716e784d1e4cfeaf3a9accd7caa33c087abd8f39040Dianne Hackborn            here.fillInStackTrace();
717e784d1e4cfeaf3a9accd7caa33c087abd8f39040Dianne Hackborn            Log.d(TAG, "Writing " + N + " ArrayMap entries", here);
718e784d1e4cfeaf3a9accd7caa33c087abd8f39040Dianne Hackborn        }
7198aee64d18b6b90e46e886a80d8de0467d682ddc4Dianne Hackborn        int startPos;
720b87655b3e551c6a32f34084c8533800bbd1aff7dDianne Hackborn        for (int i=0; i<N; i++) {
7218aee64d18b6b90e46e886a80d8de0467d682ddc4Dianne Hackborn            if (DEBUG_ARRAY_MAP) startPos = dataPosition();
7229c3e74f1f77d3b29ad47d2c74b0a0061e67c76f1Dianne Hackborn            writeString(val.keyAt(i));
723b87655b3e551c6a32f34084c8533800bbd1aff7dDianne Hackborn            writeValue(val.valueAt(i));
7248aee64d18b6b90e46e886a80d8de0467d682ddc4Dianne Hackborn            if (DEBUG_ARRAY_MAP) Log.d(TAG, "  Write #" + i + " "
7258aee64d18b6b90e46e886a80d8de0467d682ddc4Dianne Hackborn                    + (dataPosition()-startPos) + " bytes: key=0x"
7268aee64d18b6b90e46e886a80d8de0467d682ddc4Dianne Hackborn                    + Integer.toHexString(val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0)
7278aee64d18b6b90e46e886a80d8de0467d682ddc4Dianne Hackborn                    + " " + val.keyAt(i));
728b87655b3e551c6a32f34084c8533800bbd1aff7dDianne Hackborn        }
729b87655b3e551c6a32f34084c8533800bbd1aff7dDianne Hackborn    }
730b87655b3e551c6a32f34084c8533800bbd1aff7dDianne Hackborn
731b87655b3e551c6a32f34084c8533800bbd1aff7dDianne Hackborn    /**
7329c3e74f1f77d3b29ad47d2c74b0a0061e67c76f1Dianne Hackborn     * @hide For testing only.
7339c3e74f1f77d3b29ad47d2c74b0a0061e67c76f1Dianne Hackborn     */
7349c3e74f1f77d3b29ad47d2c74b0a0061e67c76f1Dianne Hackborn    public void writeArrayMap(ArrayMap<String, Object> val) {
7359c3e74f1f77d3b29ad47d2c74b0a0061e67c76f1Dianne Hackborn        writeArrayMapInternal(val);
7369c3e74f1f77d3b29ad47d2c74b0a0061e67c76f1Dianne Hackborn    }
7379c3e74f1f77d3b29ad47d2c74b0a0061e67c76f1Dianne Hackborn
7389c3e74f1f77d3b29ad47d2c74b0a0061e67c76f1Dianne Hackborn    /**
739ddb948896ca7059161e09d0063b3332352772c0aSvet Ganov     * Write an array set to the parcel.
740ddb948896ca7059161e09d0063b3332352772c0aSvet Ganov     *
741ddb948896ca7059161e09d0063b3332352772c0aSvet Ganov     * @param val The array set to write.
742ddb948896ca7059161e09d0063b3332352772c0aSvet Ganov     *
743ddb948896ca7059161e09d0063b3332352772c0aSvet Ganov     * @hide
744ddb948896ca7059161e09d0063b3332352772c0aSvet Ganov     */
745ddb948896ca7059161e09d0063b3332352772c0aSvet Ganov    public void writeArraySet(@Nullable ArraySet<? extends Object> val) {
746ddb948896ca7059161e09d0063b3332352772c0aSvet Ganov        final int size = (val != null) ? val.size() : -1;
747ddb948896ca7059161e09d0063b3332352772c0aSvet Ganov        writeInt(size);
748ddb948896ca7059161e09d0063b3332352772c0aSvet Ganov        for (int i = 0; i < size; i++) {
749ddb948896ca7059161e09d0063b3332352772c0aSvet Ganov            writeValue(val.valueAt(i));
750ddb948896ca7059161e09d0063b3332352772c0aSvet Ganov        }
751ddb948896ca7059161e09d0063b3332352772c0aSvet Ganov    }
752ddb948896ca7059161e09d0063b3332352772c0aSvet Ganov
753ddb948896ca7059161e09d0063b3332352772c0aSvet Ganov    /**
7549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Flatten a Bundle into the parcel at the current dataPosition(),
7559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * growing dataCapacity() if needed.
7569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
7579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void writeBundle(Bundle val) {
7589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (val == null) {
7599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeInt(-1);
7609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
7619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7636aff905048ba3b03724f17e2aba9089872e14cd2Dianne Hackborn        val.writeToParcel(this, 0);
7649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
767719e6b167041ffaffc2245f692714c8de191863fCraig Mautner     * Flatten a PersistableBundle into the parcel at the current dataPosition(),
768719e6b167041ffaffc2245f692714c8de191863fCraig Mautner     * growing dataCapacity() if needed.
769719e6b167041ffaffc2245f692714c8de191863fCraig Mautner     */
770719e6b167041ffaffc2245f692714c8de191863fCraig Mautner    public final void writePersistableBundle(PersistableBundle val) {
771719e6b167041ffaffc2245f692714c8de191863fCraig Mautner        if (val == null) {
772719e6b167041ffaffc2245f692714c8de191863fCraig Mautner            writeInt(-1);
773719e6b167041ffaffc2245f692714c8de191863fCraig Mautner            return;
774719e6b167041ffaffc2245f692714c8de191863fCraig Mautner        }
775719e6b167041ffaffc2245f692714c8de191863fCraig Mautner
776719e6b167041ffaffc2245f692714c8de191863fCraig Mautner        val.writeToParcel(this, 0);
777719e6b167041ffaffc2245f692714c8de191863fCraig Mautner    }
778719e6b167041ffaffc2245f692714c8de191863fCraig Mautner
779719e6b167041ffaffc2245f692714c8de191863fCraig Mautner    /**
7805ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkey     * Flatten a Size into the parcel at the current dataPosition(),
7815ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkey     * growing dataCapacity() if needed.
7825ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkey     */
7835ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkey    public final void writeSize(Size val) {
7845ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkey        writeInt(val.getWidth());
7855ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkey        writeInt(val.getHeight());
7865ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkey    }
7875ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkey
7885ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkey    /**
7895ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkey     * Flatten a SizeF into the parcel at the current dataPosition(),
7905ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkey     * growing dataCapacity() if needed.
7915ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkey     */
7925ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkey    public final void writeSizeF(SizeF val) {
7935ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkey        writeFloat(val.getWidth());
7945ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkey        writeFloat(val.getHeight());
7955ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkey    }
7965ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkey
7975ef33984d0cc50bf4654b0d8e9557ac34d44fdddJeff Sharkey    /**
7989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Flatten a List into the parcel at the current dataPosition(), growing
7999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * dataCapacity() if needed.  The List values are written using
8009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * {@link #writeValue} and must follow the specification there.
8019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
8029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void writeList(List val) {
8039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (val == null) {
8049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeInt(-1);
8059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
8069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int N = val.size();
8089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int i=0;
8099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        writeInt(N);
8109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        while (i < N) {
8119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeValue(val.get(i));
8129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            i++;
8139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
8179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Flatten an Object array into the parcel at the current dataPosition(),
8189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * growing dataCapacity() if needed.  The array values are written using
8199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * {@link #writeValue} and must follow the specification there.
8209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
8219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void writeArray(Object[] val) {
8229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (val == null) {
8239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeInt(-1);
8249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
8259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int N = val.length;
8279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int i=0;
8289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        writeInt(N);
8299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        while (i < N) {
8309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeValue(val[i]);
8319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            i++;
8329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
8369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Flatten a generic SparseArray into the parcel at the current
8379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * dataPosition(), growing dataCapacity() if needed.  The SparseArray
8389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * values are written using {@link #writeValue} and must follow the
8399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * specification there.
8409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
8419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void writeSparseArray(SparseArray<Object> val) {
8429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (val == null) {
8439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeInt(-1);
8449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
8459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int N = val.size();
8479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        writeInt(N);
8489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int i=0;
8499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        while (i < N) {
8509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeInt(val.keyAt(i));
8519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeValue(val.valueAt(i));
8529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            i++;
8539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void writeSparseBooleanArray(SparseBooleanArray val) {
8579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (val == null) {
8589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeInt(-1);
8599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
8609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int N = val.size();
8629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        writeInt(N);
8639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int i=0;
8649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        while (i < N) {
8659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeInt(val.keyAt(i));
8669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeByte((byte)(val.valueAt(i) ? 1 : 0));
8679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            i++;
8689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void writeBooleanArray(boolean[] val) {
8729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (val != null) {
8739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int N = val.length;
8749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeInt(N);
8759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int i=0; i<N; i++) {
8769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                writeInt(val[i] ? 1 : 0);
8779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
8789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
8799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeInt(-1);
8809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final boolean[] createBooleanArray() {
8849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int N = readInt();
8859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // >>2 as a fast divide-by-4 works in the create*Array() functions
8869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // because dataAvail() will never return a negative number.  4 is
8879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // the size of a stored boolean in the stream.
8889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (N >= 0 && N <= (dataAvail() >> 2)) {
8899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            boolean[] val = new boolean[N];
8909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int i=0; i<N; i++) {
8919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                val[i] = readInt() != 0;
8929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
8939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return val;
8949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
8959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return null;
8969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void readBooleanArray(boolean[] val) {
9009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int N = readInt();
9019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (N == val.length) {
9029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int i=0; i<N; i++) {
9039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                val[i] = readInt() != 0;
9049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
9059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
9069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("bad array lengths");
9079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void writeCharArray(char[] val) {
9119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (val != null) {
9129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int N = val.length;
9139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeInt(N);
9149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int i=0; i<N; i++) {
9159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                writeInt((int)val[i]);
9169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
9179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
9189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeInt(-1);
9199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final char[] createCharArray() {
9239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int N = readInt();
9249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (N >= 0 && N <= (dataAvail() >> 2)) {
9259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            char[] val = new char[N];
9269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int i=0; i<N; i++) {
9279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                val[i] = (char)readInt();
9289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
9299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return val;
9309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
9319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return null;
9329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void readCharArray(char[] val) {
9369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int N = readInt();
9379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (N == val.length) {
9389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int i=0; i<N; i++) {
9399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                val[i] = (char)readInt();
9409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
9419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
9429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("bad array lengths");
9439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void writeIntArray(int[] val) {
9479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (val != null) {
9489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int N = val.length;
9499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeInt(N);
9509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int i=0; i<N; i++) {
9519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                writeInt(val[i]);
9529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
9539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
9549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeInt(-1);
9559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final int[] createIntArray() {
9599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int N = readInt();
9609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (N >= 0 && N <= (dataAvail() >> 2)) {
9619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int[] val = new int[N];
9629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int i=0; i<N; i++) {
9639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                val[i] = readInt();
9649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
9659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return val;
9669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
9679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return null;
9689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void readIntArray(int[] val) {
9729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int N = readInt();
9739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (N == val.length) {
9749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int i=0; i<N; i++) {
9759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                val[i] = readInt();
9769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
9779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
9789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("bad array lengths");
9799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void writeLongArray(long[] val) {
9839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (val != null) {
9849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int N = val.length;
9859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeInt(N);
9869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int i=0; i<N; i++) {
9879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                writeLong(val[i]);
9889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
9899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
9909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            writeInt(-1);
9919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final long[] createLongArray() {
9959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int N = readInt();
9969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // >>3 because stored longs are 64 bits
9979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (N >= 0 && N <= (dataAvail() >> 3)) {
9989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            long[] val = new long[N];
9999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int i=0; i<N; i++) {
10009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                val[i] = readLong();
10019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
10029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return val;
10039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
10049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return null;
1005