blob: c1647c74f36360cbf070b2f6b49f2bbd6f87ec26 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package android.os;
18
Svet Ganovddb94882016-06-23 19:55:24 -070019import android.annotation.Nullable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.text.TextUtils;
Dianne Hackbornb87655b2013-07-17 19:06:22 -070021import android.util.ArrayMap;
Svet Ganovddb94882016-06-23 19:55:24 -070022import android.util.ArraySet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.util.Log;
Jeff Sharkey5ef33982014-09-04 18:13:39 -070024import android.util.Size;
25import android.util.SizeF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.util.SparseArray;
27import android.util.SparseBooleanArray;
Adam Lesinski4e862812016-11-21 16:02:24 -080028import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
Jeff Sharkey789a8fc2017-04-16 13:18:35 -060030import dalvik.annotation.optimization.FastNative;
31import dalvik.system.VMRuntime;
32
Jeff Sharkeye628b7d2017-01-17 13:50:20 -070033import libcore.util.SneakyThrow;
34
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import java.io.ByteArrayInputStream;
36import java.io.ByteArrayOutputStream;
37import java.io.FileDescriptor;
38import java.io.FileNotFoundException;
39import java.io.IOException;
40import java.io.ObjectInputStream;
41import java.io.ObjectOutputStream;
John Spurlock5002b8c2014-01-10 13:32:12 -050042import java.io.ObjectStreamClass;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import java.io.Serializable;
Makoto Onuki440a1ea2016-07-20 14:21:18 -070044import java.lang.reflect.Array;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import java.lang.reflect.Field;
Neil Fuller44e440c2015-04-20 14:39:00 +010046import java.lang.reflect.Modifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import java.util.ArrayList;
Elliott Hughesa28b83e2011-02-28 14:26:13 -080048import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import java.util.HashMap;
50import java.util.List;
51import java.util.Map;
52import java.util.Set;
Jeff Sharkey789a8fc2017-04-16 13:18:35 -060053import java.util.UUID;
Adrian Roos04505652015-10-22 16:12:01 -070054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055/**
56 * Container for a message (data and object references) that can
57 * be sent through an IBinder. A Parcel can contain both flattened data
58 * that will be unflattened on the other side of the IPC (using the various
59 * methods here for writing specific types, or the general
60 * {@link Parcelable} interface), and references to live {@link IBinder}
61 * objects that will result in the other side receiving a proxy IBinder
62 * connected with the original IBinder in the Parcel.
63 *
64 * <p class="note">Parcel is <strong>not</strong> a general-purpose
65 * serialization mechanism. This class (and the corresponding
66 * {@link Parcelable} API for placing arbitrary objects into a Parcel) is
67 * designed as a high-performance IPC transport. As such, it is not
68 * appropriate to place any Parcel data in to persistent storage: changes
69 * in the underlying implementation of any of the data in the Parcel can
70 * render older data unreadable.</p>
Samuel Tana8036662015-11-23 14:36:00 -080071 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 * <p>The bulk of the Parcel API revolves around reading and writing data
73 * of various types. There are six major classes of such functions available.</p>
Samuel Tana8036662015-11-23 14:36:00 -080074 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 * <h3>Primitives</h3>
Samuel Tana8036662015-11-23 14:36:00 -080076 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 * <p>The most basic data functions are for writing and reading primitive
78 * data types: {@link #writeByte}, {@link #readByte}, {@link #writeDouble},
79 * {@link #readDouble}, {@link #writeFloat}, {@link #readFloat}, {@link #writeInt},
80 * {@link #readInt}, {@link #writeLong}, {@link #readLong},
81 * {@link #writeString}, {@link #readString}. Most other
82 * data operations are built on top of these. The given data is written and
83 * read using the endianess of the host CPU.</p>
Samuel Tana8036662015-11-23 14:36:00 -080084 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 * <h3>Primitive Arrays</h3>
Samuel Tana8036662015-11-23 14:36:00 -080086 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 * <p>There are a variety of methods for reading and writing raw arrays
88 * of primitive objects, which generally result in writing a 4-byte length
89 * followed by the primitive data items. The methods for reading can either
90 * read the data into an existing array, or create and return a new array.
91 * These available types are:</p>
Samuel Tana8036662015-11-23 14:36:00 -080092 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 * <ul>
94 * <li> {@link #writeBooleanArray(boolean[])},
95 * {@link #readBooleanArray(boolean[])}, {@link #createBooleanArray()}
96 * <li> {@link #writeByteArray(byte[])},
97 * {@link #writeByteArray(byte[], int, int)}, {@link #readByteArray(byte[])},
98 * {@link #createByteArray()}
99 * <li> {@link #writeCharArray(char[])}, {@link #readCharArray(char[])},
100 * {@link #createCharArray()}
101 * <li> {@link #writeDoubleArray(double[])}, {@link #readDoubleArray(double[])},
102 * {@link #createDoubleArray()}
103 * <li> {@link #writeFloatArray(float[])}, {@link #readFloatArray(float[])},
104 * {@link #createFloatArray()}
105 * <li> {@link #writeIntArray(int[])}, {@link #readIntArray(int[])},
106 * {@link #createIntArray()}
107 * <li> {@link #writeLongArray(long[])}, {@link #readLongArray(long[])},
108 * {@link #createLongArray()}
109 * <li> {@link #writeStringArray(String[])}, {@link #readStringArray(String[])},
110 * {@link #createStringArray()}.
111 * <li> {@link #writeSparseBooleanArray(SparseBooleanArray)},
112 * {@link #readSparseBooleanArray()}.
113 * </ul>
Samuel Tana8036662015-11-23 14:36:00 -0800114 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 * <h3>Parcelables</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800116 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 * <p>The {@link Parcelable} protocol provides an extremely efficient (but
118 * low-level) protocol for objects to write and read themselves from Parcels.
119 * You can use the direct methods {@link #writeParcelable(Parcelable, int)}
120 * and {@link #readParcelable(ClassLoader)} or
121 * {@link #writeParcelableArray} and
122 * {@link #readParcelableArray(ClassLoader)} to write or read. These
123 * methods write both the class type and its data to the Parcel, allowing
124 * that class to be reconstructed from the appropriate class loader when
125 * later reading.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800126 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 * <p>There are also some methods that provide a more efficient way to work
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -0700128 * with Parcelables: {@link #writeTypedObject}, {@link #writeTypedArray},
129 * {@link #writeTypedList}, {@link #readTypedObject},
130 * {@link #createTypedArray} and {@link #createTypedArrayList}. These methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 * do not write the class information of the original object: instead, the
132 * caller of the read function must know what type to expect and pass in the
133 * appropriate {@link Parcelable.Creator Parcelable.Creator} instead to
134 * properly construct the new object and read its data. (To more efficient
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -0700135 * write and read a single Parceable object that is not null, you can directly
136 * call {@link Parcelable#writeToParcel Parcelable.writeToParcel} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 * {@link Parcelable.Creator#createFromParcel Parcelable.Creator.createFromParcel}
138 * yourself.)</p>
Samuel Tana8036662015-11-23 14:36:00 -0800139 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * <h3>Bundles</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800141 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 * <p>A special type-safe container, called {@link Bundle}, is available
143 * for key/value maps of heterogeneous values. This has many optimizations
144 * for improved performance when reading and writing data, and its type-safe
145 * API avoids difficult to debug type errors when finally marshalling the
146 * data contents into a Parcel. The methods to use are
147 * {@link #writeBundle(Bundle)}, {@link #readBundle()}, and
148 * {@link #readBundle(ClassLoader)}.
Samuel Tana8036662015-11-23 14:36:00 -0800149 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 * <h3>Active Objects</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800151 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 * <p>An unusual feature of Parcel is the ability to read and write active
153 * objects. For these objects the actual contents of the object is not
154 * written, rather a special token referencing the object is written. When
155 * reading the object back from the Parcel, you do not get a new instance of
156 * the object, but rather a handle that operates on the exact same object that
157 * was originally written. There are two forms of active objects available.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800158 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 * <p>{@link Binder} objects are a core facility of Android's general cross-process
160 * communication system. The {@link IBinder} interface describes an abstract
161 * protocol with a Binder object. Any such interface can be written in to
162 * a Parcel, and upon reading you will receive either the original object
163 * implementing that interface or a special proxy implementation
164 * that communicates calls back to the original object. The methods to use are
165 * {@link #writeStrongBinder(IBinder)},
166 * {@link #writeStrongInterface(IInterface)}, {@link #readStrongBinder()},
167 * {@link #writeBinderArray(IBinder[])}, {@link #readBinderArray(IBinder[])},
168 * {@link #createBinderArray()},
169 * {@link #writeBinderList(List)}, {@link #readBinderList(List)},
170 * {@link #createBinderArrayList()}.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800171 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 * <p>FileDescriptor objects, representing raw Linux file descriptor identifiers,
173 * can be written and {@link ParcelFileDescriptor} objects returned to operate
174 * on the original file descriptor. The returned file descriptor is a dup
175 * of the original file descriptor: the object and fd is different, but
176 * operating on the same underlying file stream, with the same position, etc.
177 * The methods to use are {@link #writeFileDescriptor(FileDescriptor)},
178 * {@link #readFileDescriptor()}.
Samuel Tana8036662015-11-23 14:36:00 -0800179 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 * <h3>Untyped Containers</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800181 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 * <p>A final class of methods are for writing and reading standard Java
183 * containers of arbitrary types. These all revolve around the
184 * {@link #writeValue(Object)} and {@link #readValue(ClassLoader)} methods
185 * which define the types of objects allowed. The container methods are
186 * {@link #writeArray(Object[])}, {@link #readArray(ClassLoader)},
187 * {@link #writeList(List)}, {@link #readList(List, ClassLoader)},
188 * {@link #readArrayList(ClassLoader)},
189 * {@link #writeMap(Map)}, {@link #readMap(Map, ClassLoader)},
190 * {@link #writeSparseArray(SparseArray)},
191 * {@link #readSparseArray(ClassLoader)}.
192 */
193public final class Parcel {
194 private static final boolean DEBUG_RECYCLE = false;
Dianne Hackborne784d1e2013-09-20 18:13:52 -0700195 private static final boolean DEBUG_ARRAY_MAP = false;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700196 private static final String TAG = "Parcel";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197
198 @SuppressWarnings({"UnusedDeclaration"})
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000199 private long mNativePtr; // used by native code
Jeff Sharkey047238c2012-03-07 16:51:38 -0800200
201 /**
202 * Flag indicating if {@link #mNativePtr} was allocated by this object,
203 * indicating that we're responsible for its lifecycle.
204 */
205 private boolean mOwnsNativeParcelObject;
Adrian Roos04505652015-10-22 16:12:01 -0700206 private long mNativeSize;
Jeff Sharkey047238c2012-03-07 16:51:38 -0800207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 private RuntimeException mStack;
209
210 private static final int POOL_SIZE = 6;
211 private static final Parcel[] sOwnedPool = new Parcel[POOL_SIZE];
212 private static final Parcel[] sHolderPool = new Parcel[POOL_SIZE];
213
Robert Quattlebaum9cd7af32017-01-04 13:28:01 -0800214 // Keep in sync with frameworks/native/include/private/binder/ParcelValTypes.h.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 private static final int VAL_NULL = -1;
216 private static final int VAL_STRING = 0;
217 private static final int VAL_INTEGER = 1;
218 private static final int VAL_MAP = 2;
219 private static final int VAL_BUNDLE = 3;
220 private static final int VAL_PARCELABLE = 4;
221 private static final int VAL_SHORT = 5;
222 private static final int VAL_LONG = 6;
223 private static final int VAL_FLOAT = 7;
224 private static final int VAL_DOUBLE = 8;
225 private static final int VAL_BOOLEAN = 9;
226 private static final int VAL_CHARSEQUENCE = 10;
227 private static final int VAL_LIST = 11;
228 private static final int VAL_SPARSEARRAY = 12;
229 private static final int VAL_BYTEARRAY = 13;
230 private static final int VAL_STRINGARRAY = 14;
231 private static final int VAL_IBINDER = 15;
232 private static final int VAL_PARCELABLEARRAY = 16;
233 private static final int VAL_OBJECTARRAY = 17;
234 private static final int VAL_INTARRAY = 18;
235 private static final int VAL_LONGARRAY = 19;
236 private static final int VAL_BYTE = 20;
237 private static final int VAL_SERIALIZABLE = 21;
238 private static final int VAL_SPARSEBOOLEANARRAY = 22;
239 private static final int VAL_BOOLEANARRAY = 23;
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000240 private static final int VAL_CHARSEQUENCEARRAY = 24;
Craig Mautner719e6b12014-04-04 20:29:41 -0700241 private static final int VAL_PERSISTABLEBUNDLE = 25;
Jeff Sharkey5ef33982014-09-04 18:13:39 -0700242 private static final int VAL_SIZE = 26;
243 private static final int VAL_SIZEF = 27;
Samuel Tana8036662015-11-23 14:36:00 -0800244 private static final int VAL_DOUBLEARRAY = 28;
Jeff Sharkey789a8fc2017-04-16 13:18:35 -0600245 private static final int VAL_UUID = 29;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700247 // The initial int32 in a Binder call's reply Parcel header:
Christopher Wiley80fd1202015-11-22 17:12:37 -0800248 // Keep these in sync with libbinder's binder/Status.h.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 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;
Dianne Hackborn7e714422013-09-13 17:32:57 -0700254 private static final int EX_NETWORK_MAIN_THREAD = -6;
Dianne Hackborn33d738a2014-09-12 14:23:58 -0700255 private static final int EX_UNSUPPORTED_OPERATION = -7;
Christopher Wiley80fd1202015-11-22 17:12:37 -0800256 private static final int EX_SERVICE_SPECIFIC = -8;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -0700257 private static final int EX_PARCELABLE = -9;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700258 private static final int EX_HAS_REPLY_HEADER = -128; // special; see below
Christopher Wiley80fd1202015-11-22 17:12:37 -0800259 // 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;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700262
John Reck71207b52016-09-28 13:28:09 -0700263 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000264 private static native int nativeDataSize(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700265 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000266 private static native int nativeDataAvail(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700267 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000268 private static native int nativeDataPosition(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700269 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000270 private static native int nativeDataCapacity(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700271 @FastNative
Adrian Roos04505652015-10-22 16:12:01 -0700272 private static native long nativeSetDataSize(long nativePtr, int size);
John Reck71207b52016-09-28 13:28:09 -0700273 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000274 private static native void nativeSetDataPosition(long nativePtr, int pos);
John Reck71207b52016-09-28 13:28:09 -0700275 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000276 private static native void nativeSetDataCapacity(long nativePtr, int size);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800277
John Reck71207b52016-09-28 13:28:09 -0700278 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000279 private static native boolean nativePushAllowFds(long nativePtr, boolean allowFds);
John Reck71207b52016-09-28 13:28:09 -0700280 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000281 private static native void nativeRestoreAllowFds(long nativePtr, boolean lastValue);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800282
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000283 private static native void nativeWriteByteArray(long nativePtr, byte[] b, int offset, int len);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700284 private static native void nativeWriteBlob(long nativePtr, byte[] b, int offset, int len);
John Reck71207b52016-09-28 13:28:09 -0700285 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000286 private static native void nativeWriteInt(long nativePtr, int val);
John Reck71207b52016-09-28 13:28:09 -0700287 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000288 private static native void nativeWriteLong(long nativePtr, long val);
John Reck71207b52016-09-28 13:28:09 -0700289 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000290 private static native void nativeWriteFloat(long nativePtr, float val);
John Reck71207b52016-09-28 13:28:09 -0700291 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000292 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);
Adrian Roos04505652015-10-22 16:12:01 -0700295 private static native long nativeWriteFileDescriptor(long nativePtr, FileDescriptor val);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800296
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000297 private static native byte[] nativeCreateByteArray(long nativePtr);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700298 private static native byte[] nativeReadBlob(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700299 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000300 private static native int nativeReadInt(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700301 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000302 private static native long nativeReadLong(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700303 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000304 private static native float nativeReadFloat(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700305 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000306 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);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800310
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000311 private static native long nativeCreate();
Adrian Roos04505652015-10-22 16:12:01 -0700312 private static native long nativeFreeBuffer(long nativePtr);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000313 private static native void nativeDestroy(long nativePtr);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800314
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000315 private static native byte[] nativeMarshall(long nativePtr);
Adrian Roos04505652015-10-22 16:12:01 -0700316 private static native long nativeUnmarshall(
John Spurlocke0852362015-02-04 15:47:40 -0500317 long nativePtr, byte[] data, int offset, int length);
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700318 private static native int nativeCompareData(long thisNativePtr, long otherNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -0700319 private static native long nativeAppendFrom(
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000320 long thisNativePtr, long otherNativePtr, int offset, int length);
John Reck71207b52016-09-28 13:28:09 -0700321 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000322 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);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800325
Dan Sandleraa861662015-04-21 10:24:32 -0400326 private static native long nativeGetBlobAshmemSize(long nativePtr);
Dan Sandler5ce04302015-04-09 23:50:15 -0400327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 public final static Parcelable.Creator<String> STRING_CREATOR
329 = new Parcelable.Creator<String>() {
330 public String createFromParcel(Parcel source) {
331 return source.readString();
332 }
333 public String[] newArray(int size) {
334 return new String[size];
335 }
336 };
337
338 /**
339 * Retrieve a new Parcel object from the pool.
340 */
341 public static Parcel obtain() {
342 final Parcel[] pool = sOwnedPool;
343 synchronized (pool) {
344 Parcel p;
345 for (int i=0; i<POOL_SIZE; i++) {
346 p = pool[i];
347 if (p != null) {
348 pool[i] = null;
349 if (DEBUG_RECYCLE) {
350 p.mStack = new RuntimeException();
351 }
352 return p;
353 }
354 }
355 }
356 return new Parcel(0);
357 }
358
359 /**
360 * Put a Parcel object back into the pool. You must not touch
361 * the object after this call.
362 */
363 public final void recycle() {
364 if (DEBUG_RECYCLE) mStack = null;
365 freeBuffer();
Jeff Sharkey047238c2012-03-07 16:51:38 -0800366
367 final Parcel[] pool;
368 if (mOwnsNativeParcelObject) {
369 pool = sOwnedPool;
370 } else {
371 mNativePtr = 0;
372 pool = sHolderPool;
373 }
374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 synchronized (pool) {
376 for (int i=0; i<POOL_SIZE; i++) {
377 if (pool[i] == null) {
378 pool[i] = this;
379 return;
380 }
381 }
382 }
383 }
384
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800385 /** @hide */
386 public static native long getGlobalAllocSize();
387
388 /** @hide */
389 public static native long getGlobalAllocCount();
390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 /**
392 * Returns the total amount of data contained in the parcel.
393 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800394 public final int dataSize() {
395 return nativeDataSize(mNativePtr);
396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397
398 /**
399 * Returns the amount of data remaining to be read from the
400 * parcel. That is, {@link #dataSize}-{@link #dataPosition}.
401 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800402 public final int dataAvail() {
403 return nativeDataAvail(mNativePtr);
404 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405
406 /**
407 * Returns the current position in the parcel data. Never
408 * more than {@link #dataSize}.
409 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800410 public final int dataPosition() {
411 return nativeDataPosition(mNativePtr);
412 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413
414 /**
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
418 * data buffer.
419 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800420 public final int dataCapacity() {
421 return nativeDataCapacity(mNativePtr);
422 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423
424 /**
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.
428 *
429 * @param size The new number of bytes in the Parcel.
430 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800431 public final void setDataSize(int size) {
Adrian Roos04505652015-10-22 16:12:01 -0700432 updateNativeSize(nativeSetDataSize(mNativePtr, size));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434
435 /**
436 * Move the current read/write position in the parcel.
437 * @param pos New offset in the parcel; must be between 0 and
438 * {@link #dataSize}.
439 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800440 public final void setDataPosition(int pos) {
441 nativeSetDataPosition(mNativePtr, pos);
442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443
444 /**
445 * Change the capacity (current available space) of the parcel.
446 *
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
449 * with this method.
450 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800451 public final void setDataCapacity(int size) {
452 nativeSetDataCapacity(mNativePtr, size);
453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400455 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800456 public final boolean pushAllowFds(boolean allowFds) {
457 return nativePushAllowFds(mNativePtr, allowFds);
458 }
Dianne Hackbornc04db7e2011-10-03 21:09:35 -0700459
460 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800461 public final void restoreAllowFds(boolean lastValue) {
462 nativeRestoreAllowFds(mNativePtr, lastValue);
463 }
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 /**
466 * Returns the raw bytes of the parcel.
467 *
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.
475 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800476 public final byte[] marshall() {
477 return nativeMarshall(mNativePtr);
478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479
480 /**
481 * Set the bytes in data to be the raw bytes of this Parcel.
482 */
John Spurlocke0852362015-02-04 15:47:40 -0500483 public final void unmarshall(byte[] data, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700484 updateNativeSize(nativeUnmarshall(mNativePtr, data, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486
Jeff Sharkey047238c2012-03-07 16:51:38 -0800487 public final void appendFrom(Parcel parcel, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700488 updateNativeSize(nativeAppendFrom(mNativePtr, parcel.mNativePtr, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700491 /** @hide */
492 public final int compareData(Parcel other) {
493 return nativeCompareData(mNativePtr, other.mNativePtr);
494 }
495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 /**
497 * Report whether the parcel contains any marshalled file descriptors.
498 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800499 public final boolean hasFileDescriptors() {
500 return nativeHasFileDescriptors(mNativePtr);
501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502
503 /**
504 * Store or read an IBinder interface token in the parcel at the current
505 * {@link #dataPosition}. This is used to validate that the marshalled
506 * transaction is intended for the target interface.
507 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800508 public final void writeInterfaceToken(String interfaceName) {
509 nativeWriteInterfaceToken(mNativePtr, interfaceName);
510 }
511
512 public final void enforceInterface(String interfaceName) {
513 nativeEnforceInterface(mNativePtr, interfaceName);
514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515
516 /**
Elliott Hughesa28b83e2011-02-28 14:26:13 -0800517 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 * growing {@link #dataCapacity} if needed.
519 * @param b Bytes to place into the parcel.
520 */
521 public final void writeByteArray(byte[] b) {
522 writeByteArray(b, 0, (b != null) ? b.length : 0);
523 }
524
525 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900526 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 * growing {@link #dataCapacity} if needed.
528 * @param b Bytes to place into the parcel.
529 * @param offset Index of first byte to be written.
530 * @param len Number of bytes to write.
531 */
532 public final void writeByteArray(byte[] b, int offset, int len) {
533 if (b == null) {
534 writeInt(-1);
535 return;
536 }
Elliott Hughesa28b83e2011-02-28 14:26:13 -0800537 Arrays.checkOffsetAndCount(b.length, offset, len);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800538 nativeWriteByteArray(mNativePtr, b, offset, len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 }
540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700542 * Write a blob of data into the parcel at the current {@link #dataPosition},
543 * growing {@link #dataCapacity} if needed.
544 * @param b Bytes to place into the parcel.
545 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -0700546 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700547 */
548 public final void writeBlob(byte[] b) {
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500549 writeBlob(b, 0, (b != null) ? b.length : 0);
550 }
551
552 /**
553 * Write a blob of data into the parcel at the current {@link #dataPosition},
554 * growing {@link #dataCapacity} if needed.
555 * @param b Bytes to place into the parcel.
556 * @param offset Index of first byte to be written.
557 * @param len Number of bytes to write.
558 * {@hide}
559 * {@SystemApi}
560 */
561 public final void writeBlob(byte[] b, int offset, int len) {
562 if (b == null) {
563 writeInt(-1);
564 return;
565 }
566 Arrays.checkOffsetAndCount(b.length, offset, len);
567 nativeWriteBlob(mNativePtr, b, offset, len);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700568 }
569
570 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 * Write an integer value into the parcel at the current dataPosition(),
572 * growing dataCapacity() if needed.
573 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800574 public final void writeInt(int val) {
575 nativeWriteInt(mNativePtr, val);
576 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577
578 /**
579 * Write a long integer value into the parcel at the current dataPosition(),
580 * growing dataCapacity() if needed.
581 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800582 public final void writeLong(long val) {
583 nativeWriteLong(mNativePtr, val);
584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585
586 /**
587 * Write a floating point value into the parcel at the current
588 * dataPosition(), growing dataCapacity() if needed.
589 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800590 public final void writeFloat(float val) {
591 nativeWriteFloat(mNativePtr, val);
592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593
594 /**
595 * Write a double precision floating point value into the parcel at the
596 * current dataPosition(), growing dataCapacity() if needed.
597 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800598 public final void writeDouble(double val) {
599 nativeWriteDouble(mNativePtr, val);
600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601
602 /**
603 * Write a string value into the parcel at the current dataPosition(),
604 * growing dataCapacity() if needed.
605 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800606 public final void writeString(String val) {
607 nativeWriteString(mNativePtr, val);
608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609
Eugene Susla36e866b2017-02-23 18:24:39 -0800610 /** @hide */
611 public final void writeBoolean(boolean val) {
612 writeInt(val ? 1 : 0);
613 }
614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000616 * Write a CharSequence value into the parcel at the current dataPosition(),
617 * growing dataCapacity() if needed.
618 * @hide
619 */
620 public final void writeCharSequence(CharSequence val) {
621 TextUtils.writeToParcel(val, this, 0);
622 }
623
624 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 * Write an object into the parcel at the current dataPosition(),
626 * growing dataCapacity() if needed.
627 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800628 public final void writeStrongBinder(IBinder val) {
629 nativeWriteStrongBinder(mNativePtr, val);
630 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631
632 /**
633 * Write an object into the parcel at the current dataPosition(),
634 * growing dataCapacity() if needed.
635 */
636 public final void writeStrongInterface(IInterface val) {
637 writeStrongBinder(val == null ? null : val.asBinder());
638 }
639
640 /**
641 * Write a FileDescriptor into the parcel at the current dataPosition(),
642 * growing dataCapacity() if needed.
Dan Egnorb3e4ef32010-07-20 09:03:35 -0700643 *
644 * <p class="caution">The file descriptor will not be closed, which may
645 * result in file descriptor leaks when objects are returned from Binder
646 * calls. Use {@link ParcelFileDescriptor#writeToParcel} instead, which
647 * accepts contextual flags and will close the original file descriptor
648 * if {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} is set.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800650 public final void writeFileDescriptor(FileDescriptor val) {
Adrian Roos04505652015-10-22 16:12:01 -0700651 updateNativeSize(nativeWriteFileDescriptor(mNativePtr, val));
652 }
653
654 private void updateNativeSize(long newNativeSize) {
655 if (mOwnsNativeParcelObject) {
656 if (newNativeSize > Integer.MAX_VALUE) {
657 newNativeSize = Integer.MAX_VALUE;
658 }
659 if (newNativeSize != mNativeSize) {
660 int delta = (int) (newNativeSize - mNativeSize);
661 if (delta > 0) {
662 VMRuntime.getRuntime().registerNativeAllocation(delta);
663 } else {
664 VMRuntime.getRuntime().registerNativeFree(-delta);
665 }
666 mNativeSize = newNativeSize;
667 }
668 }
Jeff Sharkey047238c2012-03-07 16:51:38 -0800669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670
671 /**
Casey Dahlin2f974b22015-11-05 12:19:13 -0800672 * {@hide}
673 * This will be the new name for writeFileDescriptor, for consistency.
674 **/
675 public final void writeRawFileDescriptor(FileDescriptor val) {
676 nativeWriteFileDescriptor(mNativePtr, val);
677 }
678
679 /**
680 * {@hide}
681 * Write an array of FileDescriptor objects into the Parcel.
682 *
683 * @param value The array of objects to be written.
684 */
685 public final void writeRawFileDescriptorArray(FileDescriptor[] value) {
686 if (value != null) {
687 int N = value.length;
688 writeInt(N);
689 for (int i=0; i<N; i++) {
690 writeRawFileDescriptor(value[i]);
691 }
692 } else {
693 writeInt(-1);
694 }
695 }
696
697 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900698 * Write a byte value into the parcel at the current dataPosition(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 * growing dataCapacity() if needed.
700 */
701 public final void writeByte(byte val) {
702 writeInt(val);
703 }
704
705 /**
706 * Please use {@link #writeBundle} instead. Flattens a Map into the parcel
707 * at the current dataPosition(),
708 * growing dataCapacity() if needed. The Map keys must be String objects.
709 * The Map values are written using {@link #writeValue} and must follow
710 * the specification there.
Samuel Tana8036662015-11-23 14:36:00 -0800711 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 * <p>It is strongly recommended to use {@link #writeBundle} instead of
713 * this method, since the Bundle class provides a type-safe API that
714 * allows you to avoid mysterious type errors at the point of marshalling.
715 */
716 public final void writeMap(Map val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700717 writeMapInternal((Map<String, Object>) val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 }
719
720 /**
721 * Flatten a Map into the parcel at the current dataPosition(),
722 * growing dataCapacity() if needed. The Map keys must be String objects.
723 */
Dianne Hackborn6aff9052009-05-22 13:20:23 -0700724 /* package */ void writeMapInternal(Map<String,Object> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 if (val == null) {
726 writeInt(-1);
727 return;
728 }
729 Set<Map.Entry<String,Object>> entries = val.entrySet();
730 writeInt(entries.size());
731 for (Map.Entry<String,Object> e : entries) {
732 writeValue(e.getKey());
733 writeValue(e.getValue());
734 }
735 }
736
737 /**
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700738 * Flatten an ArrayMap into the parcel at the current dataPosition(),
739 * growing dataCapacity() if needed. The Map keys must be String objects.
740 */
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700741 /* package */ void writeArrayMapInternal(ArrayMap<String, Object> val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700742 if (val == null) {
743 writeInt(-1);
744 return;
745 }
Samuel Tan3cefe6a2015-12-14 13:29:17 -0800746 // Keep the format of this Parcel in sync with writeToParcelInner() in
747 // frameworks/native/libs/binder/PersistableBundle.cpp.
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700748 final int N = val.size();
749 writeInt(N);
Dianne Hackborne784d1e2013-09-20 18:13:52 -0700750 if (DEBUG_ARRAY_MAP) {
751 RuntimeException here = new RuntimeException("here");
752 here.fillInStackTrace();
753 Log.d(TAG, "Writing " + N + " ArrayMap entries", here);
754 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700755 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700756 for (int i=0; i<N; i++) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700757 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700758 writeString(val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700759 writeValue(val.valueAt(i));
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700760 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Write #" + i + " "
761 + (dataPosition()-startPos) + " bytes: key=0x"
762 + Integer.toHexString(val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0)
763 + " " + val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700764 }
765 }
766
767 /**
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700768 * @hide For testing only.
769 */
770 public void writeArrayMap(ArrayMap<String, Object> val) {
771 writeArrayMapInternal(val);
772 }
773
774 /**
Svet Ganovddb94882016-06-23 19:55:24 -0700775 * Write an array set to the parcel.
776 *
777 * @param val The array set to write.
778 *
779 * @hide
780 */
781 public void writeArraySet(@Nullable ArraySet<? extends Object> val) {
782 final int size = (val != null) ? val.size() : -1;
783 writeInt(size);
784 for (int i = 0; i < size; i++) {
785 writeValue(val.valueAt(i));
786 }
787 }
788
789 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 * Flatten a Bundle into the parcel at the current dataPosition(),
791 * growing dataCapacity() if needed.
792 */
793 public final void writeBundle(Bundle val) {
794 if (val == null) {
795 writeInt(-1);
796 return;
797 }
798
Dianne Hackborn6aff9052009-05-22 13:20:23 -0700799 val.writeToParcel(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 }
801
802 /**
Craig Mautner719e6b12014-04-04 20:29:41 -0700803 * Flatten a PersistableBundle into the parcel at the current dataPosition(),
804 * growing dataCapacity() if needed.
805 */
806 public final void writePersistableBundle(PersistableBundle val) {
807 if (val == null) {
808 writeInt(-1);
809 return;
810 }
811
812 val.writeToParcel(this, 0);
813 }
814
815 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -0700816 * Flatten a Size into the parcel at the current dataPosition(),
817 * growing dataCapacity() if needed.
818 */
819 public final void writeSize(Size val) {
820 writeInt(val.getWidth());
821 writeInt(val.getHeight());
822 }
823
824 /**
825 * Flatten a SizeF into the parcel at the current dataPosition(),
826 * growing dataCapacity() if needed.
827 */
828 public final void writeSizeF(SizeF val) {
829 writeFloat(val.getWidth());
830 writeFloat(val.getHeight());
831 }
832
833 /**
Jeff Sharkey789a8fc2017-04-16 13:18:35 -0600834 * Flatten a UUID into the parcel at the current dataPosition(),
835 * growing dataCapacity() if needed.
836 */
837 public final void writeUuid(UUID val) {
838 writeLong(val.getMostSignificantBits());
839 writeLong(val.getLeastSignificantBits());
840 }
841
842 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 * Flatten a List into the parcel at the current dataPosition(), growing
844 * dataCapacity() if needed. The List values are written using
845 * {@link #writeValue} and must follow the specification there.
846 */
847 public final void writeList(List val) {
848 if (val == null) {
849 writeInt(-1);
850 return;
851 }
852 int N = val.size();
853 int i=0;
854 writeInt(N);
855 while (i < N) {
856 writeValue(val.get(i));
857 i++;
858 }
859 }
860
861 /**
862 * Flatten an Object array into the parcel at the current dataPosition(),
863 * growing dataCapacity() if needed. The array values are written using
864 * {@link #writeValue} and must follow the specification there.
865 */
866 public final void writeArray(Object[] val) {
867 if (val == null) {
868 writeInt(-1);
869 return;
870 }
871 int N = val.length;
872 int i=0;
873 writeInt(N);
874 while (i < N) {
875 writeValue(val[i]);
876 i++;
877 }
878 }
879
880 /**
881 * Flatten a generic SparseArray into the parcel at the current
882 * dataPosition(), growing dataCapacity() if needed. The SparseArray
883 * values are written using {@link #writeValue} and must follow the
884 * specification there.
885 */
886 public final void writeSparseArray(SparseArray<Object> val) {
887 if (val == null) {
888 writeInt(-1);
889 return;
890 }
891 int N = val.size();
892 writeInt(N);
893 int i=0;
894 while (i < N) {
895 writeInt(val.keyAt(i));
896 writeValue(val.valueAt(i));
897 i++;
898 }
899 }
900
901 public final void writeSparseBooleanArray(SparseBooleanArray val) {
902 if (val == null) {
903 writeInt(-1);
904 return;
905 }
906 int N = val.size();
907 writeInt(N);
908 int i=0;
909 while (i < N) {
910 writeInt(val.keyAt(i));
911 writeByte((byte)(val.valueAt(i) ? 1 : 0));
912 i++;
913 }
914 }
915
Adam Lesinski205656d2017-03-23 13:38:26 -0700916 /**
917 * @hide
918 */
Adam Lesinski4e862812016-11-21 16:02:24 -0800919 public final void writeSparseIntArray(SparseIntArray val) {
920 if (val == null) {
921 writeInt(-1);
922 return;
923 }
924 int N = val.size();
925 writeInt(N);
926 int i=0;
927 while (i < N) {
928 writeInt(val.keyAt(i));
929 writeInt(val.valueAt(i));
930 i++;
931 }
932 }
933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 public final void writeBooleanArray(boolean[] val) {
935 if (val != null) {
936 int N = val.length;
937 writeInt(N);
938 for (int i=0; i<N; i++) {
939 writeInt(val[i] ? 1 : 0);
940 }
941 } else {
942 writeInt(-1);
943 }
944 }
945
946 public final boolean[] createBooleanArray() {
947 int N = readInt();
948 // >>2 as a fast divide-by-4 works in the create*Array() functions
949 // because dataAvail() will never return a negative number. 4 is
950 // the size of a stored boolean in the stream.
951 if (N >= 0 && N <= (dataAvail() >> 2)) {
952 boolean[] val = new boolean[N];
953 for (int i=0; i<N; i++) {
954 val[i] = readInt() != 0;
955 }
956 return val;
957 } else {
958 return null;
959 }
960 }
961
962 public final void readBooleanArray(boolean[] val) {
963 int N = readInt();
964 if (N == val.length) {
965 for (int i=0; i<N; i++) {
966 val[i] = readInt() != 0;
967 }
968 } else {
969 throw new RuntimeException("bad array lengths");
970 }
971 }
972
973 public final void writeCharArray(char[] val) {
974 if (val != null) {
975 int N = val.length;
976 writeInt(N);
977 for (int i=0; i<N; i++) {
978 writeInt((int)val[i]);
979 }
980 } else {
981 writeInt(-1);
982 }
983 }
984
985 public final char[] createCharArray() {
986 int N = readInt();
987 if (N >= 0 && N <= (dataAvail() >> 2)) {
988 char[] val = new char[N];
989 for (int i=0; i<N; i++) {
990 val[i] = (char)readInt();
991 }
992 return val;
993 } else {
994 return null;
995 }
996 }
997
998 public final void readCharArray(char[] val) {
999 int N = readInt();
1000 if (N == val.length) {
1001 for (int i=0; i<N; i++) {
1002 val[i] = (char)readInt();
1003 }
1004 } else {
1005 throw new RuntimeException("bad array lengths");
1006 }
1007 }
1008
1009 public final void writeIntArray(int[] val) {
1010 if (val != null) {
1011 int N = val.length;
1012 writeInt(N);
1013 for (int i=0; i<N; i++) {
1014 writeInt(val[i]);
1015 }
1016 } else {
1017 writeInt(-1);
1018 }
1019 }
1020
1021 public final int[] createIntArray() {
1022 int N = readInt();
1023 if (N >= 0 && N <= (dataAvail() >> 2)) {
1024 int[] val = new int[N];
1025 for (int i=0; i<N; i++) {
1026 val[i] = readInt();
1027 }
1028 return val;
1029 } else {
1030 return null;
1031 }
1032 }
1033
1034 public final void readIntArray(int[] val) {
1035 int N = readInt();
1036 if (N == val.length) {
1037 for (int i=0; i<N; i++) {
1038 val[i] = readInt();
1039 }
1040 } else {
1041 throw new RuntimeException("bad array lengths");
1042 }
1043 }
1044
1045 public final void writeLongArray(long[] val) {
1046 if (val != null) {
1047 int N = val.length;
1048 writeInt(N);
1049 for (int i=0; i<N; i++) {
1050 writeLong(val[i]);
1051 }
1052 } else {
1053 writeInt(-1);
1054 }
1055 }
1056
1057 public final long[] createLongArray() {
1058 int N = readInt();
1059 // >>3 because stored longs are 64 bits
1060 if (N >= 0 && N <= (dataAvail() >> 3)) {
1061 long[] val = new long[N];
1062 for (int i=0; i<N; i++) {
1063 val[i] = readLong();
1064 }
1065 return val;
1066 } else {
1067 return null;
1068 }
1069 }
1070
1071 public final void readLongArray(long[] val) {
1072 int N = readInt();
1073 if (N == val.length) {
1074 for (int i=0; i<N; i++) {
1075 val[i] = readLong();
1076 }
1077 } else {
1078 throw new RuntimeException("bad array lengths");
1079 }
1080 }
1081
1082 public final void writeFloatArray(float[] val) {
1083 if (val != null) {
1084 int N = val.length;
1085 writeInt(N);
1086 for (int i=0; i<N; i++) {
1087 writeFloat(val[i]);
1088 }
1089 } else {
1090 writeInt(-1);
1091 }
1092 }
1093
1094 public final float[] createFloatArray() {
1095 int N = readInt();
1096 // >>2 because stored floats are 4 bytes
1097 if (N >= 0 && N <= (dataAvail() >> 2)) {
1098 float[] val = new float[N];
1099 for (int i=0; i<N; i++) {
1100 val[i] = readFloat();
1101 }
1102 return val;
1103 } else {
1104 return null;
1105 }
1106 }
1107
1108 public final void readFloatArray(float[] val) {
1109 int N = readInt();
1110 if (N == val.length) {
1111 for (int i=0; i<N; i++) {
1112 val[i] = readFloat();
1113 }
1114 } else {
1115 throw new RuntimeException("bad array lengths");
1116 }
1117 }
1118
1119 public final void writeDoubleArray(double[] val) {
1120 if (val != null) {
1121 int N = val.length;
1122 writeInt(N);
1123 for (int i=0; i<N; i++) {
1124 writeDouble(val[i]);
1125 }
1126 } else {
1127 writeInt(-1);
1128 }
1129 }
1130
1131 public final double[] createDoubleArray() {
1132 int N = readInt();
1133 // >>3 because stored doubles are 8 bytes
1134 if (N >= 0 && N <= (dataAvail() >> 3)) {
1135 double[] val = new double[N];
1136 for (int i=0; i<N; i++) {
1137 val[i] = readDouble();
1138 }
1139 return val;
1140 } else {
1141 return null;
1142 }
1143 }
1144
1145 public final void readDoubleArray(double[] val) {
1146 int N = readInt();
1147 if (N == val.length) {
1148 for (int i=0; i<N; i++) {
1149 val[i] = readDouble();
1150 }
1151 } else {
1152 throw new RuntimeException("bad array lengths");
1153 }
1154 }
1155
1156 public final void writeStringArray(String[] val) {
1157 if (val != null) {
1158 int N = val.length;
1159 writeInt(N);
1160 for (int i=0; i<N; i++) {
1161 writeString(val[i]);
1162 }
1163 } else {
1164 writeInt(-1);
1165 }
1166 }
1167
1168 public final String[] createStringArray() {
1169 int N = readInt();
1170 if (N >= 0) {
1171 String[] val = new String[N];
1172 for (int i=0; i<N; i++) {
1173 val[i] = readString();
1174 }
1175 return val;
1176 } else {
1177 return null;
1178 }
1179 }
1180
1181 public final void readStringArray(String[] val) {
1182 int N = readInt();
1183 if (N == val.length) {
1184 for (int i=0; i<N; i++) {
1185 val[i] = readString();
1186 }
1187 } else {
1188 throw new RuntimeException("bad array lengths");
1189 }
1190 }
1191
1192 public final void writeBinderArray(IBinder[] val) {
1193 if (val != null) {
1194 int N = val.length;
1195 writeInt(N);
1196 for (int i=0; i<N; i++) {
1197 writeStrongBinder(val[i]);
1198 }
1199 } else {
1200 writeInt(-1);
1201 }
1202 }
1203
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001204 /**
1205 * @hide
1206 */
1207 public final void writeCharSequenceArray(CharSequence[] val) {
1208 if (val != null) {
1209 int N = val.length;
1210 writeInt(N);
1211 for (int i=0; i<N; i++) {
1212 writeCharSequence(val[i]);
1213 }
1214 } else {
1215 writeInt(-1);
1216 }
1217 }
1218
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001219 /**
1220 * @hide
1221 */
1222 public final void writeCharSequenceList(ArrayList<CharSequence> val) {
1223 if (val != null) {
1224 int N = val.size();
1225 writeInt(N);
1226 for (int i=0; i<N; i++) {
1227 writeCharSequence(val.get(i));
1228 }
1229 } else {
1230 writeInt(-1);
1231 }
1232 }
1233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 public final IBinder[] createBinderArray() {
1235 int N = readInt();
1236 if (N >= 0) {
1237 IBinder[] val = new IBinder[N];
1238 for (int i=0; i<N; i++) {
1239 val[i] = readStrongBinder();
1240 }
1241 return val;
1242 } else {
1243 return null;
1244 }
1245 }
1246
1247 public final void readBinderArray(IBinder[] val) {
1248 int N = readInt();
1249 if (N == val.length) {
1250 for (int i=0; i<N; i++) {
1251 val[i] = readStrongBinder();
1252 }
1253 } else {
1254 throw new RuntimeException("bad array lengths");
1255 }
1256 }
1257
1258 /**
1259 * Flatten a List containing a particular object type into the parcel, at
1260 * the current dataPosition() and growing dataCapacity() if needed. The
1261 * type of the objects in the list must be one that implements Parcelable.
1262 * Unlike the generic writeList() method, however, only the raw data of the
1263 * objects is written and not their type, so you must use the corresponding
1264 * readTypedList() to unmarshall them.
1265 *
1266 * @param val The list of objects to be written.
1267 *
1268 * @see #createTypedArrayList
1269 * @see #readTypedList
1270 * @see Parcelable
1271 */
1272 public final <T extends Parcelable> void writeTypedList(List<T> val) {
1273 if (val == null) {
1274 writeInt(-1);
1275 return;
1276 }
1277 int N = val.size();
1278 int i=0;
1279 writeInt(N);
1280 while (i < N) {
1281 T item = val.get(i);
1282 if (item != null) {
1283 writeInt(1);
1284 item.writeToParcel(this, 0);
1285 } else {
1286 writeInt(0);
1287 }
1288 i++;
1289 }
1290 }
1291
1292 /**
1293 * Flatten a List containing String objects into the parcel, at
1294 * the current dataPosition() and growing dataCapacity() if needed. They
1295 * can later be retrieved with {@link #createStringArrayList} or
1296 * {@link #readStringList}.
1297 *
1298 * @param val The list of strings to be written.
1299 *
1300 * @see #createStringArrayList
1301 * @see #readStringList
1302 */
1303 public final void writeStringList(List<String> val) {
1304 if (val == null) {
1305 writeInt(-1);
1306 return;
1307 }
1308 int N = val.size();
1309 int i=0;
1310 writeInt(N);
1311 while (i < N) {
1312 writeString(val.get(i));
1313 i++;
1314 }
1315 }
1316
1317 /**
1318 * Flatten a List containing IBinder objects into the parcel, at
1319 * the current dataPosition() and growing dataCapacity() if needed. They
1320 * can later be retrieved with {@link #createBinderArrayList} or
1321 * {@link #readBinderList}.
1322 *
1323 * @param val The list of strings to be written.
1324 *
1325 * @see #createBinderArrayList
1326 * @see #readBinderList
1327 */
1328 public final void writeBinderList(List<IBinder> val) {
1329 if (val == null) {
1330 writeInt(-1);
1331 return;
1332 }
1333 int N = val.size();
1334 int i=0;
1335 writeInt(N);
1336 while (i < N) {
1337 writeStrongBinder(val.get(i));
1338 i++;
1339 }
1340 }
1341
1342 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00001343 * Flatten a {@code List} containing arbitrary {@code Parcelable} objects into this parcel
1344 * at the current position. They can later be retrieved using
1345 * {@link #readParcelableList(List, ClassLoader)} if required.
1346 *
1347 * @see #readParcelableList(List, ClassLoader)
1348 * @hide
1349 */
1350 public final <T extends Parcelable> void writeParcelableList(List<T> val, int flags) {
1351 if (val == null) {
1352 writeInt(-1);
1353 return;
1354 }
1355
1356 int N = val.size();
1357 int i=0;
1358 writeInt(N);
1359 while (i < N) {
1360 writeParcelable(val.get(i), flags);
1361 i++;
1362 }
1363 }
1364
1365 /**
Svet Ganov0f4928f2017-02-02 20:02:51 -08001366 * Flatten a homogeneous array containing a particular object type into
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 * the parcel, at
1368 * the current dataPosition() and growing dataCapacity() if needed. The
1369 * type of the objects in the array must be one that implements Parcelable.
1370 * Unlike the {@link #writeParcelableArray} method, however, only the
1371 * raw data of the objects is written and not their type, so you must use
1372 * {@link #readTypedArray} with the correct corresponding
1373 * {@link Parcelable.Creator} implementation to unmarshall them.
1374 *
1375 * @param val The array of objects to be written.
1376 * @param parcelableFlags Contextual flags as per
1377 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1378 *
1379 * @see #readTypedArray
1380 * @see #writeParcelableArray
1381 * @see Parcelable.Creator
1382 */
1383 public final <T extends Parcelable> void writeTypedArray(T[] val,
1384 int parcelableFlags) {
1385 if (val != null) {
1386 int N = val.length;
1387 writeInt(N);
Svet Ganov0f4928f2017-02-02 20:02:51 -08001388 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 T item = val[i];
1390 if (item != null) {
1391 writeInt(1);
1392 item.writeToParcel(this, parcelableFlags);
1393 } else {
1394 writeInt(0);
1395 }
1396 }
1397 } else {
1398 writeInt(-1);
1399 }
1400 }
1401
1402 /**
Svet Ganov0f4928f2017-02-02 20:02:51 -08001403 * Write a uniform (all items are null or the same class) array list of
1404 * parcelables.
1405 *
1406 * @param list The list to write.
1407 *
1408 * @hide
1409 */
1410 public final <T extends Parcelable> void writeTypedArrayList(@Nullable ArrayList<T> list,
1411 int parcelableFlags) {
1412 if (list != null) {
1413 int N = list.size();
1414 writeInt(N);
1415 boolean wroteCreator = false;
1416 for (int i = 0; i < N; i++) {
1417 T item = list.get(i);
1418 if (item != null) {
1419 writeInt(1);
1420 if (!wroteCreator) {
1421 writeParcelableCreator(item);
1422 wroteCreator = true;
1423 }
1424 item.writeToParcel(this, parcelableFlags);
1425 } else {
1426 writeInt(0);
1427 }
1428 }
1429 } else {
1430 writeInt(-1);
1431 }
1432 }
1433
1434 /**
1435 * Reads a uniform (all items are null or the same class) array list of
1436 * parcelables.
1437 *
1438 * @return The list or null.
1439 *
1440 * @hide
1441 */
1442 public final @Nullable <T> ArrayList<T> readTypedArrayList(@Nullable ClassLoader loader) {
1443 int N = readInt();
1444 if (N <= 0) {
1445 return null;
1446 }
1447 Parcelable.Creator<?> creator = null;
1448 ArrayList<T> result = new ArrayList<T>(N);
1449 for (int i = 0; i < N; i++) {
1450 if (readInt() != 0) {
1451 if (creator == null) {
1452 creator = readParcelableCreator(loader);
1453 if (creator == null) {
1454 return null;
1455 }
1456 }
1457 final T parcelable;
1458 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
1459 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
1460 (Parcelable.ClassLoaderCreator<?>) creator;
1461 parcelable = (T) classLoaderCreator.createFromParcel(this, loader);
1462 } else {
1463 parcelable = (T) creator.createFromParcel(this);
1464 }
1465 result.add(parcelable);
1466 } else {
1467 result.add(null);
1468 }
1469 }
1470 return result;
1471 }
1472
1473 /**
1474 * Write a uniform (all items are null or the same class) array set of
1475 * parcelables.
1476 *
1477 * @param set The set to write.
1478 *
1479 * @hide
1480 */
1481 public final <T extends Parcelable> void writeTypedArraySet(@Nullable ArraySet<T> set,
1482 int parcelableFlags) {
1483 if (set != null) {
1484 int N = set.size();
1485 writeInt(N);
1486 boolean wroteCreator = false;
1487 for (int i = 0; i < N; i++) {
1488 T item = set.valueAt(i);
1489 if (item != null) {
1490 writeInt(1);
1491 if (!wroteCreator) {
1492 writeParcelableCreator(item);
1493 wroteCreator = true;
1494 }
1495 item.writeToParcel(this, parcelableFlags);
1496 } else {
1497 writeInt(0);
1498 }
1499 }
1500 } else {
1501 writeInt(-1);
1502 }
1503 }
1504
1505 /**
1506 * Reads a uniform (all items are null or the same class) array set of
1507 * parcelables.
1508 *
1509 * @return The set or null.
1510 *
1511 * @hide
1512 */
1513 public final @Nullable <T> ArraySet<T> readTypedArraySet(@Nullable ClassLoader loader) {
1514 int N = readInt();
1515 if (N <= 0) {
1516 return null;
1517 }
1518 Parcelable.Creator<?> creator = null;
1519 ArraySet<T> result = new ArraySet<T>(N);
1520 for (int i = 0; i < N; i++) {
1521 T parcelable = null;
1522 if (readInt() != 0) {
1523 if (creator == null) {
1524 creator = readParcelableCreator(loader);
1525 if (creator == null) {
1526 return null;
1527 }
1528 }
1529 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
1530 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
1531 (Parcelable.ClassLoaderCreator<?>) creator;
1532 parcelable = (T) classLoaderCreator.createFromParcel(this, loader);
1533 } else {
1534 parcelable = (T) creator.createFromParcel(this);
1535 }
1536 }
1537 result.append(parcelable);
1538 }
1539 return result;
1540 }
1541
1542 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001543 * Flatten the Parcelable object into the parcel.
1544 *
1545 * @param val The Parcelable object to be written.
1546 * @param parcelableFlags Contextual flags as per
1547 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1548 *
1549 * @see #readTypedObject
1550 */
1551 public final <T extends Parcelable> void writeTypedObject(T val, int parcelableFlags) {
1552 if (val != null) {
1553 writeInt(1);
1554 val.writeToParcel(this, parcelableFlags);
1555 } else {
1556 writeInt(0);
1557 }
1558 }
1559
1560 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 * Flatten a generic object in to a parcel. The given Object value may
1562 * currently be one of the following types:
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001563 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 * <ul>
1565 * <li> null
1566 * <li> String
1567 * <li> Byte
1568 * <li> Short
1569 * <li> Integer
1570 * <li> Long
1571 * <li> Float
1572 * <li> Double
1573 * <li> Boolean
1574 * <li> String[]
1575 * <li> boolean[]
1576 * <li> byte[]
1577 * <li> int[]
1578 * <li> long[]
1579 * <li> Object[] (supporting objects of the same type defined here).
1580 * <li> {@link Bundle}
1581 * <li> Map (as supported by {@link #writeMap}).
1582 * <li> Any object that implements the {@link Parcelable} protocol.
1583 * <li> Parcelable[]
1584 * <li> CharSequence (as supported by {@link TextUtils#writeToParcel}).
1585 * <li> List (as supported by {@link #writeList}).
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001586 * <li> {@link SparseArray} (as supported by {@link #writeSparseArray(SparseArray)}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 * <li> {@link IBinder}
1588 * <li> Any object that implements Serializable (but see
1589 * {@link #writeSerializable} for caveats). Note that all of the
1590 * previous types have relatively efficient implementations for
1591 * writing to a Parcel; having to rely on the generic serialization
1592 * approach is much less efficient and should be avoided whenever
1593 * possible.
1594 * </ul>
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001595 *
1596 * <p class="caution">{@link Parcelable} objects are written with
1597 * {@link Parcelable#writeToParcel} using contextual flags of 0. When
1598 * serializing objects containing {@link ParcelFileDescriptor}s,
1599 * this may result in file descriptor leaks when they are returned from
1600 * Binder calls (where {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE}
1601 * should be used).</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 */
1603 public final void writeValue(Object v) {
1604 if (v == null) {
1605 writeInt(VAL_NULL);
1606 } else if (v instanceof String) {
1607 writeInt(VAL_STRING);
1608 writeString((String) v);
1609 } else if (v instanceof Integer) {
1610 writeInt(VAL_INTEGER);
1611 writeInt((Integer) v);
1612 } else if (v instanceof Map) {
1613 writeInt(VAL_MAP);
1614 writeMap((Map) v);
1615 } else if (v instanceof Bundle) {
1616 // Must be before Parcelable
1617 writeInt(VAL_BUNDLE);
1618 writeBundle((Bundle) v);
Samuel Tanceafe5e2015-12-11 16:50:58 -08001619 } else if (v instanceof PersistableBundle) {
1620 writeInt(VAL_PERSISTABLEBUNDLE);
1621 writePersistableBundle((PersistableBundle) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 } else if (v instanceof Parcelable) {
Samuel Tanceafe5e2015-12-11 16:50:58 -08001623 // IMPOTANT: cases for classes that implement Parcelable must
1624 // come before the Parcelable case, so that their specific VAL_*
1625 // types will be written.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 writeInt(VAL_PARCELABLE);
1627 writeParcelable((Parcelable) v, 0);
1628 } else if (v instanceof Short) {
1629 writeInt(VAL_SHORT);
1630 writeInt(((Short) v).intValue());
1631 } else if (v instanceof Long) {
1632 writeInt(VAL_LONG);
1633 writeLong((Long) v);
1634 } else if (v instanceof Float) {
1635 writeInt(VAL_FLOAT);
1636 writeFloat((Float) v);
1637 } else if (v instanceof Double) {
1638 writeInt(VAL_DOUBLE);
1639 writeDouble((Double) v);
1640 } else if (v instanceof Boolean) {
1641 writeInt(VAL_BOOLEAN);
1642 writeInt((Boolean) v ? 1 : 0);
1643 } else if (v instanceof CharSequence) {
1644 // Must be after String
1645 writeInt(VAL_CHARSEQUENCE);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001646 writeCharSequence((CharSequence) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 } else if (v instanceof List) {
1648 writeInt(VAL_LIST);
1649 writeList((List) v);
1650 } else if (v instanceof SparseArray) {
1651 writeInt(VAL_SPARSEARRAY);
1652 writeSparseArray((SparseArray) v);
1653 } else if (v instanceof boolean[]) {
1654 writeInt(VAL_BOOLEANARRAY);
1655 writeBooleanArray((boolean[]) v);
1656 } else if (v instanceof byte[]) {
1657 writeInt(VAL_BYTEARRAY);
1658 writeByteArray((byte[]) v);
1659 } else if (v instanceof String[]) {
1660 writeInt(VAL_STRINGARRAY);
1661 writeStringArray((String[]) v);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001662 } else if (v instanceof CharSequence[]) {
1663 // Must be after String[] and before Object[]
1664 writeInt(VAL_CHARSEQUENCEARRAY);
1665 writeCharSequenceArray((CharSequence[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 } else if (v instanceof IBinder) {
1667 writeInt(VAL_IBINDER);
1668 writeStrongBinder((IBinder) v);
1669 } else if (v instanceof Parcelable[]) {
1670 writeInt(VAL_PARCELABLEARRAY);
1671 writeParcelableArray((Parcelable[]) v, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 } else if (v instanceof int[]) {
1673 writeInt(VAL_INTARRAY);
1674 writeIntArray((int[]) v);
1675 } else if (v instanceof long[]) {
1676 writeInt(VAL_LONGARRAY);
1677 writeLongArray((long[]) v);
1678 } else if (v instanceof Byte) {
1679 writeInt(VAL_BYTE);
1680 writeInt((Byte) v);
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001681 } else if (v instanceof Size) {
1682 writeInt(VAL_SIZE);
1683 writeSize((Size) v);
1684 } else if (v instanceof SizeF) {
1685 writeInt(VAL_SIZEF);
1686 writeSizeF((SizeF) v);
Samuel Tana8036662015-11-23 14:36:00 -08001687 } else if (v instanceof double[]) {
1688 writeInt(VAL_DOUBLEARRAY);
1689 writeDoubleArray((double[]) v);
Jeff Sharkey789a8fc2017-04-16 13:18:35 -06001690 } else if (v instanceof UUID) {
1691 writeInt(VAL_UUID);
1692 writeUuid((UUID) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 } else {
Paul Duffinac5a0822014-02-03 15:02:17 +00001694 Class<?> clazz = v.getClass();
1695 if (clazz.isArray() && clazz.getComponentType() == Object.class) {
1696 // Only pure Object[] are written here, Other arrays of non-primitive types are
1697 // handled by serialization as this does not record the component type.
1698 writeInt(VAL_OBJECTARRAY);
1699 writeArray((Object[]) v);
1700 } else if (v instanceof Serializable) {
1701 // Must be last
1702 writeInt(VAL_SERIALIZABLE);
1703 writeSerializable((Serializable) v);
1704 } else {
1705 throw new RuntimeException("Parcel: unable to marshal value " + v);
1706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 }
1708 }
1709
1710 /**
1711 * Flatten the name of the class of the Parcelable and its contents
1712 * into the parcel.
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001713 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 * @param p The Parcelable object to be written.
1715 * @param parcelableFlags Contextual flags as per
1716 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1717 */
1718 public final void writeParcelable(Parcelable p, int parcelableFlags) {
1719 if (p == null) {
1720 writeString(null);
1721 return;
1722 }
Neil Fuller44e440c2015-04-20 14:39:00 +01001723 writeParcelableCreator(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 p.writeToParcel(this, parcelableFlags);
1725 }
1726
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001727 /** @hide */
1728 public final void writeParcelableCreator(Parcelable p) {
1729 String name = p.getClass().getName();
1730 writeString(name);
1731 }
1732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 /**
1734 * Write a generic serializable object in to a Parcel. It is strongly
1735 * recommended that this method be avoided, since the serialization
1736 * overhead is extremely large, and this approach will be much slower than
1737 * using the other approaches to writing data in to a Parcel.
1738 */
1739 public final void writeSerializable(Serializable s) {
1740 if (s == null) {
1741 writeString(null);
1742 return;
1743 }
1744 String name = s.getClass().getName();
1745 writeString(name);
1746
1747 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1748 try {
1749 ObjectOutputStream oos = new ObjectOutputStream(baos);
1750 oos.writeObject(s);
1751 oos.close();
1752
1753 writeByteArray(baos.toByteArray());
1754 } catch (IOException ioe) {
1755 throw new RuntimeException("Parcelable encountered " +
1756 "IOException writing serializable object (name = " + name +
1757 ")", ioe);
1758 }
1759 }
1760
1761 /**
1762 * Special function for writing an exception result at the header of
1763 * a parcel, to be used when returning an exception from a transaction.
1764 * Note that this currently only supports a few exception types; any other
1765 * exception will be re-thrown by this function as a RuntimeException
1766 * (to be caught by the system's last-resort exception handling when
1767 * dispatching a transaction).
Samuel Tana8036662015-11-23 14:36:00 -08001768 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 * <p>The supported exception types are:
1770 * <ul>
1771 * <li>{@link BadParcelableException}
1772 * <li>{@link IllegalArgumentException}
1773 * <li>{@link IllegalStateException}
1774 * <li>{@link NullPointerException}
1775 * <li>{@link SecurityException}
Dianne Hackborn7e714422013-09-13 17:32:57 -07001776 * <li>{@link NetworkOnMainThreadException}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 * </ul>
Samuel Tana8036662015-11-23 14:36:00 -08001778 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 * @param e The Exception to be written.
1780 *
1781 * @see #writeNoException
1782 * @see #readException
1783 */
1784 public final void writeException(Exception e) {
1785 int code = 0;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001786 if (e instanceof Parcelable
1787 && (e.getClass().getClassLoader() == Parcelable.class.getClassLoader())) {
1788 // We only send Parcelable exceptions that are in the
1789 // BootClassLoader to ensure that the receiver can unpack them
1790 code = EX_PARCELABLE;
1791 } else if (e instanceof SecurityException) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 code = EX_SECURITY;
1793 } else if (e instanceof BadParcelableException) {
1794 code = EX_BAD_PARCELABLE;
1795 } else if (e instanceof IllegalArgumentException) {
1796 code = EX_ILLEGAL_ARGUMENT;
1797 } else if (e instanceof NullPointerException) {
1798 code = EX_NULL_POINTER;
1799 } else if (e instanceof IllegalStateException) {
1800 code = EX_ILLEGAL_STATE;
Dianne Hackborn7e714422013-09-13 17:32:57 -07001801 } else if (e instanceof NetworkOnMainThreadException) {
1802 code = EX_NETWORK_MAIN_THREAD;
Dianne Hackborn33d738a2014-09-12 14:23:58 -07001803 } else if (e instanceof UnsupportedOperationException) {
1804 code = EX_UNSUPPORTED_OPERATION;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001805 } else if (e instanceof ServiceSpecificException) {
1806 code = EX_SERVICE_SPECIFIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 }
1808 writeInt(code);
Brad Fitzpatrick703e5d32010-07-15 13:16:41 -07001809 StrictMode.clearGatheredViolations();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 if (code == 0) {
1811 if (e instanceof RuntimeException) {
1812 throw (RuntimeException) e;
1813 }
1814 throw new RuntimeException(e);
1815 }
1816 writeString(e.getMessage());
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001817 switch (code) {
1818 case EX_SERVICE_SPECIFIC:
1819 writeInt(((ServiceSpecificException) e).errorCode);
1820 break;
1821 case EX_PARCELABLE:
1822 // Write parceled exception prefixed by length
1823 final int sizePosition = dataPosition();
1824 writeInt(0);
1825 writeParcelable((Parcelable) e, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
1826 final int payloadPosition = dataPosition();
1827 setDataPosition(sizePosition);
1828 writeInt(payloadPosition - sizePosition);
1829 setDataPosition(payloadPosition);
1830 break;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 }
1833
1834 /**
1835 * Special function for writing information at the front of the Parcel
1836 * indicating that no exception occurred.
1837 *
1838 * @see #writeException
1839 * @see #readException
1840 */
1841 public final void writeNoException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001842 // Despite the name of this function ("write no exception"),
1843 // it should instead be thought of as "write the RPC response
1844 // header", but because this function name is written out by
1845 // the AIDL compiler, we're not going to rename it.
1846 //
1847 // The response header, in the non-exception case (see also
1848 // writeException above, also called by the AIDL compiler), is
1849 // either a 0 (the default case), or EX_HAS_REPLY_HEADER if
1850 // StrictMode has gathered up violations that have occurred
1851 // during a Binder call, in which case we write out the number
1852 // of violations and their details, serialized, before the
1853 // actual RPC respons data. The receiving end of this is
1854 // readException(), below.
1855 if (StrictMode.hasGatheredViolations()) {
1856 writeInt(EX_HAS_REPLY_HEADER);
1857 final int sizePosition = dataPosition();
1858 writeInt(0); // total size of fat header, to be filled in later
1859 StrictMode.writeGatheredViolationsToParcel(this);
1860 final int payloadPosition = dataPosition();
1861 setDataPosition(sizePosition);
1862 writeInt(payloadPosition - sizePosition); // header size
1863 setDataPosition(payloadPosition);
1864 } else {
1865 writeInt(0);
1866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 }
1868
1869 /**
1870 * Special function for reading an exception result from the header of
1871 * a parcel, to be used after receiving the result of a transaction. This
1872 * will throw the exception for you if it had been written to the Parcel,
1873 * otherwise return and let you read the normal result data from the Parcel.
1874 *
1875 * @see #writeException
1876 * @see #writeNoException
1877 */
1878 public final void readException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001879 int code = readExceptionCode();
1880 if (code != 0) {
1881 String msg = readString();
1882 readException(code, msg);
1883 }
1884 }
1885
1886 /**
1887 * Parses the header of a Binder call's response Parcel and
1888 * returns the exception code. Deals with lite or fat headers.
1889 * In the common successful case, this header is generally zero.
1890 * In less common cases, it's a small negative number and will be
1891 * followed by an error string.
1892 *
1893 * This exists purely for android.database.DatabaseUtils and
1894 * insulating it from having to handle fat headers as returned by
1895 * e.g. StrictMode-induced RPC responses.
1896 *
1897 * @hide
1898 */
1899 public final int readExceptionCode() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 int code = readInt();
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001901 if (code == EX_HAS_REPLY_HEADER) {
1902 int headerSize = readInt();
1903 if (headerSize == 0) {
1904 Log.e(TAG, "Unexpected zero-sized Parcel reply header.");
1905 } else {
1906 // Currently the only thing in the header is StrictMode stacks,
1907 // but discussions around event/RPC tracing suggest we might
1908 // put that here too. If so, switch on sub-header tags here.
1909 // But for now, just parse out the StrictMode stuff.
1910 StrictMode.readAndHandleBinderCallViolations(this);
1911 }
1912 // And fat response headers are currently only used when
1913 // there are no exceptions, so return no error:
1914 return 0;
1915 }
1916 return code;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 }
1918
1919 /**
Mark Doliner879ea452014-01-02 12:38:07 -08001920 * Throw an exception with the given message. Not intended for use
1921 * outside the Parcel class.
1922 *
1923 * @param code Used to determine which exception class to throw.
1924 * @param msg The exception message.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 */
1926 public final void readException(int code, String msg) {
1927 switch (code) {
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001928 case EX_PARCELABLE:
1929 if (readInt() > 0) {
1930 SneakyThrow.sneakyThrow(
1931 (Exception) readParcelable(Parcelable.class.getClassLoader()));
1932 } else {
1933 throw new RuntimeException(msg + " [missing Parcelable]");
1934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 case EX_SECURITY:
1936 throw new SecurityException(msg);
1937 case EX_BAD_PARCELABLE:
1938 throw new BadParcelableException(msg);
1939 case EX_ILLEGAL_ARGUMENT:
1940 throw new IllegalArgumentException(msg);
1941 case EX_NULL_POINTER:
1942 throw new NullPointerException(msg);
1943 case EX_ILLEGAL_STATE:
1944 throw new IllegalStateException(msg);
Dianne Hackborn7e714422013-09-13 17:32:57 -07001945 case EX_NETWORK_MAIN_THREAD:
1946 throw new NetworkOnMainThreadException();
Dianne Hackborn33d738a2014-09-12 14:23:58 -07001947 case EX_UNSUPPORTED_OPERATION:
1948 throw new UnsupportedOperationException(msg);
Christopher Wiley80fd1202015-11-22 17:12:37 -08001949 case EX_SERVICE_SPECIFIC:
1950 throw new ServiceSpecificException(readInt(), msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 }
1952 throw new RuntimeException("Unknown exception code: " + code
1953 + " msg " + msg);
1954 }
1955
1956 /**
1957 * Read an integer value from the parcel at the current dataPosition().
1958 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001959 public final int readInt() {
1960 return nativeReadInt(mNativePtr);
1961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962
1963 /**
1964 * Read a long integer value from the parcel at the current dataPosition().
1965 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001966 public final long readLong() {
1967 return nativeReadLong(mNativePtr);
1968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969
1970 /**
1971 * Read a floating point value from the parcel at the current
1972 * dataPosition().
1973 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001974 public final float readFloat() {
1975 return nativeReadFloat(mNativePtr);
1976 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977
1978 /**
1979 * Read a double precision floating point value from the parcel at the
1980 * current dataPosition().
1981 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001982 public final double readDouble() {
1983 return nativeReadDouble(mNativePtr);
1984 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985
1986 /**
1987 * Read a string value from the parcel at the current dataPosition().
1988 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001989 public final String readString() {
1990 return nativeReadString(mNativePtr);
1991 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992
Eugene Susla36e866b2017-02-23 18:24:39 -08001993 /** @hide */
1994 public final boolean readBoolean() {
1995 return readInt() != 0;
1996 }
1997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001999 * Read a CharSequence value from the parcel at the current dataPosition().
2000 * @hide
2001 */
2002 public final CharSequence readCharSequence() {
2003 return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(this);
2004 }
2005
2006 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 * Read an object from the parcel at the current dataPosition().
2008 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002009 public final IBinder readStrongBinder() {
2010 return nativeReadStrongBinder(mNativePtr);
2011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012
2013 /**
2014 * Read a FileDescriptor from the parcel at the current dataPosition().
2015 */
2016 public final ParcelFileDescriptor readFileDescriptor() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002017 FileDescriptor fd = nativeReadFileDescriptor(mNativePtr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 return fd != null ? new ParcelFileDescriptor(fd) : null;
2019 }
2020
Jeff Sharkeyda5a3e12013-08-11 12:54:42 -07002021 /** {@hide} */
2022 public final FileDescriptor readRawFileDescriptor() {
2023 return nativeReadFileDescriptor(mNativePtr);
2024 }
2025
Casey Dahlin2f974b22015-11-05 12:19:13 -08002026 /**
2027 * {@hide}
2028 * Read and return a new array of FileDescriptors from the parcel.
2029 * @return the FileDescriptor array, or null if the array is null.
2030 **/
2031 public final FileDescriptor[] createRawFileDescriptorArray() {
2032 int N = readInt();
2033 if (N < 0) {
2034 return null;
2035 }
2036 FileDescriptor[] f = new FileDescriptor[N];
2037 for (int i = 0; i < N; i++) {
2038 f[i] = readRawFileDescriptor();
2039 }
2040 return f;
2041 }
2042
2043 /**
2044 * {@hide}
2045 * Read an array of FileDescriptors from a parcel.
2046 * The passed array must be exactly the length of the array in the parcel.
2047 * @return the FileDescriptor array, or null if the array is null.
2048 **/
2049 public final void readRawFileDescriptorArray(FileDescriptor[] val) {
2050 int N = readInt();
2051 if (N == val.length) {
2052 for (int i=0; i<N; i++) {
2053 val[i] = readRawFileDescriptor();
2054 }
2055 } else {
2056 throw new RuntimeException("bad array lengths");
2057 }
2058 }
2059
Jeff Sharkeye53e2d92017-03-25 23:14:06 -06002060 /** @deprecated use {@link android.system.Os#open(String, int, int)} */
2061 @Deprecated
2062 static native FileDescriptor openFileDescriptor(String file, int mode)
2063 throws FileNotFoundException;
Casey Dahlin2f974b22015-11-05 12:19:13 -08002064
Jeff Sharkeye53e2d92017-03-25 23:14:06 -06002065 /** @deprecated use {@link android.system.Os#dup(FileDescriptor)} */
2066 @Deprecated
2067 static native FileDescriptor dupFileDescriptor(FileDescriptor orig) throws IOException;
2068
2069 /** @deprecated use {@link android.system.Os#close(FileDescriptor)} */
2070 @Deprecated
2071 static native void closeFileDescriptor(FileDescriptor desc) throws IOException;
2072
2073 static native void clearFileDescriptor(FileDescriptor desc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074
2075 /**
2076 * Read a byte value from the parcel at the current dataPosition().
2077 */
2078 public final byte readByte() {
2079 return (byte)(readInt() & 0xff);
2080 }
2081
2082 /**
2083 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2084 * been written with {@link #writeBundle}. Read into an existing Map object
2085 * from the parcel at the current dataPosition().
2086 */
2087 public final void readMap(Map outVal, ClassLoader loader) {
2088 int N = readInt();
2089 readMapInternal(outVal, N, loader);
2090 }
2091
2092 /**
2093 * Read into an existing List object from the parcel at the current
2094 * dataPosition(), using the given class loader to load any enclosed
2095 * Parcelables. If it is null, the default class loader is used.
2096 */
2097 public final void readList(List outVal, ClassLoader loader) {
2098 int N = readInt();
2099 readListInternal(outVal, N, loader);
2100 }
2101
2102 /**
2103 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2104 * been written with {@link #writeBundle}. Read and return a new HashMap
2105 * object from the parcel at the current dataPosition(), using the given
2106 * class loader to load any enclosed Parcelables. Returns null if
2107 * the previously written map object was null.
2108 */
2109 public final HashMap readHashMap(ClassLoader loader)
2110 {
2111 int N = readInt();
2112 if (N < 0) {
2113 return null;
2114 }
2115 HashMap m = new HashMap(N);
2116 readMapInternal(m, N, loader);
2117 return m;
2118 }
2119
2120 /**
2121 * Read and return a new Bundle object from the parcel at the current
2122 * dataPosition(). Returns null if the previously written Bundle object was
2123 * null.
2124 */
2125 public final Bundle readBundle() {
2126 return readBundle(null);
2127 }
2128
2129 /**
2130 * Read and return a new Bundle object from the parcel at the current
2131 * dataPosition(), using the given class loader to initialize the class
2132 * loader of the Bundle for later retrieval of Parcelable objects.
2133 * Returns null if the previously written Bundle object was null.
2134 */
2135 public final Bundle readBundle(ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 int length = readInt();
2137 if (length < 0) {
Dianne Hackborn4a7d8242013-10-03 10:19:20 -07002138 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 return null;
2140 }
Samuel Tana8036662015-11-23 14:36:00 -08002141
Dianne Hackborn6aff9052009-05-22 13:20:23 -07002142 final Bundle bundle = new Bundle(this, length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 if (loader != null) {
2144 bundle.setClassLoader(loader);
2145 }
2146 return bundle;
2147 }
2148
2149 /**
Craig Mautner719e6b12014-04-04 20:29:41 -07002150 * Read and return a new Bundle object from the parcel at the current
2151 * dataPosition(). Returns null if the previously written Bundle object was
2152 * null.
2153 */
2154 public final PersistableBundle readPersistableBundle() {
2155 return readPersistableBundle(null);
2156 }
2157
2158 /**
2159 * Read and return a new Bundle object from the parcel at the current
2160 * dataPosition(), using the given class loader to initialize the class
2161 * loader of the Bundle for later retrieval of Parcelable objects.
2162 * Returns null if the previously written Bundle object was null.
2163 */
2164 public final PersistableBundle readPersistableBundle(ClassLoader loader) {
2165 int length = readInt();
2166 if (length < 0) {
2167 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
2168 return null;
2169 }
2170
2171 final PersistableBundle bundle = new PersistableBundle(this, length);
2172 if (loader != null) {
2173 bundle.setClassLoader(loader);
2174 }
2175 return bundle;
2176 }
2177
2178 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002179 * Read a Size from the parcel at the current dataPosition().
2180 */
2181 public final Size readSize() {
2182 final int width = readInt();
2183 final int height = readInt();
2184 return new Size(width, height);
2185 }
2186
2187 /**
2188 * Read a SizeF from the parcel at the current dataPosition().
2189 */
2190 public final SizeF readSizeF() {
2191 final float width = readFloat();
2192 final float height = readFloat();
2193 return new SizeF(width, height);
2194 }
2195
2196 /**
Jeff Sharkey789a8fc2017-04-16 13:18:35 -06002197 * Read a UUID from the parcel at the current dataPosition().
2198 */
2199 public final UUID readUuid() {
2200 return new UUID(readLong(), readLong());
2201 }
2202
2203 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 * Read and return a byte[] object from the parcel.
2205 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002206 public final byte[] createByteArray() {
2207 return nativeCreateByteArray(mNativePtr);
2208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209
2210 /**
2211 * Read a byte[] object from the parcel and copy it into the
2212 * given byte array.
2213 */
2214 public final void readByteArray(byte[] val) {
2215 // TODO: make this a native method to avoid the extra copy.
2216 byte[] ba = createByteArray();
2217 if (ba.length == val.length) {
2218 System.arraycopy(ba, 0, val, 0, ba.length);
2219 } else {
2220 throw new RuntimeException("bad array lengths");
2221 }
2222 }
2223
2224 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002225 * Read a blob of data from the parcel and return it as a byte array.
2226 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -07002227 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002228 */
2229 public final byte[] readBlob() {
2230 return nativeReadBlob(mNativePtr);
2231 }
2232
2233 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 * Read and return a String[] object from the parcel.
2235 * {@hide}
2236 */
2237 public final String[] readStringArray() {
2238 String[] array = null;
2239
2240 int length = readInt();
2241 if (length >= 0)
2242 {
2243 array = new String[length];
2244
2245 for (int i = 0 ; i < length ; i++)
2246 {
2247 array[i] = readString();
2248 }
2249 }
2250
2251 return array;
2252 }
2253
2254 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002255 * Read and return a CharSequence[] object from the parcel.
2256 * {@hide}
2257 */
2258 public final CharSequence[] readCharSequenceArray() {
2259 CharSequence[] array = null;
2260
2261 int length = readInt();
2262 if (length >= 0)
2263 {
2264 array = new CharSequence[length];
2265
2266 for (int i = 0 ; i < length ; i++)
2267 {
2268 array[i] = readCharSequence();
2269 }
2270 }
2271
2272 return array;
2273 }
2274
2275 /**
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002276 * Read and return an ArrayList&lt;CharSequence&gt; object from the parcel.
2277 * {@hide}
2278 */
2279 public final ArrayList<CharSequence> readCharSequenceList() {
2280 ArrayList<CharSequence> array = null;
2281
2282 int length = readInt();
2283 if (length >= 0) {
2284 array = new ArrayList<CharSequence>(length);
2285
2286 for (int i = 0 ; i < length ; i++) {
2287 array.add(readCharSequence());
2288 }
2289 }
2290
2291 return array;
2292 }
2293
2294 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 * Read and return a new ArrayList object from the parcel at the current
2296 * dataPosition(). Returns null if the previously written list object was
2297 * null. The given class loader will be used to load any enclosed
2298 * Parcelables.
2299 */
2300 public final ArrayList readArrayList(ClassLoader loader) {
2301 int N = readInt();
2302 if (N < 0) {
2303 return null;
2304 }
2305 ArrayList l = new ArrayList(N);
2306 readListInternal(l, N, loader);
2307 return l;
2308 }
2309
2310 /**
2311 * Read and return a new Object array from the parcel at the current
2312 * dataPosition(). Returns null if the previously written array was
2313 * null. The given class loader will be used to load any enclosed
2314 * Parcelables.
2315 */
2316 public final Object[] readArray(ClassLoader loader) {
2317 int N = readInt();
2318 if (N < 0) {
2319 return null;
2320 }
2321 Object[] l = new Object[N];
2322 readArrayInternal(l, N, loader);
2323 return l;
2324 }
2325
2326 /**
2327 * Read and return a new SparseArray object from the parcel at the current
2328 * dataPosition(). Returns null if the previously written list object was
2329 * null. The given class loader will be used to load any enclosed
2330 * Parcelables.
2331 */
2332 public final SparseArray readSparseArray(ClassLoader loader) {
2333 int N = readInt();
2334 if (N < 0) {
2335 return null;
2336 }
2337 SparseArray sa = new SparseArray(N);
2338 readSparseArrayInternal(sa, N, loader);
2339 return sa;
2340 }
2341
2342 /**
2343 * Read and return a new SparseBooleanArray object from the parcel at the current
2344 * dataPosition(). Returns null if the previously written list object was
2345 * null.
2346 */
2347 public final SparseBooleanArray readSparseBooleanArray() {
2348 int N = readInt();
2349 if (N < 0) {
2350 return null;
2351 }
2352 SparseBooleanArray sa = new SparseBooleanArray(N);
2353 readSparseBooleanArrayInternal(sa, N);
2354 return sa;
2355 }
2356
2357 /**
Adam Lesinski4e862812016-11-21 16:02:24 -08002358 * Read and return a new SparseIntArray object from the parcel at the current
2359 * dataPosition(). Returns null if the previously written array object was null.
Adam Lesinski205656d2017-03-23 13:38:26 -07002360 * @hide
Adam Lesinski4e862812016-11-21 16:02:24 -08002361 */
2362 public final SparseIntArray readSparseIntArray() {
2363 int N = readInt();
2364 if (N < 0) {
2365 return null;
2366 }
2367 SparseIntArray sa = new SparseIntArray(N);
2368 readSparseIntArrayInternal(sa, N);
2369 return sa;
2370 }
2371
2372 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 * Read and return a new ArrayList containing a particular object type from
2374 * the parcel that was written with {@link #writeTypedList} at the
2375 * current dataPosition(). Returns null if the
2376 * previously written list object was null. The list <em>must</em> have
2377 * previously been written via {@link #writeTypedList} with the same object
2378 * type.
2379 *
2380 * @return A newly created ArrayList containing objects with the same data
2381 * as those that were previously written.
2382 *
2383 * @see #writeTypedList
2384 */
2385 public final <T> ArrayList<T> createTypedArrayList(Parcelable.Creator<T> c) {
2386 int N = readInt();
2387 if (N < 0) {
2388 return null;
2389 }
2390 ArrayList<T> l = new ArrayList<T>(N);
2391 while (N > 0) {
2392 if (readInt() != 0) {
2393 l.add(c.createFromParcel(this));
2394 } else {
2395 l.add(null);
2396 }
2397 N--;
2398 }
2399 return l;
2400 }
2401
2402 /**
2403 * Read into the given List items containing a particular object type
2404 * that were written with {@link #writeTypedList} at the
2405 * current dataPosition(). The list <em>must</em> have
2406 * previously been written via {@link #writeTypedList} with the same object
2407 * type.
2408 *
2409 * @return A newly created ArrayList containing objects with the same data
2410 * as those that were previously written.
2411 *
2412 * @see #writeTypedList
2413 */
2414 public final <T> void readTypedList(List<T> list, Parcelable.Creator<T> c) {
2415 int M = list.size();
2416 int N = readInt();
2417 int i = 0;
2418 for (; i < M && i < N; i++) {
2419 if (readInt() != 0) {
2420 list.set(i, c.createFromParcel(this));
2421 } else {
2422 list.set(i, null);
2423 }
2424 }
2425 for (; i<N; i++) {
2426 if (readInt() != 0) {
2427 list.add(c.createFromParcel(this));
2428 } else {
2429 list.add(null);
2430 }
2431 }
2432 for (; i<M; i++) {
2433 list.remove(N);
2434 }
2435 }
2436
2437 /**
2438 * Read and return a new ArrayList containing String objects from
2439 * the parcel that was written with {@link #writeStringList} at the
2440 * current dataPosition(). Returns null if the
2441 * previously written list object was null.
2442 *
2443 * @return A newly created ArrayList containing strings with the same data
2444 * as those that were previously written.
2445 *
2446 * @see #writeStringList
2447 */
2448 public final ArrayList<String> createStringArrayList() {
2449 int N = readInt();
2450 if (N < 0) {
2451 return null;
2452 }
2453 ArrayList<String> l = new ArrayList<String>(N);
2454 while (N > 0) {
2455 l.add(readString());
2456 N--;
2457 }
2458 return l;
2459 }
2460
2461 /**
2462 * Read and return a new ArrayList containing IBinder objects from
2463 * the parcel that was written with {@link #writeBinderList} at the
2464 * current dataPosition(). Returns null if the
2465 * previously written list object was null.
2466 *
2467 * @return A newly created ArrayList containing strings with the same data
2468 * as those that were previously written.
2469 *
2470 * @see #writeBinderList
2471 */
2472 public final ArrayList<IBinder> createBinderArrayList() {
2473 int N = readInt();
2474 if (N < 0) {
2475 return null;
2476 }
2477 ArrayList<IBinder> l = new ArrayList<IBinder>(N);
2478 while (N > 0) {
2479 l.add(readStrongBinder());
2480 N--;
2481 }
2482 return l;
2483 }
2484
2485 /**
2486 * Read into the given List items String objects that were written with
2487 * {@link #writeStringList} at the current dataPosition().
2488 *
2489 * @return A newly created ArrayList containing strings with the same data
2490 * as those that were previously written.
2491 *
2492 * @see #writeStringList
2493 */
2494 public final void readStringList(List<String> list) {
2495 int M = list.size();
2496 int N = readInt();
2497 int i = 0;
2498 for (; i < M && i < N; i++) {
2499 list.set(i, readString());
2500 }
2501 for (; i<N; i++) {
2502 list.add(readString());
2503 }
2504 for (; i<M; i++) {
2505 list.remove(N);
2506 }
2507 }
2508
2509 /**
2510 * Read into the given List items IBinder objects that were written with
2511 * {@link #writeBinderList} at the current dataPosition().
2512 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 * @see #writeBinderList
2514 */
2515 public final void readBinderList(List<IBinder> list) {
2516 int M = list.size();
2517 int N = readInt();
2518 int i = 0;
2519 for (; i < M && i < N; i++) {
2520 list.set(i, readStrongBinder());
2521 }
2522 for (; i<N; i++) {
2523 list.add(readStrongBinder());
2524 }
2525 for (; i<M; i++) {
2526 list.remove(N);
2527 }
2528 }
2529
2530 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00002531 * Read the list of {@code Parcelable} objects at the current data position into the
2532 * given {@code list}. The contents of the {@code list} are replaced. If the serialized
2533 * list was {@code null}, {@code list} is cleared.
2534 *
2535 * @see #writeParcelableList(List, int)
2536 * @hide
2537 */
Eugene Susla36e866b2017-02-23 18:24:39 -08002538 public final <T extends Parcelable> List<T> readParcelableList(List<T> list, ClassLoader cl) {
Narayan Kamathbea48712016-12-01 15:38:28 +00002539 final int N = readInt();
2540 if (N == -1) {
2541 list.clear();
Eugene Susla36e866b2017-02-23 18:24:39 -08002542 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002543 }
2544
2545 final int M = list.size();
2546 int i = 0;
2547 for (; i < M && i < N; i++) {
2548 list.set(i, (T) readParcelable(cl));
2549 }
2550 for (; i<N; i++) {
2551 list.add((T) readParcelable(cl));
2552 }
2553 for (; i<M; i++) {
2554 list.remove(N);
2555 }
Eugene Susla36e866b2017-02-23 18:24:39 -08002556 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002557 }
2558
2559 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 * Read and return a new array containing a particular object type from
2561 * the parcel at the current dataPosition(). Returns null if the
2562 * previously written array was null. The array <em>must</em> have
2563 * previously been written via {@link #writeTypedArray} with the same
2564 * object type.
2565 *
2566 * @return A newly created array containing objects with the same data
2567 * as those that were previously written.
2568 *
2569 * @see #writeTypedArray
2570 */
2571 public final <T> T[] createTypedArray(Parcelable.Creator<T> c) {
2572 int N = readInt();
2573 if (N < 0) {
2574 return null;
2575 }
2576 T[] l = c.newArray(N);
2577 for (int i=0; i<N; i++) {
2578 if (readInt() != 0) {
2579 l[i] = c.createFromParcel(this);
2580 }
2581 }
2582 return l;
2583 }
2584
2585 public final <T> void readTypedArray(T[] val, Parcelable.Creator<T> c) {
2586 int N = readInt();
2587 if (N == val.length) {
2588 for (int i=0; i<N; i++) {
2589 if (readInt() != 0) {
2590 val[i] = c.createFromParcel(this);
2591 } else {
2592 val[i] = null;
2593 }
2594 }
2595 } else {
2596 throw new RuntimeException("bad array lengths");
2597 }
2598 }
2599
2600 /**
2601 * @deprecated
2602 * @hide
2603 */
2604 @Deprecated
2605 public final <T> T[] readTypedArray(Parcelable.Creator<T> c) {
2606 return createTypedArray(c);
2607 }
2608
2609 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002610 * Read and return a typed Parcelable object from a parcel.
2611 * Returns null if the previous written object was null.
2612 * The object <em>must</em> have previous been written via
2613 * {@link #writeTypedObject} with the same object type.
2614 *
2615 * @return A newly created object of the type that was previously
2616 * written.
2617 *
2618 * @see #writeTypedObject
2619 */
2620 public final <T> T readTypedObject(Parcelable.Creator<T> c) {
2621 if (readInt() != 0) {
2622 return c.createFromParcel(this);
2623 } else {
2624 return null;
2625 }
2626 }
2627
2628 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 * Write a heterogeneous array of Parcelable objects into the Parcel.
2630 * Each object in the array is written along with its class name, so
2631 * that the correct class can later be instantiated. As a result, this
2632 * has significantly more overhead than {@link #writeTypedArray}, but will
2633 * correctly handle an array containing more than one type of object.
2634 *
2635 * @param value The array of objects to be written.
2636 * @param parcelableFlags Contextual flags as per
2637 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
2638 *
2639 * @see #writeTypedArray
2640 */
2641 public final <T extends Parcelable> void writeParcelableArray(T[] value,
2642 int parcelableFlags) {
2643 if (value != null) {
2644 int N = value.length;
2645 writeInt(N);
2646 for (int i=0; i<N; i++) {
2647 writeParcelable(value[i], parcelableFlags);
2648 }
2649 } else {
2650 writeInt(-1);
2651 }
2652 }
2653
2654 /**
2655 * Read a typed object from a parcel. The given class loader will be
2656 * used to load any enclosed Parcelables. If it is null, the default class
2657 * loader will be used.
2658 */
2659 public final Object readValue(ClassLoader loader) {
2660 int type = readInt();
2661
2662 switch (type) {
2663 case VAL_NULL:
2664 return null;
2665
2666 case VAL_STRING:
2667 return readString();
2668
2669 case VAL_INTEGER:
2670 return readInt();
2671
2672 case VAL_MAP:
2673 return readHashMap(loader);
2674
2675 case VAL_PARCELABLE:
2676 return readParcelable(loader);
2677
2678 case VAL_SHORT:
2679 return (short) readInt();
2680
2681 case VAL_LONG:
2682 return readLong();
2683
2684 case VAL_FLOAT:
2685 return readFloat();
2686
2687 case VAL_DOUBLE:
2688 return readDouble();
2689
2690 case VAL_BOOLEAN:
2691 return readInt() == 1;
2692
2693 case VAL_CHARSEQUENCE:
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002694 return readCharSequence();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695
2696 case VAL_LIST:
2697 return readArrayList(loader);
2698
2699 case VAL_BOOLEANARRAY:
Samuel Tana8036662015-11-23 14:36:00 -08002700 return createBooleanArray();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701
2702 case VAL_BYTEARRAY:
2703 return createByteArray();
2704
2705 case VAL_STRINGARRAY:
2706 return readStringArray();
2707
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002708 case VAL_CHARSEQUENCEARRAY:
2709 return readCharSequenceArray();
2710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 case VAL_IBINDER:
2712 return readStrongBinder();
2713
2714 case VAL_OBJECTARRAY:
2715 return readArray(loader);
2716
2717 case VAL_INTARRAY:
2718 return createIntArray();
2719
2720 case VAL_LONGARRAY:
2721 return createLongArray();
2722
2723 case VAL_BYTE:
2724 return readByte();
2725
2726 case VAL_SERIALIZABLE:
John Spurlock5002b8c2014-01-10 13:32:12 -05002727 return readSerializable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728
2729 case VAL_PARCELABLEARRAY:
2730 return readParcelableArray(loader);
2731
2732 case VAL_SPARSEARRAY:
2733 return readSparseArray(loader);
2734
2735 case VAL_SPARSEBOOLEANARRAY:
2736 return readSparseBooleanArray();
2737
2738 case VAL_BUNDLE:
2739 return readBundle(loader); // loading will be deferred
2740
Craig Mautner719e6b12014-04-04 20:29:41 -07002741 case VAL_PERSISTABLEBUNDLE:
2742 return readPersistableBundle(loader);
2743
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002744 case VAL_SIZE:
2745 return readSize();
2746
2747 case VAL_SIZEF:
2748 return readSizeF();
2749
Samuel Tana8036662015-11-23 14:36:00 -08002750 case VAL_DOUBLEARRAY:
2751 return createDoubleArray();
2752
Jeff Sharkey789a8fc2017-04-16 13:18:35 -06002753 case VAL_UUID:
2754 return readUuid();
2755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 default:
2757 int off = dataPosition() - 4;
2758 throw new RuntimeException(
2759 "Parcel " + this + ": Unmarshalling unknown type code " + type + " at offset " + off);
2760 }
2761 }
2762
2763 /**
2764 * Read and return a new Parcelable from the parcel. The given class loader
2765 * will be used to load any enclosed Parcelables. If it is null, the default
2766 * class loader will be used.
2767 * @param loader A ClassLoader from which to instantiate the Parcelable
2768 * object, or null for the default class loader.
2769 * @return Returns the newly created Parcelable, or null if a null
2770 * object has been written.
2771 * @throws BadParcelableException Throws BadParcelableException if there
2772 * was an error trying to instantiate the Parcelable.
2773 */
Neil Fuller44e440c2015-04-20 14:39:00 +01002774 @SuppressWarnings("unchecked")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 public final <T extends Parcelable> T readParcelable(ClassLoader loader) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002776 Parcelable.Creator<?> creator = readParcelableCreator(loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002777 if (creator == null) {
2778 return null;
2779 }
2780 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002781 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2782 (Parcelable.ClassLoaderCreator<?>) creator;
2783 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002784 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002785 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002786 }
2787
2788 /** @hide */
Neil Fuller44e440c2015-04-20 14:39:00 +01002789 @SuppressWarnings("unchecked")
2790 public final <T extends Parcelable> T readCreator(Parcelable.Creator<?> creator,
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002791 ClassLoader loader) {
2792 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002793 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2794 (Parcelable.ClassLoaderCreator<?>) creator;
2795 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002796 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002797 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002798 }
2799
2800 /** @hide */
Neil Fuller44e440c2015-04-20 14:39:00 +01002801 public final Parcelable.Creator<?> readParcelableCreator(ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 String name = readString();
2803 if (name == null) {
2804 return null;
2805 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002806 Parcelable.Creator<?> creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 synchronized (mCreators) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002808 HashMap<String,Parcelable.Creator<?>> map = mCreators.get(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 if (map == null) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002810 map = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 mCreators.put(loader, map);
2812 }
2813 creator = map.get(name);
2814 if (creator == null) {
2815 try {
Neil Fuller44e440c2015-04-20 14:39:00 +01002816 // If loader == null, explicitly emulate Class.forName(String) "caller
2817 // classloader" behavior.
2818 ClassLoader parcelableClassLoader =
2819 (loader == null ? getClass().getClassLoader() : loader);
2820 // Avoid initializing the Parcelable class until we know it implements
2821 // Parcelable and has the necessary CREATOR field. http://b/1171613.
2822 Class<?> parcelableClass = Class.forName(name, false /* initialize */,
2823 parcelableClassLoader);
2824 if (!Parcelable.class.isAssignableFrom(parcelableClass)) {
2825 throw new BadParcelableException("Parcelable protocol requires that the "
2826 + "class implements Parcelable");
2827 }
2828 Field f = parcelableClass.getField("CREATOR");
2829 if ((f.getModifiers() & Modifier.STATIC) == 0) {
2830 throw new BadParcelableException("Parcelable protocol requires "
2831 + "the CREATOR object to be static on class " + name);
2832 }
2833 Class<?> creatorType = f.getType();
2834 if (!Parcelable.Creator.class.isAssignableFrom(creatorType)) {
2835 // Fail before calling Field.get(), not after, to avoid initializing
2836 // parcelableClass unnecessarily.
2837 throw new BadParcelableException("Parcelable protocol requires a "
2838 + "Parcelable.Creator object called "
2839 + "CREATOR on class " + name);
2840 }
2841 creator = (Parcelable.Creator<?>) f.get(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 }
2843 catch (IllegalAccessException e) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002844 Log.e(TAG, "Illegal access when unmarshalling: " + name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 throw new BadParcelableException(
2846 "IllegalAccessException when unmarshalling: " + name);
2847 }
2848 catch (ClassNotFoundException e) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002849 Log.e(TAG, "Class not found when unmarshalling: " + name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 throw new BadParcelableException(
2851 "ClassNotFoundException when unmarshalling: " + name);
2852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 catch (NoSuchFieldException e) {
2854 throw new BadParcelableException("Parcelable protocol requires a "
Neil Fuller44e440c2015-04-20 14:39:00 +01002855 + "Parcelable.Creator object called "
2856 + "CREATOR on class " + name);
Irfan Sheriff97a72f62013-01-11 10:45:51 -08002857 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 if (creator == null) {
2859 throw new BadParcelableException("Parcelable protocol requires a "
Neil Fuller44e440c2015-04-20 14:39:00 +01002860 + "non-null Parcelable.Creator object called "
2861 + "CREATOR on class " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 }
2863
2864 map.put(name, creator);
2865 }
2866 }
2867
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002868 return creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 }
2870
2871 /**
2872 * Read and return a new Parcelable array from the parcel.
2873 * The given class loader will be used to load any enclosed
2874 * Parcelables.
2875 * @return the Parcelable array, or null if the array is null
2876 */
2877 public final Parcelable[] readParcelableArray(ClassLoader loader) {
2878 int N = readInt();
2879 if (N < 0) {
2880 return null;
2881 }
2882 Parcelable[] p = new Parcelable[N];
2883 for (int i = 0; i < N; i++) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002884 p[i] = readParcelable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 }
2886 return p;
2887 }
2888
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002889 /** @hide */
2890 public final <T extends Parcelable> T[] readParcelableArray(ClassLoader loader,
2891 Class<T> clazz) {
2892 int N = readInt();
2893 if (N < 0) {
2894 return null;
2895 }
2896 T[] p = (T[]) Array.newInstance(clazz, N);
2897 for (int i = 0; i < N; i++) {
2898 p[i] = readParcelable(loader);
2899 }
2900 return p;
2901 }
2902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 /**
2904 * Read and return a new Serializable object from the parcel.
2905 * @return the Serializable object, or null if the Serializable name
2906 * wasn't found in the parcel.
2907 */
2908 public final Serializable readSerializable() {
John Spurlock5002b8c2014-01-10 13:32:12 -05002909 return readSerializable(null);
2910 }
2911
2912 private final Serializable readSerializable(final ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 String name = readString();
2914 if (name == null) {
2915 // For some reason we were unable to read the name of the Serializable (either there
2916 // is nothing left in the Parcel to read, or the next value wasn't a String), so
2917 // return null, which indicates that the name wasn't found in the parcel.
2918 return null;
2919 }
2920
2921 byte[] serializedData = createByteArray();
2922 ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
2923 try {
John Spurlock5002b8c2014-01-10 13:32:12 -05002924 ObjectInputStream ois = new ObjectInputStream(bais) {
2925 @Override
2926 protected Class<?> resolveClass(ObjectStreamClass osClass)
2927 throws IOException, ClassNotFoundException {
2928 // try the custom classloader if provided
2929 if (loader != null) {
2930 Class<?> c = Class.forName(osClass.getName(), false, loader);
2931 if (c != null) {
2932 return c;
2933 }
2934 }
2935 return super.resolveClass(osClass);
2936 }
2937 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 return (Serializable) ois.readObject();
2939 } catch (IOException ioe) {
2940 throw new RuntimeException("Parcelable encountered " +
2941 "IOException reading a Serializable object (name = " + name +
2942 ")", ioe);
2943 } catch (ClassNotFoundException cnfe) {
John Spurlock5002b8c2014-01-10 13:32:12 -05002944 throw new RuntimeException("Parcelable encountered " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 "ClassNotFoundException reading a Serializable object (name = "
2946 + name + ")", cnfe);
2947 }
2948 }
2949
2950 // Cache of previously looked up CREATOR.createFromParcel() methods for
2951 // particular classes. Keys are the names of the classes, values are
2952 // Method objects.
Neil Fuller44e440c2015-04-20 14:39:00 +01002953 private static final HashMap<ClassLoader,HashMap<String,Parcelable.Creator<?>>>
2954 mCreators = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955
Narayan Kamathb34a4612014-01-23 14:17:11 +00002956 /** @hide for internal use only. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 static protected final Parcel obtain(int obj) {
Ashok Bhat8ab665d2014-01-22 16:00:20 +00002958 throw new UnsupportedOperationException();
2959 }
2960
2961 /** @hide */
2962 static protected final Parcel obtain(long obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 final Parcel[] pool = sHolderPool;
2964 synchronized (pool) {
2965 Parcel p;
2966 for (int i=0; i<POOL_SIZE; i++) {
2967 p = pool[i];
2968 if (p != null) {
2969 pool[i] = null;
2970 if (DEBUG_RECYCLE) {
2971 p.mStack = new RuntimeException();
2972 }
2973 p.init(obj);
2974 return p;
2975 }
2976 }
2977 }
2978 return new Parcel(obj);
2979 }
2980
Ashok Bhat8ab665d2014-01-22 16:00:20 +00002981 private Parcel(long nativePtr) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 if (DEBUG_RECYCLE) {
2983 mStack = new RuntimeException();
2984 }
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002985 //Log.i(TAG, "Initializing obj=0x" + Integer.toHexString(obj), mStack);
Jeff Sharkey047238c2012-03-07 16:51:38 -08002986 init(nativePtr);
2987 }
2988
Ashok Bhat8ab665d2014-01-22 16:00:20 +00002989 private void init(long nativePtr) {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002990 if (nativePtr != 0) {
2991 mNativePtr = nativePtr;
2992 mOwnsNativeParcelObject = false;
2993 } else {
2994 mNativePtr = nativeCreate();
2995 mOwnsNativeParcelObject = true;
2996 }
2997 }
2998
2999 private void freeBuffer() {
3000 if (mOwnsNativeParcelObject) {
Adrian Roos04505652015-10-22 16:12:01 -07003001 updateNativeSize(nativeFreeBuffer(mNativePtr));
Jeff Sharkey047238c2012-03-07 16:51:38 -08003002 }
3003 }
3004
3005 private void destroy() {
3006 if (mNativePtr != 0) {
3007 if (mOwnsNativeParcelObject) {
3008 nativeDestroy(mNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -07003009 updateNativeSize(0);
Jeff Sharkey047238c2012-03-07 16:51:38 -08003010 }
3011 mNativePtr = 0;
3012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 }
3014
3015 @Override
3016 protected void finalize() throws Throwable {
3017 if (DEBUG_RECYCLE) {
3018 if (mStack != null) {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003019 Log.w(TAG, "Client did not call Parcel.recycle()", mStack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 }
3021 }
3022 destroy();
3023 }
3024
Dianne Hackborn6aff9052009-05-22 13:20:23 -07003025 /* package */ void readMapInternal(Map outVal, int N,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 ClassLoader loader) {
3027 while (N > 0) {
3028 Object key = readValue(loader);
3029 Object value = readValue(loader);
3030 outVal.put(key, value);
3031 N--;
3032 }
3033 }
3034
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003035 /* package */ void readArrayMapInternal(ArrayMap outVal, int N,
3036 ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003037 if (DEBUG_ARRAY_MAP) {
3038 RuntimeException here = new RuntimeException("here");
3039 here.fillInStackTrace();
3040 Log.d(TAG, "Reading " + N + " ArrayMap entries", here);
3041 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003042 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003043 while (N > 0) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003044 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003045 String key = readString();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003046 Object value = readValue(loader);
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003047 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " "
3048 + (dataPosition()-startPos) + " bytes: key=0x"
3049 + Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key);
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003050 outVal.append(key, value);
3051 N--;
3052 }
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003053 outVal.validate();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003054 }
3055
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003056 /* package */ void readArrayMapSafelyInternal(ArrayMap outVal, int N,
3057 ClassLoader loader) {
3058 if (DEBUG_ARRAY_MAP) {
3059 RuntimeException here = new RuntimeException("here");
3060 here.fillInStackTrace();
3061 Log.d(TAG, "Reading safely " + N + " ArrayMap entries", here);
3062 }
3063 while (N > 0) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003064 String key = readString();
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003065 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read safe #" + (N-1) + ": key=0x"
3066 + (key != null ? key.hashCode() : 0) + " " + key);
3067 Object value = readValue(loader);
3068 outVal.put(key, value);
3069 N--;
3070 }
3071 }
3072
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003073 /**
3074 * @hide For testing only.
3075 */
3076 public void readArrayMap(ArrayMap outVal, ClassLoader loader) {
3077 final int N = readInt();
3078 if (N < 0) {
3079 return;
3080 }
3081 readArrayMapInternal(outVal, N, loader);
3082 }
3083
Svet Ganovddb94882016-06-23 19:55:24 -07003084 /**
3085 * Reads an array set.
3086 *
3087 * @param loader The class loader to use.
3088 *
3089 * @hide
3090 */
3091 public @Nullable ArraySet<? extends Object> readArraySet(ClassLoader loader) {
3092 final int size = readInt();
3093 if (size < 0) {
3094 return null;
3095 }
3096 ArraySet<Object> result = new ArraySet<>(size);
3097 for (int i = 0; i < size; i++) {
3098 Object value = readValue(loader);
3099 result.append(value);
3100 }
3101 return result;
3102 }
3103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 private void readListInternal(List outVal, int N,
3105 ClassLoader loader) {
3106 while (N > 0) {
3107 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003108 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 outVal.add(value);
3110 N--;
3111 }
3112 }
3113
3114 private void readArrayInternal(Object[] outVal, int N,
3115 ClassLoader loader) {
3116 for (int i = 0; i < N; i++) {
3117 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003118 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119 outVal[i] = value;
3120 }
3121 }
3122
3123 private void readSparseArrayInternal(SparseArray outVal, int N,
3124 ClassLoader loader) {
3125 while (N > 0) {
3126 int key = readInt();
3127 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003128 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003129 outVal.append(key, value);
3130 N--;
3131 }
3132 }
3133
3134
3135 private void readSparseBooleanArrayInternal(SparseBooleanArray outVal, int N) {
3136 while (N > 0) {
3137 int key = readInt();
3138 boolean value = this.readByte() == 1;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003139 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003140 outVal.append(key, value);
3141 N--;
3142 }
3143 }
Dan Sandler5ce04302015-04-09 23:50:15 -04003144
Adam Lesinski4e862812016-11-21 16:02:24 -08003145 private void readSparseIntArrayInternal(SparseIntArray outVal, int N) {
3146 while (N > 0) {
3147 int key = readInt();
3148 int value = readInt();
3149 outVal.append(key, value);
3150 N--;
3151 }
3152 }
3153
Dan Sandler5ce04302015-04-09 23:50:15 -04003154 /**
3155 * @hide For testing
3156 */
3157 public long getBlobAshmemSize() {
3158 return nativeGetBlobAshmemSize(mNativePtr);
3159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003160}