2 * Copyright (C) 2006 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 import android.annotation.Nullable;
20 import android.text.TextUtils;
21 import android.util.ArrayMap;
22 import android.util.ArraySet;
23 import android.util.Log;
24 import android.util.Size;
25 import android.util.SizeF;
26 import android.util.SparseArray;
27 import android.util.SparseBooleanArray;
28 import android.util.SparseIntArray;
30 import dalvik.annotation.optimization.FastNative;
31 import dalvik.system.VMRuntime;
33 import libcore.util.SneakyThrow;
35 import java.io.ByteArrayInputStream;
36 import java.io.ByteArrayOutputStream;
37 import java.io.FileDescriptor;
38 import java.io.FileNotFoundException;
39 import java.io.IOException;
40 import java.io.ObjectInputStream;
41 import java.io.ObjectOutputStream;
42 import java.io.ObjectStreamClass;
43 import java.io.Serializable;
44 import java.lang.reflect.Array;
45 import java.lang.reflect.Field;
46 import java.lang.reflect.Modifier;
47 import java.util.ArrayList;
48 import java.util.Arrays;
49 import java.util.HashMap;
50 import java.util.List;
55 * Container for a message (data and object references) that can
56 * be sent through an IBinder. A Parcel can contain both flattened data
57 * that will be unflattened on the other side of the IPC (using the various
58 * methods here for writing specific types, or the general
59 * {@link Parcelable} interface), and references to live {@link IBinder}
60 * objects that will result in the other side receiving a proxy IBinder
61 * connected with the original IBinder in the Parcel.
63 * <p class="note">Parcel is <strong>not</strong> a general-purpose
64 * serialization mechanism. This class (and the corresponding
65 * {@link Parcelable} API for placing arbitrary objects into a Parcel) is
66 * designed as a high-performance IPC transport. As such, it is not
67 * appropriate to place any Parcel data in to persistent storage: changes
68 * in the underlying implementation of any of the data in the Parcel can
69 * render older data unreadable.</p>
71 * <p>The bulk of the Parcel API revolves around reading and writing data
72 * of various types. There are six major classes of such functions available.</p>
76 * <p>The most basic data functions are for writing and reading primitive
77 * data types: {@link #writeByte}, {@link #readByte}, {@link #writeDouble},
78 * {@link #readDouble}, {@link #writeFloat}, {@link #readFloat}, {@link #writeInt},
79 * {@link #readInt}, {@link #writeLong}, {@link #readLong},
80 * {@link #writeString}, {@link #readString}. Most other
81 * data operations are built on top of these. The given data is written and
82 * read using the endianess of the host CPU.</p>
84 * <h3>Primitive Arrays</h3>
86 * <p>There are a variety of methods for reading and writing raw arrays
87 * of primitive objects, which generally result in writing a 4-byte length
88 * followed by the primitive data items. The methods for reading can either
89 * read the data into an existing array, or create and return a new array.
90 * These available types are:</p>
93 * <li> {@link #writeBooleanArray(boolean[])},
94 * {@link #readBooleanArray(boolean[])}, {@link #createBooleanArray()}
95 * <li> {@link #writeByteArray(byte[])},
96 * {@link #writeByteArray(byte[], int, int)}, {@link #readByteArray(byte[])},
97 * {@link #createByteArray()}
98 * <li> {@link #writeCharArray(char[])}, {@link #readCharArray(char[])},
99 * {@link #createCharArray()}
100 * <li> {@link #writeDoubleArray(double[])}, {@link #readDoubleArray(double[])},
101 * {@link #createDoubleArray()}
102 * <li> {@link #writeFloatArray(float[])}, {@link #readFloatArray(float[])},
103 * {@link #createFloatArray()}
104 * <li> {@link #writeIntArray(int[])}, {@link #readIntArray(int[])},
105 * {@link #createIntArray()}
106 * <li> {@link #writeLongArray(long[])}, {@link #readLongArray(long[])},
107 * {@link #createLongArray()}
108 * <li> {@link #writeStringArray(String[])}, {@link #readStringArray(String[])},
109 * {@link #createStringArray()}.
110 * <li> {@link #writeSparseBooleanArray(SparseBooleanArray)},
111 * {@link #readSparseBooleanArray()}.
114 * <h3>Parcelables</h3>
116 * <p>The {@link Parcelable} protocol provides an extremely efficient (but
117 * low-level) protocol for objects to write and read themselves from Parcels.
118 * You can use the direct methods {@link #writeParcelable(Parcelable, int)}
119 * and {@link #readParcelable(ClassLoader)} or
120 * {@link #writeParcelableArray} and
121 * {@link #readParcelableArray(ClassLoader)} to write or read. These
122 * methods write both the class type and its data to the Parcel, allowing
123 * that class to be reconstructed from the appropriate class loader when
126 * <p>There are also some methods that provide a more efficient way to work
127 * with Parcelables: {@link #writeTypedObject}, {@link #writeTypedArray},
128 * {@link #writeTypedList}, {@link #readTypedObject},
129 * {@link #createTypedArray} and {@link #createTypedArrayList}. These methods
130 * do not write the class information of the original object: instead, the
131 * caller of the read function must know what type to expect and pass in the
132 * appropriate {@link Parcelable.Creator Parcelable.Creator} instead to
133 * properly construct the new object and read its data. (To more efficient
134 * write and read a single Parceable object that is not null, you can directly
135 * call {@link Parcelable#writeToParcel Parcelable.writeToParcel} and
136 * {@link Parcelable.Creator#createFromParcel Parcelable.Creator.createFromParcel}
141 * <p>A special type-safe container, called {@link Bundle}, is available
142 * for key/value maps of heterogeneous values. This has many optimizations
143 * for improved performance when reading and writing data, and its type-safe
144 * API avoids difficult to debug type errors when finally marshalling the
145 * data contents into a Parcel. The methods to use are
146 * {@link #writeBundle(Bundle)}, {@link #readBundle()}, and
147 * {@link #readBundle(ClassLoader)}.
149 * <h3>Active Objects</h3>
151 * <p>An unusual feature of Parcel is the ability to read and write active
152 * objects. For these objects the actual contents of the object is not
153 * written, rather a special token referencing the object is written. When
154 * reading the object back from the Parcel, you do not get a new instance of
155 * the object, but rather a handle that operates on the exact same object that
156 * was originally written. There are two forms of active objects available.</p>
158 * <p>{@link Binder} objects are a core facility of Android's general cross-process
159 * communication system. The {@link IBinder} interface describes an abstract
160 * protocol with a Binder object. Any such interface can be written in to
161 * a Parcel, and upon reading you will receive either the original object
162 * implementing that interface or a special proxy implementation
163 * that communicates calls back to the original object. The methods to use are
164 * {@link #writeStrongBinder(IBinder)},
165 * {@link #writeStrongInterface(IInterface)}, {@link #readStrongBinder()},
166 * {@link #writeBinderArray(IBinder[])}, {@link #readBinderArray(IBinder[])},
167 * {@link #createBinderArray()},
168 * {@link #writeBinderList(List)}, {@link #readBinderList(List)},
169 * {@link #createBinderArrayList()}.</p>
171 * <p>FileDescriptor objects, representing raw Linux file descriptor identifiers,
172 * can be written and {@link ParcelFileDescriptor} objects returned to operate
173 * on the original file descriptor. The returned file descriptor is a dup
174 * of the original file descriptor: the object and fd is different, but
175 * operating on the same underlying file stream, with the same position, etc.
176 * The methods to use are {@link #writeFileDescriptor(FileDescriptor)},
177 * {@link #readFileDescriptor()}.
179 * <h3>Untyped Containers</h3>
181 * <p>A final class of methods are for writing and reading standard Java
182 * containers of arbitrary types. These all revolve around the
183 * {@link #writeValue(Object)} and {@link #readValue(ClassLoader)} methods
184 * which define the types of objects allowed. The container methods are
185 * {@link #writeArray(Object[])}, {@link #readArray(ClassLoader)},
186 * {@link #writeList(List)}, {@link #readList(List, ClassLoader)},
187 * {@link #readArrayList(ClassLoader)},
188 * {@link #writeMap(Map)}, {@link #readMap(Map, ClassLoader)},
189 * {@link #writeSparseArray(SparseArray)},
190 * {@link #readSparseArray(ClassLoader)}.
192 public final class Parcel {
193 private static final boolean DEBUG_RECYCLE = false;
194 private static final boolean DEBUG_ARRAY_MAP = false;
195 private static final String TAG = "Parcel";
197 @SuppressWarnings({"UnusedDeclaration"})
198 private long mNativePtr; // used by native code
201 * Flag indicating if {@link #mNativePtr} was allocated by this object,
202 * indicating that we're responsible for its lifecycle.
204 private boolean mOwnsNativeParcelObject;
205 private long mNativeSize;
207 private ArrayMap<Class, Object> mClassCookies;
209 private RuntimeException mStack;
211 private static final int POOL_SIZE = 6;
212 private static final Parcel[] sOwnedPool = new Parcel[POOL_SIZE];
213 private static final Parcel[] sHolderPool = new Parcel[POOL_SIZE];
215 // Keep in sync with frameworks/native/include/private/binder/ParcelValTypes.h.
216 private static final int VAL_NULL = -1;
217 private static final int VAL_STRING = 0;
218 private static final int VAL_INTEGER = 1;
219 private static final int VAL_MAP = 2;
220 private static final int VAL_BUNDLE = 3;
221 private static final int VAL_PARCELABLE = 4;
222 private static final int VAL_SHORT = 5;
223 private static final int VAL_LONG = 6;
224 private static final int VAL_FLOAT = 7;
225 private static final int VAL_DOUBLE = 8;
226 private static final int VAL_BOOLEAN = 9;
227 private static final int VAL_CHARSEQUENCE = 10;
228 private static final int VAL_LIST = 11;
229 private static final int VAL_SPARSEARRAY = 12;
230 private static final int VAL_BYTEARRAY = 13;
231 private static final int VAL_STRINGARRAY = 14;
232 private static final int VAL_IBINDER = 15;
233 private static final int VAL_PARCELABLEARRAY = 16;
234 private static final int VAL_OBJECTARRAY = 17;
235 private static final int VAL_INTARRAY = 18;
236 private static final int VAL_LONGARRAY = 19;
237 private static final int VAL_BYTE = 20;
238 private static final int VAL_SERIALIZABLE = 21;
239 private static final int VAL_SPARSEBOOLEANARRAY = 22;
240 private static final int VAL_BOOLEANARRAY = 23;
241 private static final int VAL_CHARSEQUENCEARRAY = 24;
242 private static final int VAL_PERSISTABLEBUNDLE = 25;
243 private static final int VAL_SIZE = 26;
244 private static final int VAL_SIZEF = 27;
245 private static final int VAL_DOUBLEARRAY = 28;
247 // The initial int32 in a Binder call's reply Parcel header:
248 // Keep these in sync with libbinder's binder/Status.h.
249 private static final int EX_SECURITY = -1;
250 private static final int EX_BAD_PARCELABLE = -2;
251 private static final int EX_ILLEGAL_ARGUMENT = -3;
252 private static final int EX_NULL_POINTER = -4;
253 private static final int EX_ILLEGAL_STATE = -5;
254 private static final int EX_NETWORK_MAIN_THREAD = -6;
255 private static final int EX_UNSUPPORTED_OPERATION = -7;
256 private static final int EX_SERVICE_SPECIFIC = -8;
257 private static final int EX_PARCELABLE = -9;
258 private static final int EX_HAS_REPLY_HEADER = -128; // special; see below
259 // EX_TRANSACTION_FAILED is used exclusively in native code.
260 // see libbinder's binder/Status.h
261 private static final int EX_TRANSACTION_FAILED = -129;
264 private static native int nativeDataSize(long nativePtr);
266 private static native int nativeDataAvail(long nativePtr);
268 private static native int nativeDataPosition(long nativePtr);
270 private static native int nativeDataCapacity(long nativePtr);
272 private static native long nativeSetDataSize(long nativePtr, int size);
274 private static native void nativeSetDataPosition(long nativePtr, int pos);
276 private static native void nativeSetDataCapacity(long nativePtr, int size);
279 private static native boolean nativePushAllowFds(long nativePtr, boolean allowFds);
281 private static native void nativeRestoreAllowFds(long nativePtr, boolean lastValue);
283 private static native void nativeWriteByteArray(long nativePtr, byte[] b, int offset, int len);
284 private static native void nativeWriteBlob(long nativePtr, byte[] b, int offset, int len);
286 private static native void nativeWriteInt(long nativePtr, int val);
288 private static native void nativeWriteLong(long nativePtr, long val);
290 private static native void nativeWriteFloat(long nativePtr, float val);
292 private static native void nativeWriteDouble(long nativePtr, double val);
293 private static native void nativeWriteString(long nativePtr, String val);
294 private static native void nativeWriteStrongBinder(long nativePtr, IBinder val);
295 private static native long nativeWriteFileDescriptor(long nativePtr, FileDescriptor val);
297 private static native byte[] nativeCreateByteArray(long nativePtr);
298 private static native byte[] nativeReadBlob(long nativePtr);
300 private static native int nativeReadInt(long nativePtr);
302 private static native long nativeReadLong(long nativePtr);
304 private static native float nativeReadFloat(long nativePtr);
306 private static native double nativeReadDouble(long nativePtr);
307 private static native String nativeReadString(long nativePtr);
308 private static native IBinder nativeReadStrongBinder(long nativePtr);
309 private static native FileDescriptor nativeReadFileDescriptor(long nativePtr);
311 private static native long nativeCreate();
312 private static native long nativeFreeBuffer(long nativePtr);
313 private static native void nativeDestroy(long nativePtr);
315 private static native byte[] nativeMarshall(long nativePtr);
316 private static native long nativeUnmarshall(
317 long nativePtr, byte[] data, int offset, int length);
318 private static native int nativeCompareData(long thisNativePtr, long otherNativePtr);
319 private static native long nativeAppendFrom(
320 long thisNativePtr, long otherNativePtr, int offset, int length);
322 private static native boolean nativeHasFileDescriptors(long nativePtr);
323 private static native void nativeWriteInterfaceToken(long nativePtr, String interfaceName);
324 private static native void nativeEnforceInterface(long nativePtr, String interfaceName);
326 private static native long nativeGetBlobAshmemSize(long nativePtr);
328 public final static Parcelable.Creator<String> STRING_CREATOR
329 = new Parcelable.Creator<String>() {
330 public String createFromParcel(Parcel source) {
331 return source.readString();
333 public String[] newArray(int size) {
334 return new String[size];
339 * Retrieve a new Parcel object from the pool.
341 public static Parcel obtain() {
342 final Parcel[] pool = sOwnedPool;
343 synchronized (pool) {
345 for (int i=0; i<POOL_SIZE; i++) {
350 p.mStack = new RuntimeException();
356 return new Parcel(0);
360 * Put a Parcel object back into the pool. You must not touch
361 * the object after this call.
363 public final void recycle() {
364 if (DEBUG_RECYCLE) mStack = null;
368 if (mOwnsNativeParcelObject) {
375 synchronized (pool) {
376 for (int i=0; i<POOL_SIZE; i++) {
377 if (pool[i] == null) {
386 public static native long getGlobalAllocSize();
389 public static native long getGlobalAllocCount();
392 * Returns the total amount of data contained in the parcel.
394 public final int dataSize() {
395 return nativeDataSize(mNativePtr);
399 * Returns the amount of data remaining to be read from the
400 * parcel. That is, {@link #dataSize}-{@link #dataPosition}.
402 public final int dataAvail() {
403 return nativeDataAvail(mNativePtr);
407 * Returns the current position in the parcel data. Never
408 * more than {@link #dataSize}.
410 public final int dataPosition() {
411 return nativeDataPosition(mNativePtr);
415 * Returns the total amount of space in the parcel. This is always
416 * >= {@link #dataSize}. The difference between it and dataSize() is the
417 * amount of room left until the parcel needs to re-allocate its
420 public final int dataCapacity() {
421 return nativeDataCapacity(mNativePtr);
425 * Change the amount of data in the parcel. Can be either smaller or
426 * larger than the current size. If larger than the current capacity,
427 * more memory will be allocated.
429 * @param size The new number of bytes in the Parcel.
431 public final void setDataSize(int size) {
432 updateNativeSize(nativeSetDataSize(mNativePtr, size));
436 * Move the current read/write position in the parcel.
437 * @param pos New offset in the parcel; must be between 0 and
440 public final void setDataPosition(int pos) {
441 nativeSetDataPosition(mNativePtr, pos);
445 * Change the capacity (current available space) of the parcel.
447 * @param size The new capacity of the parcel, in bytes. Can not be
448 * less than {@link #dataSize} -- that is, you can not drop existing data
451 public final void setDataCapacity(int size) {
452 nativeSetDataCapacity(mNativePtr, size);
456 public final boolean pushAllowFds(boolean allowFds) {
457 return nativePushAllowFds(mNativePtr, allowFds);
461 public final void restoreAllowFds(boolean lastValue) {
462 nativeRestoreAllowFds(mNativePtr, lastValue);
466 * Returns the raw bytes of the parcel.
468 * <p class="note">The data you retrieve here <strong>must not</strong>
469 * be placed in any kind of persistent storage (on local disk, across
470 * a network, etc). For that, you should use standard serialization
471 * or another kind of general serialization mechanism. The Parcel
472 * marshalled representation is highly optimized for local IPC, and as
473 * such does not attempt to maintain compatibility with data created
474 * in different versions of the platform.
476 public final byte[] marshall() {
477 return nativeMarshall(mNativePtr);
481 * Set the bytes in data to be the raw bytes of this Parcel.
483 public final void unmarshall(byte[] data, int offset, int length) {
484 updateNativeSize(nativeUnmarshall(mNativePtr, data, offset, length));
487 public final void appendFrom(Parcel parcel, int offset, int length) {
488 updateNativeSize(nativeAppendFrom(mNativePtr, parcel.mNativePtr, offset, length));
492 public final int compareData(Parcel other) {
493 return nativeCompareData(mNativePtr, other.mNativePtr);
497 public final void setClassCookie(Class clz, Object cookie) {
498 if (mClassCookies == null) {
499 mClassCookies = new ArrayMap<>();
501 mClassCookies.put(clz, cookie);
505 public final Object getClassCookie(Class clz) {
506 return mClassCookies != null ? mClassCookies.get(clz) : null;
510 public final void adoptClassCookies(Parcel from) {
511 mClassCookies = from.mClassCookies;
515 * Report whether the parcel contains any marshalled file descriptors.
517 public final boolean hasFileDescriptors() {
518 return nativeHasFileDescriptors(mNativePtr);
522 * Store or read an IBinder interface token in the parcel at the current
523 * {@link #dataPosition}. This is used to validate that the marshalled
524 * transaction is intended for the target interface.
526 public final void writeInterfaceToken(String interfaceName) {
527 nativeWriteInterfaceToken(mNativePtr, interfaceName);
530 public final void enforceInterface(String interfaceName) {
531 nativeEnforceInterface(mNativePtr, interfaceName);
535 * Write a byte array into the parcel at the current {@link #dataPosition},
536 * growing {@link #dataCapacity} if needed.
537 * @param b Bytes to place into the parcel.
539 public final void writeByteArray(byte[] b) {
540 writeByteArray(b, 0, (b != null) ? b.length : 0);
544 * Write a byte array into the parcel at the current {@link #dataPosition},
545 * growing {@link #dataCapacity} if needed.
546 * @param b Bytes to place into the parcel.
547 * @param offset Index of first byte to be written.
548 * @param len Number of bytes to write.
550 public final void writeByteArray(byte[] b, int offset, int len) {
555 Arrays.checkOffsetAndCount(b.length, offset, len);
556 nativeWriteByteArray(mNativePtr, b, offset, len);
560 * Write a blob of data into the parcel at the current {@link #dataPosition},
561 * growing {@link #dataCapacity} if needed.
562 * @param b Bytes to place into the parcel.
566 public final void writeBlob(byte[] b) {
567 writeBlob(b, 0, (b != null) ? b.length : 0);
571 * Write a blob of data into the parcel at the current {@link #dataPosition},
572 * growing {@link #dataCapacity} if needed.
573 * @param b Bytes to place into the parcel.
574 * @param offset Index of first byte to be written.
575 * @param len Number of bytes to write.
579 public final void writeBlob(byte[] b, int offset, int len) {
584 Arrays.checkOffsetAndCount(b.length, offset, len);
585 nativeWriteBlob(mNativePtr, b, offset, len);
589 * Write an integer value into the parcel at the current dataPosition(),
590 * growing dataCapacity() if needed.
592 public final void writeInt(int val) {
593 nativeWriteInt(mNativePtr, val);
597 * Write a long integer value into the parcel at the current dataPosition(),
598 * growing dataCapacity() if needed.
600 public final void writeLong(long val) {
601 nativeWriteLong(mNativePtr, val);
605 * Write a floating point value into the parcel at the current
606 * dataPosition(), growing dataCapacity() if needed.
608 public final void writeFloat(float val) {
609 nativeWriteFloat(mNativePtr, val);
613 * Write a double precision floating point value into the parcel at the
614 * current dataPosition(), growing dataCapacity() if needed.
616 public final void writeDouble(double val) {
617 nativeWriteDouble(mNativePtr, val);
621 * Write a string value into the parcel at the current dataPosition(),
622 * growing dataCapacity() if needed.
624 public final void writeString(String val) {
625 nativeWriteString(mNativePtr, val);
629 public final void writeBoolean(boolean val) {
630 writeInt(val ? 1 : 0);
634 * Write a CharSequence value into the parcel at the current dataPosition(),
635 * growing dataCapacity() if needed.
638 public final void writeCharSequence(CharSequence val) {
639 TextUtils.writeToParcel(val, this, 0);
643 * Write an object into the parcel at the current dataPosition(),
644 * growing dataCapacity() if needed.
646 public final void writeStrongBinder(IBinder val) {
647 nativeWriteStrongBinder(mNativePtr, val);
651 * Write an object into the parcel at the current dataPosition(),
652 * growing dataCapacity() if needed.
654 public final void writeStrongInterface(IInterface val) {
655 writeStrongBinder(val == null ? null : val.asBinder());
659 * Write a FileDescriptor into the parcel at the current dataPosition(),
660 * growing dataCapacity() if needed.
662 * <p class="caution">The file descriptor will not be closed, which may
663 * result in file descriptor leaks when objects are returned from Binder
664 * calls. Use {@link ParcelFileDescriptor#writeToParcel} instead, which
665 * accepts contextual flags and will close the original file descriptor
666 * if {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} is set.</p>
668 public final void writeFileDescriptor(FileDescriptor val) {
669 updateNativeSize(nativeWriteFileDescriptor(mNativePtr, val));
672 private void updateNativeSize(long newNativeSize) {
673 if (mOwnsNativeParcelObject) {
674 if (newNativeSize > Integer.MAX_VALUE) {
675 newNativeSize = Integer.MAX_VALUE;
677 if (newNativeSize != mNativeSize) {
678 int delta = (int) (newNativeSize - mNativeSize);
680 VMRuntime.getRuntime().registerNativeAllocation(delta);
682 VMRuntime.getRuntime().registerNativeFree(-delta);
684 mNativeSize = newNativeSize;
691 * This will be the new name for writeFileDescriptor, for consistency.
693 public final void writeRawFileDescriptor(FileDescriptor val) {
694 nativeWriteFileDescriptor(mNativePtr, val);
699 * Write an array of FileDescriptor objects into the Parcel.
701 * @param value The array of objects to be written.
703 public final void writeRawFileDescriptorArray(FileDescriptor[] value) {
705 int N = value.length;
707 for (int i=0; i<N; i++) {
708 writeRawFileDescriptor(value[i]);
716 * Write a byte value into the parcel at the current dataPosition(),
717 * growing dataCapacity() if needed.
719 public final void writeByte(byte val) {
724 * Please use {@link #writeBundle} instead. Flattens a Map into the parcel
725 * at the current dataPosition(),
726 * growing dataCapacity() if needed. The Map keys must be String objects.
727 * The Map values are written using {@link #writeValue} and must follow
728 * the specification there.
730 * <p>It is strongly recommended to use {@link #writeBundle} instead of
731 * this method, since the Bundle class provides a type-safe API that
732 * allows you to avoid mysterious type errors at the point of marshalling.
734 public final void writeMap(Map val) {
735 writeMapInternal((Map<String, Object>) val);
739 * Flatten a Map into the parcel at the current dataPosition(),
740 * growing dataCapacity() if needed. The Map keys must be String objects.
742 /* package */ void writeMapInternal(Map<String,Object> val) {
747 Set<Map.Entry<String,Object>> entries = val.entrySet();
748 writeInt(entries.size());
749 for (Map.Entry<String,Object> e : entries) {
750 writeValue(e.getKey());
751 writeValue(e.getValue());
756 * Flatten an ArrayMap into the parcel at the current dataPosition(),
757 * growing dataCapacity() if needed. The Map keys must be String objects.
759 /* package */ void writeArrayMapInternal(ArrayMap<String, Object> val) {
764 // Keep the format of this Parcel in sync with writeToParcelInner() in
765 // frameworks/native/libs/binder/PersistableBundle.cpp.
766 final int N = val.size();
768 if (DEBUG_ARRAY_MAP) {
769 RuntimeException here = new RuntimeException("here");
770 here.fillInStackTrace();
771 Log.d(TAG, "Writing " + N + " ArrayMap entries", here);
774 for (int i=0; i<N; i++) {
775 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
776 writeString(val.keyAt(i));
777 writeValue(val.valueAt(i));
778 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Write #" + i + " "
779 + (dataPosition()-startPos) + " bytes: key=0x"
780 + Integer.toHexString(val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0)
781 + " " + val.keyAt(i));
786 * @hide For testing only.
788 public void writeArrayMap(ArrayMap<String, Object> val) {
789 writeArrayMapInternal(val);
793 * Write an array set to the parcel.
795 * @param val The array set to write.
799 public void writeArraySet(@Nullable ArraySet<? extends Object> val) {
800 final int size = (val != null) ? val.size() : -1;
802 for (int i = 0; i < size; i++) {
803 writeValue(val.valueAt(i));
808 * Flatten a Bundle into the parcel at the current dataPosition(),
809 * growing dataCapacity() if needed.
811 public final void writeBundle(Bundle val) {
817 val.writeToParcel(this, 0);
821 * Flatten a PersistableBundle into the parcel at the current dataPosition(),
822 * growing dataCapacity() if needed.
824 public final void writePersistableBundle(PersistableBundle val) {
830 val.writeToParcel(this, 0);
834 * Flatten a Size into the parcel at the current dataPosition(),
835 * growing dataCapacity() if needed.
837 public final void writeSize(Size val) {
838 writeInt(val.getWidth());
839 writeInt(val.getHeight());
843 * Flatten a SizeF into the parcel at the current dataPosition(),
844 * growing dataCapacity() if needed.
846 public final void writeSizeF(SizeF val) {
847 writeFloat(val.getWidth());
848 writeFloat(val.getHeight());
852 * Flatten a List into the parcel at the current dataPosition(), growing
853 * dataCapacity() if needed. The List values are written using
854 * {@link #writeValue} and must follow the specification there.
856 public final void writeList(List val) {
865 writeValue(val.get(i));
871 * Flatten an Object array into the parcel at the current dataPosition(),
872 * growing dataCapacity() if needed. The array values are written using
873 * {@link #writeValue} and must follow the specification there.
875 public final void writeArray(Object[] val) {
890 * Flatten a generic SparseArray into the parcel at the current
891 * dataPosition(), growing dataCapacity() if needed. The SparseArray
892 * values are written using {@link #writeValue} and must follow the
893 * specification there.
895 public final void writeSparseArray(SparseArray<Object> val) {
904 writeInt(val.keyAt(i));
905 writeValue(val.valueAt(i));
910 public final void writeSparseBooleanArray(SparseBooleanArray val) {
919 writeInt(val.keyAt(i));
920 writeByte((byte)(val.valueAt(i) ? 1 : 0));
928 public final void writeSparseIntArray(SparseIntArray val) {
937 writeInt(val.keyAt(i));
938 writeInt(val.valueAt(i));
943 public final void writeBooleanArray(boolean[] val) {
947 for (int i=0; i<N; i++) {
948 writeInt(val[i] ? 1 : 0);
955 public final boolean[] createBooleanArray() {
957 // >>2 as a fast divide-by-4 works in the create*Array() functions
958 // because dataAvail() will never return a negative number. 4 is
959 // the size of a stored boolean in the stream.
960 if (N >= 0 && N <= (dataAvail() >> 2)) {
961 boolean[] val = new boolean[N];
962 for (int i=0; i<N; i++) {
963 val[i] = readInt() != 0;
971 public final void readBooleanArray(boolean[] val) {
973 if (N == val.length) {
974 for (int i=0; i<N; i++) {
975 val[i] = readInt() != 0;
978 throw new RuntimeException("bad array lengths");
982 public final void writeCharArray(char[] val) {
986 for (int i=0; i<N; i++) {
987 writeInt((int)val[i]);
994 public final char[] createCharArray() {
996 if (N >= 0 && N <= (dataAvail() >> 2)) {
997 char[] val = new char[N];
998 for (int i=0; i<N; i++) {
999 val[i] = (char)readInt();
1007 public final void readCharArray(char[] val) {
1009 if (N == val.length) {
1010 for (int i=0; i<N; i++) {
1011 val[i] = (char)readInt();
1014 throw new RuntimeException("bad array lengths");
1018 public final void writeIntArray(int[] val) {
1022 for (int i=0; i<N; i++) {
1030 public final int[] createIntArray() {
1032 if (N >= 0 && N <= (dataAvail() >> 2)) {
1033 int[] val = new int[N];
1034 for (int i=0; i<N; i++) {
1043 public final void readIntArray(int[] val) {
1045 if (N == val.length) {
1046 for (int i=0; i<N; i++) {
1050 throw new RuntimeException("bad array lengths");
1054 public final void writeLongArray(long[] val) {
1058 for (int i=0; i<N; i++) {
1066 public final long[] createLongArray() {
1068 // >>3 because stored longs are 64 bits
1069 if (N >= 0 && N <= (dataAvail() >> 3)) {
1070 long[] val = new long[N];
1071 for (int i=0; i<N; i++) {
1072 val[i] = readLong();
1080 public final void readLongArray(long[] val) {
1082 if (N == val.length) {
1083 for (int i=0; i<N; i++) {
1084 val[i] = readLong();
1087 throw new RuntimeException("bad array lengths");
1091 public final void writeFloatArray(float[] val) {
1095 for (int i=0; i<N; i++) {
1103 public final float[] createFloatArray() {
1105 // >>2 because stored floats are 4 bytes
1106 if (N >= 0 && N <= (dataAvail() >> 2)) {
1107 float[] val = new float[N];
1108 for (int i=0; i<N; i++) {
1109 val[i] = readFloat();
1117 public final void readFloatArray(float[] val) {
1119 if (N == val.length) {
1120 for (int i=0; i<N; i++) {
1121 val[i] = readFloat();
1124 throw new RuntimeException("bad array lengths");
1128 public final void writeDoubleArray(double[] val) {
1132 for (int i=0; i<N; i++) {
1133 writeDouble(val[i]);
1140 public final double[] createDoubleArray() {
1142 // >>3 because stored doubles are 8 bytes
1143 if (N >= 0 && N <= (dataAvail() >> 3)) {
1144 double[] val = new double[N];
1145 for (int i=0; i<N; i++) {
1146 val[i] = readDouble();
1154 public final void readDoubleArray(double[] val) {
1156 if (N == val.length) {
1157 for (int i=0; i<N; i++) {
1158 val[i] = readDouble();
1161 throw new RuntimeException("bad array lengths");
1165 public final void writeStringArray(String[] val) {
1169 for (int i=0; i<N; i++) {
1170 writeString(val[i]);
1177 public final String[] createStringArray() {
1180 String[] val = new String[N];
1181 for (int i=0; i<N; i++) {
1182 val[i] = readString();
1190 public final void readStringArray(String[] val) {
1192 if (N == val.length) {
1193 for (int i=0; i<N; i++) {
1194 val[i] = readString();
1197 throw new RuntimeException("bad array lengths");
1201 public final void writeBinderArray(IBinder[] val) {
1205 for (int i=0; i<N; i++) {
1206 writeStrongBinder(val[i]);
1216 public final void writeCharSequenceArray(CharSequence[] val) {
1220 for (int i=0; i<N; i++) {
1221 writeCharSequence(val[i]);
1231 public final void writeCharSequenceList(ArrayList<CharSequence> val) {
1235 for (int i=0; i<N; i++) {
1236 writeCharSequence(val.get(i));
1243 public final IBinder[] createBinderArray() {
1246 IBinder[] val = new IBinder[N];
1247 for (int i=0; i<N; i++) {
1248 val[i] = readStrongBinder();
1256 public final void readBinderArray(IBinder[] val) {
1258 if (N == val.length) {
1259 for (int i=0; i<N; i++) {
1260 val[i] = readStrongBinder();
1263 throw new RuntimeException("bad array lengths");
1268 * Flatten a List containing a particular object type into the parcel, at
1269 * the current dataPosition() and growing dataCapacity() if needed. The
1270 * type of the objects in the list must be one that implements Parcelable.
1271 * Unlike the generic writeList() method, however, only the raw data of the
1272 * objects is written and not their type, so you must use the corresponding
1273 * readTypedList() to unmarshall them.
1275 * @param val The list of objects to be written.
1277 * @see #createTypedArrayList
1278 * @see #readTypedList
1281 public final <T extends Parcelable> void writeTypedList(List<T> val) {
1290 T item = val.get(i);
1293 item.writeToParcel(this, 0);
1302 * Flatten a List containing String objects into the parcel, at
1303 * the current dataPosition() and growing dataCapacity() if needed. They
1304 * can later be retrieved with {@link #createStringArrayList} or
1305 * {@link #readStringList}.
1307 * @param val The list of strings to be written.
1309 * @see #createStringArrayList
1310 * @see #readStringList
1312 public final void writeStringList(List<String> val) {
1321 writeString(val.get(i));
1327 * Flatten a List containing IBinder objects into the parcel, at
1328 * the current dataPosition() and growing dataCapacity() if needed. They
1329 * can later be retrieved with {@link #createBinderArrayList} or
1330 * {@link #readBinderList}.
1332 * @param val The list of strings to be written.
1334 * @see #createBinderArrayList
1335 * @see #readBinderList
1337 public final void writeBinderList(List<IBinder> val) {
1346 writeStrongBinder(val.get(i));
1352 * Flatten a {@code List} containing arbitrary {@code Parcelable} objects into this parcel
1353 * at the current position. They can later be retrieved using
1354 * {@link #readParcelableList(List, ClassLoader)} if required.
1356 * @see #readParcelableList(List, ClassLoader)
1359 public final <T extends Parcelable> void writeParcelableList(List<T> val, int flags) {
1369 writeParcelable(val.get(i), flags);
1375 * Flatten a homogeneous array containing a particular object type into
1377 * the current dataPosition() and growing dataCapacity() if needed. The
1378 * type of the objects in the array must be one that implements Parcelable.
1379 * Unlike the {@link #writeParcelableArray} method, however, only the
1380 * raw data of the objects is written and not their type, so you must use
1381 * {@link #readTypedArray} with the correct corresponding
1382 * {@link Parcelable.Creator} implementation to unmarshall them.
1384 * @param val The array of objects to be written.
1385 * @param parcelableFlags Contextual flags as per
1386 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1388 * @see #readTypedArray
1389 * @see #writeParcelableArray
1390 * @see Parcelable.Creator
1392 public final <T extends Parcelable> void writeTypedArray(T[] val,
1393 int parcelableFlags) {
1397 for (int i = 0; i < N; i++) {
1401 item.writeToParcel(this, parcelableFlags);
1412 * Write a uniform (all items are null or the same class) array list of
1415 * @param list The list to write.
1419 public final <T extends Parcelable> void writeTypedArrayList(@Nullable ArrayList<T> list,
1420 int parcelableFlags) {
1422 int N = list.size();
1424 boolean wroteCreator = false;
1425 for (int i = 0; i < N; i++) {
1426 T item = list.get(i);
1429 if (!wroteCreator) {
1430 writeParcelableCreator(item);
1431 wroteCreator = true;
1433 item.writeToParcel(this, parcelableFlags);
1444 * Reads a uniform (all items are null or the same class) array list of
1447 * @return The list or null.
1451 public final @Nullable <T> ArrayList<T> readTypedArrayList(@Nullable ClassLoader loader) {
1456 Parcelable.Creator<?> creator = null;
1457 ArrayList<T> result = new ArrayList<T>(N);
1458 for (int i = 0; i < N; i++) {
1459 if (readInt() != 0) {
1460 if (creator == null) {
1461 creator = readParcelableCreator(loader);
1462 if (creator == null) {
1467 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
1468 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
1469 (Parcelable.ClassLoaderCreator<?>) creator;
1470 parcelable = (T) classLoaderCreator.createFromParcel(this, loader);
1472 parcelable = (T) creator.createFromParcel(this);
1474 result.add(parcelable);
1483 * Write a uniform (all items are null or the same class) array set of
1486 * @param set The set to write.
1490 public final <T extends Parcelable> void writeTypedArraySet(@Nullable ArraySet<T> set,
1491 int parcelableFlags) {
1495 boolean wroteCreator = false;
1496 for (int i = 0; i < N; i++) {
1497 T item = set.valueAt(i);
1500 if (!wroteCreator) {
1501 writeParcelableCreator(item);
1502 wroteCreator = true;
1504 item.writeToParcel(this, parcelableFlags);
1515 * Reads a uniform (all items are null or the same class) array set of
1518 * @return The set or null.
1522 public final @Nullable <T> ArraySet<T> readTypedArraySet(@Nullable ClassLoader loader) {
1527 Parcelable.Creator<?> creator = null;
1528 ArraySet<T> result = new ArraySet<T>(N);
1529 for (int i = 0; i < N; i++) {
1530 T parcelable = null;
1531 if (readInt() != 0) {
1532 if (creator == null) {
1533 creator = readParcelableCreator(loader);
1534 if (creator == null) {
1538 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
1539 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
1540 (Parcelable.ClassLoaderCreator<?>) creator;
1541 parcelable = (T) classLoaderCreator.createFromParcel(this, loader);
1543 parcelable = (T) creator.createFromParcel(this);
1546 result.append(parcelable);
1552 * Flatten the Parcelable object into the parcel.
1554 * @param val The Parcelable object to be written.
1555 * @param parcelableFlags Contextual flags as per
1556 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1558 * @see #readTypedObject
1560 public final <T extends Parcelable> void writeTypedObject(T val, int parcelableFlags) {
1563 val.writeToParcel(this, parcelableFlags);
1570 * Flatten a generic object in to a parcel. The given Object value may
1571 * currently be one of the following types:
1588 * <li> Object[] (supporting objects of the same type defined here).
1589 * <li> {@link Bundle}
1590 * <li> Map (as supported by {@link #writeMap}).
1591 * <li> Any object that implements the {@link Parcelable} protocol.
1593 * <li> CharSequence (as supported by {@link TextUtils#writeToParcel}).
1594 * <li> List (as supported by {@link #writeList}).
1595 * <li> {@link SparseArray} (as supported by {@link #writeSparseArray(SparseArray)}).
1596 * <li> {@link IBinder}
1597 * <li> Any object that implements Serializable (but see
1598 * {@link #writeSerializable} for caveats). Note that all of the
1599 * previous types have relatively efficient implementations for
1600 * writing to a Parcel; having to rely on the generic serialization
1601 * approach is much less efficient and should be avoided whenever
1605 * <p class="caution">{@link Parcelable} objects are written with
1606 * {@link Parcelable#writeToParcel} using contextual flags of 0. When
1607 * serializing objects containing {@link ParcelFileDescriptor}s,
1608 * this may result in file descriptor leaks when they are returned from
1609 * Binder calls (where {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE}
1610 * should be used).</p>
1612 public final void writeValue(Object v) {
1615 } else if (v instanceof String) {
1616 writeInt(VAL_STRING);
1617 writeString((String) v);
1618 } else if (v instanceof Integer) {
1619 writeInt(VAL_INTEGER);
1620 writeInt((Integer) v);
1621 } else if (v instanceof Map) {
1624 } else if (v instanceof Bundle) {
1625 // Must be before Parcelable
1626 writeInt(VAL_BUNDLE);
1627 writeBundle((Bundle) v);
1628 } else if (v instanceof PersistableBundle) {
1629 writeInt(VAL_PERSISTABLEBUNDLE);
1630 writePersistableBundle((PersistableBundle) v);
1631 } else if (v instanceof Parcelable) {
1632 // IMPOTANT: cases for classes that implement Parcelable must
1633 // come before the Parcelable case, so that their specific VAL_*
1634 // types will be written.
1635 writeInt(VAL_PARCELABLE);
1636 writeParcelable((Parcelable) v, 0);
1637 } else if (v instanceof Short) {
1638 writeInt(VAL_SHORT);
1639 writeInt(((Short) v).intValue());
1640 } else if (v instanceof Long) {
1642 writeLong((Long) v);
1643 } else if (v instanceof Float) {
1644 writeInt(VAL_FLOAT);
1645 writeFloat((Float) v);
1646 } else if (v instanceof Double) {
1647 writeInt(VAL_DOUBLE);
1648 writeDouble((Double) v);
1649 } else if (v instanceof Boolean) {
1650 writeInt(VAL_BOOLEAN);
1651 writeInt((Boolean) v ? 1 : 0);
1652 } else if (v instanceof CharSequence) {
1653 // Must be after String
1654 writeInt(VAL_CHARSEQUENCE);
1655 writeCharSequence((CharSequence) v);
1656 } else if (v instanceof List) {
1658 writeList((List) v);
1659 } else if (v instanceof SparseArray) {
1660 writeInt(VAL_SPARSEARRAY);
1661 writeSparseArray((SparseArray) v);
1662 } else if (v instanceof boolean[]) {
1663 writeInt(VAL_BOOLEANARRAY);
1664 writeBooleanArray((boolean[]) v);
1665 } else if (v instanceof byte[]) {
1666 writeInt(VAL_BYTEARRAY);
1667 writeByteArray((byte[]) v);
1668 } else if (v instanceof String[]) {
1669 writeInt(VAL_STRINGARRAY);
1670 writeStringArray((String[]) v);
1671 } else if (v instanceof CharSequence[]) {
1672 // Must be after String[] and before Object[]
1673 writeInt(VAL_CHARSEQUENCEARRAY);
1674 writeCharSequenceArray((CharSequence[]) v);
1675 } else if (v instanceof IBinder) {
1676 writeInt(VAL_IBINDER);
1677 writeStrongBinder((IBinder) v);
1678 } else if (v instanceof Parcelable[]) {
1679 writeInt(VAL_PARCELABLEARRAY);
1680 writeParcelableArray((Parcelable[]) v, 0);
1681 } else if (v instanceof int[]) {
1682 writeInt(VAL_INTARRAY);
1683 writeIntArray((int[]) v);
1684 } else if (v instanceof long[]) {
1685 writeInt(VAL_LONGARRAY);
1686 writeLongArray((long[]) v);
1687 } else if (v instanceof Byte) {
1690 } else if (v instanceof Size) {
1692 writeSize((Size) v);
1693 } else if (v instanceof SizeF) {
1694 writeInt(VAL_SIZEF);
1695 writeSizeF((SizeF) v);
1696 } else if (v instanceof double[]) {
1697 writeInt(VAL_DOUBLEARRAY);
1698 writeDoubleArray((double[]) v);
1700 Class<?> clazz = v.getClass();
1701 if (clazz.isArray() && clazz.getComponentType() == Object.class) {
1702 // Only pure Object[] are written here, Other arrays of non-primitive types are
1703 // handled by serialization as this does not record the component type.
1704 writeInt(VAL_OBJECTARRAY);
1705 writeArray((Object[]) v);
1706 } else if (v instanceof Serializable) {
1708 writeInt(VAL_SERIALIZABLE);
1709 writeSerializable((Serializable) v);
1711 throw new RuntimeException("Parcel: unable to marshal value " + v);
1717 * Flatten the name of the class of the Parcelable and its contents
1720 * @param p The Parcelable object to be written.
1721 * @param parcelableFlags Contextual flags as per
1722 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1724 public final void writeParcelable(Parcelable p, int parcelableFlags) {
1729 writeParcelableCreator(p);
1730 p.writeToParcel(this, parcelableFlags);
1734 public final void writeParcelableCreator(Parcelable p) {
1735 String name = p.getClass().getName();
1740 * Write a generic serializable object in to a Parcel. It is strongly
1741 * recommended that this method be avoided, since the serialization
1742 * overhead is extremely large, and this approach will be much slower than
1743 * using the other approaches to writing data in to a Parcel.
1745 public final void writeSerializable(Serializable s) {
1750 String name = s.getClass().getName();
1753 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1755 ObjectOutputStream oos = new ObjectOutputStream(baos);
1759 writeByteArray(baos.toByteArray());
1760 } catch (IOException ioe) {
1761 throw new RuntimeException("Parcelable encountered " +
1762 "IOException writing serializable object (name = " + name +
1768 * Special function for writing an exception result at the header of
1769 * a parcel, to be used when returning an exception from a transaction.
1770 * Note that this currently only supports a few exception types; any other
1771 * exception will be re-thrown by this function as a RuntimeException
1772 * (to be caught by the system's last-resort exception handling when
1773 * dispatching a transaction).
1775 * <p>The supported exception types are:
1777 * <li>{@link BadParcelableException}
1778 * <li>{@link IllegalArgumentException}
1779 * <li>{@link IllegalStateException}
1780 * <li>{@link NullPointerException}
1781 * <li>{@link SecurityException}
1782 * <li>{@link NetworkOnMainThreadException}
1785 * @param e The Exception to be written.
1787 * @see #writeNoException
1788 * @see #readException
1790 public final void writeException(Exception e) {
1792 if (e instanceof Parcelable
1793 && (e.getClass().getClassLoader() == Parcelable.class.getClassLoader())) {
1794 // We only send Parcelable exceptions that are in the
1795 // BootClassLoader to ensure that the receiver can unpack them
1796 code = EX_PARCELABLE;
1797 } else if (e instanceof SecurityException) {
1799 } else if (e instanceof BadParcelableException) {
1800 code = EX_BAD_PARCELABLE;
1801 } else if (e instanceof IllegalArgumentException) {
1802 code = EX_ILLEGAL_ARGUMENT;
1803 } else if (e instanceof NullPointerException) {
1804 code = EX_NULL_POINTER;
1805 } else if (e instanceof IllegalStateException) {
1806 code = EX_ILLEGAL_STATE;
1807 } else if (e instanceof NetworkOnMainThreadException) {
1808 code = EX_NETWORK_MAIN_THREAD;
1809 } else if (e instanceof UnsupportedOperationException) {
1810 code = EX_UNSUPPORTED_OPERATION;
1811 } else if (e instanceof ServiceSpecificException) {
1812 code = EX_SERVICE_SPECIFIC;
1815 StrictMode.clearGatheredViolations();
1817 if (e instanceof RuntimeException) {
1818 throw (RuntimeException) e;
1820 throw new RuntimeException(e);
1822 writeString(e.getMessage());
1824 case EX_SERVICE_SPECIFIC:
1825 writeInt(((ServiceSpecificException) e).errorCode);
1828 // Write parceled exception prefixed by length
1829 final int sizePosition = dataPosition();
1831 writeParcelable((Parcelable) e, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
1832 final int payloadPosition = dataPosition();
1833 setDataPosition(sizePosition);
1834 writeInt(payloadPosition - sizePosition);
1835 setDataPosition(payloadPosition);
1841 * Special function for writing information at the front of the Parcel
1842 * indicating that no exception occurred.
1844 * @see #writeException
1845 * @see #readException
1847 public final void writeNoException() {
1848 // Despite the name of this function ("write no exception"),
1849 // it should instead be thought of as "write the RPC response
1850 // header", but because this function name is written out by
1851 // the AIDL compiler, we're not going to rename it.
1853 // The response header, in the non-exception case (see also
1854 // writeException above, also called by the AIDL compiler), is
1855 // either a 0 (the default case), or EX_HAS_REPLY_HEADER if
1856 // StrictMode has gathered up violations that have occurred
1857 // during a Binder call, in which case we write out the number
1858 // of violations and their details, serialized, before the
1859 // actual RPC respons data. The receiving end of this is
1860 // readException(), below.
1861 if (StrictMode.hasGatheredViolations()) {
1862 writeInt(EX_HAS_REPLY_HEADER);
1863 final int sizePosition = dataPosition();
1864 writeInt(0); // total size of fat header, to be filled in later
1865 StrictMode.writeGatheredViolationsToParcel(this);
1866 final int payloadPosition = dataPosition();
1867 setDataPosition(sizePosition);
1868 writeInt(payloadPosition - sizePosition); // header size
1869 setDataPosition(payloadPosition);
1876 * Special function for reading an exception result from the header of
1877 * a parcel, to be used after receiving the result of a transaction. This
1878 * will throw the exception for you if it had been written to the Parcel,
1879 * otherwise return and let you read the normal result data from the Parcel.
1881 * @see #writeException
1882 * @see #writeNoException
1884 public final void readException() {
1885 int code = readExceptionCode();
1887 String msg = readString();
1888 readException(code, msg);
1893 * Parses the header of a Binder call's response Parcel and
1894 * returns the exception code. Deals with lite or fat headers.
1895 * In the common successful case, this header is generally zero.
1896 * In less common cases, it's a small negative number and will be
1897 * followed by an error string.
1899 * This exists purely for android.database.DatabaseUtils and
1900 * insulating it from having to handle fat headers as returned by
1901 * e.g. StrictMode-induced RPC responses.
1905 public final int readExceptionCode() {
1906 int code = readInt();
1907 if (code == EX_HAS_REPLY_HEADER) {
1908 int headerSize = readInt();
1909 if (headerSize == 0) {
1910 Log.e(TAG, "Unexpected zero-sized Parcel reply header.");
1912 // Currently the only thing in the header is StrictMode stacks,
1913 // but discussions around event/RPC tracing suggest we might
1914 // put that here too. If so, switch on sub-header tags here.
1915 // But for now, just parse out the StrictMode stuff.
1916 StrictMode.readAndHandleBinderCallViolations(this);
1918 // And fat response headers are currently only used when
1919 // there are no exceptions, so return no error:
1926 * Throw an exception with the given message. Not intended for use
1927 * outside the Parcel class.
1929 * @param code Used to determine which exception class to throw.
1930 * @param msg The exception message.
1932 public final void readException(int code, String msg) {
1935 if (readInt() > 0) {
1936 SneakyThrow.sneakyThrow(
1937 (Exception) readParcelable(Parcelable.class.getClassLoader()));
1939 throw new RuntimeException(msg + " [missing Parcelable]");
1942 throw new SecurityException(msg);
1943 case EX_BAD_PARCELABLE:
1944 throw new BadParcelableException(msg);
1945 case EX_ILLEGAL_ARGUMENT:
1946 throw new IllegalArgumentException(msg);
1947 case EX_NULL_POINTER:
1948 throw new NullPointerException(msg);
1949 case EX_ILLEGAL_STATE:
1950 throw new IllegalStateException(msg);
1951 case EX_NETWORK_MAIN_THREAD:
1952 throw new NetworkOnMainThreadException();
1953 case EX_UNSUPPORTED_OPERATION:
1954 throw new UnsupportedOperationException(msg);
1955 case EX_SERVICE_SPECIFIC:
1956 throw new ServiceSpecificException(readInt(), msg);
1958 throw new RuntimeException("Unknown exception code: " + code
1963 * Read an integer value from the parcel at the current dataPosition().
1965 public final int readInt() {
1966 return nativeReadInt(mNativePtr);
1970 * Read a long integer value from the parcel at the current dataPosition().
1972 public final long readLong() {
1973 return nativeReadLong(mNativePtr);
1977 * Read a floating point value from the parcel at the current
1980 public final float readFloat() {
1981 return nativeReadFloat(mNativePtr);
1985 * Read a double precision floating point value from the parcel at the
1986 * current dataPosition().
1988 public final double readDouble() {
1989 return nativeReadDouble(mNativePtr);
1993 * Read a string value from the parcel at the current dataPosition().
1995 public final String readString() {
1996 return nativeReadString(mNativePtr);
2000 public final boolean readBoolean() {
2001 return readInt() != 0;
2005 * Read a CharSequence value from the parcel at the current dataPosition().
2008 public final CharSequence readCharSequence() {
2009 return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(this);
2013 * Read an object from the parcel at the current dataPosition().
2015 public final IBinder readStrongBinder() {
2016 return nativeReadStrongBinder(mNativePtr);
2020 * Read a FileDescriptor from the parcel at the current dataPosition().
2022 public final ParcelFileDescriptor readFileDescriptor() {
2023 FileDescriptor fd = nativeReadFileDescriptor(mNativePtr);
2024 return fd != null ? new ParcelFileDescriptor(fd) : null;
2028 public final FileDescriptor readRawFileDescriptor() {
2029 return nativeReadFileDescriptor(mNativePtr);
2034 * Read and return a new array of FileDescriptors from the parcel.
2035 * @return the FileDescriptor array, or null if the array is null.
2037 public final FileDescriptor[] createRawFileDescriptorArray() {
2042 FileDescriptor[] f = new FileDescriptor[N];
2043 for (int i = 0; i < N; i++) {
2044 f[i] = readRawFileDescriptor();
2051 * Read an array of FileDescriptors from a parcel.
2052 * The passed array must be exactly the length of the array in the parcel.
2053 * @return the FileDescriptor array, or null if the array is null.
2055 public final void readRawFileDescriptorArray(FileDescriptor[] val) {
2057 if (N == val.length) {
2058 for (int i=0; i<N; i++) {
2059 val[i] = readRawFileDescriptor();
2062 throw new RuntimeException("bad array lengths");
2066 /** @deprecated use {@link android.system.Os#open(String, int, int)} */
2068 static native FileDescriptor openFileDescriptor(String file, int mode)
2069 throws FileNotFoundException;
2071 /** @deprecated use {@link android.system.Os#dup(FileDescriptor)} */
2073 static native FileDescriptor dupFileDescriptor(FileDescriptor orig) throws IOException;
2075 /** @deprecated use {@link android.system.Os#close(FileDescriptor)} */
2077 static native void closeFileDescriptor(FileDescriptor desc) throws IOException;
2079 static native void clearFileDescriptor(FileDescriptor desc);
2082 * Read a byte value from the parcel at the current dataPosition().
2084 public final byte readByte() {
2085 return (byte)(readInt() & 0xff);
2089 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2090 * been written with {@link #writeBundle}. Read into an existing Map object
2091 * from the parcel at the current dataPosition().
2093 public final void readMap(Map outVal, ClassLoader loader) {
2095 readMapInternal(outVal, N, loader);
2099 * Read into an existing List object from the parcel at the current
2100 * dataPosition(), using the given class loader to load any enclosed
2101 * Parcelables. If it is null, the default class loader is used.
2103 public final void readList(List outVal, ClassLoader loader) {
2105 readListInternal(outVal, N, loader);
2109 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2110 * been written with {@link #writeBundle}. Read and return a new HashMap
2111 * object from the parcel at the current dataPosition(), using the given
2112 * class loader to load any enclosed Parcelables. Returns null if
2113 * the previously written map object was null.
2115 public final HashMap readHashMap(ClassLoader loader)
2121 HashMap m = new HashMap(N);
2122 readMapInternal(m, N, loader);
2127 * Read and return a new Bundle object from the parcel at the current
2128 * dataPosition(). Returns null if the previously written Bundle object was
2131 public final Bundle readBundle() {
2132 return readBundle(null);
2136 * Read and return a new Bundle object from the parcel at the current
2137 * dataPosition(), using the given class loader to initialize the class
2138 * loader of the Bundle for later retrieval of Parcelable objects.
2139 * Returns null if the previously written Bundle object was null.
2141 public final Bundle readBundle(ClassLoader loader) {
2142 int length = readInt();
2144 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
2148 final Bundle bundle = new Bundle(this, length);
2149 if (loader != null) {
2150 bundle.setClassLoader(loader);
2156 * Read and return a new Bundle object from the parcel at the current
2157 * dataPosition(). Returns null if the previously written Bundle object was
2160 public final PersistableBundle readPersistableBundle() {
2161 return readPersistableBundle(null);
2165 * Read and return a new Bundle object from the parcel at the current
2166 * dataPosition(), using the given class loader to initialize the class
2167 * loader of the Bundle for later retrieval of Parcelable objects.
2168 * Returns null if the previously written Bundle object was null.
2170 public final PersistableBundle readPersistableBundle(ClassLoader loader) {
2171 int length = readInt();
2173 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
2177 final PersistableBundle bundle = new PersistableBundle(this, length);
2178 if (loader != null) {
2179 bundle.setClassLoader(loader);
2185 * Read a Size from the parcel at the current dataPosition().
2187 public final Size readSize() {
2188 final int width = readInt();
2189 final int height = readInt();
2190 return new Size(width, height);
2194 * Read a SizeF from the parcel at the current dataPosition().
2196 public final SizeF readSizeF() {
2197 final float width = readFloat();
2198 final float height = readFloat();
2199 return new SizeF(width, height);
2203 * Read and return a byte[] object from the parcel.
2205 public final byte[] createByteArray() {
2206 return nativeCreateByteArray(mNativePtr);
2210 * Read a byte[] object from the parcel and copy it into the
2213 public final void readByteArray(byte[] val) {
2214 // TODO: make this a native method to avoid the extra copy.
2215 byte[] ba = createByteArray();
2216 if (ba.length == val.length) {
2217 System.arraycopy(ba, 0, val, 0, ba.length);
2219 throw new RuntimeException("bad array lengths");
2224 * Read a blob of data from the parcel and return it as a byte array.
2228 public final byte[] readBlob() {
2229 return nativeReadBlob(mNativePtr);
2233 * Read and return a String[] object from the parcel.
2236 public final String[] readStringArray() {
2237 String[] array = null;
2239 int length = readInt();
2242 array = new String[length];
2244 for (int i = 0 ; i < length ; i++)
2246 array[i] = readString();
2254 * Read and return a CharSequence[] object from the parcel.
2257 public final CharSequence[] readCharSequenceArray() {
2258 CharSequence[] array = null;
2260 int length = readInt();
2263 array = new CharSequence[length];
2265 for (int i = 0 ; i < length ; i++)
2267 array[i] = readCharSequence();
2275 * Read and return an ArrayList<CharSequence> object from the parcel.
2278 public final ArrayList<CharSequence> readCharSequenceList() {
2279 ArrayList<CharSequence> array = null;
2281 int length = readInt();
2283 array = new ArrayList<CharSequence>(length);
2285 for (int i = 0 ; i < length ; i++) {
2286 array.add(readCharSequence());
2294 * Read and return a new ArrayList object from the parcel at the current
2295 * dataPosition(). Returns null if the previously written list object was
2296 * null. The given class loader will be used to load any enclosed
2299 public final ArrayList readArrayList(ClassLoader loader) {
2304 ArrayList l = new ArrayList(N);
2305 readListInternal(l, N, loader);
2310 * Read and return a new Object array from the parcel at the current
2311 * dataPosition(). Returns null if the previously written array was
2312 * null. The given class loader will be used to load any enclosed
2315 public final Object[] readArray(ClassLoader loader) {
2320 Object[] l = new Object[N];
2321 readArrayInternal(l, N, loader);
2326 * Read and return a new SparseArray object from the parcel at the current
2327 * dataPosition(). Returns null if the previously written list object was
2328 * null. The given class loader will be used to load any enclosed
2331 public final SparseArray readSparseArray(ClassLoader loader) {
2336 SparseArray sa = new SparseArray(N);
2337 readSparseArrayInternal(sa, N, loader);
2342 * Read and return a new SparseBooleanArray object from the parcel at the current
2343 * dataPosition(). Returns null if the previously written list object was
2346 public final SparseBooleanArray readSparseBooleanArray() {
2351 SparseBooleanArray sa = new SparseBooleanArray(N);
2352 readSparseBooleanArrayInternal(sa, N);
2357 * Read and return a new SparseIntArray object from the parcel at the current
2358 * dataPosition(). Returns null if the previously written array object was null.
2361 public final SparseIntArray readSparseIntArray() {
2366 SparseIntArray sa = new SparseIntArray(N);
2367 readSparseIntArrayInternal(sa, N);
2372 * Read and return a new ArrayList containing a particular object type from
2373 * the parcel that was written with {@link #writeTypedList} at the
2374 * current dataPosition(). Returns null if the
2375 * previously written list object was null. The list <em>must</em> have
2376 * previously been written via {@link #writeTypedList} with the same object
2379 * @return A newly created ArrayList containing objects with the same data
2380 * as those that were previously written.
2382 * @see #writeTypedList
2384 public final <T> ArrayList<T> createTypedArrayList(Parcelable.Creator<T> c) {
2389 ArrayList<T> l = new ArrayList<T>(N);
2391 if (readInt() != 0) {
2392 l.add(c.createFromParcel(this));
2402 * Read into the given List items containing a particular object type
2403 * that were written with {@link #writeTypedList} at the
2404 * current dataPosition(). The list <em>must</em> have
2405 * previously been written via {@link #writeTypedList} with the same object
2408 * @return A newly created ArrayList containing objects with the same data
2409 * as those that were previously written.
2411 * @see #writeTypedList
2413 public final <T> void readTypedList(List<T> list, Parcelable.Creator<T> c) {
2414 int M = list.size();
2417 for (; i < M && i < N; i++) {
2418 if (readInt() != 0) {
2419 list.set(i, c.createFromParcel(this));
2425 if (readInt() != 0) {
2426 list.add(c.createFromParcel(this));
2437 * Read and return a new ArrayList containing String objects from
2438 * the parcel that was written with {@link #writeStringList} at the
2439 * current dataPosition(). Returns null if the
2440 * previously written list object was null.
2442 * @return A newly created ArrayList containing strings with the same data
2443 * as those that were previously written.
2445 * @see #writeStringList
2447 public final ArrayList<String> createStringArrayList() {
2452 ArrayList<String> l = new ArrayList<String>(N);
2454 l.add(readString());
2461 * Read and return a new ArrayList containing IBinder objects from
2462 * the parcel that was written with {@link #writeBinderList} at the
2463 * current dataPosition(). Returns null if the
2464 * previously written list object was null.
2466 * @return A newly created ArrayList containing strings with the same data
2467 * as those that were previously written.
2469 * @see #writeBinderList
2471 public final ArrayList<IBinder> createBinderArrayList() {
2476 ArrayList<IBinder> l = new ArrayList<IBinder>(N);
2478 l.add(readStrongBinder());
2485 * Read into the given List items String objects that were written with
2486 * {@link #writeStringList} at the current dataPosition().
2488 * @return A newly created ArrayList containing strings with the same data
2489 * as those that were previously written.
2491 * @see #writeStringList
2493 public final void readStringList(List<String> list) {
2494 int M = list.size();
2497 for (; i < M && i < N; i++) {
2498 list.set(i, readString());
2501 list.add(readString());
2509 * Read into the given List items IBinder objects that were written with
2510 * {@link #writeBinderList} at the current dataPosition().
2512 * @see #writeBinderList
2514 public final void readBinderList(List<IBinder> list) {
2515 int M = list.size();
2518 for (; i < M && i < N; i++) {
2519 list.set(i, readStrongBinder());
2522 list.add(readStrongBinder());
2530 * Read the list of {@code Parcelable} objects at the current data position into the
2531 * given {@code list}. The contents of the {@code list} are replaced. If the serialized
2532 * list was {@code null}, {@code list} is cleared.
2534 * @see #writeParcelableList(List, int)
2537 public final <T extends Parcelable> List<T> readParcelableList(List<T> list, ClassLoader cl) {
2538 final int N = readInt();
2544 final int M = list.size();
2546 for (; i < M && i < N; i++) {
2547 list.set(i, (T) readParcelable(cl));
2550 list.add((T) readParcelable(cl));
2559 * Read and return a new array containing a particular object type from
2560 * the parcel at the current dataPosition(). Returns null if the
2561 * previously written array was null. The array <em>must</em> have
2562 * previously been written via {@link #writeTypedArray} with the same
2565 * @return A newly created array containing objects with the same data
2566 * as those that were previously written.
2568 * @see #writeTypedArray
2570 public final <T> T[] createTypedArray(Parcelable.Creator<T> c) {
2575 T[] l = c.newArray(N);
2576 for (int i=0; i<N; i++) {
2577 if (readInt() != 0) {
2578 l[i] = c.createFromParcel(this);
2584 public final <T> void readTypedArray(T[] val, Parcelable.Creator<T> c) {
2586 if (N == val.length) {
2587 for (int i=0; i<N; i++) {
2588 if (readInt() != 0) {
2589 val[i] = c.createFromParcel(this);
2595 throw new RuntimeException("bad array lengths");
2604 public final <T> T[] readTypedArray(Parcelable.Creator<T> c) {
2605 return createTypedArray(c);
2609 * Read and return a typed Parcelable object from a parcel.
2610 * Returns null if the previous written object was null.
2611 * The object <em>must</em> have previous been written via
2612 * {@link #writeTypedObject} with the same object type.
2614 * @return A newly created object of the type that was previously
2617 * @see #writeTypedObject
2619 public final <T> T readTypedObject(Parcelable.Creator<T> c) {
2620 if (readInt() != 0) {
2621 return c.createFromParcel(this);
2628 * Write a heterogeneous array of Parcelable objects into the Parcel.
2629 * Each object in the array is written along with its class name, so
2630 * that the correct class can later be instantiated. As a result, this
2631 * has significantly more overhead than {@link #writeTypedArray}, but will
2632 * correctly handle an array containing more than one type of object.
2634 * @param value The array of objects to be written.
2635 * @param parcelableFlags Contextual flags as per
2636 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
2638 * @see #writeTypedArray
2640 public final <T extends Parcelable> void writeParcelableArray(T[] value,
2641 int parcelableFlags) {
2642 if (value != null) {
2643 int N = value.length;
2645 for (int i=0; i<N; i++) {
2646 writeParcelable(value[i], parcelableFlags);
2654 * Read a typed object from a parcel. The given class loader will be
2655 * used to load any enclosed Parcelables. If it is null, the default class
2656 * loader will be used.
2658 public final Object readValue(ClassLoader loader) {
2659 int type = readInt();
2666 return readString();
2672 return readHashMap(loader);
2674 case VAL_PARCELABLE:
2675 return readParcelable(loader);
2678 return (short) readInt();
2687 return readDouble();
2690 return readInt() == 1;
2692 case VAL_CHARSEQUENCE:
2693 return readCharSequence();
2696 return readArrayList(loader);
2698 case VAL_BOOLEANARRAY:
2699 return createBooleanArray();
2702 return createByteArray();
2704 case VAL_STRINGARRAY:
2705 return readStringArray();
2707 case VAL_CHARSEQUENCEARRAY:
2708 return readCharSequenceArray();
2711 return readStrongBinder();
2713 case VAL_OBJECTARRAY:
2714 return readArray(loader);
2717 return createIntArray();
2720 return createLongArray();
2725 case VAL_SERIALIZABLE:
2726 return readSerializable(loader);
2728 case VAL_PARCELABLEARRAY:
2729 return readParcelableArray(loader);
2731 case VAL_SPARSEARRAY:
2732 return readSparseArray(loader);
2734 case VAL_SPARSEBOOLEANARRAY:
2735 return readSparseBooleanArray();
2738 return readBundle(loader); // loading will be deferred
2740 case VAL_PERSISTABLEBUNDLE:
2741 return readPersistableBundle(loader);
2749 case VAL_DOUBLEARRAY:
2750 return createDoubleArray();
2753 int off = dataPosition() - 4;
2754 throw new RuntimeException(
2755 "Parcel " + this + ": Unmarshalling unknown type code " + type + " at offset " + off);
2760 * Read and return a new Parcelable from the parcel. The given class loader
2761 * will be used to load any enclosed Parcelables. If it is null, the default
2762 * class loader will be used.
2763 * @param loader A ClassLoader from which to instantiate the Parcelable
2764 * object, or null for the default class loader.
2765 * @return Returns the newly created Parcelable, or null if a null
2766 * object has been written.
2767 * @throws BadParcelableException Throws BadParcelableException if there
2768 * was an error trying to instantiate the Parcelable.
2770 @SuppressWarnings("unchecked")
2771 public final <T extends Parcelable> T readParcelable(ClassLoader loader) {
2772 Parcelable.Creator<?> creator = readParcelableCreator(loader);
2773 if (creator == null) {
2776 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
2777 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2778 (Parcelable.ClassLoaderCreator<?>) creator;
2779 return (T) classLoaderCreator.createFromParcel(this, loader);
2781 return (T) creator.createFromParcel(this);
2785 @SuppressWarnings("unchecked")
2786 public final <T extends Parcelable> T readCreator(Parcelable.Creator<?> creator,
2787 ClassLoader loader) {
2788 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
2789 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2790 (Parcelable.ClassLoaderCreator<?>) creator;
2791 return (T) classLoaderCreator.createFromParcel(this, loader);
2793 return (T) creator.createFromParcel(this);
2797 public final Parcelable.Creator<?> readParcelableCreator(ClassLoader loader) {
2798 String name = readString();
2802 Parcelable.Creator<?> creator;
2803 synchronized (mCreators) {
2804 HashMap<String,Parcelable.Creator<?>> map = mCreators.get(loader);
2806 map = new HashMap<>();
2807 mCreators.put(loader, map);
2809 creator = map.get(name);
2810 if (creator == null) {
2812 // If loader == null, explicitly emulate Class.forName(String) "caller
2813 // classloader" behavior.
2814 ClassLoader parcelableClassLoader =
2815 (loader == null ? getClass().getClassLoader() : loader);
2816 // Avoid initializing the Parcelable class until we know it implements
2817 // Parcelable and has the necessary CREATOR field. http://b/1171613.
2818 Class<?> parcelableClass = Class.forName(name, false /* initialize */,
2819 parcelableClassLoader);
2820 if (!Parcelable.class.isAssignableFrom(parcelableClass)) {
2821 throw new BadParcelableException("Parcelable protocol requires that the "
2822 + "class implements Parcelable");
2824 Field f = parcelableClass.getField("CREATOR");
2825 if ((f.getModifiers() & Modifier.STATIC) == 0) {
2826 throw new BadParcelableException("Parcelable protocol requires "
2827 + "the CREATOR object to be static on class " + name);
2829 Class<?> creatorType = f.getType();
2830 if (!Parcelable.Creator.class.isAssignableFrom(creatorType)) {
2831 // Fail before calling Field.get(), not after, to avoid initializing
2832 // parcelableClass unnecessarily.
2833 throw new BadParcelableException("Parcelable protocol requires a "
2834 + "Parcelable.Creator object called "
2835 + "CREATOR on class " + name);
2837 creator = (Parcelable.Creator<?>) f.get(null);
2839 catch (IllegalAccessException e) {
2840 Log.e(TAG, "Illegal access when unmarshalling: " + name, e);
2841 throw new BadParcelableException(
2842 "IllegalAccessException when unmarshalling: " + name);
2844 catch (ClassNotFoundException e) {
2845 Log.e(TAG, "Class not found when unmarshalling: " + name, e);
2846 throw new BadParcelableException(
2847 "ClassNotFoundException when unmarshalling: " + name);
2849 catch (NoSuchFieldException e) {
2850 throw new BadParcelableException("Parcelable protocol requires a "
2851 + "Parcelable.Creator object called "
2852 + "CREATOR on class " + name);
2854 if (creator == null) {
2855 throw new BadParcelableException("Parcelable protocol requires a "
2856 + "non-null Parcelable.Creator object called "
2857 + "CREATOR on class " + name);
2860 map.put(name, creator);
2868 * Read and return a new Parcelable array from the parcel.
2869 * The given class loader will be used to load any enclosed
2871 * @return the Parcelable array, or null if the array is null
2873 public final Parcelable[] readParcelableArray(ClassLoader loader) {
2878 Parcelable[] p = new Parcelable[N];
2879 for (int i = 0; i < N; i++) {
2880 p[i] = readParcelable(loader);
2886 public final <T extends Parcelable> T[] readParcelableArray(ClassLoader loader,
2892 T[] p = (T[]) Array.newInstance(clazz, N);
2893 for (int i = 0; i < N; i++) {
2894 p[i] = readParcelable(loader);
2900 * Read and return a new Serializable object from the parcel.
2901 * @return the Serializable object, or null if the Serializable name
2902 * wasn't found in the parcel.
2904 public final Serializable readSerializable() {
2905 return readSerializable(null);
2908 private final Serializable readSerializable(final ClassLoader loader) {
2909 String name = readString();
2911 // For some reason we were unable to read the name of the Serializable (either there
2912 // is nothing left in the Parcel to read, or the next value wasn't a String), so
2913 // return null, which indicates that the name wasn't found in the parcel.
2917 byte[] serializedData = createByteArray();
2918 ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
2920 ObjectInputStream ois = new ObjectInputStream(bais) {
2922 protected Class<?> resolveClass(ObjectStreamClass osClass)
2923 throws IOException, ClassNotFoundException {
2924 // try the custom classloader if provided
2925 if (loader != null) {
2926 Class<?> c = Class.forName(osClass.getName(), false, loader);
2931 return super.resolveClass(osClass);
2934 return (Serializable) ois.readObject();
2935 } catch (IOException ioe) {
2936 throw new RuntimeException("Parcelable encountered " +
2937 "IOException reading a Serializable object (name = " + name +
2939 } catch (ClassNotFoundException cnfe) {
2940 throw new RuntimeException("Parcelable encountered " +
2941 "ClassNotFoundException reading a Serializable object (name = "
2942 + name + ")", cnfe);
2946 // Cache of previously looked up CREATOR.createFromParcel() methods for
2947 // particular classes. Keys are the names of the classes, values are
2949 private static final HashMap<ClassLoader,HashMap<String,Parcelable.Creator<?>>>
2950 mCreators = new HashMap<>();
2952 /** @hide for internal use only. */
2953 static protected final Parcel obtain(int obj) {
2954 throw new UnsupportedOperationException();
2958 static protected final Parcel obtain(long obj) {
2959 final Parcel[] pool = sHolderPool;
2960 synchronized (pool) {
2962 for (int i=0; i<POOL_SIZE; i++) {
2966 if (DEBUG_RECYCLE) {
2967 p.mStack = new RuntimeException();
2974 return new Parcel(obj);
2977 private Parcel(long nativePtr) {
2978 if (DEBUG_RECYCLE) {
2979 mStack = new RuntimeException();
2981 //Log.i(TAG, "Initializing obj=0x" + Integer.toHexString(obj), mStack);
2985 private void init(long nativePtr) {
2986 if (nativePtr != 0) {
2987 mNativePtr = nativePtr;
2988 mOwnsNativeParcelObject = false;
2990 mNativePtr = nativeCreate();
2991 mOwnsNativeParcelObject = true;
2995 private void freeBuffer() {
2996 if (mOwnsNativeParcelObject) {
2997 updateNativeSize(nativeFreeBuffer(mNativePtr));
3001 private void destroy() {
3002 if (mNativePtr != 0) {
3003 if (mOwnsNativeParcelObject) {
3004 nativeDestroy(mNativePtr);
3005 updateNativeSize(0);
3012 protected void finalize() throws Throwable {
3013 if (DEBUG_RECYCLE) {
3014 if (mStack != null) {
3015 Log.w(TAG, "Client did not call Parcel.recycle()", mStack);
3021 /* package */ void readMapInternal(Map outVal, int N,
3022 ClassLoader loader) {
3024 Object key = readValue(loader);
3025 Object value = readValue(loader);
3026 outVal.put(key, value);
3031 /* package */ void readArrayMapInternal(ArrayMap outVal, int N,
3032 ClassLoader loader) {
3033 if (DEBUG_ARRAY_MAP) {
3034 RuntimeException here = new RuntimeException("here");
3035 here.fillInStackTrace();
3036 Log.d(TAG, "Reading " + N + " ArrayMap entries", here);
3040 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
3041 String key = readString();
3042 Object value = readValue(loader);
3043 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " "
3044 + (dataPosition()-startPos) + " bytes: key=0x"
3045 + Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key);
3046 outVal.append(key, value);
3052 /* package */ void readArrayMapSafelyInternal(ArrayMap outVal, int N,
3053 ClassLoader loader) {
3054 if (DEBUG_ARRAY_MAP) {
3055 RuntimeException here = new RuntimeException("here");
3056 here.fillInStackTrace();
3057 Log.d(TAG, "Reading safely " + N + " ArrayMap entries", here);
3060 String key = readString();
3061 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read safe #" + (N-1) + ": key=0x"
3062 + (key != null ? key.hashCode() : 0) + " " + key);
3063 Object value = readValue(loader);
3064 outVal.put(key, value);
3070 * @hide For testing only.
3072 public void readArrayMap(ArrayMap outVal, ClassLoader loader) {
3073 final int N = readInt();
3077 readArrayMapInternal(outVal, N, loader);
3081 * Reads an array set.
3083 * @param loader The class loader to use.
3087 public @Nullable ArraySet<? extends Object> readArraySet(ClassLoader loader) {
3088 final int size = readInt();
3092 ArraySet<Object> result = new ArraySet<>(size);
3093 for (int i = 0; i < size; i++) {
3094 Object value = readValue(loader);
3095 result.append(value);
3100 private void readListInternal(List outVal, int N,
3101 ClassLoader loader) {
3103 Object value = readValue(loader);
3104 //Log.d(TAG, "Unmarshalling value=" + value);
3110 private void readArrayInternal(Object[] outVal, int N,
3111 ClassLoader loader) {
3112 for (int i = 0; i < N; i++) {
3113 Object value = readValue(loader);
3114 //Log.d(TAG, "Unmarshalling value=" + value);
3119 private void readSparseArrayInternal(SparseArray outVal, int N,
3120 ClassLoader loader) {
3122 int key = readInt();
3123 Object value = readValue(loader);
3124 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
3125 outVal.append(key, value);
3131 private void readSparseBooleanArrayInternal(SparseBooleanArray outVal, int N) {
3133 int key = readInt();
3134 boolean value = this.readByte() == 1;
3135 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
3136 outVal.append(key, value);
3141 private void readSparseIntArrayInternal(SparseIntArray outVal, int N) {
3143 int key = readInt();
3144 int value = readInt();
3145 outVal.append(key, value);
3153 public long getBlobAshmemSize() {
3154 return nativeGetBlobAshmemSize(mNativePtr);