blob: b9cdcc096962964fe46f133ea562f9cd8ce026c6 [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
Jake Whartonb1f474c2018-05-30 15:11:13 -040019import android.annotation.NonNull;
Svet Ganovddb94882016-06-23 19:55:24 -070020import android.annotation.Nullable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.text.TextUtils;
Dianne Hackbornb87655b2013-07-17 19:06:22 -070022import android.util.ArrayMap;
Svet Ganovddb94882016-06-23 19:55:24 -070023import android.util.ArraySet;
Fyodor Kupolov3b946f42017-11-27 10:40:46 -080024import android.util.ExceptionUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.util.Log;
Jeff Sharkey5ef33982014-09-04 18:13:39 -070026import android.util.Size;
27import android.util.SizeF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.util.SparseArray;
29import android.util.SparseBooleanArray;
Adam Lesinski4e862812016-11-21 16:02:24 -080030import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
Makoto Onukib148b6c2017-06-27 13:38:38 -070032import dalvik.annotation.optimization.CriticalNative;
Jeff Sharkey789a8fc2017-04-16 13:18:35 -060033import dalvik.annotation.optimization.FastNative;
34import dalvik.system.VMRuntime;
35
Pete Gillin60f55a252018-05-10 15:40:32 +010036import libcore.util.ArrayUtils;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -070037import libcore.util.SneakyThrow;
38
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import java.io.ByteArrayInputStream;
40import java.io.ByteArrayOutputStream;
41import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import java.io.IOException;
43import java.io.ObjectInputStream;
44import java.io.ObjectOutputStream;
John Spurlock5002b8c2014-01-10 13:32:12 -050045import java.io.ObjectStreamClass;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import java.io.Serializable;
Makoto Onuki440a1ea2016-07-20 14:21:18 -070047import java.lang.reflect.Array;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import java.lang.reflect.Field;
Neil Fuller44e440c2015-04-20 14:39:00 +010049import java.lang.reflect.Modifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import java.util.ArrayList;
51import java.util.HashMap;
52import java.util.List;
53import java.util.Map;
54import java.util.Set;
Adrian Roos04505652015-10-22 16:12:01 -070055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056/**
57 * Container for a message (data and object references) that can
58 * be sent through an IBinder. A Parcel can contain both flattened data
59 * that will be unflattened on the other side of the IPC (using the various
60 * methods here for writing specific types, or the general
61 * {@link Parcelable} interface), and references to live {@link IBinder}
62 * objects that will result in the other side receiving a proxy IBinder
63 * connected with the original IBinder in the Parcel.
64 *
65 * <p class="note">Parcel is <strong>not</strong> a general-purpose
66 * serialization mechanism. This class (and the corresponding
67 * {@link Parcelable} API for placing arbitrary objects into a Parcel) is
68 * designed as a high-performance IPC transport. As such, it is not
69 * appropriate to place any Parcel data in to persistent storage: changes
70 * in the underlying implementation of any of the data in the Parcel can
71 * render older data unreadable.</p>
Samuel Tana8036662015-11-23 14:36:00 -080072 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 * <p>The bulk of the Parcel API revolves around reading and writing data
74 * of various types. There are six major classes of such functions available.</p>
Samuel Tana8036662015-11-23 14:36:00 -080075 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 * <h3>Primitives</h3>
Samuel Tana8036662015-11-23 14:36:00 -080077 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 * <p>The most basic data functions are for writing and reading primitive
79 * data types: {@link #writeByte}, {@link #readByte}, {@link #writeDouble},
80 * {@link #readDouble}, {@link #writeFloat}, {@link #readFloat}, {@link #writeInt},
81 * {@link #readInt}, {@link #writeLong}, {@link #readLong},
82 * {@link #writeString}, {@link #readString}. Most other
83 * data operations are built on top of these. The given data is written and
84 * read using the endianess of the host CPU.</p>
Samuel Tana8036662015-11-23 14:36:00 -080085 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 * <h3>Primitive Arrays</h3>
Samuel Tana8036662015-11-23 14:36:00 -080087 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 * <p>There are a variety of methods for reading and writing raw arrays
89 * of primitive objects, which generally result in writing a 4-byte length
90 * followed by the primitive data items. The methods for reading can either
91 * read the data into an existing array, or create and return a new array.
92 * These available types are:</p>
Samuel Tana8036662015-11-23 14:36:00 -080093 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * <ul>
95 * <li> {@link #writeBooleanArray(boolean[])},
96 * {@link #readBooleanArray(boolean[])}, {@link #createBooleanArray()}
97 * <li> {@link #writeByteArray(byte[])},
98 * {@link #writeByteArray(byte[], int, int)}, {@link #readByteArray(byte[])},
99 * {@link #createByteArray()}
100 * <li> {@link #writeCharArray(char[])}, {@link #readCharArray(char[])},
101 * {@link #createCharArray()}
102 * <li> {@link #writeDoubleArray(double[])}, {@link #readDoubleArray(double[])},
103 * {@link #createDoubleArray()}
104 * <li> {@link #writeFloatArray(float[])}, {@link #readFloatArray(float[])},
105 * {@link #createFloatArray()}
106 * <li> {@link #writeIntArray(int[])}, {@link #readIntArray(int[])},
107 * {@link #createIntArray()}
108 * <li> {@link #writeLongArray(long[])}, {@link #readLongArray(long[])},
109 * {@link #createLongArray()}
110 * <li> {@link #writeStringArray(String[])}, {@link #readStringArray(String[])},
111 * {@link #createStringArray()}.
112 * <li> {@link #writeSparseBooleanArray(SparseBooleanArray)},
113 * {@link #readSparseBooleanArray()}.
114 * </ul>
Samuel Tana8036662015-11-23 14:36:00 -0800115 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 * <h3>Parcelables</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800117 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 * <p>The {@link Parcelable} protocol provides an extremely efficient (but
119 * low-level) protocol for objects to write and read themselves from Parcels.
120 * You can use the direct methods {@link #writeParcelable(Parcelable, int)}
121 * and {@link #readParcelable(ClassLoader)} or
122 * {@link #writeParcelableArray} and
123 * {@link #readParcelableArray(ClassLoader)} to write or read. These
124 * methods write both the class type and its data to the Parcel, allowing
125 * that class to be reconstructed from the appropriate class loader when
126 * later reading.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800127 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 * <p>There are also some methods that provide a more efficient way to work
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -0700129 * with Parcelables: {@link #writeTypedObject}, {@link #writeTypedArray},
130 * {@link #writeTypedList}, {@link #readTypedObject},
131 * {@link #createTypedArray} and {@link #createTypedArrayList}. These methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 * do not write the class information of the original object: instead, the
133 * caller of the read function must know what type to expect and pass in the
134 * appropriate {@link Parcelable.Creator Parcelable.Creator} instead to
135 * properly construct the new object and read its data. (To more efficient
Bin Chenb6b12b52017-07-11 11:01:44 +0800136 * write and read a single Parcelable object that is not null, you can directly
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -0700137 * call {@link Parcelable#writeToParcel Parcelable.writeToParcel} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * {@link Parcelable.Creator#createFromParcel Parcelable.Creator.createFromParcel}
139 * yourself.)</p>
Samuel Tana8036662015-11-23 14:36:00 -0800140 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 * <h3>Bundles</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800142 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 * <p>A special type-safe container, called {@link Bundle}, is available
144 * for key/value maps of heterogeneous values. This has many optimizations
145 * for improved performance when reading and writing data, and its type-safe
146 * API avoids difficult to debug type errors when finally marshalling the
147 * data contents into a Parcel. The methods to use are
148 * {@link #writeBundle(Bundle)}, {@link #readBundle()}, and
149 * {@link #readBundle(ClassLoader)}.
Samuel Tana8036662015-11-23 14:36:00 -0800150 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 * <h3>Active Objects</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800152 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 * <p>An unusual feature of Parcel is the ability to read and write active
154 * objects. For these objects the actual contents of the object is not
155 * written, rather a special token referencing the object is written. When
156 * reading the object back from the Parcel, you do not get a new instance of
157 * the object, but rather a handle that operates on the exact same object that
158 * was originally written. There are two forms of active objects available.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800159 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 * <p>{@link Binder} objects are a core facility of Android's general cross-process
161 * communication system. The {@link IBinder} interface describes an abstract
162 * protocol with a Binder object. Any such interface can be written in to
163 * a Parcel, and upon reading you will receive either the original object
164 * implementing that interface or a special proxy implementation
165 * that communicates calls back to the original object. The methods to use are
166 * {@link #writeStrongBinder(IBinder)},
167 * {@link #writeStrongInterface(IInterface)}, {@link #readStrongBinder()},
168 * {@link #writeBinderArray(IBinder[])}, {@link #readBinderArray(IBinder[])},
169 * {@link #createBinderArray()},
170 * {@link #writeBinderList(List)}, {@link #readBinderList(List)},
171 * {@link #createBinderArrayList()}.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800172 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 * <p>FileDescriptor objects, representing raw Linux file descriptor identifiers,
174 * can be written and {@link ParcelFileDescriptor} objects returned to operate
175 * on the original file descriptor. The returned file descriptor is a dup
176 * of the original file descriptor: the object and fd is different, but
177 * operating on the same underlying file stream, with the same position, etc.
178 * The methods to use are {@link #writeFileDescriptor(FileDescriptor)},
179 * {@link #readFileDescriptor()}.
Samuel Tana8036662015-11-23 14:36:00 -0800180 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <h3>Untyped Containers</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800182 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 * <p>A final class of methods are for writing and reading standard Java
184 * containers of arbitrary types. These all revolve around the
185 * {@link #writeValue(Object)} and {@link #readValue(ClassLoader)} methods
186 * which define the types of objects allowed. The container methods are
187 * {@link #writeArray(Object[])}, {@link #readArray(ClassLoader)},
188 * {@link #writeList(List)}, {@link #readList(List, ClassLoader)},
189 * {@link #readArrayList(ClassLoader)},
190 * {@link #writeMap(Map)}, {@link #readMap(Map, ClassLoader)},
191 * {@link #writeSparseArray(SparseArray)},
192 * {@link #readSparseArray(ClassLoader)}.
193 */
194public final class Parcel {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -0800195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 private static final boolean DEBUG_RECYCLE = false;
Dianne Hackborne784d1e2013-09-20 18:13:52 -0700197 private static final boolean DEBUG_ARRAY_MAP = false;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700198 private static final String TAG = "Parcel";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199
200 @SuppressWarnings({"UnusedDeclaration"})
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000201 private long mNativePtr; // used by native code
Jeff Sharkey047238c2012-03-07 16:51:38 -0800202
203 /**
204 * Flag indicating if {@link #mNativePtr} was allocated by this object,
205 * indicating that we're responsible for its lifecycle.
206 */
207 private boolean mOwnsNativeParcelObject;
Adrian Roos04505652015-10-22 16:12:01 -0700208 private long mNativeSize;
Jeff Sharkey047238c2012-03-07 16:51:38 -0800209
Dianne Hackborn98305522017-05-05 17:53:53 -0700210 private ArrayMap<Class, Object> mClassCookies;
211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 private RuntimeException mStack;
213
Fyodor Kupolova81b8c02017-11-13 15:51:03 -0800214 /**
215 * Whether or not to parcel the stack trace of an exception. This has a performance
216 * impact, so should only be included in specific processes and only on debug builds.
217 */
218 private static boolean sParcelExceptionStackTrace;
219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 private static final int POOL_SIZE = 6;
221 private static final Parcel[] sOwnedPool = new Parcel[POOL_SIZE];
222 private static final Parcel[] sHolderPool = new Parcel[POOL_SIZE];
223
Robert Quattlebaum9cd7af32017-01-04 13:28:01 -0800224 // Keep in sync with frameworks/native/include/private/binder/ParcelValTypes.h.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 private static final int VAL_NULL = -1;
226 private static final int VAL_STRING = 0;
227 private static final int VAL_INTEGER = 1;
228 private static final int VAL_MAP = 2;
229 private static final int VAL_BUNDLE = 3;
230 private static final int VAL_PARCELABLE = 4;
231 private static final int VAL_SHORT = 5;
232 private static final int VAL_LONG = 6;
233 private static final int VAL_FLOAT = 7;
234 private static final int VAL_DOUBLE = 8;
235 private static final int VAL_BOOLEAN = 9;
236 private static final int VAL_CHARSEQUENCE = 10;
237 private static final int VAL_LIST = 11;
238 private static final int VAL_SPARSEARRAY = 12;
239 private static final int VAL_BYTEARRAY = 13;
240 private static final int VAL_STRINGARRAY = 14;
241 private static final int VAL_IBINDER = 15;
242 private static final int VAL_PARCELABLEARRAY = 16;
243 private static final int VAL_OBJECTARRAY = 17;
244 private static final int VAL_INTARRAY = 18;
245 private static final int VAL_LONGARRAY = 19;
246 private static final int VAL_BYTE = 20;
247 private static final int VAL_SERIALIZABLE = 21;
248 private static final int VAL_SPARSEBOOLEANARRAY = 22;
249 private static final int VAL_BOOLEANARRAY = 23;
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000250 private static final int VAL_CHARSEQUENCEARRAY = 24;
Craig Mautner719e6b12014-04-04 20:29:41 -0700251 private static final int VAL_PERSISTABLEBUNDLE = 25;
Jeff Sharkey5ef33982014-09-04 18:13:39 -0700252 private static final int VAL_SIZE = 26;
253 private static final int VAL_SIZEF = 27;
Samuel Tana8036662015-11-23 14:36:00 -0800254 private static final int VAL_DOUBLEARRAY = 28;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700256 // The initial int32 in a Binder call's reply Parcel header:
Christopher Wiley80fd1202015-11-22 17:12:37 -0800257 // Keep these in sync with libbinder's binder/Status.h.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 private static final int EX_SECURITY = -1;
259 private static final int EX_BAD_PARCELABLE = -2;
260 private static final int EX_ILLEGAL_ARGUMENT = -3;
261 private static final int EX_NULL_POINTER = -4;
262 private static final int EX_ILLEGAL_STATE = -5;
Dianne Hackborn7e714422013-09-13 17:32:57 -0700263 private static final int EX_NETWORK_MAIN_THREAD = -6;
Dianne Hackborn33d738a2014-09-12 14:23:58 -0700264 private static final int EX_UNSUPPORTED_OPERATION = -7;
Christopher Wiley80fd1202015-11-22 17:12:37 -0800265 private static final int EX_SERVICE_SPECIFIC = -8;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -0700266 private static final int EX_PARCELABLE = -9;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700267 private static final int EX_HAS_REPLY_HEADER = -128; // special; see below
Christopher Wiley80fd1202015-11-22 17:12:37 -0800268 // EX_TRANSACTION_FAILED is used exclusively in native code.
269 // see libbinder's binder/Status.h
270 private static final int EX_TRANSACTION_FAILED = -129;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700271
Makoto Onukib148b6c2017-06-27 13:38:38 -0700272 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000273 private static native int nativeDataSize(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700274 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000275 private static native int nativeDataAvail(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700276 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000277 private static native int nativeDataPosition(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700278 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000279 private static native int nativeDataCapacity(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700280 @FastNative
Adrian Roos04505652015-10-22 16:12:01 -0700281 private static native long nativeSetDataSize(long nativePtr, int size);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700282 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000283 private static native void nativeSetDataPosition(long nativePtr, int pos);
John Reck71207b52016-09-28 13:28:09 -0700284 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000285 private static native void nativeSetDataCapacity(long nativePtr, int size);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800286
Makoto Onukib148b6c2017-06-27 13:38:38 -0700287 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000288 private static native boolean nativePushAllowFds(long nativePtr, boolean allowFds);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700289 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000290 private static native void nativeRestoreAllowFds(long nativePtr, boolean lastValue);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800291
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000292 private static native void nativeWriteByteArray(long nativePtr, byte[] b, int offset, int len);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700293 private static native void nativeWriteBlob(long nativePtr, byte[] b, int offset, int len);
John Reck71207b52016-09-28 13:28:09 -0700294 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000295 private static native void nativeWriteInt(long nativePtr, int val);
John Reck71207b52016-09-28 13:28:09 -0700296 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000297 private static native void nativeWriteLong(long nativePtr, long val);
John Reck71207b52016-09-28 13:28:09 -0700298 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000299 private static native void nativeWriteFloat(long nativePtr, float val);
John Reck71207b52016-09-28 13:28:09 -0700300 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000301 private static native void nativeWriteDouble(long nativePtr, double val);
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700302 static native void nativeWriteString(long nativePtr, String val);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000303 private static native void nativeWriteStrongBinder(long nativePtr, IBinder val);
Adrian Roos04505652015-10-22 16:12:01 -0700304 private static native long nativeWriteFileDescriptor(long nativePtr, FileDescriptor val);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800305
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000306 private static native byte[] nativeCreateByteArray(long nativePtr);
Jocelyn Dang46100442017-05-05 15:40:49 -0700307 private static native boolean nativeReadByteArray(long nativePtr, byte[] dest, int destLen);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700308 private static native byte[] nativeReadBlob(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700309 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000310 private static native int nativeReadInt(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700311 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000312 private static native long nativeReadLong(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700313 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000314 private static native float nativeReadFloat(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700315 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000316 private static native double nativeReadDouble(long nativePtr);
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700317 static native String nativeReadString(long nativePtr);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000318 private static native IBinder nativeReadStrongBinder(long nativePtr);
319 private static native FileDescriptor nativeReadFileDescriptor(long nativePtr);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800320
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000321 private static native long nativeCreate();
Adrian Roos04505652015-10-22 16:12:01 -0700322 private static native long nativeFreeBuffer(long nativePtr);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000323 private static native void nativeDestroy(long nativePtr);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800324
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000325 private static native byte[] nativeMarshall(long nativePtr);
Adrian Roos04505652015-10-22 16:12:01 -0700326 private static native long nativeUnmarshall(
John Spurlocke0852362015-02-04 15:47:40 -0500327 long nativePtr, byte[] data, int offset, int length);
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700328 private static native int nativeCompareData(long thisNativePtr, long otherNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -0700329 private static native long nativeAppendFrom(
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000330 long thisNativePtr, long otherNativePtr, int offset, int length);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700331 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000332 private static native boolean nativeHasFileDescriptors(long nativePtr);
333 private static native void nativeWriteInterfaceToken(long nativePtr, String interfaceName);
334 private static native void nativeEnforceInterface(long nativePtr, String interfaceName);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800335
Fyodor Kupolova81b8c02017-11-13 15:51:03 -0800336 /** Last time exception with a stack trace was written */
337 private static volatile long sLastWriteExceptionStackTrace;
338 /** Used for throttling of writing stack trace, which is costly */
339 private static final int WRITE_EXCEPTION_STACK_TRACE_THRESHOLD_MS = 1000;
340
Makoto Onukib148b6c2017-06-27 13:38:38 -0700341 @CriticalNative
Dan Sandleraa861662015-04-21 10:24:32 -0400342 private static native long nativeGetBlobAshmemSize(long nativePtr);
Dan Sandler5ce04302015-04-09 23:50:15 -0400343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 public final static Parcelable.Creator<String> STRING_CREATOR
345 = new Parcelable.Creator<String>() {
346 public String createFromParcel(Parcel source) {
347 return source.readString();
348 }
349 public String[] newArray(int size) {
350 return new String[size];
351 }
352 };
353
354 /**
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700355 * @hide
356 */
357 public static class ReadWriteHelper {
358 public static final ReadWriteHelper DEFAULT = new ReadWriteHelper();
359
360 /**
361 * Called when writing a string to a parcel. Subclasses wanting to write a string
362 * must use {@link #writeStringNoHelper(String)} to avoid
363 * infinity recursive calls.
364 */
365 public void writeString(Parcel p, String s) {
366 nativeWriteString(p.mNativePtr, s);
367 }
368
369 /**
370 * Called when reading a string to a parcel. Subclasses wanting to read a string
371 * must use {@link #readStringNoHelper()} to avoid
372 * infinity recursive calls.
373 */
374 public String readString(Parcel p) {
375 return nativeReadString(p.mNativePtr);
376 }
377 }
378
379 private ReadWriteHelper mReadWriteHelper = ReadWriteHelper.DEFAULT;
380
381 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 * Retrieve a new Parcel object from the pool.
383 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400384 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 public static Parcel obtain() {
386 final Parcel[] pool = sOwnedPool;
387 synchronized (pool) {
388 Parcel p;
389 for (int i=0; i<POOL_SIZE; i++) {
390 p = pool[i];
391 if (p != null) {
392 pool[i] = null;
393 if (DEBUG_RECYCLE) {
394 p.mStack = new RuntimeException();
395 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700396 p.mReadWriteHelper = ReadWriteHelper.DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 return p;
398 }
399 }
400 }
401 return new Parcel(0);
402 }
403
404 /**
405 * Put a Parcel object back into the pool. You must not touch
406 * the object after this call.
407 */
408 public final void recycle() {
409 if (DEBUG_RECYCLE) mStack = null;
410 freeBuffer();
Jeff Sharkey047238c2012-03-07 16:51:38 -0800411
412 final Parcel[] pool;
413 if (mOwnsNativeParcelObject) {
414 pool = sOwnedPool;
415 } else {
416 mNativePtr = 0;
417 pool = sHolderPool;
418 }
419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 synchronized (pool) {
421 for (int i=0; i<POOL_SIZE; i++) {
422 if (pool[i] == null) {
423 pool[i] = this;
424 return;
425 }
426 }
427 }
428 }
429
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700430 /**
431 * Set a {@link ReadWriteHelper}, which can be used to avoid having duplicate strings, for
432 * example.
433 *
434 * @hide
435 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400436 public void setReadWriteHelper(@Nullable ReadWriteHelper helper) {
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700437 mReadWriteHelper = helper != null ? helper : ReadWriteHelper.DEFAULT;
438 }
439
440 /**
441 * @return whether this parcel has a {@link ReadWriteHelper}.
442 *
443 * @hide
444 */
445 public boolean hasReadWriteHelper() {
446 return (mReadWriteHelper != null) && (mReadWriteHelper != ReadWriteHelper.DEFAULT);
447 }
448
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800449 /** @hide */
450 public static native long getGlobalAllocSize();
451
452 /** @hide */
453 public static native long getGlobalAllocCount();
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 /**
456 * Returns the total amount of data contained in the parcel.
457 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800458 public final int dataSize() {
459 return nativeDataSize(mNativePtr);
460 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461
462 /**
463 * Returns the amount of data remaining to be read from the
464 * parcel. That is, {@link #dataSize}-{@link #dataPosition}.
465 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800466 public final int dataAvail() {
467 return nativeDataAvail(mNativePtr);
468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469
470 /**
471 * Returns the current position in the parcel data. Never
472 * more than {@link #dataSize}.
473 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800474 public final int dataPosition() {
475 return nativeDataPosition(mNativePtr);
476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477
478 /**
479 * Returns the total amount of space in the parcel. This is always
480 * >= {@link #dataSize}. The difference between it and dataSize() is the
481 * amount of room left until the parcel needs to re-allocate its
482 * data buffer.
483 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800484 public final int dataCapacity() {
485 return nativeDataCapacity(mNativePtr);
486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487
488 /**
489 * Change the amount of data in the parcel. Can be either smaller or
490 * larger than the current size. If larger than the current capacity,
491 * more memory will be allocated.
492 *
493 * @param size The new number of bytes in the Parcel.
494 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800495 public final void setDataSize(int size) {
Michael Wachenschwanz138bebf2017-05-18 22:09:18 +0000496 updateNativeSize(nativeSetDataSize(mNativePtr, size));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800497 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498
499 /**
500 * Move the current read/write position in the parcel.
501 * @param pos New offset in the parcel; must be between 0 and
502 * {@link #dataSize}.
503 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800504 public final void setDataPosition(int pos) {
505 nativeSetDataPosition(mNativePtr, pos);
506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507
508 /**
509 * Change the capacity (current available space) of the parcel.
510 *
511 * @param size The new capacity of the parcel, in bytes. Can not be
512 * less than {@link #dataSize} -- that is, you can not drop existing data
513 * with this method.
514 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800515 public final void setDataCapacity(int size) {
516 nativeSetDataCapacity(mNativePtr, size);
517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400519 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800520 public final boolean pushAllowFds(boolean allowFds) {
521 return nativePushAllowFds(mNativePtr, allowFds);
522 }
Dianne Hackbornc04db7e2011-10-03 21:09:35 -0700523
524 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800525 public final void restoreAllowFds(boolean lastValue) {
526 nativeRestoreAllowFds(mNativePtr, lastValue);
527 }
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 /**
530 * Returns the raw bytes of the parcel.
531 *
532 * <p class="note">The data you retrieve here <strong>must not</strong>
533 * be placed in any kind of persistent storage (on local disk, across
534 * a network, etc). For that, you should use standard serialization
535 * or another kind of general serialization mechanism. The Parcel
536 * marshalled representation is highly optimized for local IPC, and as
537 * such does not attempt to maintain compatibility with data created
538 * in different versions of the platform.
539 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800540 public final byte[] marshall() {
541 return nativeMarshall(mNativePtr);
542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543
544 /**
545 * Set the bytes in data to be the raw bytes of this Parcel.
546 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400547 public final void unmarshall(@NonNull byte[] data, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700548 updateNativeSize(nativeUnmarshall(mNativePtr, data, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550
Jeff Sharkey047238c2012-03-07 16:51:38 -0800551 public final void appendFrom(Parcel parcel, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700552 updateNativeSize(nativeAppendFrom(mNativePtr, parcel.mNativePtr, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700555 /** @hide */
556 public final int compareData(Parcel other) {
557 return nativeCompareData(mNativePtr, other.mNativePtr);
558 }
559
Dianne Hackborn98305522017-05-05 17:53:53 -0700560 /** @hide */
561 public final void setClassCookie(Class clz, Object cookie) {
562 if (mClassCookies == null) {
563 mClassCookies = new ArrayMap<>();
564 }
565 mClassCookies.put(clz, cookie);
566 }
567
568 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400569 @Nullable
Dianne Hackborn98305522017-05-05 17:53:53 -0700570 public final Object getClassCookie(Class clz) {
571 return mClassCookies != null ? mClassCookies.get(clz) : null;
572 }
573
574 /** @hide */
575 public final void adoptClassCookies(Parcel from) {
576 mClassCookies = from.mClassCookies;
577 }
578
Adrian Roosfb921842017-10-26 14:49:56 +0200579 /** @hide */
580 public Map<Class, Object> copyClassCookies() {
581 return new ArrayMap<>(mClassCookies);
582 }
583
584 /** @hide */
585 public void putClassCookies(Map<Class, Object> cookies) {
586 if (cookies == null) {
587 return;
588 }
589 if (mClassCookies == null) {
590 mClassCookies = new ArrayMap<>();
591 }
592 mClassCookies.putAll(cookies);
593 }
594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 /**
596 * Report whether the parcel contains any marshalled file descriptors.
597 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800598 public final boolean hasFileDescriptors() {
599 return nativeHasFileDescriptors(mNativePtr);
600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601
602 /**
603 * Store or read an IBinder interface token in the parcel at the current
604 * {@link #dataPosition}. This is used to validate that the marshalled
605 * transaction is intended for the target interface.
606 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800607 public final void writeInterfaceToken(String interfaceName) {
608 nativeWriteInterfaceToken(mNativePtr, interfaceName);
609 }
610
611 public final void enforceInterface(String interfaceName) {
612 nativeEnforceInterface(mNativePtr, interfaceName);
613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614
615 /**
Elliott Hughesa28b83e2011-02-28 14:26:13 -0800616 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 * growing {@link #dataCapacity} if needed.
618 * @param b Bytes to place into the parcel.
619 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400620 public final void writeByteArray(@Nullable byte[] b) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 writeByteArray(b, 0, (b != null) ? b.length : 0);
622 }
623
624 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900625 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 * growing {@link #dataCapacity} if needed.
627 * @param b Bytes to place into the parcel.
628 * @param offset Index of first byte to be written.
629 * @param len Number of bytes to write.
630 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400631 public final void writeByteArray(@Nullable byte[] b, int offset, int len) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 if (b == null) {
633 writeInt(-1);
634 return;
635 }
Pete Gillin60f55a252018-05-10 15:40:32 +0100636 ArrayUtils.throwsIfOutOfBounds(b.length, offset, len);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800637 nativeWriteByteArray(mNativePtr, b, offset, len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 }
639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700641 * Write a blob of data into the parcel at the current {@link #dataPosition},
642 * growing {@link #dataCapacity} if needed.
643 * @param b Bytes to place into the parcel.
644 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -0700645 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700646 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400647 public final void writeBlob(@Nullable byte[] b) {
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500648 writeBlob(b, 0, (b != null) ? b.length : 0);
649 }
650
651 /**
652 * Write a blob of data into the parcel at the current {@link #dataPosition},
653 * growing {@link #dataCapacity} if needed.
654 * @param b Bytes to place into the parcel.
655 * @param offset Index of first byte to be written.
656 * @param len Number of bytes to write.
657 * {@hide}
658 * {@SystemApi}
659 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400660 public final void writeBlob(@Nullable byte[] b, int offset, int len) {
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500661 if (b == null) {
662 writeInt(-1);
663 return;
664 }
Pete Gillin60f55a252018-05-10 15:40:32 +0100665 ArrayUtils.throwsIfOutOfBounds(b.length, offset, len);
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500666 nativeWriteBlob(mNativePtr, b, offset, len);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700667 }
668
669 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 * Write an integer value into the parcel at the current dataPosition(),
671 * growing dataCapacity() if needed.
672 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800673 public final void writeInt(int val) {
674 nativeWriteInt(mNativePtr, val);
675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676
677 /**
678 * Write a long integer value into the parcel at the current dataPosition(),
679 * growing dataCapacity() if needed.
680 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800681 public final void writeLong(long val) {
682 nativeWriteLong(mNativePtr, val);
683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684
685 /**
686 * Write a floating point value into the parcel at the current
687 * dataPosition(), growing dataCapacity() if needed.
688 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800689 public final void writeFloat(float val) {
690 nativeWriteFloat(mNativePtr, val);
691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692
693 /**
694 * Write a double precision floating point value into the parcel at the
695 * current dataPosition(), growing dataCapacity() if needed.
696 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800697 public final void writeDouble(double val) {
698 nativeWriteDouble(mNativePtr, val);
699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700
701 /**
702 * Write a string value into the parcel at the current dataPosition(),
703 * growing dataCapacity() if needed.
704 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400705 public final void writeString(@Nullable String val) {
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700706 mReadWriteHelper.writeString(this, val);
707 }
708
709 /**
710 * Write a string without going though a {@link ReadWriteHelper}. Subclasses of
711 * {@link ReadWriteHelper} must use this method instead of {@link #writeString} to avoid
712 * infinity recursive calls.
713 *
714 * @hide
715 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400716 public void writeStringNoHelper(@Nullable String val) {
Jeff Sharkey047238c2012-03-07 16:51:38 -0800717 nativeWriteString(mNativePtr, val);
718 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719
Jake Wharton2ffa4312018-04-23 17:47:14 -0400720 /**
721 * Write a boolean value into the parcel at the current dataPosition(),
722 * growing dataCapacity() if needed.
723 *
724 * <p>Note: This method currently delegates to writeInt with a value of 1 or 0
725 * for true or false, respectively, but may change in the future.
726 */
Eugene Susla36e866b2017-02-23 18:24:39 -0800727 public final void writeBoolean(boolean val) {
728 writeInt(val ? 1 : 0);
729 }
730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000732 * Write a CharSequence value into the parcel at the current dataPosition(),
733 * growing dataCapacity() if needed.
734 * @hide
735 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400736 public final void writeCharSequence(@Nullable CharSequence val) {
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000737 TextUtils.writeToParcel(val, this, 0);
738 }
739
740 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 * Write an object into the parcel at the current dataPosition(),
742 * growing dataCapacity() if needed.
743 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800744 public final void writeStrongBinder(IBinder val) {
745 nativeWriteStrongBinder(mNativePtr, val);
746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747
748 /**
749 * Write an object into the parcel at the current dataPosition(),
750 * growing dataCapacity() if needed.
751 */
752 public final void writeStrongInterface(IInterface val) {
753 writeStrongBinder(val == null ? null : val.asBinder());
754 }
755
756 /**
757 * Write a FileDescriptor into the parcel at the current dataPosition(),
758 * growing dataCapacity() if needed.
Dan Egnorb3e4ef32010-07-20 09:03:35 -0700759 *
760 * <p class="caution">The file descriptor will not be closed, which may
761 * result in file descriptor leaks when objects are returned from Binder
762 * calls. Use {@link ParcelFileDescriptor#writeToParcel} instead, which
763 * accepts contextual flags and will close the original file descriptor
764 * if {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} is set.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400766 public final void writeFileDescriptor(@NonNull FileDescriptor val) {
Adrian Roos04505652015-10-22 16:12:01 -0700767 updateNativeSize(nativeWriteFileDescriptor(mNativePtr, val));
768 }
769
770 private void updateNativeSize(long newNativeSize) {
771 if (mOwnsNativeParcelObject) {
772 if (newNativeSize > Integer.MAX_VALUE) {
773 newNativeSize = Integer.MAX_VALUE;
774 }
775 if (newNativeSize != mNativeSize) {
776 int delta = (int) (newNativeSize - mNativeSize);
777 if (delta > 0) {
778 VMRuntime.getRuntime().registerNativeAllocation(delta);
779 } else {
780 VMRuntime.getRuntime().registerNativeFree(-delta);
781 }
782 mNativeSize = newNativeSize;
783 }
784 }
Jeff Sharkey047238c2012-03-07 16:51:38 -0800785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786
787 /**
Casey Dahlin2f974b22015-11-05 12:19:13 -0800788 * {@hide}
789 * This will be the new name for writeFileDescriptor, for consistency.
790 **/
Jake Whartonb1f474c2018-05-30 15:11:13 -0400791 public final void writeRawFileDescriptor(@NonNull FileDescriptor val) {
Casey Dahlin2f974b22015-11-05 12:19:13 -0800792 nativeWriteFileDescriptor(mNativePtr, val);
793 }
794
795 /**
796 * {@hide}
797 * Write an array of FileDescriptor objects into the Parcel.
798 *
799 * @param value The array of objects to be written.
800 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400801 public final void writeRawFileDescriptorArray(@Nullable FileDescriptor[] value) {
Casey Dahlin2f974b22015-11-05 12:19:13 -0800802 if (value != null) {
803 int N = value.length;
804 writeInt(N);
805 for (int i=0; i<N; i++) {
806 writeRawFileDescriptor(value[i]);
807 }
808 } else {
809 writeInt(-1);
810 }
811 }
812
813 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900814 * Write a byte value into the parcel at the current dataPosition(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 * growing dataCapacity() if needed.
Jake Wharton2ffa4312018-04-23 17:47:14 -0400816 *
817 * <p>Note: This method currently delegates to writeInt but may change in
818 * the future.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 */
820 public final void writeByte(byte val) {
821 writeInt(val);
822 }
823
824 /**
825 * Please use {@link #writeBundle} instead. Flattens a Map into the parcel
826 * at the current dataPosition(),
827 * growing dataCapacity() if needed. The Map keys must be String objects.
828 * The Map values are written using {@link #writeValue} and must follow
829 * the specification there.
Samuel Tana8036662015-11-23 14:36:00 -0800830 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 * <p>It is strongly recommended to use {@link #writeBundle} instead of
832 * this method, since the Bundle class provides a type-safe API that
833 * allows you to avoid mysterious type errors at the point of marshalling.
834 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400835 public final void writeMap(@Nullable Map val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700836 writeMapInternal((Map<String, Object>) val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 }
838
839 /**
840 * Flatten a Map into the parcel at the current dataPosition(),
841 * growing dataCapacity() if needed. The Map keys must be String objects.
842 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400843 /* package */ void writeMapInternal(@Nullable Map<String,Object> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 if (val == null) {
845 writeInt(-1);
846 return;
847 }
848 Set<Map.Entry<String,Object>> entries = val.entrySet();
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700849 int size = entries.size();
850 writeInt(size);
851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 for (Map.Entry<String,Object> e : entries) {
853 writeValue(e.getKey());
854 writeValue(e.getValue());
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700855 size--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 }
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700857
858 if (size != 0) {
859 throw new BadParcelableException("Map size does not match number of entries!");
860 }
861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 }
863
864 /**
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700865 * Flatten an ArrayMap into the parcel at the current dataPosition(),
866 * growing dataCapacity() if needed. The Map keys must be String objects.
867 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400868 /* package */ void writeArrayMapInternal(@Nullable ArrayMap<String, Object> val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700869 if (val == null) {
870 writeInt(-1);
871 return;
872 }
Samuel Tan3cefe6a2015-12-14 13:29:17 -0800873 // Keep the format of this Parcel in sync with writeToParcelInner() in
874 // frameworks/native/libs/binder/PersistableBundle.cpp.
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700875 final int N = val.size();
876 writeInt(N);
Dianne Hackborne784d1e2013-09-20 18:13:52 -0700877 if (DEBUG_ARRAY_MAP) {
878 RuntimeException here = new RuntimeException("here");
879 here.fillInStackTrace();
880 Log.d(TAG, "Writing " + N + " ArrayMap entries", here);
881 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700882 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700883 for (int i=0; i<N; i++) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700884 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700885 writeString(val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700886 writeValue(val.valueAt(i));
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700887 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Write #" + i + " "
888 + (dataPosition()-startPos) + " bytes: key=0x"
889 + Integer.toHexString(val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0)
890 + " " + val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700891 }
892 }
893
894 /**
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700895 * @hide For testing only.
896 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400897 public void writeArrayMap(@Nullable ArrayMap<String, Object> val) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700898 writeArrayMapInternal(val);
899 }
900
901 /**
Svet Ganov8455ba22019-01-02 13:05:56 -0800902 * Flatten an {@link ArrayMap} with string keys containing a particular object
903 * type into the parcel at the current dataPosition() and growing dataCapacity()
904 * if needed. The type of the objects in the array must be one that implements
905 * Parcelable. Only the raw data of the objects is written and not their type,
906 * so you must use the corresponding {@link #createTypedArrayMap(Parcelable.Creator)}
907 *
908 * @param val The map of objects to be written.
909 * @param parcelableFlags The parcelable flags to use.
910 *
911 * @see #createTypedArrayMap(Parcelable.Creator)
912 * @see Parcelable
913 */
914 public <T extends Parcelable> void writeTypedArrayMap(@Nullable ArrayMap<String, T> val,
915 int parcelableFlags) {
916 if (val == null) {
917 writeInt(-1);
918 return;
919 }
920 final int count = val.size();
921 writeInt(count);
922 for (int i = 0; i < count; i++) {
923 writeString(val.keyAt(i));
924 writeTypedObject(val.valueAt(i), parcelableFlags);
925 }
926 }
927
928 /**
Svet Ganovddb94882016-06-23 19:55:24 -0700929 * Write an array set to the parcel.
930 *
931 * @param val The array set to write.
932 *
933 * @hide
934 */
935 public void writeArraySet(@Nullable ArraySet<? extends Object> val) {
936 final int size = (val != null) ? val.size() : -1;
937 writeInt(size);
938 for (int i = 0; i < size; i++) {
939 writeValue(val.valueAt(i));
940 }
941 }
942
943 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 * Flatten a Bundle into the parcel at the current dataPosition(),
945 * growing dataCapacity() if needed.
946 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400947 public final void writeBundle(@Nullable Bundle val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 if (val == null) {
949 writeInt(-1);
950 return;
951 }
952
Dianne Hackborn6aff9052009-05-22 13:20:23 -0700953 val.writeToParcel(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 }
955
956 /**
Craig Mautner719e6b12014-04-04 20:29:41 -0700957 * Flatten a PersistableBundle into the parcel at the current dataPosition(),
958 * growing dataCapacity() if needed.
959 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400960 public final void writePersistableBundle(@Nullable PersistableBundle val) {
Craig Mautner719e6b12014-04-04 20:29:41 -0700961 if (val == null) {
962 writeInt(-1);
963 return;
964 }
965
966 val.writeToParcel(this, 0);
967 }
968
969 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -0700970 * Flatten a Size into the parcel at the current dataPosition(),
971 * growing dataCapacity() if needed.
972 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400973 public final void writeSize(@NonNull Size val) {
Jeff Sharkey5ef33982014-09-04 18:13:39 -0700974 writeInt(val.getWidth());
975 writeInt(val.getHeight());
976 }
977
978 /**
979 * Flatten a SizeF into the parcel at the current dataPosition(),
980 * growing dataCapacity() if needed.
981 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400982 public final void writeSizeF(@NonNull SizeF val) {
Jeff Sharkey5ef33982014-09-04 18:13:39 -0700983 writeFloat(val.getWidth());
984 writeFloat(val.getHeight());
985 }
986
987 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 * Flatten a List into the parcel at the current dataPosition(), growing
989 * dataCapacity() if needed. The List values are written using
990 * {@link #writeValue} and must follow the specification there.
991 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400992 public final void writeList(@Nullable List val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 if (val == null) {
994 writeInt(-1);
995 return;
996 }
997 int N = val.size();
998 int i=0;
999 writeInt(N);
1000 while (i < N) {
1001 writeValue(val.get(i));
1002 i++;
1003 }
1004 }
1005
1006 /**
1007 * Flatten an Object array into the parcel at the current dataPosition(),
1008 * growing dataCapacity() if needed. The array values are written using
1009 * {@link #writeValue} and must follow the specification there.
1010 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001011 public final void writeArray(@Nullable Object[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 if (val == null) {
1013 writeInt(-1);
1014 return;
1015 }
1016 int N = val.length;
1017 int i=0;
1018 writeInt(N);
1019 while (i < N) {
1020 writeValue(val[i]);
1021 i++;
1022 }
1023 }
1024
1025 /**
1026 * Flatten a generic SparseArray into the parcel at the current
1027 * dataPosition(), growing dataCapacity() if needed. The SparseArray
1028 * values are written using {@link #writeValue} and must follow the
1029 * specification there.
1030 */
Svet Ganov8455ba22019-01-02 13:05:56 -08001031 public final <T> void writeSparseArray(@Nullable SparseArray<T> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 if (val == null) {
1033 writeInt(-1);
1034 return;
1035 }
1036 int N = val.size();
1037 writeInt(N);
1038 int i=0;
1039 while (i < N) {
1040 writeInt(val.keyAt(i));
1041 writeValue(val.valueAt(i));
1042 i++;
1043 }
1044 }
1045
Jake Whartonb1f474c2018-05-30 15:11:13 -04001046 public final void writeSparseBooleanArray(@Nullable SparseBooleanArray val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 if (val == null) {
1048 writeInt(-1);
1049 return;
1050 }
1051 int N = val.size();
1052 writeInt(N);
1053 int i=0;
1054 while (i < N) {
1055 writeInt(val.keyAt(i));
1056 writeByte((byte)(val.valueAt(i) ? 1 : 0));
1057 i++;
1058 }
1059 }
1060
Adam Lesinski205656d2017-03-23 13:38:26 -07001061 /**
1062 * @hide
1063 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001064 public final void writeSparseIntArray(@Nullable SparseIntArray val) {
Adam Lesinski4e862812016-11-21 16:02:24 -08001065 if (val == null) {
1066 writeInt(-1);
1067 return;
1068 }
1069 int N = val.size();
1070 writeInt(N);
1071 int i=0;
1072 while (i < N) {
1073 writeInt(val.keyAt(i));
1074 writeInt(val.valueAt(i));
1075 i++;
1076 }
1077 }
1078
Jake Whartonb1f474c2018-05-30 15:11:13 -04001079 public final void writeBooleanArray(@Nullable boolean[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 if (val != null) {
1081 int N = val.length;
1082 writeInt(N);
1083 for (int i=0; i<N; i++) {
1084 writeInt(val[i] ? 1 : 0);
1085 }
1086 } else {
1087 writeInt(-1);
1088 }
1089 }
1090
Jake Whartonb1f474c2018-05-30 15:11:13 -04001091 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 public final boolean[] createBooleanArray() {
1093 int N = readInt();
1094 // >>2 as a fast divide-by-4 works in the create*Array() functions
1095 // because dataAvail() will never return a negative number. 4 is
1096 // the size of a stored boolean in the stream.
1097 if (N >= 0 && N <= (dataAvail() >> 2)) {
1098 boolean[] val = new boolean[N];
1099 for (int i=0; i<N; i++) {
1100 val[i] = readInt() != 0;
1101 }
1102 return val;
1103 } else {
1104 return null;
1105 }
1106 }
1107
Jake Whartonb1f474c2018-05-30 15:11:13 -04001108 public final void readBooleanArray(@NonNull boolean[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 int N = readInt();
1110 if (N == val.length) {
1111 for (int i=0; i<N; i++) {
1112 val[i] = readInt() != 0;
1113 }
1114 } else {
1115 throw new RuntimeException("bad array lengths");
1116 }
1117 }
1118
Jake Whartonb1f474c2018-05-30 15:11:13 -04001119 public final void writeCharArray(@Nullable char[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 if (val != null) {
1121 int N = val.length;
1122 writeInt(N);
1123 for (int i=0; i<N; i++) {
1124 writeInt((int)val[i]);
1125 }
1126 } else {
1127 writeInt(-1);
1128 }
1129 }
1130
Jake Whartonb1f474c2018-05-30 15:11:13 -04001131 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 public final char[] createCharArray() {
1133 int N = readInt();
1134 if (N >= 0 && N <= (dataAvail() >> 2)) {
1135 char[] val = new char[N];
1136 for (int i=0; i<N; i++) {
1137 val[i] = (char)readInt();
1138 }
1139 return val;
1140 } else {
1141 return null;
1142 }
1143 }
1144
Jake Whartonb1f474c2018-05-30 15:11:13 -04001145 public final void readCharArray(@NonNull char[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 int N = readInt();
1147 if (N == val.length) {
1148 for (int i=0; i<N; i++) {
1149 val[i] = (char)readInt();
1150 }
1151 } else {
1152 throw new RuntimeException("bad array lengths");
1153 }
1154 }
1155
Jake Whartonb1f474c2018-05-30 15:11:13 -04001156 public final void writeIntArray(@Nullable int[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 if (val != null) {
1158 int N = val.length;
1159 writeInt(N);
1160 for (int i=0; i<N; i++) {
1161 writeInt(val[i]);
1162 }
1163 } else {
1164 writeInt(-1);
1165 }
1166 }
1167
Jake Whartonb1f474c2018-05-30 15:11:13 -04001168 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 public final int[] createIntArray() {
1170 int N = readInt();
1171 if (N >= 0 && N <= (dataAvail() >> 2)) {
1172 int[] val = new int[N];
1173 for (int i=0; i<N; i++) {
1174 val[i] = readInt();
1175 }
1176 return val;
1177 } else {
1178 return null;
1179 }
1180 }
1181
Jake Whartonb1f474c2018-05-30 15:11:13 -04001182 public final void readIntArray(@NonNull int[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 int N = readInt();
1184 if (N == val.length) {
1185 for (int i=0; i<N; i++) {
1186 val[i] = readInt();
1187 }
1188 } else {
1189 throw new RuntimeException("bad array lengths");
1190 }
1191 }
1192
Jake Whartonb1f474c2018-05-30 15:11:13 -04001193 public final void writeLongArray(@Nullable long[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 if (val != null) {
1195 int N = val.length;
1196 writeInt(N);
1197 for (int i=0; i<N; i++) {
1198 writeLong(val[i]);
1199 }
1200 } else {
1201 writeInt(-1);
1202 }
1203 }
1204
Jake Whartonb1f474c2018-05-30 15:11:13 -04001205 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 public final long[] createLongArray() {
1207 int N = readInt();
1208 // >>3 because stored longs are 64 bits
1209 if (N >= 0 && N <= (dataAvail() >> 3)) {
1210 long[] val = new long[N];
1211 for (int i=0; i<N; i++) {
1212 val[i] = readLong();
1213 }
1214 return val;
1215 } else {
1216 return null;
1217 }
1218 }
1219
Jake Whartonb1f474c2018-05-30 15:11:13 -04001220 public final void readLongArray(@NonNull long[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 int N = readInt();
1222 if (N == val.length) {
1223 for (int i=0; i<N; i++) {
1224 val[i] = readLong();
1225 }
1226 } else {
1227 throw new RuntimeException("bad array lengths");
1228 }
1229 }
1230
Jake Whartonb1f474c2018-05-30 15:11:13 -04001231 public final void writeFloatArray(@Nullable float[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 if (val != null) {
1233 int N = val.length;
1234 writeInt(N);
1235 for (int i=0; i<N; i++) {
1236 writeFloat(val[i]);
1237 }
1238 } else {
1239 writeInt(-1);
1240 }
1241 }
1242
Jake Whartonb1f474c2018-05-30 15:11:13 -04001243 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 public final float[] createFloatArray() {
1245 int N = readInt();
1246 // >>2 because stored floats are 4 bytes
1247 if (N >= 0 && N <= (dataAvail() >> 2)) {
1248 float[] val = new float[N];
1249 for (int i=0; i<N; i++) {
1250 val[i] = readFloat();
1251 }
1252 return val;
1253 } else {
1254 return null;
1255 }
1256 }
1257
Jake Whartonb1f474c2018-05-30 15:11:13 -04001258 public final void readFloatArray(@NonNull float[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 int N = readInt();
1260 if (N == val.length) {
1261 for (int i=0; i<N; i++) {
1262 val[i] = readFloat();
1263 }
1264 } else {
1265 throw new RuntimeException("bad array lengths");
1266 }
1267 }
1268
Jake Whartonb1f474c2018-05-30 15:11:13 -04001269 public final void writeDoubleArray(@Nullable double[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 if (val != null) {
1271 int N = val.length;
1272 writeInt(N);
1273 for (int i=0; i<N; i++) {
1274 writeDouble(val[i]);
1275 }
1276 } else {
1277 writeInt(-1);
1278 }
1279 }
1280
Jake Whartonb1f474c2018-05-30 15:11:13 -04001281 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 public final double[] createDoubleArray() {
1283 int N = readInt();
1284 // >>3 because stored doubles are 8 bytes
1285 if (N >= 0 && N <= (dataAvail() >> 3)) {
1286 double[] val = new double[N];
1287 for (int i=0; i<N; i++) {
1288 val[i] = readDouble();
1289 }
1290 return val;
1291 } else {
1292 return null;
1293 }
1294 }
1295
Jake Whartonb1f474c2018-05-30 15:11:13 -04001296 public final void readDoubleArray(@NonNull double[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 int N = readInt();
1298 if (N == val.length) {
1299 for (int i=0; i<N; i++) {
1300 val[i] = readDouble();
1301 }
1302 } else {
1303 throw new RuntimeException("bad array lengths");
1304 }
1305 }
1306
Jake Whartonb1f474c2018-05-30 15:11:13 -04001307 public final void writeStringArray(@Nullable String[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 if (val != null) {
1309 int N = val.length;
1310 writeInt(N);
1311 for (int i=0; i<N; i++) {
1312 writeString(val[i]);
1313 }
1314 } else {
1315 writeInt(-1);
1316 }
1317 }
1318
Jake Whartonb1f474c2018-05-30 15:11:13 -04001319 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 public final String[] createStringArray() {
1321 int N = readInt();
1322 if (N >= 0) {
1323 String[] val = new String[N];
1324 for (int i=0; i<N; i++) {
1325 val[i] = readString();
1326 }
1327 return val;
1328 } else {
1329 return null;
1330 }
1331 }
1332
Jake Whartonb1f474c2018-05-30 15:11:13 -04001333 public final void readStringArray(@NonNull String[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 int N = readInt();
1335 if (N == val.length) {
1336 for (int i=0; i<N; i++) {
1337 val[i] = readString();
1338 }
1339 } else {
1340 throw new RuntimeException("bad array lengths");
1341 }
1342 }
1343
Jake Whartonb1f474c2018-05-30 15:11:13 -04001344 public final void writeBinderArray(@Nullable IBinder[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 if (val != null) {
1346 int N = val.length;
1347 writeInt(N);
1348 for (int i=0; i<N; i++) {
1349 writeStrongBinder(val[i]);
1350 }
1351 } else {
1352 writeInt(-1);
1353 }
1354 }
1355
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001356 /**
1357 * @hide
1358 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001359 public final void writeCharSequenceArray(@Nullable CharSequence[] val) {
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001360 if (val != null) {
1361 int N = val.length;
1362 writeInt(N);
1363 for (int i=0; i<N; i++) {
1364 writeCharSequence(val[i]);
1365 }
1366 } else {
1367 writeInt(-1);
1368 }
1369 }
1370
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001371 /**
1372 * @hide
1373 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001374 public final void writeCharSequenceList(@Nullable ArrayList<CharSequence> val) {
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001375 if (val != null) {
1376 int N = val.size();
1377 writeInt(N);
1378 for (int i=0; i<N; i++) {
1379 writeCharSequence(val.get(i));
1380 }
1381 } else {
1382 writeInt(-1);
1383 }
1384 }
1385
Jake Whartonb1f474c2018-05-30 15:11:13 -04001386 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 public final IBinder[] createBinderArray() {
1388 int N = readInt();
1389 if (N >= 0) {
1390 IBinder[] val = new IBinder[N];
1391 for (int i=0; i<N; i++) {
1392 val[i] = readStrongBinder();
1393 }
1394 return val;
1395 } else {
1396 return null;
1397 }
1398 }
1399
Jake Whartonb1f474c2018-05-30 15:11:13 -04001400 public final void readBinderArray(@NonNull IBinder[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 int N = readInt();
1402 if (N == val.length) {
1403 for (int i=0; i<N; i++) {
1404 val[i] = readStrongBinder();
1405 }
1406 } else {
1407 throw new RuntimeException("bad array lengths");
1408 }
1409 }
1410
1411 /**
1412 * Flatten a List containing a particular object type into the parcel, at
1413 * the current dataPosition() and growing dataCapacity() if needed. The
1414 * type of the objects in the list must be one that implements Parcelable.
1415 * Unlike the generic writeList() method, however, only the raw data of the
1416 * objects is written and not their type, so you must use the corresponding
1417 * readTypedList() to unmarshall them.
1418 *
1419 * @param val The list of objects to be written.
1420 *
1421 * @see #createTypedArrayList
1422 * @see #readTypedList
1423 * @see Parcelable
1424 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001425 public final <T extends Parcelable> void writeTypedList(@Nullable List<T> val) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001426 writeTypedList(val, 0);
1427 }
1428
1429 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08001430 * Flatten a {@link SparseArray} containing a particular object type into the parcel
1431 * at the current dataPosition() and growing dataCapacity() if needed. The
1432 * type of the objects in the array must be one that implements Parcelable.
1433 * Unlike the generic {@link #writeSparseArray(SparseArray)} method, however, only
1434 * the raw data of the objects is written and not their type, so you must use the
1435 * corresponding {@link #createTypedSparseArray(Parcelable.Creator)}.
1436 *
1437 * @param val The list of objects to be written.
1438 * @param parcelableFlags The parcelable flags to use.
1439 *
1440 * @see #createTypedSparseArray(Parcelable.Creator)
1441 * @see Parcelable
1442 */
1443 public final <T extends Parcelable> void writeTypedSparseArray(@Nullable SparseArray<T> val,
1444 int parcelableFlags) {
1445 if (val == null) {
1446 writeInt(-1);
1447 return;
1448 }
1449 final int count = val.size();
1450 writeInt(count);
1451 for (int i = 0; i < count; i++) {
1452 writeInt(val.keyAt(i));
1453 writeTypedObject(val.valueAt(i), parcelableFlags);
1454 }
1455 }
1456
1457 /**
Sunny Goyal0e60f222017-09-21 21:39:20 -07001458 * @hide
1459 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001460 public <T extends Parcelable> void writeTypedList(@Nullable List<T> val, int parcelableFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 if (val == null) {
1462 writeInt(-1);
1463 return;
1464 }
1465 int N = val.size();
1466 int i=0;
1467 writeInt(N);
1468 while (i < N) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001469 writeTypedObject(val.get(i), parcelableFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 i++;
1471 }
1472 }
1473
1474 /**
1475 * Flatten a List containing String objects into the parcel, at
1476 * the current dataPosition() and growing dataCapacity() if needed. They
1477 * can later be retrieved with {@link #createStringArrayList} or
1478 * {@link #readStringList}.
1479 *
1480 * @param val The list of strings to be written.
1481 *
1482 * @see #createStringArrayList
1483 * @see #readStringList
1484 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001485 public final void writeStringList(@Nullable List<String> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 if (val == null) {
1487 writeInt(-1);
1488 return;
1489 }
1490 int N = val.size();
1491 int i=0;
1492 writeInt(N);
1493 while (i < N) {
1494 writeString(val.get(i));
1495 i++;
1496 }
1497 }
1498
1499 /**
1500 * Flatten a List containing IBinder objects into the parcel, at
1501 * the current dataPosition() and growing dataCapacity() if needed. They
1502 * can later be retrieved with {@link #createBinderArrayList} or
1503 * {@link #readBinderList}.
1504 *
1505 * @param val The list of strings to be written.
1506 *
1507 * @see #createBinderArrayList
1508 * @see #readBinderList
1509 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001510 public final void writeBinderList(@Nullable List<IBinder> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 if (val == null) {
1512 writeInt(-1);
1513 return;
1514 }
1515 int N = val.size();
1516 int i=0;
1517 writeInt(N);
1518 while (i < N) {
1519 writeStrongBinder(val.get(i));
1520 i++;
1521 }
1522 }
1523
1524 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00001525 * Flatten a {@code List} containing arbitrary {@code Parcelable} objects into this parcel
1526 * at the current position. They can later be retrieved using
1527 * {@link #readParcelableList(List, ClassLoader)} if required.
1528 *
1529 * @see #readParcelableList(List, ClassLoader)
Narayan Kamathbea48712016-12-01 15:38:28 +00001530 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001531 public final <T extends Parcelable> void writeParcelableList(@Nullable List<T> val, int flags) {
Narayan Kamathbea48712016-12-01 15:38:28 +00001532 if (val == null) {
1533 writeInt(-1);
1534 return;
1535 }
1536
1537 int N = val.size();
1538 int i=0;
1539 writeInt(N);
1540 while (i < N) {
1541 writeParcelable(val.get(i), flags);
1542 i++;
1543 }
1544 }
1545
1546 /**
Svet Ganov0f4928f2017-02-02 20:02:51 -08001547 * Flatten a homogeneous array containing a particular object type into
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 * the parcel, at
1549 * the current dataPosition() and growing dataCapacity() if needed. The
1550 * type of the objects in the array must be one that implements Parcelable.
1551 * Unlike the {@link #writeParcelableArray} method, however, only the
1552 * raw data of the objects is written and not their type, so you must use
1553 * {@link #readTypedArray} with the correct corresponding
1554 * {@link Parcelable.Creator} implementation to unmarshall them.
1555 *
1556 * @param val The array of objects to be written.
1557 * @param parcelableFlags Contextual flags as per
1558 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1559 *
1560 * @see #readTypedArray
1561 * @see #writeParcelableArray
1562 * @see Parcelable.Creator
1563 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001564 public final <T extends Parcelable> void writeTypedArray(@Nullable T[] val,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 int parcelableFlags) {
1566 if (val != null) {
1567 int N = val.length;
1568 writeInt(N);
Svet Ganov0f4928f2017-02-02 20:02:51 -08001569 for (int i = 0; i < N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001570 writeTypedObject(val[i], parcelableFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 }
1572 } else {
1573 writeInt(-1);
1574 }
1575 }
1576
1577 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001578 * Flatten the Parcelable object into the parcel.
1579 *
1580 * @param val The Parcelable object to be written.
1581 * @param parcelableFlags Contextual flags as per
1582 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1583 *
1584 * @see #readTypedObject
1585 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001586 public final <T extends Parcelable> void writeTypedObject(@Nullable T val,
1587 int parcelableFlags) {
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001588 if (val != null) {
1589 writeInt(1);
1590 val.writeToParcel(this, parcelableFlags);
1591 } else {
1592 writeInt(0);
1593 }
1594 }
1595
1596 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 * Flatten a generic object in to a parcel. The given Object value may
1598 * currently be one of the following types:
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001599 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 * <ul>
1601 * <li> null
1602 * <li> String
1603 * <li> Byte
1604 * <li> Short
1605 * <li> Integer
1606 * <li> Long
1607 * <li> Float
1608 * <li> Double
1609 * <li> Boolean
1610 * <li> String[]
1611 * <li> boolean[]
1612 * <li> byte[]
1613 * <li> int[]
1614 * <li> long[]
1615 * <li> Object[] (supporting objects of the same type defined here).
1616 * <li> {@link Bundle}
1617 * <li> Map (as supported by {@link #writeMap}).
1618 * <li> Any object that implements the {@link Parcelable} protocol.
1619 * <li> Parcelable[]
1620 * <li> CharSequence (as supported by {@link TextUtils#writeToParcel}).
1621 * <li> List (as supported by {@link #writeList}).
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001622 * <li> {@link SparseArray} (as supported by {@link #writeSparseArray(SparseArray)}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 * <li> {@link IBinder}
1624 * <li> Any object that implements Serializable (but see
1625 * {@link #writeSerializable} for caveats). Note that all of the
1626 * previous types have relatively efficient implementations for
1627 * writing to a Parcel; having to rely on the generic serialization
1628 * approach is much less efficient and should be avoided whenever
1629 * possible.
1630 * </ul>
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001631 *
1632 * <p class="caution">{@link Parcelable} objects are written with
1633 * {@link Parcelable#writeToParcel} using contextual flags of 0. When
1634 * serializing objects containing {@link ParcelFileDescriptor}s,
1635 * this may result in file descriptor leaks when they are returned from
1636 * Binder calls (where {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE}
1637 * should be used).</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001639 public final void writeValue(@Nullable Object v) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 if (v == null) {
1641 writeInt(VAL_NULL);
1642 } else if (v instanceof String) {
1643 writeInt(VAL_STRING);
1644 writeString((String) v);
1645 } else if (v instanceof Integer) {
1646 writeInt(VAL_INTEGER);
1647 writeInt((Integer) v);
1648 } else if (v instanceof Map) {
1649 writeInt(VAL_MAP);
1650 writeMap((Map) v);
1651 } else if (v instanceof Bundle) {
1652 // Must be before Parcelable
1653 writeInt(VAL_BUNDLE);
1654 writeBundle((Bundle) v);
Samuel Tanceafe5e2015-12-11 16:50:58 -08001655 } else if (v instanceof PersistableBundle) {
1656 writeInt(VAL_PERSISTABLEBUNDLE);
1657 writePersistableBundle((PersistableBundle) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 } else if (v instanceof Parcelable) {
Samuel Tanceafe5e2015-12-11 16:50:58 -08001659 // IMPOTANT: cases for classes that implement Parcelable must
1660 // come before the Parcelable case, so that their specific VAL_*
1661 // types will be written.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 writeInt(VAL_PARCELABLE);
1663 writeParcelable((Parcelable) v, 0);
1664 } else if (v instanceof Short) {
1665 writeInt(VAL_SHORT);
1666 writeInt(((Short) v).intValue());
1667 } else if (v instanceof Long) {
1668 writeInt(VAL_LONG);
1669 writeLong((Long) v);
1670 } else if (v instanceof Float) {
1671 writeInt(VAL_FLOAT);
1672 writeFloat((Float) v);
1673 } else if (v instanceof Double) {
1674 writeInt(VAL_DOUBLE);
1675 writeDouble((Double) v);
1676 } else if (v instanceof Boolean) {
1677 writeInt(VAL_BOOLEAN);
1678 writeInt((Boolean) v ? 1 : 0);
1679 } else if (v instanceof CharSequence) {
1680 // Must be after String
1681 writeInt(VAL_CHARSEQUENCE);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001682 writeCharSequence((CharSequence) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 } else if (v instanceof List) {
1684 writeInt(VAL_LIST);
1685 writeList((List) v);
1686 } else if (v instanceof SparseArray) {
1687 writeInt(VAL_SPARSEARRAY);
1688 writeSparseArray((SparseArray) v);
1689 } else if (v instanceof boolean[]) {
1690 writeInt(VAL_BOOLEANARRAY);
1691 writeBooleanArray((boolean[]) v);
1692 } else if (v instanceof byte[]) {
1693 writeInt(VAL_BYTEARRAY);
1694 writeByteArray((byte[]) v);
1695 } else if (v instanceof String[]) {
1696 writeInt(VAL_STRINGARRAY);
1697 writeStringArray((String[]) v);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001698 } else if (v instanceof CharSequence[]) {
1699 // Must be after String[] and before Object[]
1700 writeInt(VAL_CHARSEQUENCEARRAY);
1701 writeCharSequenceArray((CharSequence[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 } else if (v instanceof IBinder) {
1703 writeInt(VAL_IBINDER);
1704 writeStrongBinder((IBinder) v);
1705 } else if (v instanceof Parcelable[]) {
1706 writeInt(VAL_PARCELABLEARRAY);
1707 writeParcelableArray((Parcelable[]) v, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 } else if (v instanceof int[]) {
1709 writeInt(VAL_INTARRAY);
1710 writeIntArray((int[]) v);
1711 } else if (v instanceof long[]) {
1712 writeInt(VAL_LONGARRAY);
1713 writeLongArray((long[]) v);
1714 } else if (v instanceof Byte) {
1715 writeInt(VAL_BYTE);
1716 writeInt((Byte) v);
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001717 } else if (v instanceof Size) {
1718 writeInt(VAL_SIZE);
1719 writeSize((Size) v);
1720 } else if (v instanceof SizeF) {
1721 writeInt(VAL_SIZEF);
1722 writeSizeF((SizeF) v);
Samuel Tana8036662015-11-23 14:36:00 -08001723 } else if (v instanceof double[]) {
1724 writeInt(VAL_DOUBLEARRAY);
1725 writeDoubleArray((double[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 } else {
Paul Duffinac5a0822014-02-03 15:02:17 +00001727 Class<?> clazz = v.getClass();
1728 if (clazz.isArray() && clazz.getComponentType() == Object.class) {
1729 // Only pure Object[] are written here, Other arrays of non-primitive types are
1730 // handled by serialization as this does not record the component type.
1731 writeInt(VAL_OBJECTARRAY);
1732 writeArray((Object[]) v);
1733 } else if (v instanceof Serializable) {
1734 // Must be last
1735 writeInt(VAL_SERIALIZABLE);
1736 writeSerializable((Serializable) v);
1737 } else {
1738 throw new RuntimeException("Parcel: unable to marshal value " + v);
1739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 }
1741 }
1742
1743 /**
1744 * Flatten the name of the class of the Parcelable and its contents
1745 * into the parcel.
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001746 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 * @param p The Parcelable object to be written.
1748 * @param parcelableFlags Contextual flags as per
1749 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1750 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001751 public final void writeParcelable(@Nullable Parcelable p, int parcelableFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 if (p == null) {
1753 writeString(null);
1754 return;
1755 }
Neil Fuller44e440c2015-04-20 14:39:00 +01001756 writeParcelableCreator(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 p.writeToParcel(this, parcelableFlags);
1758 }
1759
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001760 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001761 public final void writeParcelableCreator(@NonNull Parcelable p) {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001762 String name = p.getClass().getName();
1763 writeString(name);
1764 }
1765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 /**
1767 * Write a generic serializable object in to a Parcel. It is strongly
1768 * recommended that this method be avoided, since the serialization
1769 * overhead is extremely large, and this approach will be much slower than
1770 * using the other approaches to writing data in to a Parcel.
1771 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001772 public final void writeSerializable(@Nullable Serializable s) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 if (s == null) {
1774 writeString(null);
1775 return;
1776 }
1777 String name = s.getClass().getName();
1778 writeString(name);
1779
1780 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1781 try {
1782 ObjectOutputStream oos = new ObjectOutputStream(baos);
1783 oos.writeObject(s);
1784 oos.close();
1785
1786 writeByteArray(baos.toByteArray());
1787 } catch (IOException ioe) {
1788 throw new RuntimeException("Parcelable encountered " +
1789 "IOException writing serializable object (name = " + name +
1790 ")", ioe);
1791 }
1792 }
1793
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001794 /** @hide For debugging purposes */
1795 public static void setStackTraceParceling(boolean enabled) {
1796 sParcelExceptionStackTrace = enabled;
1797 }
1798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 /**
1800 * Special function for writing an exception result at the header of
1801 * a parcel, to be used when returning an exception from a transaction.
1802 * Note that this currently only supports a few exception types; any other
1803 * exception will be re-thrown by this function as a RuntimeException
1804 * (to be caught by the system's last-resort exception handling when
1805 * dispatching a transaction).
Samuel Tana8036662015-11-23 14:36:00 -08001806 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 * <p>The supported exception types are:
1808 * <ul>
1809 * <li>{@link BadParcelableException}
1810 * <li>{@link IllegalArgumentException}
1811 * <li>{@link IllegalStateException}
1812 * <li>{@link NullPointerException}
1813 * <li>{@link SecurityException}
Dianne Hackborn18482ae2017-08-01 17:41:00 -07001814 * <li>{@link UnsupportedOperationException}
Dianne Hackborn7e714422013-09-13 17:32:57 -07001815 * <li>{@link NetworkOnMainThreadException}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 * </ul>
Samuel Tana8036662015-11-23 14:36:00 -08001817 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 * @param e The Exception to be written.
1819 *
1820 * @see #writeNoException
1821 * @see #readException
1822 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001823 public final void writeException(@NonNull Exception e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 int code = 0;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001825 if (e instanceof Parcelable
1826 && (e.getClass().getClassLoader() == Parcelable.class.getClassLoader())) {
1827 // We only send Parcelable exceptions that are in the
1828 // BootClassLoader to ensure that the receiver can unpack them
1829 code = EX_PARCELABLE;
1830 } else if (e instanceof SecurityException) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 code = EX_SECURITY;
1832 } else if (e instanceof BadParcelableException) {
1833 code = EX_BAD_PARCELABLE;
1834 } else if (e instanceof IllegalArgumentException) {
1835 code = EX_ILLEGAL_ARGUMENT;
1836 } else if (e instanceof NullPointerException) {
1837 code = EX_NULL_POINTER;
1838 } else if (e instanceof IllegalStateException) {
1839 code = EX_ILLEGAL_STATE;
Dianne Hackborn7e714422013-09-13 17:32:57 -07001840 } else if (e instanceof NetworkOnMainThreadException) {
1841 code = EX_NETWORK_MAIN_THREAD;
Dianne Hackborn33d738a2014-09-12 14:23:58 -07001842 } else if (e instanceof UnsupportedOperationException) {
1843 code = EX_UNSUPPORTED_OPERATION;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001844 } else if (e instanceof ServiceSpecificException) {
1845 code = EX_SERVICE_SPECIFIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 }
1847 writeInt(code);
Brad Fitzpatrick703e5d32010-07-15 13:16:41 -07001848 StrictMode.clearGatheredViolations();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 if (code == 0) {
1850 if (e instanceof RuntimeException) {
1851 throw (RuntimeException) e;
1852 }
1853 throw new RuntimeException(e);
1854 }
1855 writeString(e.getMessage());
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001856 final long timeNow = sParcelExceptionStackTrace ? SystemClock.elapsedRealtime() : 0;
1857 if (sParcelExceptionStackTrace && (timeNow - sLastWriteExceptionStackTrace
1858 > WRITE_EXCEPTION_STACK_TRACE_THRESHOLD_MS)) {
1859 sLastWriteExceptionStackTrace = timeNow;
1860 final int sizePosition = dataPosition();
1861 writeInt(0); // Header size will be filled in later
1862 StackTraceElement[] stackTrace = e.getStackTrace();
1863 final int truncatedSize = Math.min(stackTrace.length, 5);
1864 StringBuilder sb = new StringBuilder();
1865 for (int i = 0; i < truncatedSize; i++) {
Fyodor Kupolov679d9982017-11-21 10:42:23 -08001866 sb.append("\tat ").append(stackTrace[i]).append('\n');
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001867 }
1868 writeString(sb.toString());
1869 final int payloadPosition = dataPosition();
1870 setDataPosition(sizePosition);
1871 // Write stack trace header size. Used in native side to skip the header
1872 writeInt(payloadPosition - sizePosition);
1873 setDataPosition(payloadPosition);
1874 } else {
1875 writeInt(0);
1876 }
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001877 switch (code) {
1878 case EX_SERVICE_SPECIFIC:
1879 writeInt(((ServiceSpecificException) e).errorCode);
1880 break;
1881 case EX_PARCELABLE:
1882 // Write parceled exception prefixed by length
1883 final int sizePosition = dataPosition();
1884 writeInt(0);
1885 writeParcelable((Parcelable) e, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
1886 final int payloadPosition = dataPosition();
1887 setDataPosition(sizePosition);
1888 writeInt(payloadPosition - sizePosition);
1889 setDataPosition(payloadPosition);
1890 break;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 }
1893
1894 /**
1895 * Special function for writing information at the front of the Parcel
1896 * indicating that no exception occurred.
1897 *
1898 * @see #writeException
1899 * @see #readException
1900 */
1901 public final void writeNoException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001902 // Despite the name of this function ("write no exception"),
1903 // it should instead be thought of as "write the RPC response
1904 // header", but because this function name is written out by
1905 // the AIDL compiler, we're not going to rename it.
1906 //
1907 // The response header, in the non-exception case (see also
1908 // writeException above, also called by the AIDL compiler), is
1909 // either a 0 (the default case), or EX_HAS_REPLY_HEADER if
1910 // StrictMode has gathered up violations that have occurred
1911 // during a Binder call, in which case we write out the number
1912 // of violations and their details, serialized, before the
1913 // actual RPC respons data. The receiving end of this is
1914 // readException(), below.
1915 if (StrictMode.hasGatheredViolations()) {
1916 writeInt(EX_HAS_REPLY_HEADER);
1917 final int sizePosition = dataPosition();
1918 writeInt(0); // total size of fat header, to be filled in later
1919 StrictMode.writeGatheredViolationsToParcel(this);
1920 final int payloadPosition = dataPosition();
1921 setDataPosition(sizePosition);
1922 writeInt(payloadPosition - sizePosition); // header size
1923 setDataPosition(payloadPosition);
1924 } else {
1925 writeInt(0);
1926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 }
1928
1929 /**
1930 * Special function for reading an exception result from the header of
1931 * a parcel, to be used after receiving the result of a transaction. This
1932 * will throw the exception for you if it had been written to the Parcel,
1933 * otherwise return and let you read the normal result data from the Parcel.
1934 *
1935 * @see #writeException
1936 * @see #writeNoException
1937 */
1938 public final void readException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001939 int code = readExceptionCode();
1940 if (code != 0) {
1941 String msg = readString();
Takamasa Kuramitsu2214b822018-04-02 14:44:59 +09001942 readException(code, msg);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001943 }
1944 }
1945
1946 /**
1947 * Parses the header of a Binder call's response Parcel and
1948 * returns the exception code. Deals with lite or fat headers.
1949 * In the common successful case, this header is generally zero.
1950 * In less common cases, it's a small negative number and will be
1951 * followed by an error string.
1952 *
1953 * This exists purely for android.database.DatabaseUtils and
1954 * insulating it from having to handle fat headers as returned by
1955 * e.g. StrictMode-induced RPC responses.
1956 *
1957 * @hide
1958 */
1959 public final int readExceptionCode() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 int code = readInt();
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001961 if (code == EX_HAS_REPLY_HEADER) {
1962 int headerSize = readInt();
1963 if (headerSize == 0) {
1964 Log.e(TAG, "Unexpected zero-sized Parcel reply header.");
1965 } else {
1966 // Currently the only thing in the header is StrictMode stacks,
1967 // but discussions around event/RPC tracing suggest we might
1968 // put that here too. If so, switch on sub-header tags here.
1969 // But for now, just parse out the StrictMode stuff.
1970 StrictMode.readAndHandleBinderCallViolations(this);
1971 }
1972 // And fat response headers are currently only used when
1973 // there are no exceptions, so return no error:
1974 return 0;
1975 }
1976 return code;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 }
1978
1979 /**
Mark Doliner879ea452014-01-02 12:38:07 -08001980 * Throw an exception with the given message. Not intended for use
1981 * outside the Parcel class.
1982 *
1983 * @param code Used to determine which exception class to throw.
1984 * @param msg The exception message.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 */
1986 public final void readException(int code, String msg) {
Takamasa Kuramitsu2214b822018-04-02 14:44:59 +09001987 String remoteStackTrace = null;
1988 final int remoteStackPayloadSize = readInt();
1989 if (remoteStackPayloadSize > 0) {
1990 remoteStackTrace = readString();
1991 }
1992 Exception e = createException(code, msg);
1993 // Attach remote stack trace if availalble
1994 if (remoteStackTrace != null) {
1995 RemoteException cause = new RemoteException(
1996 "Remote stack trace:\n" + remoteStackTrace, null, false, false);
1997 try {
1998 Throwable rootCause = ExceptionUtils.getRootCause(e);
1999 if (rootCause != null) {
2000 rootCause.initCause(cause);
2001 }
2002 } catch (RuntimeException ex) {
2003 Log.e(TAG, "Cannot set cause " + cause + " for " + e, ex);
2004 }
2005 }
2006 SneakyThrow.sneakyThrow(e);
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002007 }
2008
2009 /**
2010 * Creates an exception with the given message.
2011 *
2012 * @param code Used to determine which exception class to throw.
2013 * @param msg The exception message.
2014 */
2015 private Exception createException(int code, String msg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 switch (code) {
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002017 case EX_PARCELABLE:
2018 if (readInt() > 0) {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002019 return (Exception) readParcelable(Parcelable.class.getClassLoader());
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002020 } else {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002021 return new RuntimeException(msg + " [missing Parcelable]");
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 case EX_SECURITY:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002024 return new SecurityException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 case EX_BAD_PARCELABLE:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002026 return new BadParcelableException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 case EX_ILLEGAL_ARGUMENT:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002028 return new IllegalArgumentException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 case EX_NULL_POINTER:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002030 return new NullPointerException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 case EX_ILLEGAL_STATE:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002032 return new IllegalStateException(msg);
Dianne Hackborn7e714422013-09-13 17:32:57 -07002033 case EX_NETWORK_MAIN_THREAD:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002034 return new NetworkOnMainThreadException();
Dianne Hackborn33d738a2014-09-12 14:23:58 -07002035 case EX_UNSUPPORTED_OPERATION:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002036 return new UnsupportedOperationException(msg);
Christopher Wiley80fd1202015-11-22 17:12:37 -08002037 case EX_SERVICE_SPECIFIC:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002038 return new ServiceSpecificException(readInt(), msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 }
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002040 return new RuntimeException("Unknown exception code: " + code
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 + " msg " + msg);
2042 }
2043
2044 /**
2045 * Read an integer value from the parcel at the current dataPosition().
2046 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002047 public final int readInt() {
2048 return nativeReadInt(mNativePtr);
2049 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050
2051 /**
2052 * Read a long integer value from the parcel at the current dataPosition().
2053 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002054 public final long readLong() {
2055 return nativeReadLong(mNativePtr);
2056 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057
2058 /**
2059 * Read a floating point value from the parcel at the current
2060 * dataPosition().
2061 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002062 public final float readFloat() {
2063 return nativeReadFloat(mNativePtr);
2064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065
2066 /**
2067 * Read a double precision floating point value from the parcel at the
2068 * current dataPosition().
2069 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002070 public final double readDouble() {
2071 return nativeReadDouble(mNativePtr);
2072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073
2074 /**
2075 * Read a string value from the parcel at the current dataPosition().
2076 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002077 @Nullable
Jeff Sharkey047238c2012-03-07 16:51:38 -08002078 public final String readString() {
Makoto Onuki4501c61d2017-07-27 15:56:40 -07002079 return mReadWriteHelper.readString(this);
2080 }
2081
2082 /**
2083 * Read a string without going though a {@link ReadWriteHelper}. Subclasses of
2084 * {@link ReadWriteHelper} must use this method instead of {@link #readString} to avoid
2085 * infinity recursive calls.
2086 *
2087 * @hide
2088 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002089 @Nullable
Makoto Onuki4501c61d2017-07-27 15:56:40 -07002090 public String readStringNoHelper() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002091 return nativeReadString(mNativePtr);
2092 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093
Jake Wharton2ffa4312018-04-23 17:47:14 -04002094 /**
2095 * Read a boolean value from the parcel at the current dataPosition().
2096 */
Eugene Susla36e866b2017-02-23 18:24:39 -08002097 public final boolean readBoolean() {
2098 return readInt() != 0;
2099 }
2100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002102 * Read a CharSequence value from the parcel at the current dataPosition().
2103 * @hide
2104 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002105 @Nullable
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002106 public final CharSequence readCharSequence() {
2107 return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(this);
2108 }
2109
2110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 * Read an object from the parcel at the current dataPosition().
2112 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002113 public final IBinder readStrongBinder() {
2114 return nativeReadStrongBinder(mNativePtr);
2115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116
2117 /**
2118 * Read a FileDescriptor from the parcel at the current dataPosition().
2119 */
2120 public final ParcelFileDescriptor readFileDescriptor() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002121 FileDescriptor fd = nativeReadFileDescriptor(mNativePtr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 return fd != null ? new ParcelFileDescriptor(fd) : null;
2123 }
2124
Jeff Sharkeyda5a3e12013-08-11 12:54:42 -07002125 /** {@hide} */
2126 public final FileDescriptor readRawFileDescriptor() {
2127 return nativeReadFileDescriptor(mNativePtr);
2128 }
2129
Casey Dahlin2f974b22015-11-05 12:19:13 -08002130 /**
2131 * {@hide}
2132 * Read and return a new array of FileDescriptors from the parcel.
2133 * @return the FileDescriptor array, or null if the array is null.
2134 **/
Jake Whartonb1f474c2018-05-30 15:11:13 -04002135 @Nullable
Casey Dahlin2f974b22015-11-05 12:19:13 -08002136 public final FileDescriptor[] createRawFileDescriptorArray() {
2137 int N = readInt();
2138 if (N < 0) {
2139 return null;
2140 }
2141 FileDescriptor[] f = new FileDescriptor[N];
2142 for (int i = 0; i < N; i++) {
2143 f[i] = readRawFileDescriptor();
2144 }
2145 return f;
2146 }
2147
2148 /**
2149 * {@hide}
2150 * Read an array of FileDescriptors from a parcel.
2151 * The passed array must be exactly the length of the array in the parcel.
2152 * @return the FileDescriptor array, or null if the array is null.
2153 **/
2154 public final void readRawFileDescriptorArray(FileDescriptor[] val) {
2155 int N = readInt();
2156 if (N == val.length) {
2157 for (int i=0; i<N; i++) {
2158 val[i] = readRawFileDescriptor();
2159 }
2160 } else {
2161 throw new RuntimeException("bad array lengths");
2162 }
2163 }
2164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 /**
2166 * Read a byte value from the parcel at the current dataPosition().
2167 */
2168 public final byte readByte() {
2169 return (byte)(readInt() & 0xff);
2170 }
2171
2172 /**
2173 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2174 * been written with {@link #writeBundle}. Read into an existing Map object
2175 * from the parcel at the current dataPosition().
2176 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002177 public final void readMap(@NonNull Map outVal, @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 int N = readInt();
2179 readMapInternal(outVal, N, loader);
2180 }
2181
2182 /**
2183 * Read into an existing List object from the parcel at the current
2184 * dataPosition(), using the given class loader to load any enclosed
2185 * Parcelables. If it is null, the default class loader is used.
2186 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002187 public final void readList(@NonNull List outVal, @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 int N = readInt();
2189 readListInternal(outVal, N, loader);
2190 }
2191
2192 /**
2193 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2194 * been written with {@link #writeBundle}. Read and return a new HashMap
2195 * object from the parcel at the current dataPosition(), using the given
2196 * class loader to load any enclosed Parcelables. Returns null if
2197 * the previously written map object was null.
2198 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002199 @Nullable
2200 public final HashMap readHashMap(@Nullable ClassLoader loader)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 {
2202 int N = readInt();
2203 if (N < 0) {
2204 return null;
2205 }
2206 HashMap m = new HashMap(N);
2207 readMapInternal(m, N, loader);
2208 return m;
2209 }
2210
2211 /**
2212 * Read and return a new Bundle object from the parcel at the current
2213 * dataPosition(). Returns null if the previously written Bundle object was
2214 * null.
2215 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002216 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 public final Bundle readBundle() {
2218 return readBundle(null);
2219 }
2220
2221 /**
2222 * Read and return a new Bundle object from the parcel at the current
2223 * dataPosition(), using the given class loader to initialize the class
2224 * loader of the Bundle for later retrieval of Parcelable objects.
2225 * Returns null if the previously written Bundle object was null.
2226 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002227 @Nullable
2228 public final Bundle readBundle(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 int length = readInt();
2230 if (length < 0) {
Dianne Hackborn4a7d8242013-10-03 10:19:20 -07002231 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 return null;
2233 }
Samuel Tana8036662015-11-23 14:36:00 -08002234
Dianne Hackborn6aff9052009-05-22 13:20:23 -07002235 final Bundle bundle = new Bundle(this, length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 if (loader != null) {
2237 bundle.setClassLoader(loader);
2238 }
2239 return bundle;
2240 }
2241
2242 /**
Craig Mautner719e6b12014-04-04 20:29:41 -07002243 * Read and return a new Bundle object from the parcel at the current
2244 * dataPosition(). Returns null if the previously written Bundle object was
2245 * null.
2246 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002247 @Nullable
Craig Mautner719e6b12014-04-04 20:29:41 -07002248 public final PersistableBundle readPersistableBundle() {
2249 return readPersistableBundle(null);
2250 }
2251
2252 /**
2253 * Read and return a new Bundle object from the parcel at the current
2254 * dataPosition(), using the given class loader to initialize the class
2255 * loader of the Bundle for later retrieval of Parcelable objects.
2256 * Returns null if the previously written Bundle object was null.
2257 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002258 @Nullable
2259 public final PersistableBundle readPersistableBundle(@Nullable ClassLoader loader) {
Craig Mautner719e6b12014-04-04 20:29:41 -07002260 int length = readInt();
2261 if (length < 0) {
2262 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
2263 return null;
2264 }
2265
2266 final PersistableBundle bundle = new PersistableBundle(this, length);
2267 if (loader != null) {
2268 bundle.setClassLoader(loader);
2269 }
2270 return bundle;
2271 }
2272
2273 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002274 * Read a Size from the parcel at the current dataPosition().
2275 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002276 @NonNull
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002277 public final Size readSize() {
2278 final int width = readInt();
2279 final int height = readInt();
2280 return new Size(width, height);
2281 }
2282
2283 /**
2284 * Read a SizeF from the parcel at the current dataPosition().
2285 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002286 @NonNull
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002287 public final SizeF readSizeF() {
2288 final float width = readFloat();
2289 final float height = readFloat();
2290 return new SizeF(width, height);
2291 }
2292
2293 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 * Read and return a byte[] object from the parcel.
2295 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002296 @Nullable
Jeff Sharkey047238c2012-03-07 16:51:38 -08002297 public final byte[] createByteArray() {
2298 return nativeCreateByteArray(mNativePtr);
2299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002300
2301 /**
2302 * Read a byte[] object from the parcel and copy it into the
2303 * given byte array.
2304 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002305 public final void readByteArray(@NonNull byte[] val) {
Jocelyn Dang46100442017-05-05 15:40:49 -07002306 boolean valid = nativeReadByteArray(mNativePtr, val, (val != null) ? val.length : 0);
2307 if (!valid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 throw new RuntimeException("bad array lengths");
2309 }
2310 }
2311
2312 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002313 * Read a blob of data from the parcel and return it as a byte array.
2314 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -07002315 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002316 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002317 @Nullable
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002318 public final byte[] readBlob() {
2319 return nativeReadBlob(mNativePtr);
2320 }
2321
2322 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 * Read and return a String[] object from the parcel.
2324 * {@hide}
2325 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002326 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 public final String[] readStringArray() {
2328 String[] array = null;
2329
2330 int length = readInt();
2331 if (length >= 0)
2332 {
2333 array = new String[length];
2334
2335 for (int i = 0 ; i < length ; i++)
2336 {
2337 array[i] = readString();
2338 }
2339 }
2340
2341 return array;
2342 }
2343
2344 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002345 * Read and return a CharSequence[] object from the parcel.
2346 * {@hide}
2347 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002348 @Nullable
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002349 public final CharSequence[] readCharSequenceArray() {
2350 CharSequence[] array = null;
2351
2352 int length = readInt();
2353 if (length >= 0)
2354 {
2355 array = new CharSequence[length];
2356
2357 for (int i = 0 ; i < length ; i++)
2358 {
2359 array[i] = readCharSequence();
2360 }
2361 }
2362
2363 return array;
2364 }
2365
2366 /**
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002367 * Read and return an ArrayList&lt;CharSequence&gt; object from the parcel.
2368 * {@hide}
2369 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002370 @Nullable
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002371 public final ArrayList<CharSequence> readCharSequenceList() {
2372 ArrayList<CharSequence> array = null;
2373
2374 int length = readInt();
2375 if (length >= 0) {
2376 array = new ArrayList<CharSequence>(length);
2377
2378 for (int i = 0 ; i < length ; i++) {
2379 array.add(readCharSequence());
2380 }
2381 }
2382
2383 return array;
2384 }
2385
2386 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 * Read and return a new ArrayList object from the parcel at the current
2388 * dataPosition(). Returns null if the previously written list object was
2389 * null. The given class loader will be used to load any enclosed
2390 * Parcelables.
2391 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002392 @Nullable
2393 public final ArrayList readArrayList(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 int N = readInt();
2395 if (N < 0) {
2396 return null;
2397 }
2398 ArrayList l = new ArrayList(N);
2399 readListInternal(l, N, loader);
2400 return l;
2401 }
2402
2403 /**
2404 * Read and return a new Object array from the parcel at the current
2405 * dataPosition(). Returns null if the previously written array was
2406 * null. The given class loader will be used to load any enclosed
2407 * Parcelables.
2408 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002409 @Nullable
2410 public final Object[] readArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 int N = readInt();
2412 if (N < 0) {
2413 return null;
2414 }
2415 Object[] l = new Object[N];
2416 readArrayInternal(l, N, loader);
2417 return l;
2418 }
2419
2420 /**
2421 * Read and return a new SparseArray object from the parcel at the current
2422 * dataPosition(). Returns null if the previously written list object was
2423 * null. The given class loader will be used to load any enclosed
2424 * Parcelables.
2425 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002426 @Nullable
Svet Ganov8455ba22019-01-02 13:05:56 -08002427 public final <T> SparseArray<T> readSparseArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 int N = readInt();
2429 if (N < 0) {
2430 return null;
2431 }
2432 SparseArray sa = new SparseArray(N);
2433 readSparseArrayInternal(sa, N, loader);
2434 return sa;
2435 }
2436
2437 /**
2438 * Read and return a new SparseBooleanArray object from the parcel at the current
2439 * dataPosition(). Returns null if the previously written list object was
2440 * null.
2441 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002442 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 public final SparseBooleanArray readSparseBooleanArray() {
2444 int N = readInt();
2445 if (N < 0) {
2446 return null;
2447 }
2448 SparseBooleanArray sa = new SparseBooleanArray(N);
2449 readSparseBooleanArrayInternal(sa, N);
2450 return sa;
2451 }
2452
2453 /**
Adam Lesinski4e862812016-11-21 16:02:24 -08002454 * Read and return a new SparseIntArray object from the parcel at the current
2455 * dataPosition(). Returns null if the previously written array object was null.
Adam Lesinski205656d2017-03-23 13:38:26 -07002456 * @hide
Adam Lesinski4e862812016-11-21 16:02:24 -08002457 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002458 @Nullable
Adam Lesinski4e862812016-11-21 16:02:24 -08002459 public final SparseIntArray readSparseIntArray() {
2460 int N = readInt();
2461 if (N < 0) {
2462 return null;
2463 }
2464 SparseIntArray sa = new SparseIntArray(N);
2465 readSparseIntArrayInternal(sa, N);
2466 return sa;
2467 }
2468
2469 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 * Read and return a new ArrayList containing a particular object type from
2471 * the parcel that was written with {@link #writeTypedList} at the
2472 * current dataPosition(). Returns null if the
2473 * previously written list object was null. The list <em>must</em> have
2474 * previously been written via {@link #writeTypedList} with the same object
2475 * type.
2476 *
2477 * @return A newly created ArrayList containing objects with the same data
2478 * as those that were previously written.
2479 *
2480 * @see #writeTypedList
2481 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002482 @Nullable
2483 public final <T> ArrayList<T> createTypedArrayList(@NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 int N = readInt();
2485 if (N < 0) {
2486 return null;
2487 }
2488 ArrayList<T> l = new ArrayList<T>(N);
2489 while (N > 0) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002490 l.add(readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 N--;
2492 }
2493 return l;
2494 }
2495
2496 /**
2497 * Read into the given List items containing a particular object type
2498 * that were written with {@link #writeTypedList} at the
2499 * current dataPosition(). The list <em>must</em> have
2500 * previously been written via {@link #writeTypedList} with the same object
2501 * type.
2502 *
2503 * @return A newly created ArrayList containing objects with the same data
2504 * as those that were previously written.
2505 *
2506 * @see #writeTypedList
2507 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002508 public final <T> void readTypedList(@NonNull List<T> list, @NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 int M = list.size();
2510 int N = readInt();
2511 int i = 0;
2512 for (; i < M && i < N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002513 list.set(i, readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 }
2515 for (; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002516 list.add(readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 }
2518 for (; i<M; i++) {
2519 list.remove(N);
2520 }
2521 }
2522
2523 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08002524 * Read into a new {@link SparseArray} items containing a particular object type
2525 * that were written with {@link #writeTypedSparseArray(SparseArray, int)} at the
2526 * current dataPosition(). The list <em>must</em> have previously been written
2527 * via {@link #writeTypedSparseArray(SparseArray, int)} with the same object type.
2528 *
2529 * @param creator The creator to use when for instantiation.
2530 *
2531 * @return A newly created {@link SparseArray} containing objects with the same data
2532 * as those that were previously written.
2533 *
2534 * @see #writeTypedSparseArray(SparseArray, int)
2535 */
2536 public final @Nullable <T extends Parcelable> SparseArray<T> createTypedSparseArray(
2537 @NonNull Parcelable.Creator<T> creator) {
2538 final int count = readInt();
2539 if (count < 0) {
2540 return null;
2541 }
2542 final SparseArray<T> array = new SparseArray<>(count);
2543 for (int i = 0; i < count; i++) {
2544 final int index = readInt();
2545 final T value = readTypedObject(creator);
2546 array.append(index, value);
2547 }
2548 return array;
2549 }
2550
2551 /**
2552 * Read into a new {@link ArrayMap} with string keys items containing a particular
2553 * object type that were written with {@link #writeTypedArrayMap(ArrayMap, int)} at the
2554 * current dataPosition(). The list <em>must</em> have previously been written
2555 * via {@link #writeTypedArrayMap(ArrayMap, int)} with the same object type.
2556 *
2557 * @param creator The creator to use when for instantiation.
2558 *
2559 * @return A newly created {@link ArrayMap} containing objects with the same data
2560 * as those that were previously written.
2561 *
2562 * @see #writeTypedArrayMap(ArrayMap, int)
2563 */
2564 public final @Nullable <T extends Parcelable> ArrayMap<String, T> createTypedArrayMap(
2565 @NonNull Parcelable.Creator<T> creator) {
2566 final int count = readInt();
2567 if (count < 0) {
2568 return null;
2569 }
2570 final ArrayMap<String, T> map = new ArrayMap<>(count);
2571 for (int i = 0; i < count; i++) {
2572 final String key = readString();
2573 final T value = readTypedObject(creator);
2574 map.append(key, value);
2575 }
2576 return map;
2577 }
2578
2579 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 * Read and return a new ArrayList containing String objects from
2581 * the parcel that was written with {@link #writeStringList} at the
2582 * current dataPosition(). Returns null if the
2583 * previously written list object was null.
2584 *
2585 * @return A newly created ArrayList containing strings with the same data
2586 * as those that were previously written.
2587 *
2588 * @see #writeStringList
2589 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002590 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 public final ArrayList<String> createStringArrayList() {
2592 int N = readInt();
2593 if (N < 0) {
2594 return null;
2595 }
2596 ArrayList<String> l = new ArrayList<String>(N);
2597 while (N > 0) {
2598 l.add(readString());
2599 N--;
2600 }
2601 return l;
2602 }
2603
2604 /**
2605 * Read and return a new ArrayList containing IBinder objects from
2606 * the parcel that was written with {@link #writeBinderList} at the
2607 * current dataPosition(). Returns null if the
2608 * previously written list object was null.
2609 *
2610 * @return A newly created ArrayList containing strings with the same data
2611 * as those that were previously written.
2612 *
2613 * @see #writeBinderList
2614 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002615 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 public final ArrayList<IBinder> createBinderArrayList() {
2617 int N = readInt();
2618 if (N < 0) {
2619 return null;
2620 }
2621 ArrayList<IBinder> l = new ArrayList<IBinder>(N);
2622 while (N > 0) {
2623 l.add(readStrongBinder());
2624 N--;
2625 }
2626 return l;
2627 }
2628
2629 /**
2630 * Read into the given List items String objects that were written with
2631 * {@link #writeStringList} at the current dataPosition().
2632 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 * @see #writeStringList
2634 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002635 public final void readStringList(@NonNull List<String> list) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 int M = list.size();
2637 int N = readInt();
2638 int i = 0;
2639 for (; i < M && i < N; i++) {
2640 list.set(i, readString());
2641 }
2642 for (; i<N; i++) {
2643 list.add(readString());
2644 }
2645 for (; i<M; i++) {
2646 list.remove(N);
2647 }
2648 }
2649
2650 /**
2651 * Read into the given List items IBinder objects that were written with
2652 * {@link #writeBinderList} at the current dataPosition().
2653 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 * @see #writeBinderList
2655 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002656 public final void readBinderList(@NonNull List<IBinder> list) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 int M = list.size();
2658 int N = readInt();
2659 int i = 0;
2660 for (; i < M && i < N; i++) {
2661 list.set(i, readStrongBinder());
2662 }
2663 for (; i<N; i++) {
2664 list.add(readStrongBinder());
2665 }
2666 for (; i<M; i++) {
2667 list.remove(N);
2668 }
2669 }
2670
2671 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00002672 * Read the list of {@code Parcelable} objects at the current data position into the
2673 * given {@code list}. The contents of the {@code list} are replaced. If the serialized
2674 * list was {@code null}, {@code list} is cleared.
2675 *
2676 * @see #writeParcelableList(List, int)
Narayan Kamathbea48712016-12-01 15:38:28 +00002677 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002678 @NonNull
2679 public final <T extends Parcelable> List<T> readParcelableList(@NonNull List<T> list,
2680 @Nullable ClassLoader cl) {
Narayan Kamathbea48712016-12-01 15:38:28 +00002681 final int N = readInt();
2682 if (N == -1) {
2683 list.clear();
Eugene Susla36e866b2017-02-23 18:24:39 -08002684 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002685 }
2686
2687 final int M = list.size();
2688 int i = 0;
2689 for (; i < M && i < N; i++) {
2690 list.set(i, (T) readParcelable(cl));
2691 }
2692 for (; i<N; i++) {
2693 list.add((T) readParcelable(cl));
2694 }
2695 for (; i<M; i++) {
2696 list.remove(N);
2697 }
Eugene Susla36e866b2017-02-23 18:24:39 -08002698 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002699 }
2700
2701 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 * Read and return a new array containing a particular object type from
2703 * the parcel at the current dataPosition(). Returns null if the
2704 * previously written array was null. The array <em>must</em> have
2705 * previously been written via {@link #writeTypedArray} with the same
2706 * object type.
2707 *
2708 * @return A newly created array containing objects with the same data
2709 * as those that were previously written.
2710 *
2711 * @see #writeTypedArray
2712 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002713 @Nullable
2714 public final <T> T[] createTypedArray(@NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 int N = readInt();
2716 if (N < 0) {
2717 return null;
2718 }
2719 T[] l = c.newArray(N);
2720 for (int i=0; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002721 l[i] = readTypedObject(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 }
2723 return l;
2724 }
2725
Jake Whartonb1f474c2018-05-30 15:11:13 -04002726 public final <T> void readTypedArray(@NonNull T[] val, @NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 int N = readInt();
2728 if (N == val.length) {
2729 for (int i=0; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002730 val[i] = readTypedObject(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 }
2732 } else {
2733 throw new RuntimeException("bad array lengths");
2734 }
2735 }
2736
2737 /**
2738 * @deprecated
2739 * @hide
2740 */
2741 @Deprecated
2742 public final <T> T[] readTypedArray(Parcelable.Creator<T> c) {
2743 return createTypedArray(c);
2744 }
2745
2746 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002747 * Read and return a typed Parcelable object from a parcel.
2748 * Returns null if the previous written object was null.
2749 * The object <em>must</em> have previous been written via
2750 * {@link #writeTypedObject} with the same object type.
2751 *
2752 * @return A newly created object of the type that was previously
2753 * written.
2754 *
2755 * @see #writeTypedObject
2756 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002757 @Nullable
2758 public final <T> T readTypedObject(@NonNull Parcelable.Creator<T> c) {
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002759 if (readInt() != 0) {
2760 return c.createFromParcel(this);
2761 } else {
2762 return null;
2763 }
2764 }
2765
2766 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 * Write a heterogeneous array of Parcelable objects into the Parcel.
2768 * Each object in the array is written along with its class name, so
2769 * that the correct class can later be instantiated. As a result, this
2770 * has significantly more overhead than {@link #writeTypedArray}, but will
2771 * correctly handle an array containing more than one type of object.
2772 *
2773 * @param value The array of objects to be written.
2774 * @param parcelableFlags Contextual flags as per
2775 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
2776 *
2777 * @see #writeTypedArray
2778 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002779 public final <T extends Parcelable> void writeParcelableArray(@Nullable T[] value,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 int parcelableFlags) {
2781 if (value != null) {
2782 int N = value.length;
2783 writeInt(N);
2784 for (int i=0; i<N; i++) {
2785 writeParcelable(value[i], parcelableFlags);
2786 }
2787 } else {
2788 writeInt(-1);
2789 }
2790 }
2791
2792 /**
2793 * Read a typed object from a parcel. The given class loader will be
2794 * used to load any enclosed Parcelables. If it is null, the default class
2795 * loader will be used.
2796 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002797 @Nullable
2798 public final Object readValue(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 int type = readInt();
2800
2801 switch (type) {
2802 case VAL_NULL:
2803 return null;
2804
2805 case VAL_STRING:
2806 return readString();
2807
2808 case VAL_INTEGER:
2809 return readInt();
2810
2811 case VAL_MAP:
2812 return readHashMap(loader);
2813
2814 case VAL_PARCELABLE:
2815 return readParcelable(loader);
2816
2817 case VAL_SHORT:
2818 return (short) readInt();
2819
2820 case VAL_LONG:
2821 return readLong();
2822
2823 case VAL_FLOAT:
2824 return readFloat();
2825
2826 case VAL_DOUBLE:
2827 return readDouble();
2828
2829 case VAL_BOOLEAN:
2830 return readInt() == 1;
2831
2832 case VAL_CHARSEQUENCE:
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002833 return readCharSequence();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834
2835 case VAL_LIST:
2836 return readArrayList(loader);
2837
2838 case VAL_BOOLEANARRAY:
Samuel Tana8036662015-11-23 14:36:00 -08002839 return createBooleanArray();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840
2841 case VAL_BYTEARRAY:
2842 return createByteArray();
2843
2844 case VAL_STRINGARRAY:
2845 return readStringArray();
2846
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002847 case VAL_CHARSEQUENCEARRAY:
2848 return readCharSequenceArray();
2849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 case VAL_IBINDER:
2851 return readStrongBinder();
2852
2853 case VAL_OBJECTARRAY:
2854 return readArray(loader);
2855
2856 case VAL_INTARRAY:
2857 return createIntArray();
2858
2859 case VAL_LONGARRAY:
2860 return createLongArray();
2861
2862 case VAL_BYTE:
2863 return readByte();
2864
2865 case VAL_SERIALIZABLE:
John Spurlock5002b8c2014-01-10 13:32:12 -05002866 return readSerializable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867
2868 case VAL_PARCELABLEARRAY:
2869 return readParcelableArray(loader);
2870
2871 case VAL_SPARSEARRAY:
2872 return readSparseArray(loader);
2873
2874 case VAL_SPARSEBOOLEANARRAY:
2875 return readSparseBooleanArray();
2876
2877 case VAL_BUNDLE:
2878 return readBundle(loader); // loading will be deferred
2879
Craig Mautner719e6b12014-04-04 20:29:41 -07002880 case VAL_PERSISTABLEBUNDLE:
2881 return readPersistableBundle(loader);
2882
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002883 case VAL_SIZE:
2884 return readSize();
2885
2886 case VAL_SIZEF:
2887 return readSizeF();
2888
Samuel Tana8036662015-11-23 14:36:00 -08002889 case VAL_DOUBLEARRAY:
2890 return createDoubleArray();
2891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 default:
2893 int off = dataPosition() - 4;
2894 throw new RuntimeException(
2895 "Parcel " + this + ": Unmarshalling unknown type code " + type + " at offset " + off);
2896 }
2897 }
2898
2899 /**
2900 * Read and return a new Parcelable from the parcel. The given class loader
2901 * will be used to load any enclosed Parcelables. If it is null, the default
2902 * class loader will be used.
2903 * @param loader A ClassLoader from which to instantiate the Parcelable
2904 * object, or null for the default class loader.
2905 * @return Returns the newly created Parcelable, or null if a null
2906 * object has been written.
2907 * @throws BadParcelableException Throws BadParcelableException if there
2908 * was an error trying to instantiate the Parcelable.
2909 */
Neil Fuller44e440c2015-04-20 14:39:00 +01002910 @SuppressWarnings("unchecked")
Jake Whartonb1f474c2018-05-30 15:11:13 -04002911 @Nullable
2912 public final <T extends Parcelable> T readParcelable(@Nullable ClassLoader loader) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002913 Parcelable.Creator<?> creator = readParcelableCreator(loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002914 if (creator == null) {
2915 return null;
2916 }
2917 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002918 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2919 (Parcelable.ClassLoaderCreator<?>) creator;
2920 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002921 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002922 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002923 }
2924
2925 /** @hide */
Neil Fuller44e440c2015-04-20 14:39:00 +01002926 @SuppressWarnings("unchecked")
Jake Whartonb1f474c2018-05-30 15:11:13 -04002927 @Nullable
2928 public final <T extends Parcelable> T readCreator(@NonNull Parcelable.Creator<?> creator,
2929 @Nullable ClassLoader loader) {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002930 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002931 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2932 (Parcelable.ClassLoaderCreator<?>) creator;
2933 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002934 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002935 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002936 }
2937
2938 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002939 @Nullable
2940 public final Parcelable.Creator<?> readParcelableCreator(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 String name = readString();
2942 if (name == null) {
2943 return null;
2944 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002945 Parcelable.Creator<?> creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 synchronized (mCreators) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002947 HashMap<String,Parcelable.Creator<?>> map = mCreators.get(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002948 if (map == null) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002949 map = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 mCreators.put(loader, map);
2951 }
2952 creator = map.get(name);
2953 if (creator == null) {
2954 try {
Neil Fuller44e440c2015-04-20 14:39:00 +01002955 // If loader == null, explicitly emulate Class.forName(String) "caller
2956 // classloader" behavior.
2957 ClassLoader parcelableClassLoader =
2958 (loader == null ? getClass().getClassLoader() : loader);
2959 // Avoid initializing the Parcelable class until we know it implements
2960 // Parcelable and has the necessary CREATOR field. http://b/1171613.
2961 Class<?> parcelableClass = Class.forName(name, false /* initialize */,
2962 parcelableClassLoader);
2963 if (!Parcelable.class.isAssignableFrom(parcelableClass)) {
Dianne Hackbornced54392018-02-26 13:07:42 -08002964 throw new BadParcelableException("Parcelable protocol requires subclassing "
2965 + "from Parcelable on class " + name);
Neil Fuller44e440c2015-04-20 14:39:00 +01002966 }
2967 Field f = parcelableClass.getField("CREATOR");
2968 if ((f.getModifiers() & Modifier.STATIC) == 0) {
2969 throw new BadParcelableException("Parcelable protocol requires "
2970 + "the CREATOR object to be static on class " + name);
2971 }
2972 Class<?> creatorType = f.getType();
2973 if (!Parcelable.Creator.class.isAssignableFrom(creatorType)) {
2974 // Fail before calling Field.get(), not after, to avoid initializing
2975 // parcelableClass unnecessarily.
2976 throw new BadParcelableException("Parcelable protocol requires a "
2977 + "Parcelable.Creator object called "
2978 + "CREATOR on class " + name);
2979 }
2980 creator = (Parcelable.Creator<?>) f.get(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 }
2982 catch (IllegalAccessException e) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002983 Log.e(TAG, "Illegal access when unmarshalling: " + name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 throw new BadParcelableException(
2985 "IllegalAccessException when unmarshalling: " + name);
2986 }
2987 catch (ClassNotFoundException e) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002988 Log.e(TAG, "Class not found when unmarshalling: " + name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989 throw new BadParcelableException(
2990 "ClassNotFoundException when unmarshalling: " + name);
2991 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992 catch (NoSuchFieldException e) {
2993 throw new BadParcelableException("Parcelable protocol requires a "
Neil Fuller44e440c2015-04-20 14:39:00 +01002994 + "Parcelable.Creator object called "
2995 + "CREATOR on class " + name);
Irfan Sheriff97a72f62013-01-11 10:45:51 -08002996 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 if (creator == null) {
2998 throw new BadParcelableException("Parcelable protocol requires a "
Neil Fuller44e440c2015-04-20 14:39:00 +01002999 + "non-null Parcelable.Creator object called "
3000 + "CREATOR on class " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 }
3002
3003 map.put(name, creator);
3004 }
3005 }
3006
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08003007 return creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008 }
3009
3010 /**
3011 * Read and return a new Parcelable array from the parcel.
3012 * The given class loader will be used to load any enclosed
3013 * Parcelables.
3014 * @return the Parcelable array, or null if the array is null
3015 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003016 @Nullable
3017 public final Parcelable[] readParcelableArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 int N = readInt();
3019 if (N < 0) {
3020 return null;
3021 }
3022 Parcelable[] p = new Parcelable[N];
3023 for (int i = 0; i < N; i++) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003024 p[i] = readParcelable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 }
3026 return p;
3027 }
3028
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003029 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003030 @Nullable
3031 public final <T extends Parcelable> T[] readParcelableArray(@Nullable ClassLoader loader,
3032 @NonNull Class<T> clazz) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003033 int N = readInt();
3034 if (N < 0) {
3035 return null;
3036 }
3037 T[] p = (T[]) Array.newInstance(clazz, N);
3038 for (int i = 0; i < N; i++) {
3039 p[i] = readParcelable(loader);
3040 }
3041 return p;
3042 }
3043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 /**
3045 * Read and return a new Serializable object from the parcel.
3046 * @return the Serializable object, or null if the Serializable name
3047 * wasn't found in the parcel.
3048 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003049 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003050 public final Serializable readSerializable() {
John Spurlock5002b8c2014-01-10 13:32:12 -05003051 return readSerializable(null);
3052 }
3053
Jake Whartonb1f474c2018-05-30 15:11:13 -04003054 @Nullable
3055 private final Serializable readSerializable(@Nullable final ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 String name = readString();
3057 if (name == null) {
3058 // For some reason we were unable to read the name of the Serializable (either there
3059 // is nothing left in the Parcel to read, or the next value wasn't a String), so
3060 // return null, which indicates that the name wasn't found in the parcel.
3061 return null;
3062 }
3063
3064 byte[] serializedData = createByteArray();
3065 ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
3066 try {
John Spurlock5002b8c2014-01-10 13:32:12 -05003067 ObjectInputStream ois = new ObjectInputStream(bais) {
3068 @Override
3069 protected Class<?> resolveClass(ObjectStreamClass osClass)
3070 throws IOException, ClassNotFoundException {
3071 // try the custom classloader if provided
3072 if (loader != null) {
3073 Class<?> c = Class.forName(osClass.getName(), false, loader);
3074 if (c != null) {
3075 return c;
3076 }
3077 }
3078 return super.resolveClass(osClass);
3079 }
3080 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081 return (Serializable) ois.readObject();
3082 } catch (IOException ioe) {
3083 throw new RuntimeException("Parcelable encountered " +
3084 "IOException reading a Serializable object (name = " + name +
3085 ")", ioe);
3086 } catch (ClassNotFoundException cnfe) {
John Spurlock5002b8c2014-01-10 13:32:12 -05003087 throw new RuntimeException("Parcelable encountered " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 "ClassNotFoundException reading a Serializable object (name = "
3089 + name + ")", cnfe);
3090 }
3091 }
3092
3093 // Cache of previously looked up CREATOR.createFromParcel() methods for
3094 // particular classes. Keys are the names of the classes, values are
3095 // Method objects.
Neil Fuller44e440c2015-04-20 14:39:00 +01003096 private static final HashMap<ClassLoader,HashMap<String,Parcelable.Creator<?>>>
3097 mCreators = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003098
Narayan Kamathb34a4612014-01-23 14:17:11 +00003099 /** @hide for internal use only. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 static protected final Parcel obtain(int obj) {
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003101 throw new UnsupportedOperationException();
3102 }
3103
3104 /** @hide */
3105 static protected final Parcel obtain(long obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106 final Parcel[] pool = sHolderPool;
3107 synchronized (pool) {
3108 Parcel p;
3109 for (int i=0; i<POOL_SIZE; i++) {
3110 p = pool[i];
3111 if (p != null) {
3112 pool[i] = null;
3113 if (DEBUG_RECYCLE) {
3114 p.mStack = new RuntimeException();
3115 }
3116 p.init(obj);
3117 return p;
3118 }
3119 }
3120 }
3121 return new Parcel(obj);
3122 }
3123
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003124 private Parcel(long nativePtr) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 if (DEBUG_RECYCLE) {
3126 mStack = new RuntimeException();
3127 }
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003128 //Log.i(TAG, "Initializing obj=0x" + Integer.toHexString(obj), mStack);
Jeff Sharkey047238c2012-03-07 16:51:38 -08003129 init(nativePtr);
3130 }
3131
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003132 private void init(long nativePtr) {
Jeff Sharkey047238c2012-03-07 16:51:38 -08003133 if (nativePtr != 0) {
3134 mNativePtr = nativePtr;
3135 mOwnsNativeParcelObject = false;
3136 } else {
3137 mNativePtr = nativeCreate();
3138 mOwnsNativeParcelObject = true;
3139 }
3140 }
3141
3142 private void freeBuffer() {
3143 if (mOwnsNativeParcelObject) {
Adrian Roos04505652015-10-22 16:12:01 -07003144 updateNativeSize(nativeFreeBuffer(mNativePtr));
Jeff Sharkey047238c2012-03-07 16:51:38 -08003145 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -07003146 mReadWriteHelper = ReadWriteHelper.DEFAULT;
Jeff Sharkey047238c2012-03-07 16:51:38 -08003147 }
3148
3149 private void destroy() {
3150 if (mNativePtr != 0) {
3151 if (mOwnsNativeParcelObject) {
3152 nativeDestroy(mNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -07003153 updateNativeSize(0);
Jeff Sharkey047238c2012-03-07 16:51:38 -08003154 }
3155 mNativePtr = 0;
3156 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -07003157 mReadWriteHelper = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 }
3159
3160 @Override
3161 protected void finalize() throws Throwable {
3162 if (DEBUG_RECYCLE) {
3163 if (mStack != null) {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003164 Log.w(TAG, "Client did not call Parcel.recycle()", mStack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 }
3166 }
3167 destroy();
3168 }
3169
Jake Whartonb1f474c2018-05-30 15:11:13 -04003170 /* package */ void readMapInternal(@NonNull Map outVal, int N,
3171 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 while (N > 0) {
3173 Object key = readValue(loader);
3174 Object value = readValue(loader);
3175 outVal.put(key, value);
3176 N--;
3177 }
3178 }
3179
Jake Whartonb1f474c2018-05-30 15:11:13 -04003180 /* package */ void readArrayMapInternal(@NonNull ArrayMap outVal, int N,
3181 @Nullable ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003182 if (DEBUG_ARRAY_MAP) {
3183 RuntimeException here = new RuntimeException("here");
3184 here.fillInStackTrace();
3185 Log.d(TAG, "Reading " + N + " ArrayMap entries", here);
3186 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003187 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003188 while (N > 0) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003189 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003190 String key = readString();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003191 Object value = readValue(loader);
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003192 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " "
3193 + (dataPosition()-startPos) + " bytes: key=0x"
3194 + Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key);
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003195 outVal.append(key, value);
3196 N--;
3197 }
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003198 outVal.validate();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003199 }
3200
Jake Whartonb1f474c2018-05-30 15:11:13 -04003201 /* package */ void readArrayMapSafelyInternal(@NonNull ArrayMap outVal, int N,
3202 @Nullable ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003203 if (DEBUG_ARRAY_MAP) {
3204 RuntimeException here = new RuntimeException("here");
3205 here.fillInStackTrace();
3206 Log.d(TAG, "Reading safely " + N + " ArrayMap entries", here);
3207 }
3208 while (N > 0) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003209 String key = readString();
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003210 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read safe #" + (N-1) + ": key=0x"
3211 + (key != null ? key.hashCode() : 0) + " " + key);
3212 Object value = readValue(loader);
3213 outVal.put(key, value);
3214 N--;
3215 }
3216 }
3217
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003218 /**
3219 * @hide For testing only.
3220 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003221 public void readArrayMap(@NonNull ArrayMap outVal, @Nullable ClassLoader loader) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003222 final int N = readInt();
3223 if (N < 0) {
3224 return;
3225 }
3226 readArrayMapInternal(outVal, N, loader);
3227 }
3228
Svet Ganovddb94882016-06-23 19:55:24 -07003229 /**
3230 * Reads an array set.
3231 *
3232 * @param loader The class loader to use.
3233 *
3234 * @hide
3235 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003236 public @Nullable ArraySet<? extends Object> readArraySet(@Nullable ClassLoader loader) {
Svet Ganovddb94882016-06-23 19:55:24 -07003237 final int size = readInt();
3238 if (size < 0) {
3239 return null;
3240 }
3241 ArraySet<Object> result = new ArraySet<>(size);
3242 for (int i = 0; i < size; i++) {
3243 Object value = readValue(loader);
3244 result.append(value);
3245 }
3246 return result;
3247 }
3248
Jake Whartonb1f474c2018-05-30 15:11:13 -04003249 private void readListInternal(@NonNull List outVal, int N,
3250 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 while (N > 0) {
3252 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003253 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 outVal.add(value);
3255 N--;
3256 }
3257 }
3258
Jake Whartonb1f474c2018-05-30 15:11:13 -04003259 private void readArrayInternal(@NonNull Object[] outVal, int N,
3260 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 for (int i = 0; i < N; i++) {
3262 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003263 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 outVal[i] = value;
3265 }
3266 }
3267
Jake Whartonb1f474c2018-05-30 15:11:13 -04003268 private void readSparseArrayInternal(@NonNull SparseArray outVal, int N,
3269 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 while (N > 0) {
3271 int key = readInt();
3272 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003273 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274 outVal.append(key, value);
3275 N--;
3276 }
3277 }
3278
3279
Jake Whartonb1f474c2018-05-30 15:11:13 -04003280 private void readSparseBooleanArrayInternal(@NonNull SparseBooleanArray outVal, int N) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 while (N > 0) {
3282 int key = readInt();
3283 boolean value = this.readByte() == 1;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003284 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003285 outVal.append(key, value);
3286 N--;
3287 }
3288 }
Dan Sandler5ce04302015-04-09 23:50:15 -04003289
Jake Whartonb1f474c2018-05-30 15:11:13 -04003290 private void readSparseIntArrayInternal(@NonNull SparseIntArray outVal, int N) {
Adam Lesinski4e862812016-11-21 16:02:24 -08003291 while (N > 0) {
3292 int key = readInt();
3293 int value = readInt();
3294 outVal.append(key, value);
3295 N--;
3296 }
3297 }
3298
Dan Sandler5ce04302015-04-09 23:50:15 -04003299 /**
3300 * @hide For testing
3301 */
3302 public long getBlobAshmemSize() {
3303 return nativeGetBlobAshmemSize(mNativePtr);
3304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305}