blob: 79e9ba5f5bccb2955501873da379818f054075d0 [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
Olivier Gaillardbab444a2019-01-30 17:11:40 +0000336 @CriticalNative
337 private static native boolean nativeReplaceCallingWorkSourceUid(
338 long nativePtr, int workSourceUid);
339 @CriticalNative
340 private static native int nativeReadCallingWorkSourceUid(long nativePtr);
341
Fyodor Kupolova81b8c02017-11-13 15:51:03 -0800342 /** Last time exception with a stack trace was written */
343 private static volatile long sLastWriteExceptionStackTrace;
344 /** Used for throttling of writing stack trace, which is costly */
345 private static final int WRITE_EXCEPTION_STACK_TRACE_THRESHOLD_MS = 1000;
346
Makoto Onukib148b6c2017-06-27 13:38:38 -0700347 @CriticalNative
Dan Sandleraa861662015-04-21 10:24:32 -0400348 private static native long nativeGetBlobAshmemSize(long nativePtr);
Dan Sandler5ce04302015-04-09 23:50:15 -0400349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 public final static Parcelable.Creator<String> STRING_CREATOR
351 = new Parcelable.Creator<String>() {
352 public String createFromParcel(Parcel source) {
353 return source.readString();
354 }
355 public String[] newArray(int size) {
356 return new String[size];
357 }
358 };
359
360 /**
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700361 * @hide
362 */
363 public static class ReadWriteHelper {
364 public static final ReadWriteHelper DEFAULT = new ReadWriteHelper();
365
366 /**
367 * Called when writing a string to a parcel. Subclasses wanting to write a string
368 * must use {@link #writeStringNoHelper(String)} to avoid
369 * infinity recursive calls.
370 */
371 public void writeString(Parcel p, String s) {
372 nativeWriteString(p.mNativePtr, s);
373 }
374
375 /**
376 * Called when reading a string to a parcel. Subclasses wanting to read a string
377 * must use {@link #readStringNoHelper()} to avoid
378 * infinity recursive calls.
379 */
380 public String readString(Parcel p) {
381 return nativeReadString(p.mNativePtr);
382 }
383 }
384
385 private ReadWriteHelper mReadWriteHelper = ReadWriteHelper.DEFAULT;
386
387 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 * Retrieve a new Parcel object from the pool.
389 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400390 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 public static Parcel obtain() {
392 final Parcel[] pool = sOwnedPool;
393 synchronized (pool) {
394 Parcel p;
395 for (int i=0; i<POOL_SIZE; i++) {
396 p = pool[i];
397 if (p != null) {
398 pool[i] = null;
399 if (DEBUG_RECYCLE) {
400 p.mStack = new RuntimeException();
401 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700402 p.mReadWriteHelper = ReadWriteHelper.DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 return p;
404 }
405 }
406 }
407 return new Parcel(0);
408 }
409
410 /**
411 * Put a Parcel object back into the pool. You must not touch
412 * the object after this call.
413 */
414 public final void recycle() {
415 if (DEBUG_RECYCLE) mStack = null;
416 freeBuffer();
Jeff Sharkey047238c2012-03-07 16:51:38 -0800417
418 final Parcel[] pool;
419 if (mOwnsNativeParcelObject) {
420 pool = sOwnedPool;
421 } else {
422 mNativePtr = 0;
423 pool = sHolderPool;
424 }
425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 synchronized (pool) {
427 for (int i=0; i<POOL_SIZE; i++) {
428 if (pool[i] == null) {
429 pool[i] = this;
430 return;
431 }
432 }
433 }
434 }
435
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700436 /**
437 * Set a {@link ReadWriteHelper}, which can be used to avoid having duplicate strings, for
438 * example.
439 *
440 * @hide
441 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400442 public void setReadWriteHelper(@Nullable ReadWriteHelper helper) {
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700443 mReadWriteHelper = helper != null ? helper : ReadWriteHelper.DEFAULT;
444 }
445
446 /**
447 * @return whether this parcel has a {@link ReadWriteHelper}.
448 *
449 * @hide
450 */
451 public boolean hasReadWriteHelper() {
452 return (mReadWriteHelper != null) && (mReadWriteHelper != ReadWriteHelper.DEFAULT);
453 }
454
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800455 /** @hide */
456 public static native long getGlobalAllocSize();
457
458 /** @hide */
459 public static native long getGlobalAllocCount();
460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 /**
462 * Returns the total amount of data contained in the parcel.
463 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800464 public final int dataSize() {
465 return nativeDataSize(mNativePtr);
466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467
468 /**
469 * Returns the amount of data remaining to be read from the
470 * parcel. That is, {@link #dataSize}-{@link #dataPosition}.
471 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800472 public final int dataAvail() {
473 return nativeDataAvail(mNativePtr);
474 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475
476 /**
477 * Returns the current position in the parcel data. Never
478 * more than {@link #dataSize}.
479 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800480 public final int dataPosition() {
481 return nativeDataPosition(mNativePtr);
482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483
484 /**
485 * Returns the total amount of space in the parcel. This is always
486 * >= {@link #dataSize}. The difference between it and dataSize() is the
487 * amount of room left until the parcel needs to re-allocate its
488 * data buffer.
489 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800490 public final int dataCapacity() {
491 return nativeDataCapacity(mNativePtr);
492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493
494 /**
495 * Change the amount of data in the parcel. Can be either smaller or
496 * larger than the current size. If larger than the current capacity,
497 * more memory will be allocated.
498 *
499 * @param size The new number of bytes in the Parcel.
500 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800501 public final void setDataSize(int size) {
Michael Wachenschwanz138bebf2017-05-18 22:09:18 +0000502 updateNativeSize(nativeSetDataSize(mNativePtr, size));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504
505 /**
506 * Move the current read/write position in the parcel.
507 * @param pos New offset in the parcel; must be between 0 and
508 * {@link #dataSize}.
509 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800510 public final void setDataPosition(int pos) {
511 nativeSetDataPosition(mNativePtr, pos);
512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513
514 /**
515 * Change the capacity (current available space) of the parcel.
516 *
517 * @param size The new capacity of the parcel, in bytes. Can not be
518 * less than {@link #dataSize} -- that is, you can not drop existing data
519 * with this method.
520 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800521 public final void setDataCapacity(int size) {
522 nativeSetDataCapacity(mNativePtr, size);
523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400525 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800526 public final boolean pushAllowFds(boolean allowFds) {
527 return nativePushAllowFds(mNativePtr, allowFds);
528 }
Dianne Hackbornc04db7e2011-10-03 21:09:35 -0700529
530 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800531 public final void restoreAllowFds(boolean lastValue) {
532 nativeRestoreAllowFds(mNativePtr, lastValue);
533 }
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 /**
536 * Returns the raw bytes of the parcel.
537 *
538 * <p class="note">The data you retrieve here <strong>must not</strong>
539 * be placed in any kind of persistent storage (on local disk, across
540 * a network, etc). For that, you should use standard serialization
541 * or another kind of general serialization mechanism. The Parcel
542 * marshalled representation is highly optimized for local IPC, and as
543 * such does not attempt to maintain compatibility with data created
544 * in different versions of the platform.
545 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800546 public final byte[] marshall() {
547 return nativeMarshall(mNativePtr);
548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549
550 /**
551 * Set the bytes in data to be the raw bytes of this Parcel.
552 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400553 public final void unmarshall(@NonNull byte[] data, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700554 updateNativeSize(nativeUnmarshall(mNativePtr, data, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556
Jeff Sharkey047238c2012-03-07 16:51:38 -0800557 public final void appendFrom(Parcel parcel, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700558 updateNativeSize(nativeAppendFrom(mNativePtr, parcel.mNativePtr, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800559 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700561 /** @hide */
562 public final int compareData(Parcel other) {
563 return nativeCompareData(mNativePtr, other.mNativePtr);
564 }
565
Dianne Hackborn98305522017-05-05 17:53:53 -0700566 /** @hide */
567 public final void setClassCookie(Class clz, Object cookie) {
568 if (mClassCookies == null) {
569 mClassCookies = new ArrayMap<>();
570 }
571 mClassCookies.put(clz, cookie);
572 }
573
574 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400575 @Nullable
Dianne Hackborn98305522017-05-05 17:53:53 -0700576 public final Object getClassCookie(Class clz) {
577 return mClassCookies != null ? mClassCookies.get(clz) : null;
578 }
579
580 /** @hide */
581 public final void adoptClassCookies(Parcel from) {
582 mClassCookies = from.mClassCookies;
583 }
584
Adrian Roosfb921842017-10-26 14:49:56 +0200585 /** @hide */
586 public Map<Class, Object> copyClassCookies() {
587 return new ArrayMap<>(mClassCookies);
588 }
589
590 /** @hide */
591 public void putClassCookies(Map<Class, Object> cookies) {
592 if (cookies == null) {
593 return;
594 }
595 if (mClassCookies == null) {
596 mClassCookies = new ArrayMap<>();
597 }
598 mClassCookies.putAll(cookies);
599 }
600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 /**
602 * Report whether the parcel contains any marshalled file descriptors.
603 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800604 public final boolean hasFileDescriptors() {
605 return nativeHasFileDescriptors(mNativePtr);
606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607
608 /**
609 * Store or read an IBinder interface token in the parcel at the current
610 * {@link #dataPosition}. This is used to validate that the marshalled
611 * transaction is intended for the target interface.
612 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800613 public final void writeInterfaceToken(String interfaceName) {
614 nativeWriteInterfaceToken(mNativePtr, interfaceName);
615 }
616
617 public final void enforceInterface(String interfaceName) {
618 nativeEnforceInterface(mNativePtr, interfaceName);
619 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620
621 /**
Olivier Gaillardbab444a2019-01-30 17:11:40 +0000622 * Writes the work source uid to the request headers.
623 *
624 * <p>It requires the headers to have been written/read already to replace the work source.
625 *
626 * @return true if the request headers have been updated.
627 *
628 * @hide
629 */
630 public boolean replaceCallingWorkSourceUid(int workSourceUid) {
631 return nativeReplaceCallingWorkSourceUid(mNativePtr, workSourceUid);
632 }
633
634 /**
635 * Reads the work source uid from the request headers.
636 *
637 * <p>Unlike other read methods, this method does not read the parcel at the current
638 * {@link #dataPosition}. It will set the {@link #dataPosition} before the read and restore the
639 * position after reading the request header.
640 *
641 * @return the work source uid or {@link Binder#UNSET_WORKSOURCE} if headers have not been
642 * written/parsed yet.
643 *
644 * @hide
645 */
646 public int readCallingWorkSourceUid() {
647 return nativeReadCallingWorkSourceUid(mNativePtr);
648 }
649
650 /**
Elliott Hughesa28b83e2011-02-28 14:26:13 -0800651 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 * growing {@link #dataCapacity} if needed.
653 * @param b Bytes to place into the parcel.
654 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400655 public final void writeByteArray(@Nullable byte[] b) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 writeByteArray(b, 0, (b != null) ? b.length : 0);
657 }
658
659 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900660 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 * growing {@link #dataCapacity} if needed.
662 * @param b Bytes to place into the parcel.
663 * @param offset Index of first byte to be written.
664 * @param len Number of bytes to write.
665 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400666 public final void writeByteArray(@Nullable byte[] b, int offset, int len) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 if (b == null) {
668 writeInt(-1);
669 return;
670 }
Pete Gillin60f55a252018-05-10 15:40:32 +0100671 ArrayUtils.throwsIfOutOfBounds(b.length, offset, len);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800672 nativeWriteByteArray(mNativePtr, b, offset, len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 }
674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700676 * Write a blob of data into the parcel at the current {@link #dataPosition},
677 * growing {@link #dataCapacity} if needed.
678 * @param b Bytes to place into the parcel.
679 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -0700680 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700681 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400682 public final void writeBlob(@Nullable byte[] b) {
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500683 writeBlob(b, 0, (b != null) ? b.length : 0);
684 }
685
686 /**
687 * Write a blob of data into the parcel at the current {@link #dataPosition},
688 * growing {@link #dataCapacity} if needed.
689 * @param b Bytes to place into the parcel.
690 * @param offset Index of first byte to be written.
691 * @param len Number of bytes to write.
692 * {@hide}
693 * {@SystemApi}
694 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400695 public final void writeBlob(@Nullable byte[] b, int offset, int len) {
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500696 if (b == null) {
697 writeInt(-1);
698 return;
699 }
Pete Gillin60f55a252018-05-10 15:40:32 +0100700 ArrayUtils.throwsIfOutOfBounds(b.length, offset, len);
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500701 nativeWriteBlob(mNativePtr, b, offset, len);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700702 }
703
704 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 * Write an integer value into the parcel at the current dataPosition(),
706 * growing dataCapacity() if needed.
707 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800708 public final void writeInt(int val) {
709 nativeWriteInt(mNativePtr, val);
710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711
712 /**
713 * Write a long integer value into the parcel at the current dataPosition(),
714 * growing dataCapacity() if needed.
715 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800716 public final void writeLong(long val) {
717 nativeWriteLong(mNativePtr, val);
718 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719
720 /**
721 * Write a floating point value into the parcel at the current
722 * dataPosition(), growing dataCapacity() if needed.
723 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800724 public final void writeFloat(float val) {
725 nativeWriteFloat(mNativePtr, val);
726 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727
728 /**
729 * Write a double precision floating point value into the parcel at the
730 * current dataPosition(), growing dataCapacity() if needed.
731 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800732 public final void writeDouble(double val) {
733 nativeWriteDouble(mNativePtr, val);
734 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735
736 /**
737 * Write a string value into the parcel at the current dataPosition(),
738 * growing dataCapacity() if needed.
739 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400740 public final void writeString(@Nullable String val) {
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700741 mReadWriteHelper.writeString(this, val);
742 }
743
744 /**
745 * Write a string without going though a {@link ReadWriteHelper}. Subclasses of
746 * {@link ReadWriteHelper} must use this method instead of {@link #writeString} to avoid
747 * infinity recursive calls.
748 *
749 * @hide
750 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400751 public void writeStringNoHelper(@Nullable String val) {
Jeff Sharkey047238c2012-03-07 16:51:38 -0800752 nativeWriteString(mNativePtr, val);
753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754
Jake Wharton2ffa4312018-04-23 17:47:14 -0400755 /**
756 * Write a boolean value into the parcel at the current dataPosition(),
757 * growing dataCapacity() if needed.
758 *
759 * <p>Note: This method currently delegates to writeInt with a value of 1 or 0
760 * for true or false, respectively, but may change in the future.
761 */
Eugene Susla36e866b2017-02-23 18:24:39 -0800762 public final void writeBoolean(boolean val) {
763 writeInt(val ? 1 : 0);
764 }
765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000767 * Write a CharSequence value into the parcel at the current dataPosition(),
768 * growing dataCapacity() if needed.
769 * @hide
770 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400771 public final void writeCharSequence(@Nullable CharSequence val) {
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000772 TextUtils.writeToParcel(val, this, 0);
773 }
774
775 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 * Write an object into the parcel at the current dataPosition(),
777 * growing dataCapacity() if needed.
778 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800779 public final void writeStrongBinder(IBinder val) {
780 nativeWriteStrongBinder(mNativePtr, val);
781 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782
783 /**
784 * Write an object into the parcel at the current dataPosition(),
785 * growing dataCapacity() if needed.
786 */
787 public final void writeStrongInterface(IInterface val) {
788 writeStrongBinder(val == null ? null : val.asBinder());
789 }
790
791 /**
792 * Write a FileDescriptor into the parcel at the current dataPosition(),
793 * growing dataCapacity() if needed.
Dan Egnorb3e4ef32010-07-20 09:03:35 -0700794 *
795 * <p class="caution">The file descriptor will not be closed, which may
796 * result in file descriptor leaks when objects are returned from Binder
797 * calls. Use {@link ParcelFileDescriptor#writeToParcel} instead, which
798 * accepts contextual flags and will close the original file descriptor
799 * if {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} is set.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400801 public final void writeFileDescriptor(@NonNull FileDescriptor val) {
Adrian Roos04505652015-10-22 16:12:01 -0700802 updateNativeSize(nativeWriteFileDescriptor(mNativePtr, val));
803 }
804
805 private void updateNativeSize(long newNativeSize) {
806 if (mOwnsNativeParcelObject) {
807 if (newNativeSize > Integer.MAX_VALUE) {
808 newNativeSize = Integer.MAX_VALUE;
809 }
810 if (newNativeSize != mNativeSize) {
811 int delta = (int) (newNativeSize - mNativeSize);
812 if (delta > 0) {
813 VMRuntime.getRuntime().registerNativeAllocation(delta);
814 } else {
815 VMRuntime.getRuntime().registerNativeFree(-delta);
816 }
817 mNativeSize = newNativeSize;
818 }
819 }
Jeff Sharkey047238c2012-03-07 16:51:38 -0800820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821
822 /**
Casey Dahlin2f974b22015-11-05 12:19:13 -0800823 * {@hide}
824 * This will be the new name for writeFileDescriptor, for consistency.
825 **/
Jake Whartonb1f474c2018-05-30 15:11:13 -0400826 public final void writeRawFileDescriptor(@NonNull FileDescriptor val) {
Casey Dahlin2f974b22015-11-05 12:19:13 -0800827 nativeWriteFileDescriptor(mNativePtr, val);
828 }
829
830 /**
831 * {@hide}
832 * Write an array of FileDescriptor objects into the Parcel.
833 *
834 * @param value The array of objects to be written.
835 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400836 public final void writeRawFileDescriptorArray(@Nullable FileDescriptor[] value) {
Casey Dahlin2f974b22015-11-05 12:19:13 -0800837 if (value != null) {
838 int N = value.length;
839 writeInt(N);
840 for (int i=0; i<N; i++) {
841 writeRawFileDescriptor(value[i]);
842 }
843 } else {
844 writeInt(-1);
845 }
846 }
847
848 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900849 * Write a byte value into the parcel at the current dataPosition(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 * growing dataCapacity() if needed.
Jake Wharton2ffa4312018-04-23 17:47:14 -0400851 *
852 * <p>Note: This method currently delegates to writeInt but may change in
853 * the future.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 */
855 public final void writeByte(byte val) {
856 writeInt(val);
857 }
858
859 /**
860 * Please use {@link #writeBundle} instead. Flattens a Map into the parcel
861 * at the current dataPosition(),
862 * growing dataCapacity() if needed. The Map keys must be String objects.
863 * The Map values are written using {@link #writeValue} and must follow
864 * the specification there.
Samuel Tana8036662015-11-23 14:36:00 -0800865 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 * <p>It is strongly recommended to use {@link #writeBundle} instead of
867 * this method, since the Bundle class provides a type-safe API that
868 * allows you to avoid mysterious type errors at the point of marshalling.
869 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400870 public final void writeMap(@Nullable Map val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700871 writeMapInternal((Map<String, Object>) val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 }
873
874 /**
875 * Flatten a Map into the parcel at the current dataPosition(),
876 * growing dataCapacity() if needed. The Map keys must be String objects.
877 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400878 /* package */ void writeMapInternal(@Nullable Map<String,Object> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 if (val == null) {
880 writeInt(-1);
881 return;
882 }
883 Set<Map.Entry<String,Object>> entries = val.entrySet();
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700884 int size = entries.size();
885 writeInt(size);
886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 for (Map.Entry<String,Object> e : entries) {
888 writeValue(e.getKey());
889 writeValue(e.getValue());
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700890 size--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 }
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700892
893 if (size != 0) {
894 throw new BadParcelableException("Map size does not match number of entries!");
895 }
896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 }
898
899 /**
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700900 * Flatten an ArrayMap into the parcel at the current dataPosition(),
901 * growing dataCapacity() if needed. The Map keys must be String objects.
902 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400903 /* package */ void writeArrayMapInternal(@Nullable ArrayMap<String, Object> val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700904 if (val == null) {
905 writeInt(-1);
906 return;
907 }
Samuel Tan3cefe6a2015-12-14 13:29:17 -0800908 // Keep the format of this Parcel in sync with writeToParcelInner() in
909 // frameworks/native/libs/binder/PersistableBundle.cpp.
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700910 final int N = val.size();
911 writeInt(N);
Dianne Hackborne784d1e2013-09-20 18:13:52 -0700912 if (DEBUG_ARRAY_MAP) {
913 RuntimeException here = new RuntimeException("here");
914 here.fillInStackTrace();
915 Log.d(TAG, "Writing " + N + " ArrayMap entries", here);
916 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700917 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700918 for (int i=0; i<N; i++) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700919 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700920 writeString(val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700921 writeValue(val.valueAt(i));
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700922 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Write #" + i + " "
923 + (dataPosition()-startPos) + " bytes: key=0x"
924 + Integer.toHexString(val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0)
925 + " " + val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700926 }
927 }
928
929 /**
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700930 * @hide For testing only.
931 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400932 public void writeArrayMap(@Nullable ArrayMap<String, Object> val) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700933 writeArrayMapInternal(val);
934 }
935
936 /**
Svet Ganov8455ba22019-01-02 13:05:56 -0800937 * Flatten an {@link ArrayMap} with string keys containing a particular object
938 * type into the parcel at the current dataPosition() and growing dataCapacity()
939 * if needed. The type of the objects in the array must be one that implements
940 * Parcelable. Only the raw data of the objects is written and not their type,
941 * so you must use the corresponding {@link #createTypedArrayMap(Parcelable.Creator)}
942 *
943 * @param val The map of objects to be written.
944 * @param parcelableFlags The parcelable flags to use.
945 *
946 * @see #createTypedArrayMap(Parcelable.Creator)
947 * @see Parcelable
948 */
949 public <T extends Parcelable> void writeTypedArrayMap(@Nullable ArrayMap<String, T> val,
950 int parcelableFlags) {
951 if (val == null) {
952 writeInt(-1);
953 return;
954 }
955 final int count = val.size();
956 writeInt(count);
957 for (int i = 0; i < count; i++) {
958 writeString(val.keyAt(i));
959 writeTypedObject(val.valueAt(i), parcelableFlags);
960 }
961 }
962
963 /**
Svet Ganovddb94882016-06-23 19:55:24 -0700964 * Write an array set to the parcel.
965 *
966 * @param val The array set to write.
967 *
968 * @hide
969 */
970 public void writeArraySet(@Nullable ArraySet<? extends Object> val) {
971 final int size = (val != null) ? val.size() : -1;
972 writeInt(size);
973 for (int i = 0; i < size; i++) {
974 writeValue(val.valueAt(i));
975 }
976 }
977
978 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 * Flatten a Bundle into the parcel at the current dataPosition(),
980 * growing dataCapacity() if needed.
981 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400982 public final void writeBundle(@Nullable Bundle val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 if (val == null) {
984 writeInt(-1);
985 return;
986 }
987
Dianne Hackborn6aff9052009-05-22 13:20:23 -0700988 val.writeToParcel(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 }
990
991 /**
Craig Mautner719e6b12014-04-04 20:29:41 -0700992 * Flatten a PersistableBundle into the parcel at the current dataPosition(),
993 * growing dataCapacity() if needed.
994 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400995 public final void writePersistableBundle(@Nullable PersistableBundle val) {
Craig Mautner719e6b12014-04-04 20:29:41 -0700996 if (val == null) {
997 writeInt(-1);
998 return;
999 }
1000
1001 val.writeToParcel(this, 0);
1002 }
1003
1004 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001005 * Flatten a Size into the parcel at the current dataPosition(),
1006 * growing dataCapacity() if needed.
1007 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001008 public final void writeSize(@NonNull Size val) {
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001009 writeInt(val.getWidth());
1010 writeInt(val.getHeight());
1011 }
1012
1013 /**
1014 * Flatten a SizeF into the parcel at the current dataPosition(),
1015 * growing dataCapacity() if needed.
1016 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001017 public final void writeSizeF(@NonNull SizeF val) {
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001018 writeFloat(val.getWidth());
1019 writeFloat(val.getHeight());
1020 }
1021
1022 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 * Flatten a List into the parcel at the current dataPosition(), growing
1024 * dataCapacity() if needed. The List values are written using
1025 * {@link #writeValue} and must follow the specification there.
1026 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001027 public final void writeList(@Nullable List val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 if (val == null) {
1029 writeInt(-1);
1030 return;
1031 }
1032 int N = val.size();
1033 int i=0;
1034 writeInt(N);
1035 while (i < N) {
1036 writeValue(val.get(i));
1037 i++;
1038 }
1039 }
1040
1041 /**
1042 * Flatten an Object array into the parcel at the current dataPosition(),
1043 * growing dataCapacity() if needed. The array values are written using
1044 * {@link #writeValue} and must follow the specification there.
1045 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001046 public final void writeArray(@Nullable Object[] 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.length;
1052 int i=0;
1053 writeInt(N);
1054 while (i < N) {
1055 writeValue(val[i]);
1056 i++;
1057 }
1058 }
1059
1060 /**
1061 * Flatten a generic SparseArray into the parcel at the current
1062 * dataPosition(), growing dataCapacity() if needed. The SparseArray
1063 * values are written using {@link #writeValue} and must follow the
1064 * specification there.
1065 */
Svet Ganov8455ba22019-01-02 13:05:56 -08001066 public final <T> void writeSparseArray(@Nullable SparseArray<T> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 if (val == null) {
1068 writeInt(-1);
1069 return;
1070 }
1071 int N = val.size();
1072 writeInt(N);
1073 int i=0;
1074 while (i < N) {
1075 writeInt(val.keyAt(i));
1076 writeValue(val.valueAt(i));
1077 i++;
1078 }
1079 }
1080
Jake Whartonb1f474c2018-05-30 15:11:13 -04001081 public final void writeSparseBooleanArray(@Nullable SparseBooleanArray val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 if (val == null) {
1083 writeInt(-1);
1084 return;
1085 }
1086 int N = val.size();
1087 writeInt(N);
1088 int i=0;
1089 while (i < N) {
1090 writeInt(val.keyAt(i));
1091 writeByte((byte)(val.valueAt(i) ? 1 : 0));
1092 i++;
1093 }
1094 }
1095
Adam Lesinski205656d2017-03-23 13:38:26 -07001096 /**
1097 * @hide
1098 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001099 public final void writeSparseIntArray(@Nullable SparseIntArray val) {
Adam Lesinski4e862812016-11-21 16:02:24 -08001100 if (val == null) {
1101 writeInt(-1);
1102 return;
1103 }
1104 int N = val.size();
1105 writeInt(N);
1106 int i=0;
1107 while (i < N) {
1108 writeInt(val.keyAt(i));
1109 writeInt(val.valueAt(i));
1110 i++;
1111 }
1112 }
1113
Jake Whartonb1f474c2018-05-30 15:11:13 -04001114 public final void writeBooleanArray(@Nullable boolean[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 if (val != null) {
1116 int N = val.length;
1117 writeInt(N);
1118 for (int i=0; i<N; i++) {
1119 writeInt(val[i] ? 1 : 0);
1120 }
1121 } else {
1122 writeInt(-1);
1123 }
1124 }
1125
Jake Whartonb1f474c2018-05-30 15:11:13 -04001126 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 public final boolean[] createBooleanArray() {
1128 int N = readInt();
1129 // >>2 as a fast divide-by-4 works in the create*Array() functions
1130 // because dataAvail() will never return a negative number. 4 is
1131 // the size of a stored boolean in the stream.
1132 if (N >= 0 && N <= (dataAvail() >> 2)) {
1133 boolean[] val = new boolean[N];
1134 for (int i=0; i<N; i++) {
1135 val[i] = readInt() != 0;
1136 }
1137 return val;
1138 } else {
1139 return null;
1140 }
1141 }
1142
Jake Whartonb1f474c2018-05-30 15:11:13 -04001143 public final void readBooleanArray(@NonNull boolean[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 int N = readInt();
1145 if (N == val.length) {
1146 for (int i=0; i<N; i++) {
1147 val[i] = readInt() != 0;
1148 }
1149 } else {
1150 throw new RuntimeException("bad array lengths");
1151 }
1152 }
1153
Jake Whartonb1f474c2018-05-30 15:11:13 -04001154 public final void writeCharArray(@Nullable char[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 if (val != null) {
1156 int N = val.length;
1157 writeInt(N);
1158 for (int i=0; i<N; i++) {
1159 writeInt((int)val[i]);
1160 }
1161 } else {
1162 writeInt(-1);
1163 }
1164 }
1165
Jake Whartonb1f474c2018-05-30 15:11:13 -04001166 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 public final char[] createCharArray() {
1168 int N = readInt();
1169 if (N >= 0 && N <= (dataAvail() >> 2)) {
1170 char[] val = new char[N];
1171 for (int i=0; i<N; i++) {
1172 val[i] = (char)readInt();
1173 }
1174 return val;
1175 } else {
1176 return null;
1177 }
1178 }
1179
Jake Whartonb1f474c2018-05-30 15:11:13 -04001180 public final void readCharArray(@NonNull char[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 int N = readInt();
1182 if (N == val.length) {
1183 for (int i=0; i<N; i++) {
1184 val[i] = (char)readInt();
1185 }
1186 } else {
1187 throw new RuntimeException("bad array lengths");
1188 }
1189 }
1190
Jake Whartonb1f474c2018-05-30 15:11:13 -04001191 public final void writeIntArray(@Nullable int[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 if (val != null) {
1193 int N = val.length;
1194 writeInt(N);
1195 for (int i=0; i<N; i++) {
1196 writeInt(val[i]);
1197 }
1198 } else {
1199 writeInt(-1);
1200 }
1201 }
1202
Jake Whartonb1f474c2018-05-30 15:11:13 -04001203 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 public final int[] createIntArray() {
1205 int N = readInt();
1206 if (N >= 0 && N <= (dataAvail() >> 2)) {
1207 int[] val = new int[N];
1208 for (int i=0; i<N; i++) {
1209 val[i] = readInt();
1210 }
1211 return val;
1212 } else {
1213 return null;
1214 }
1215 }
1216
Jake Whartonb1f474c2018-05-30 15:11:13 -04001217 public final void readIntArray(@NonNull int[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 int N = readInt();
1219 if (N == val.length) {
1220 for (int i=0; i<N; i++) {
1221 val[i] = readInt();
1222 }
1223 } else {
1224 throw new RuntimeException("bad array lengths");
1225 }
1226 }
1227
Jake Whartonb1f474c2018-05-30 15:11:13 -04001228 public final void writeLongArray(@Nullable long[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 if (val != null) {
1230 int N = val.length;
1231 writeInt(N);
1232 for (int i=0; i<N; i++) {
1233 writeLong(val[i]);
1234 }
1235 } else {
1236 writeInt(-1);
1237 }
1238 }
1239
Jake Whartonb1f474c2018-05-30 15:11:13 -04001240 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 public final long[] createLongArray() {
1242 int N = readInt();
1243 // >>3 because stored longs are 64 bits
1244 if (N >= 0 && N <= (dataAvail() >> 3)) {
1245 long[] val = new long[N];
1246 for (int i=0; i<N; i++) {
1247 val[i] = readLong();
1248 }
1249 return val;
1250 } else {
1251 return null;
1252 }
1253 }
1254
Jake Whartonb1f474c2018-05-30 15:11:13 -04001255 public final void readLongArray(@NonNull long[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 int N = readInt();
1257 if (N == val.length) {
1258 for (int i=0; i<N; i++) {
1259 val[i] = readLong();
1260 }
1261 } else {
1262 throw new RuntimeException("bad array lengths");
1263 }
1264 }
1265
Jake Whartonb1f474c2018-05-30 15:11:13 -04001266 public final void writeFloatArray(@Nullable float[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 if (val != null) {
1268 int N = val.length;
1269 writeInt(N);
1270 for (int i=0; i<N; i++) {
1271 writeFloat(val[i]);
1272 }
1273 } else {
1274 writeInt(-1);
1275 }
1276 }
1277
Jake Whartonb1f474c2018-05-30 15:11:13 -04001278 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 public final float[] createFloatArray() {
1280 int N = readInt();
1281 // >>2 because stored floats are 4 bytes
1282 if (N >= 0 && N <= (dataAvail() >> 2)) {
1283 float[] val = new float[N];
1284 for (int i=0; i<N; i++) {
1285 val[i] = readFloat();
1286 }
1287 return val;
1288 } else {
1289 return null;
1290 }
1291 }
1292
Jake Whartonb1f474c2018-05-30 15:11:13 -04001293 public final void readFloatArray(@NonNull float[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 int N = readInt();
1295 if (N == val.length) {
1296 for (int i=0; i<N; i++) {
1297 val[i] = readFloat();
1298 }
1299 } else {
1300 throw new RuntimeException("bad array lengths");
1301 }
1302 }
1303
Jake Whartonb1f474c2018-05-30 15:11:13 -04001304 public final void writeDoubleArray(@Nullable double[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 if (val != null) {
1306 int N = val.length;
1307 writeInt(N);
1308 for (int i=0; i<N; i++) {
1309 writeDouble(val[i]);
1310 }
1311 } else {
1312 writeInt(-1);
1313 }
1314 }
1315
Jake Whartonb1f474c2018-05-30 15:11:13 -04001316 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 public final double[] createDoubleArray() {
1318 int N = readInt();
1319 // >>3 because stored doubles are 8 bytes
1320 if (N >= 0 && N <= (dataAvail() >> 3)) {
1321 double[] val = new double[N];
1322 for (int i=0; i<N; i++) {
1323 val[i] = readDouble();
1324 }
1325 return val;
1326 } else {
1327 return null;
1328 }
1329 }
1330
Jake Whartonb1f474c2018-05-30 15:11:13 -04001331 public final void readDoubleArray(@NonNull double[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 int N = readInt();
1333 if (N == val.length) {
1334 for (int i=0; i<N; i++) {
1335 val[i] = readDouble();
1336 }
1337 } else {
1338 throw new RuntimeException("bad array lengths");
1339 }
1340 }
1341
Jake Whartonb1f474c2018-05-30 15:11:13 -04001342 public final void writeStringArray(@Nullable String[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 if (val != null) {
1344 int N = val.length;
1345 writeInt(N);
1346 for (int i=0; i<N; i++) {
1347 writeString(val[i]);
1348 }
1349 } else {
1350 writeInt(-1);
1351 }
1352 }
1353
Jake Whartonb1f474c2018-05-30 15:11:13 -04001354 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 public final String[] createStringArray() {
1356 int N = readInt();
1357 if (N >= 0) {
1358 String[] val = new String[N];
1359 for (int i=0; i<N; i++) {
1360 val[i] = readString();
1361 }
1362 return val;
1363 } else {
1364 return null;
1365 }
1366 }
1367
Jake Whartonb1f474c2018-05-30 15:11:13 -04001368 public final void readStringArray(@NonNull String[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 int N = readInt();
1370 if (N == val.length) {
1371 for (int i=0; i<N; i++) {
1372 val[i] = readString();
1373 }
1374 } else {
1375 throw new RuntimeException("bad array lengths");
1376 }
1377 }
1378
Jake Whartonb1f474c2018-05-30 15:11:13 -04001379 public final void writeBinderArray(@Nullable IBinder[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 if (val != null) {
1381 int N = val.length;
1382 writeInt(N);
1383 for (int i=0; i<N; i++) {
1384 writeStrongBinder(val[i]);
1385 }
1386 } else {
1387 writeInt(-1);
1388 }
1389 }
1390
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001391 /**
1392 * @hide
1393 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001394 public final void writeCharSequenceArray(@Nullable CharSequence[] val) {
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001395 if (val != null) {
1396 int N = val.length;
1397 writeInt(N);
1398 for (int i=0; i<N; i++) {
1399 writeCharSequence(val[i]);
1400 }
1401 } else {
1402 writeInt(-1);
1403 }
1404 }
1405
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001406 /**
1407 * @hide
1408 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001409 public final void writeCharSequenceList(@Nullable ArrayList<CharSequence> val) {
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001410 if (val != null) {
1411 int N = val.size();
1412 writeInt(N);
1413 for (int i=0; i<N; i++) {
1414 writeCharSequence(val.get(i));
1415 }
1416 } else {
1417 writeInt(-1);
1418 }
1419 }
1420
Jake Whartonb1f474c2018-05-30 15:11:13 -04001421 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 public final IBinder[] createBinderArray() {
1423 int N = readInt();
1424 if (N >= 0) {
1425 IBinder[] val = new IBinder[N];
1426 for (int i=0; i<N; i++) {
1427 val[i] = readStrongBinder();
1428 }
1429 return val;
1430 } else {
1431 return null;
1432 }
1433 }
1434
Jake Whartonb1f474c2018-05-30 15:11:13 -04001435 public final void readBinderArray(@NonNull IBinder[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 int N = readInt();
1437 if (N == val.length) {
1438 for (int i=0; i<N; i++) {
1439 val[i] = readStrongBinder();
1440 }
1441 } else {
1442 throw new RuntimeException("bad array lengths");
1443 }
1444 }
1445
1446 /**
1447 * Flatten a List containing a particular object type into the parcel, at
1448 * the current dataPosition() and growing dataCapacity() if needed. The
1449 * type of the objects in the list must be one that implements Parcelable.
1450 * Unlike the generic writeList() method, however, only the raw data of the
1451 * objects is written and not their type, so you must use the corresponding
1452 * readTypedList() to unmarshall them.
1453 *
1454 * @param val The list of objects to be written.
1455 *
1456 * @see #createTypedArrayList
1457 * @see #readTypedList
1458 * @see Parcelable
1459 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001460 public final <T extends Parcelable> void writeTypedList(@Nullable List<T> val) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001461 writeTypedList(val, 0);
1462 }
1463
1464 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08001465 * Flatten a {@link SparseArray} containing a particular object type into the parcel
1466 * at the current dataPosition() and growing dataCapacity() if needed. The
1467 * type of the objects in the array must be one that implements Parcelable.
1468 * Unlike the generic {@link #writeSparseArray(SparseArray)} method, however, only
1469 * the raw data of the objects is written and not their type, so you must use the
1470 * corresponding {@link #createTypedSparseArray(Parcelable.Creator)}.
1471 *
1472 * @param val The list of objects to be written.
1473 * @param parcelableFlags The parcelable flags to use.
1474 *
1475 * @see #createTypedSparseArray(Parcelable.Creator)
1476 * @see Parcelable
1477 */
1478 public final <T extends Parcelable> void writeTypedSparseArray(@Nullable SparseArray<T> val,
1479 int parcelableFlags) {
1480 if (val == null) {
1481 writeInt(-1);
1482 return;
1483 }
1484 final int count = val.size();
1485 writeInt(count);
1486 for (int i = 0; i < count; i++) {
1487 writeInt(val.keyAt(i));
1488 writeTypedObject(val.valueAt(i), parcelableFlags);
1489 }
1490 }
1491
1492 /**
Sunny Goyal0e60f222017-09-21 21:39:20 -07001493 * @hide
1494 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001495 public <T extends Parcelable> void writeTypedList(@Nullable List<T> val, int parcelableFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 if (val == null) {
1497 writeInt(-1);
1498 return;
1499 }
1500 int N = val.size();
1501 int i=0;
1502 writeInt(N);
1503 while (i < N) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001504 writeTypedObject(val.get(i), parcelableFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 i++;
1506 }
1507 }
1508
1509 /**
1510 * Flatten a List containing String objects into the parcel, at
1511 * the current dataPosition() and growing dataCapacity() if needed. They
1512 * can later be retrieved with {@link #createStringArrayList} or
1513 * {@link #readStringList}.
1514 *
1515 * @param val The list of strings to be written.
1516 *
1517 * @see #createStringArrayList
1518 * @see #readStringList
1519 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001520 public final void writeStringList(@Nullable List<String> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 if (val == null) {
1522 writeInt(-1);
1523 return;
1524 }
1525 int N = val.size();
1526 int i=0;
1527 writeInt(N);
1528 while (i < N) {
1529 writeString(val.get(i));
1530 i++;
1531 }
1532 }
1533
1534 /**
1535 * Flatten a List containing IBinder objects into the parcel, at
1536 * the current dataPosition() and growing dataCapacity() if needed. They
1537 * can later be retrieved with {@link #createBinderArrayList} or
1538 * {@link #readBinderList}.
1539 *
1540 * @param val The list of strings to be written.
1541 *
1542 * @see #createBinderArrayList
1543 * @see #readBinderList
1544 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001545 public final void writeBinderList(@Nullable List<IBinder> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 if (val == null) {
1547 writeInt(-1);
1548 return;
1549 }
1550 int N = val.size();
1551 int i=0;
1552 writeInt(N);
1553 while (i < N) {
1554 writeStrongBinder(val.get(i));
1555 i++;
1556 }
1557 }
1558
1559 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00001560 * Flatten a {@code List} containing arbitrary {@code Parcelable} objects into this parcel
1561 * at the current position. They can later be retrieved using
1562 * {@link #readParcelableList(List, ClassLoader)} if required.
1563 *
1564 * @see #readParcelableList(List, ClassLoader)
Narayan Kamathbea48712016-12-01 15:38:28 +00001565 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001566 public final <T extends Parcelable> void writeParcelableList(@Nullable List<T> val, int flags) {
Narayan Kamathbea48712016-12-01 15:38:28 +00001567 if (val == null) {
1568 writeInt(-1);
1569 return;
1570 }
1571
1572 int N = val.size();
1573 int i=0;
1574 writeInt(N);
1575 while (i < N) {
1576 writeParcelable(val.get(i), flags);
1577 i++;
1578 }
1579 }
1580
1581 /**
Svet Ganov0f4928f2017-02-02 20:02:51 -08001582 * Flatten a homogeneous array containing a particular object type into
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 * the parcel, at
1584 * the current dataPosition() and growing dataCapacity() if needed. The
1585 * type of the objects in the array must be one that implements Parcelable.
1586 * Unlike the {@link #writeParcelableArray} method, however, only the
1587 * raw data of the objects is written and not their type, so you must use
1588 * {@link #readTypedArray} with the correct corresponding
1589 * {@link Parcelable.Creator} implementation to unmarshall them.
1590 *
1591 * @param val The array of objects to be written.
1592 * @param parcelableFlags Contextual flags as per
1593 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1594 *
1595 * @see #readTypedArray
1596 * @see #writeParcelableArray
1597 * @see Parcelable.Creator
1598 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001599 public final <T extends Parcelable> void writeTypedArray(@Nullable T[] val,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 int parcelableFlags) {
1601 if (val != null) {
1602 int N = val.length;
1603 writeInt(N);
Svet Ganov0f4928f2017-02-02 20:02:51 -08001604 for (int i = 0; i < N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001605 writeTypedObject(val[i], parcelableFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 }
1607 } else {
1608 writeInt(-1);
1609 }
1610 }
1611
1612 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001613 * Flatten the Parcelable object into the parcel.
1614 *
1615 * @param val The Parcelable object to be written.
1616 * @param parcelableFlags Contextual flags as per
1617 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1618 *
1619 * @see #readTypedObject
1620 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001621 public final <T extends Parcelable> void writeTypedObject(@Nullable T val,
1622 int parcelableFlags) {
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001623 if (val != null) {
1624 writeInt(1);
1625 val.writeToParcel(this, parcelableFlags);
1626 } else {
1627 writeInt(0);
1628 }
1629 }
1630
1631 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 * Flatten a generic object in to a parcel. The given Object value may
1633 * currently be one of the following types:
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001634 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 * <ul>
1636 * <li> null
1637 * <li> String
1638 * <li> Byte
1639 * <li> Short
1640 * <li> Integer
1641 * <li> Long
1642 * <li> Float
1643 * <li> Double
1644 * <li> Boolean
1645 * <li> String[]
1646 * <li> boolean[]
1647 * <li> byte[]
1648 * <li> int[]
1649 * <li> long[]
1650 * <li> Object[] (supporting objects of the same type defined here).
1651 * <li> {@link Bundle}
1652 * <li> Map (as supported by {@link #writeMap}).
1653 * <li> Any object that implements the {@link Parcelable} protocol.
1654 * <li> Parcelable[]
1655 * <li> CharSequence (as supported by {@link TextUtils#writeToParcel}).
1656 * <li> List (as supported by {@link #writeList}).
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001657 * <li> {@link SparseArray} (as supported by {@link #writeSparseArray(SparseArray)}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 * <li> {@link IBinder}
1659 * <li> Any object that implements Serializable (but see
1660 * {@link #writeSerializable} for caveats). Note that all of the
1661 * previous types have relatively efficient implementations for
1662 * writing to a Parcel; having to rely on the generic serialization
1663 * approach is much less efficient and should be avoided whenever
1664 * possible.
1665 * </ul>
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001666 *
1667 * <p class="caution">{@link Parcelable} objects are written with
1668 * {@link Parcelable#writeToParcel} using contextual flags of 0. When
1669 * serializing objects containing {@link ParcelFileDescriptor}s,
1670 * this may result in file descriptor leaks when they are returned from
1671 * Binder calls (where {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE}
1672 * should be used).</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001674 public final void writeValue(@Nullable Object v) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 if (v == null) {
1676 writeInt(VAL_NULL);
1677 } else if (v instanceof String) {
1678 writeInt(VAL_STRING);
1679 writeString((String) v);
1680 } else if (v instanceof Integer) {
1681 writeInt(VAL_INTEGER);
1682 writeInt((Integer) v);
1683 } else if (v instanceof Map) {
1684 writeInt(VAL_MAP);
1685 writeMap((Map) v);
1686 } else if (v instanceof Bundle) {
1687 // Must be before Parcelable
1688 writeInt(VAL_BUNDLE);
1689 writeBundle((Bundle) v);
Samuel Tanceafe5e2015-12-11 16:50:58 -08001690 } else if (v instanceof PersistableBundle) {
1691 writeInt(VAL_PERSISTABLEBUNDLE);
1692 writePersistableBundle((PersistableBundle) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 } else if (v instanceof Parcelable) {
Samuel Tanceafe5e2015-12-11 16:50:58 -08001694 // IMPOTANT: cases for classes that implement Parcelable must
1695 // come before the Parcelable case, so that their specific VAL_*
1696 // types will be written.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 writeInt(VAL_PARCELABLE);
1698 writeParcelable((Parcelable) v, 0);
1699 } else if (v instanceof Short) {
1700 writeInt(VAL_SHORT);
1701 writeInt(((Short) v).intValue());
1702 } else if (v instanceof Long) {
1703 writeInt(VAL_LONG);
1704 writeLong((Long) v);
1705 } else if (v instanceof Float) {
1706 writeInt(VAL_FLOAT);
1707 writeFloat((Float) v);
1708 } else if (v instanceof Double) {
1709 writeInt(VAL_DOUBLE);
1710 writeDouble((Double) v);
1711 } else if (v instanceof Boolean) {
1712 writeInt(VAL_BOOLEAN);
1713 writeInt((Boolean) v ? 1 : 0);
1714 } else if (v instanceof CharSequence) {
1715 // Must be after String
1716 writeInt(VAL_CHARSEQUENCE);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001717 writeCharSequence((CharSequence) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 } else if (v instanceof List) {
1719 writeInt(VAL_LIST);
1720 writeList((List) v);
1721 } else if (v instanceof SparseArray) {
1722 writeInt(VAL_SPARSEARRAY);
1723 writeSparseArray((SparseArray) v);
1724 } else if (v instanceof boolean[]) {
1725 writeInt(VAL_BOOLEANARRAY);
1726 writeBooleanArray((boolean[]) v);
1727 } else if (v instanceof byte[]) {
1728 writeInt(VAL_BYTEARRAY);
1729 writeByteArray((byte[]) v);
1730 } else if (v instanceof String[]) {
1731 writeInt(VAL_STRINGARRAY);
1732 writeStringArray((String[]) v);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001733 } else if (v instanceof CharSequence[]) {
1734 // Must be after String[] and before Object[]
1735 writeInt(VAL_CHARSEQUENCEARRAY);
1736 writeCharSequenceArray((CharSequence[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 } else if (v instanceof IBinder) {
1738 writeInt(VAL_IBINDER);
1739 writeStrongBinder((IBinder) v);
1740 } else if (v instanceof Parcelable[]) {
1741 writeInt(VAL_PARCELABLEARRAY);
1742 writeParcelableArray((Parcelable[]) v, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 } else if (v instanceof int[]) {
1744 writeInt(VAL_INTARRAY);
1745 writeIntArray((int[]) v);
1746 } else if (v instanceof long[]) {
1747 writeInt(VAL_LONGARRAY);
1748 writeLongArray((long[]) v);
1749 } else if (v instanceof Byte) {
1750 writeInt(VAL_BYTE);
1751 writeInt((Byte) v);
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001752 } else if (v instanceof Size) {
1753 writeInt(VAL_SIZE);
1754 writeSize((Size) v);
1755 } else if (v instanceof SizeF) {
1756 writeInt(VAL_SIZEF);
1757 writeSizeF((SizeF) v);
Samuel Tana8036662015-11-23 14:36:00 -08001758 } else if (v instanceof double[]) {
1759 writeInt(VAL_DOUBLEARRAY);
1760 writeDoubleArray((double[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 } else {
Paul Duffinac5a0822014-02-03 15:02:17 +00001762 Class<?> clazz = v.getClass();
1763 if (clazz.isArray() && clazz.getComponentType() == Object.class) {
1764 // Only pure Object[] are written here, Other arrays of non-primitive types are
1765 // handled by serialization as this does not record the component type.
1766 writeInt(VAL_OBJECTARRAY);
1767 writeArray((Object[]) v);
1768 } else if (v instanceof Serializable) {
1769 // Must be last
1770 writeInt(VAL_SERIALIZABLE);
1771 writeSerializable((Serializable) v);
1772 } else {
1773 throw new RuntimeException("Parcel: unable to marshal value " + v);
1774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 }
1776 }
1777
1778 /**
1779 * Flatten the name of the class of the Parcelable and its contents
1780 * into the parcel.
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001781 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 * @param p The Parcelable object to be written.
1783 * @param parcelableFlags Contextual flags as per
1784 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1785 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001786 public final void writeParcelable(@Nullable Parcelable p, int parcelableFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 if (p == null) {
1788 writeString(null);
1789 return;
1790 }
Neil Fuller44e440c2015-04-20 14:39:00 +01001791 writeParcelableCreator(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 p.writeToParcel(this, parcelableFlags);
1793 }
1794
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001795 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001796 public final void writeParcelableCreator(@NonNull Parcelable p) {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001797 String name = p.getClass().getName();
1798 writeString(name);
1799 }
1800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 /**
1802 * Write a generic serializable object in to a Parcel. It is strongly
1803 * recommended that this method be avoided, since the serialization
1804 * overhead is extremely large, and this approach will be much slower than
1805 * using the other approaches to writing data in to a Parcel.
1806 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001807 public final void writeSerializable(@Nullable Serializable s) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 if (s == null) {
1809 writeString(null);
1810 return;
1811 }
1812 String name = s.getClass().getName();
1813 writeString(name);
1814
1815 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1816 try {
1817 ObjectOutputStream oos = new ObjectOutputStream(baos);
1818 oos.writeObject(s);
1819 oos.close();
1820
1821 writeByteArray(baos.toByteArray());
1822 } catch (IOException ioe) {
1823 throw new RuntimeException("Parcelable encountered " +
1824 "IOException writing serializable object (name = " + name +
1825 ")", ioe);
1826 }
1827 }
1828
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001829 /** @hide For debugging purposes */
1830 public static void setStackTraceParceling(boolean enabled) {
1831 sParcelExceptionStackTrace = enabled;
1832 }
1833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 /**
1835 * Special function for writing an exception result at the header of
1836 * a parcel, to be used when returning an exception from a transaction.
1837 * Note that this currently only supports a few exception types; any other
1838 * exception will be re-thrown by this function as a RuntimeException
1839 * (to be caught by the system's last-resort exception handling when
1840 * dispatching a transaction).
Samuel Tana8036662015-11-23 14:36:00 -08001841 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 * <p>The supported exception types are:
1843 * <ul>
1844 * <li>{@link BadParcelableException}
1845 * <li>{@link IllegalArgumentException}
1846 * <li>{@link IllegalStateException}
1847 * <li>{@link NullPointerException}
1848 * <li>{@link SecurityException}
Dianne Hackborn18482ae2017-08-01 17:41:00 -07001849 * <li>{@link UnsupportedOperationException}
Dianne Hackborn7e714422013-09-13 17:32:57 -07001850 * <li>{@link NetworkOnMainThreadException}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 * </ul>
Samuel Tana8036662015-11-23 14:36:00 -08001852 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 * @param e The Exception to be written.
1854 *
1855 * @see #writeNoException
1856 * @see #readException
1857 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001858 public final void writeException(@NonNull Exception e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 int code = 0;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001860 if (e instanceof Parcelable
1861 && (e.getClass().getClassLoader() == Parcelable.class.getClassLoader())) {
1862 // We only send Parcelable exceptions that are in the
1863 // BootClassLoader to ensure that the receiver can unpack them
1864 code = EX_PARCELABLE;
1865 } else if (e instanceof SecurityException) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 code = EX_SECURITY;
1867 } else if (e instanceof BadParcelableException) {
1868 code = EX_BAD_PARCELABLE;
1869 } else if (e instanceof IllegalArgumentException) {
1870 code = EX_ILLEGAL_ARGUMENT;
1871 } else if (e instanceof NullPointerException) {
1872 code = EX_NULL_POINTER;
1873 } else if (e instanceof IllegalStateException) {
1874 code = EX_ILLEGAL_STATE;
Dianne Hackborn7e714422013-09-13 17:32:57 -07001875 } else if (e instanceof NetworkOnMainThreadException) {
1876 code = EX_NETWORK_MAIN_THREAD;
Dianne Hackborn33d738a2014-09-12 14:23:58 -07001877 } else if (e instanceof UnsupportedOperationException) {
1878 code = EX_UNSUPPORTED_OPERATION;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001879 } else if (e instanceof ServiceSpecificException) {
1880 code = EX_SERVICE_SPECIFIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 }
1882 writeInt(code);
Brad Fitzpatrick703e5d32010-07-15 13:16:41 -07001883 StrictMode.clearGatheredViolations();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 if (code == 0) {
1885 if (e instanceof RuntimeException) {
1886 throw (RuntimeException) e;
1887 }
1888 throw new RuntimeException(e);
1889 }
1890 writeString(e.getMessage());
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001891 final long timeNow = sParcelExceptionStackTrace ? SystemClock.elapsedRealtime() : 0;
1892 if (sParcelExceptionStackTrace && (timeNow - sLastWriteExceptionStackTrace
1893 > WRITE_EXCEPTION_STACK_TRACE_THRESHOLD_MS)) {
1894 sLastWriteExceptionStackTrace = timeNow;
1895 final int sizePosition = dataPosition();
1896 writeInt(0); // Header size will be filled in later
1897 StackTraceElement[] stackTrace = e.getStackTrace();
1898 final int truncatedSize = Math.min(stackTrace.length, 5);
1899 StringBuilder sb = new StringBuilder();
1900 for (int i = 0; i < truncatedSize; i++) {
Fyodor Kupolov679d9982017-11-21 10:42:23 -08001901 sb.append("\tat ").append(stackTrace[i]).append('\n');
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001902 }
1903 writeString(sb.toString());
1904 final int payloadPosition = dataPosition();
1905 setDataPosition(sizePosition);
1906 // Write stack trace header size. Used in native side to skip the header
1907 writeInt(payloadPosition - sizePosition);
1908 setDataPosition(payloadPosition);
1909 } else {
1910 writeInt(0);
1911 }
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001912 switch (code) {
1913 case EX_SERVICE_SPECIFIC:
1914 writeInt(((ServiceSpecificException) e).errorCode);
1915 break;
1916 case EX_PARCELABLE:
1917 // Write parceled exception prefixed by length
1918 final int sizePosition = dataPosition();
1919 writeInt(0);
1920 writeParcelable((Parcelable) e, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
1921 final int payloadPosition = dataPosition();
1922 setDataPosition(sizePosition);
1923 writeInt(payloadPosition - sizePosition);
1924 setDataPosition(payloadPosition);
1925 break;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 }
1928
1929 /**
1930 * Special function for writing information at the front of the Parcel
1931 * indicating that no exception occurred.
1932 *
1933 * @see #writeException
1934 * @see #readException
1935 */
1936 public final void writeNoException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001937 // Despite the name of this function ("write no exception"),
1938 // it should instead be thought of as "write the RPC response
1939 // header", but because this function name is written out by
1940 // the AIDL compiler, we're not going to rename it.
1941 //
1942 // The response header, in the non-exception case (see also
1943 // writeException above, also called by the AIDL compiler), is
1944 // either a 0 (the default case), or EX_HAS_REPLY_HEADER if
1945 // StrictMode has gathered up violations that have occurred
1946 // during a Binder call, in which case we write out the number
1947 // of violations and their details, serialized, before the
1948 // actual RPC respons data. The receiving end of this is
1949 // readException(), below.
1950 if (StrictMode.hasGatheredViolations()) {
1951 writeInt(EX_HAS_REPLY_HEADER);
1952 final int sizePosition = dataPosition();
1953 writeInt(0); // total size of fat header, to be filled in later
1954 StrictMode.writeGatheredViolationsToParcel(this);
1955 final int payloadPosition = dataPosition();
1956 setDataPosition(sizePosition);
1957 writeInt(payloadPosition - sizePosition); // header size
1958 setDataPosition(payloadPosition);
1959 } else {
1960 writeInt(0);
1961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 }
1963
1964 /**
1965 * Special function for reading an exception result from the header of
1966 * a parcel, to be used after receiving the result of a transaction. This
1967 * will throw the exception for you if it had been written to the Parcel,
1968 * otherwise return and let you read the normal result data from the Parcel.
1969 *
1970 * @see #writeException
1971 * @see #writeNoException
1972 */
1973 public final void readException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001974 int code = readExceptionCode();
1975 if (code != 0) {
1976 String msg = readString();
Takamasa Kuramitsu2214b822018-04-02 14:44:59 +09001977 readException(code, msg);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001978 }
1979 }
1980
1981 /**
1982 * Parses the header of a Binder call's response Parcel and
1983 * returns the exception code. Deals with lite or fat headers.
1984 * In the common successful case, this header is generally zero.
1985 * In less common cases, it's a small negative number and will be
1986 * followed by an error string.
1987 *
1988 * This exists purely for android.database.DatabaseUtils and
1989 * insulating it from having to handle fat headers as returned by
1990 * e.g. StrictMode-induced RPC responses.
1991 *
1992 * @hide
1993 */
1994 public final int readExceptionCode() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 int code = readInt();
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001996 if (code == EX_HAS_REPLY_HEADER) {
1997 int headerSize = readInt();
1998 if (headerSize == 0) {
1999 Log.e(TAG, "Unexpected zero-sized Parcel reply header.");
2000 } else {
2001 // Currently the only thing in the header is StrictMode stacks,
2002 // but discussions around event/RPC tracing suggest we might
2003 // put that here too. If so, switch on sub-header tags here.
2004 // But for now, just parse out the StrictMode stuff.
2005 StrictMode.readAndHandleBinderCallViolations(this);
2006 }
2007 // And fat response headers are currently only used when
2008 // there are no exceptions, so return no error:
2009 return 0;
2010 }
2011 return code;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 }
2013
2014 /**
Mark Doliner879ea452014-01-02 12:38:07 -08002015 * Throw an exception with the given message. Not intended for use
2016 * outside the Parcel class.
2017 *
2018 * @param code Used to determine which exception class to throw.
2019 * @param msg The exception message.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 */
2021 public final void readException(int code, String msg) {
Takamasa Kuramitsu2214b822018-04-02 14:44:59 +09002022 String remoteStackTrace = null;
2023 final int remoteStackPayloadSize = readInt();
2024 if (remoteStackPayloadSize > 0) {
2025 remoteStackTrace = readString();
2026 }
2027 Exception e = createException(code, msg);
2028 // Attach remote stack trace if availalble
2029 if (remoteStackTrace != null) {
2030 RemoteException cause = new RemoteException(
2031 "Remote stack trace:\n" + remoteStackTrace, null, false, false);
2032 try {
2033 Throwable rootCause = ExceptionUtils.getRootCause(e);
2034 if (rootCause != null) {
2035 rootCause.initCause(cause);
2036 }
2037 } catch (RuntimeException ex) {
2038 Log.e(TAG, "Cannot set cause " + cause + " for " + e, ex);
2039 }
2040 }
2041 SneakyThrow.sneakyThrow(e);
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002042 }
2043
2044 /**
2045 * Creates an exception with the given message.
2046 *
2047 * @param code Used to determine which exception class to throw.
2048 * @param msg The exception message.
2049 */
2050 private Exception createException(int code, String msg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 switch (code) {
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002052 case EX_PARCELABLE:
2053 if (readInt() > 0) {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002054 return (Exception) readParcelable(Parcelable.class.getClassLoader());
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002055 } else {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002056 return new RuntimeException(msg + " [missing Parcelable]");
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002057 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 case EX_SECURITY:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002059 return new SecurityException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 case EX_BAD_PARCELABLE:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002061 return new BadParcelableException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 case EX_ILLEGAL_ARGUMENT:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002063 return new IllegalArgumentException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 case EX_NULL_POINTER:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002065 return new NullPointerException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 case EX_ILLEGAL_STATE:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002067 return new IllegalStateException(msg);
Dianne Hackborn7e714422013-09-13 17:32:57 -07002068 case EX_NETWORK_MAIN_THREAD:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002069 return new NetworkOnMainThreadException();
Dianne Hackborn33d738a2014-09-12 14:23:58 -07002070 case EX_UNSUPPORTED_OPERATION:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002071 return new UnsupportedOperationException(msg);
Christopher Wiley80fd1202015-11-22 17:12:37 -08002072 case EX_SERVICE_SPECIFIC:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002073 return new ServiceSpecificException(readInt(), msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 }
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002075 return new RuntimeException("Unknown exception code: " + code
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 + " msg " + msg);
2077 }
2078
2079 /**
2080 * Read an integer value from the parcel at the current dataPosition().
2081 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002082 public final int readInt() {
2083 return nativeReadInt(mNativePtr);
2084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085
2086 /**
2087 * Read a long integer value from the parcel at the current dataPosition().
2088 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002089 public final long readLong() {
2090 return nativeReadLong(mNativePtr);
2091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092
2093 /**
2094 * Read a floating point value from the parcel at the current
2095 * dataPosition().
2096 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002097 public final float readFloat() {
2098 return nativeReadFloat(mNativePtr);
2099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100
2101 /**
2102 * Read a double precision floating point value from the parcel at the
2103 * current dataPosition().
2104 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002105 public final double readDouble() {
2106 return nativeReadDouble(mNativePtr);
2107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108
2109 /**
2110 * Read a string value from the parcel at the current dataPosition().
2111 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002112 @Nullable
Jeff Sharkey047238c2012-03-07 16:51:38 -08002113 public final String readString() {
Makoto Onuki4501c61d2017-07-27 15:56:40 -07002114 return mReadWriteHelper.readString(this);
2115 }
2116
2117 /**
2118 * Read a string without going though a {@link ReadWriteHelper}. Subclasses of
2119 * {@link ReadWriteHelper} must use this method instead of {@link #readString} to avoid
2120 * infinity recursive calls.
2121 *
2122 * @hide
2123 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002124 @Nullable
Makoto Onuki4501c61d2017-07-27 15:56:40 -07002125 public String readStringNoHelper() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002126 return nativeReadString(mNativePtr);
2127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128
Jake Wharton2ffa4312018-04-23 17:47:14 -04002129 /**
2130 * Read a boolean value from the parcel at the current dataPosition().
2131 */
Eugene Susla36e866b2017-02-23 18:24:39 -08002132 public final boolean readBoolean() {
2133 return readInt() != 0;
2134 }
2135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002137 * Read a CharSequence value from the parcel at the current dataPosition().
2138 * @hide
2139 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002140 @Nullable
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002141 public final CharSequence readCharSequence() {
2142 return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(this);
2143 }
2144
2145 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 * Read an object from the parcel at the current dataPosition().
2147 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002148 public final IBinder readStrongBinder() {
2149 return nativeReadStrongBinder(mNativePtr);
2150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151
2152 /**
2153 * Read a FileDescriptor from the parcel at the current dataPosition().
2154 */
2155 public final ParcelFileDescriptor readFileDescriptor() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002156 FileDescriptor fd = nativeReadFileDescriptor(mNativePtr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 return fd != null ? new ParcelFileDescriptor(fd) : null;
2158 }
2159
Jeff Sharkeyda5a3e12013-08-11 12:54:42 -07002160 /** {@hide} */
2161 public final FileDescriptor readRawFileDescriptor() {
2162 return nativeReadFileDescriptor(mNativePtr);
2163 }
2164
Casey Dahlin2f974b22015-11-05 12:19:13 -08002165 /**
2166 * {@hide}
2167 * Read and return a new array of FileDescriptors from the parcel.
2168 * @return the FileDescriptor array, or null if the array is null.
2169 **/
Jake Whartonb1f474c2018-05-30 15:11:13 -04002170 @Nullable
Casey Dahlin2f974b22015-11-05 12:19:13 -08002171 public final FileDescriptor[] createRawFileDescriptorArray() {
2172 int N = readInt();
2173 if (N < 0) {
2174 return null;
2175 }
2176 FileDescriptor[] f = new FileDescriptor[N];
2177 for (int i = 0; i < N; i++) {
2178 f[i] = readRawFileDescriptor();
2179 }
2180 return f;
2181 }
2182
2183 /**
2184 * {@hide}
2185 * Read an array of FileDescriptors from a parcel.
2186 * The passed array must be exactly the length of the array in the parcel.
2187 * @return the FileDescriptor array, or null if the array is null.
2188 **/
2189 public final void readRawFileDescriptorArray(FileDescriptor[] val) {
2190 int N = readInt();
2191 if (N == val.length) {
2192 for (int i=0; i<N; i++) {
2193 val[i] = readRawFileDescriptor();
2194 }
2195 } else {
2196 throw new RuntimeException("bad array lengths");
2197 }
2198 }
2199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 /**
2201 * Read a byte value from the parcel at the current dataPosition().
2202 */
2203 public final byte readByte() {
2204 return (byte)(readInt() & 0xff);
2205 }
2206
2207 /**
2208 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2209 * been written with {@link #writeBundle}. Read into an existing Map object
2210 * from the parcel at the current dataPosition().
2211 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002212 public final void readMap(@NonNull Map outVal, @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 int N = readInt();
2214 readMapInternal(outVal, N, loader);
2215 }
2216
2217 /**
2218 * Read into an existing List object from the parcel at the current
2219 * dataPosition(), using the given class loader to load any enclosed
2220 * Parcelables. If it is null, the default class loader is used.
2221 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002222 public final void readList(@NonNull List outVal, @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 int N = readInt();
2224 readListInternal(outVal, N, loader);
2225 }
2226
2227 /**
2228 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2229 * been written with {@link #writeBundle}. Read and return a new HashMap
2230 * object from the parcel at the current dataPosition(), using the given
2231 * class loader to load any enclosed Parcelables. Returns null if
2232 * the previously written map object was null.
2233 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002234 @Nullable
2235 public final HashMap readHashMap(@Nullable ClassLoader loader)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 {
2237 int N = readInt();
2238 if (N < 0) {
2239 return null;
2240 }
2241 HashMap m = new HashMap(N);
2242 readMapInternal(m, N, loader);
2243 return m;
2244 }
2245
2246 /**
2247 * Read and return a new Bundle object from the parcel at the current
2248 * dataPosition(). Returns null if the previously written Bundle object was
2249 * null.
2250 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002251 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 public final Bundle readBundle() {
2253 return readBundle(null);
2254 }
2255
2256 /**
2257 * Read and return a new Bundle object from the parcel at the current
2258 * dataPosition(), using the given class loader to initialize the class
2259 * loader of the Bundle for later retrieval of Parcelable objects.
2260 * Returns null if the previously written Bundle object was null.
2261 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002262 @Nullable
2263 public final Bundle readBundle(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 int length = readInt();
2265 if (length < 0) {
Dianne Hackborn4a7d8242013-10-03 10:19:20 -07002266 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 return null;
2268 }
Samuel Tana8036662015-11-23 14:36:00 -08002269
Dianne Hackborn6aff9052009-05-22 13:20:23 -07002270 final Bundle bundle = new Bundle(this, length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 if (loader != null) {
2272 bundle.setClassLoader(loader);
2273 }
2274 return bundle;
2275 }
2276
2277 /**
Craig Mautner719e6b12014-04-04 20:29:41 -07002278 * Read and return a new Bundle object from the parcel at the current
2279 * dataPosition(). Returns null if the previously written Bundle object was
2280 * null.
2281 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002282 @Nullable
Craig Mautner719e6b12014-04-04 20:29:41 -07002283 public final PersistableBundle readPersistableBundle() {
2284 return readPersistableBundle(null);
2285 }
2286
2287 /**
2288 * Read and return a new Bundle object from the parcel at the current
2289 * dataPosition(), using the given class loader to initialize the class
2290 * loader of the Bundle for later retrieval of Parcelable objects.
2291 * Returns null if the previously written Bundle object was null.
2292 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002293 @Nullable
2294 public final PersistableBundle readPersistableBundle(@Nullable ClassLoader loader) {
Craig Mautner719e6b12014-04-04 20:29:41 -07002295 int length = readInt();
2296 if (length < 0) {
2297 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
2298 return null;
2299 }
2300
2301 final PersistableBundle bundle = new PersistableBundle(this, length);
2302 if (loader != null) {
2303 bundle.setClassLoader(loader);
2304 }
2305 return bundle;
2306 }
2307
2308 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002309 * Read a Size from the parcel at the current dataPosition().
2310 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002311 @NonNull
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002312 public final Size readSize() {
2313 final int width = readInt();
2314 final int height = readInt();
2315 return new Size(width, height);
2316 }
2317
2318 /**
2319 * Read a SizeF from the parcel at the current dataPosition().
2320 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002321 @NonNull
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002322 public final SizeF readSizeF() {
2323 final float width = readFloat();
2324 final float height = readFloat();
2325 return new SizeF(width, height);
2326 }
2327
2328 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 * Read and return a byte[] object from the parcel.
2330 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002331 @Nullable
Jeff Sharkey047238c2012-03-07 16:51:38 -08002332 public final byte[] createByteArray() {
2333 return nativeCreateByteArray(mNativePtr);
2334 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335
2336 /**
2337 * Read a byte[] object from the parcel and copy it into the
2338 * given byte array.
2339 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002340 public final void readByteArray(@NonNull byte[] val) {
Jocelyn Dang46100442017-05-05 15:40:49 -07002341 boolean valid = nativeReadByteArray(mNativePtr, val, (val != null) ? val.length : 0);
2342 if (!valid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 throw new RuntimeException("bad array lengths");
2344 }
2345 }
2346
2347 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002348 * Read a blob of data from the parcel and return it as a byte array.
2349 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -07002350 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002351 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002352 @Nullable
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002353 public final byte[] readBlob() {
2354 return nativeReadBlob(mNativePtr);
2355 }
2356
2357 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 * Read and return a String[] object from the parcel.
2359 * {@hide}
2360 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002361 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 public final String[] readStringArray() {
2363 String[] array = null;
2364
2365 int length = readInt();
2366 if (length >= 0)
2367 {
2368 array = new String[length];
2369
2370 for (int i = 0 ; i < length ; i++)
2371 {
2372 array[i] = readString();
2373 }
2374 }
2375
2376 return array;
2377 }
2378
2379 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002380 * Read and return a CharSequence[] object from the parcel.
2381 * {@hide}
2382 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002383 @Nullable
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002384 public final CharSequence[] readCharSequenceArray() {
2385 CharSequence[] array = null;
2386
2387 int length = readInt();
2388 if (length >= 0)
2389 {
2390 array = new CharSequence[length];
2391
2392 for (int i = 0 ; i < length ; i++)
2393 {
2394 array[i] = readCharSequence();
2395 }
2396 }
2397
2398 return array;
2399 }
2400
2401 /**
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002402 * Read and return an ArrayList&lt;CharSequence&gt; object from the parcel.
2403 * {@hide}
2404 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002405 @Nullable
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002406 public final ArrayList<CharSequence> readCharSequenceList() {
2407 ArrayList<CharSequence> array = null;
2408
2409 int length = readInt();
2410 if (length >= 0) {
2411 array = new ArrayList<CharSequence>(length);
2412
2413 for (int i = 0 ; i < length ; i++) {
2414 array.add(readCharSequence());
2415 }
2416 }
2417
2418 return array;
2419 }
2420
2421 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 * Read and return a new ArrayList object from the parcel at the current
2423 * dataPosition(). Returns null if the previously written list object was
2424 * null. The given class loader will be used to load any enclosed
2425 * Parcelables.
2426 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002427 @Nullable
2428 public final ArrayList readArrayList(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 int N = readInt();
2430 if (N < 0) {
2431 return null;
2432 }
2433 ArrayList l = new ArrayList(N);
2434 readListInternal(l, N, loader);
2435 return l;
2436 }
2437
2438 /**
2439 * Read and return a new Object array from the parcel at the current
2440 * dataPosition(). Returns null if the previously written array was
2441 * null. The given class loader will be used to load any enclosed
2442 * Parcelables.
2443 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002444 @Nullable
2445 public final Object[] readArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 int N = readInt();
2447 if (N < 0) {
2448 return null;
2449 }
2450 Object[] l = new Object[N];
2451 readArrayInternal(l, N, loader);
2452 return l;
2453 }
2454
2455 /**
2456 * Read and return a new SparseArray object from the parcel at the current
2457 * dataPosition(). Returns null if the previously written list object was
2458 * null. The given class loader will be used to load any enclosed
2459 * Parcelables.
2460 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002461 @Nullable
Svet Ganov8455ba22019-01-02 13:05:56 -08002462 public final <T> SparseArray<T> readSparseArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 int N = readInt();
2464 if (N < 0) {
2465 return null;
2466 }
2467 SparseArray sa = new SparseArray(N);
2468 readSparseArrayInternal(sa, N, loader);
2469 return sa;
2470 }
2471
2472 /**
2473 * Read and return a new SparseBooleanArray object from the parcel at the current
2474 * dataPosition(). Returns null if the previously written list object was
2475 * null.
2476 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002477 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 public final SparseBooleanArray readSparseBooleanArray() {
2479 int N = readInt();
2480 if (N < 0) {
2481 return null;
2482 }
2483 SparseBooleanArray sa = new SparseBooleanArray(N);
2484 readSparseBooleanArrayInternal(sa, N);
2485 return sa;
2486 }
2487
2488 /**
Adam Lesinski4e862812016-11-21 16:02:24 -08002489 * Read and return a new SparseIntArray object from the parcel at the current
2490 * dataPosition(). Returns null if the previously written array object was null.
Adam Lesinski205656d2017-03-23 13:38:26 -07002491 * @hide
Adam Lesinski4e862812016-11-21 16:02:24 -08002492 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002493 @Nullable
Adam Lesinski4e862812016-11-21 16:02:24 -08002494 public final SparseIntArray readSparseIntArray() {
2495 int N = readInt();
2496 if (N < 0) {
2497 return null;
2498 }
2499 SparseIntArray sa = new SparseIntArray(N);
2500 readSparseIntArrayInternal(sa, N);
2501 return sa;
2502 }
2503
2504 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 * Read and return a new ArrayList containing a particular object type from
2506 * the parcel that was written with {@link #writeTypedList} at the
2507 * current dataPosition(). Returns null if the
2508 * previously written list object was null. The list <em>must</em> have
2509 * previously been written via {@link #writeTypedList} with the same object
2510 * type.
2511 *
2512 * @return A newly created ArrayList containing objects with the same data
2513 * as those that were previously written.
2514 *
2515 * @see #writeTypedList
2516 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002517 @Nullable
2518 public final <T> ArrayList<T> createTypedArrayList(@NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 int N = readInt();
2520 if (N < 0) {
2521 return null;
2522 }
2523 ArrayList<T> l = new ArrayList<T>(N);
2524 while (N > 0) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002525 l.add(readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 N--;
2527 }
2528 return l;
2529 }
2530
2531 /**
2532 * Read into the given List items containing a particular object type
2533 * that were written with {@link #writeTypedList} at the
2534 * current dataPosition(). The list <em>must</em> have
2535 * previously been written via {@link #writeTypedList} with the same object
2536 * type.
2537 *
2538 * @return A newly created ArrayList containing objects with the same data
2539 * as those that were previously written.
2540 *
2541 * @see #writeTypedList
2542 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002543 public final <T> void readTypedList(@NonNull List<T> list, @NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 int M = list.size();
2545 int N = readInt();
2546 int i = 0;
2547 for (; i < M && i < N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002548 list.set(i, readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 }
2550 for (; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002551 list.add(readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 }
2553 for (; i<M; i++) {
2554 list.remove(N);
2555 }
2556 }
2557
2558 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08002559 * Read into a new {@link SparseArray} items containing a particular object type
2560 * that were written with {@link #writeTypedSparseArray(SparseArray, int)} at the
2561 * current dataPosition(). The list <em>must</em> have previously been written
2562 * via {@link #writeTypedSparseArray(SparseArray, int)} with the same object type.
2563 *
2564 * @param creator The creator to use when for instantiation.
2565 *
2566 * @return A newly created {@link SparseArray} containing objects with the same data
2567 * as those that were previously written.
2568 *
2569 * @see #writeTypedSparseArray(SparseArray, int)
2570 */
2571 public final @Nullable <T extends Parcelable> SparseArray<T> createTypedSparseArray(
2572 @NonNull Parcelable.Creator<T> creator) {
2573 final int count = readInt();
2574 if (count < 0) {
2575 return null;
2576 }
2577 final SparseArray<T> array = new SparseArray<>(count);
2578 for (int i = 0; i < count; i++) {
2579 final int index = readInt();
2580 final T value = readTypedObject(creator);
2581 array.append(index, value);
2582 }
2583 return array;
2584 }
2585
2586 /**
2587 * Read into a new {@link ArrayMap} with string keys items containing a particular
2588 * object type that were written with {@link #writeTypedArrayMap(ArrayMap, int)} at the
2589 * current dataPosition(). The list <em>must</em> have previously been written
2590 * via {@link #writeTypedArrayMap(ArrayMap, int)} with the same object type.
2591 *
2592 * @param creator The creator to use when for instantiation.
2593 *
2594 * @return A newly created {@link ArrayMap} containing objects with the same data
2595 * as those that were previously written.
2596 *
2597 * @see #writeTypedArrayMap(ArrayMap, int)
2598 */
2599 public final @Nullable <T extends Parcelable> ArrayMap<String, T> createTypedArrayMap(
2600 @NonNull Parcelable.Creator<T> creator) {
2601 final int count = readInt();
2602 if (count < 0) {
2603 return null;
2604 }
2605 final ArrayMap<String, T> map = new ArrayMap<>(count);
2606 for (int i = 0; i < count; i++) {
2607 final String key = readString();
2608 final T value = readTypedObject(creator);
2609 map.append(key, value);
2610 }
2611 return map;
2612 }
2613
2614 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 * Read and return a new ArrayList containing String objects from
2616 * the parcel that was written with {@link #writeStringList} at the
2617 * current dataPosition(). Returns null if the
2618 * previously written list object was null.
2619 *
2620 * @return A newly created ArrayList containing strings with the same data
2621 * as those that were previously written.
2622 *
2623 * @see #writeStringList
2624 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002625 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 public final ArrayList<String> createStringArrayList() {
2627 int N = readInt();
2628 if (N < 0) {
2629 return null;
2630 }
2631 ArrayList<String> l = new ArrayList<String>(N);
2632 while (N > 0) {
2633 l.add(readString());
2634 N--;
2635 }
2636 return l;
2637 }
2638
2639 /**
2640 * Read and return a new ArrayList containing IBinder objects from
2641 * the parcel that was written with {@link #writeBinderList} at the
2642 * current dataPosition(). Returns null if the
2643 * previously written list object was null.
2644 *
2645 * @return A newly created ArrayList containing strings with the same data
2646 * as those that were previously written.
2647 *
2648 * @see #writeBinderList
2649 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002650 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 public final ArrayList<IBinder> createBinderArrayList() {
2652 int N = readInt();
2653 if (N < 0) {
2654 return null;
2655 }
2656 ArrayList<IBinder> l = new ArrayList<IBinder>(N);
2657 while (N > 0) {
2658 l.add(readStrongBinder());
2659 N--;
2660 }
2661 return l;
2662 }
2663
2664 /**
2665 * Read into the given List items String objects that were written with
2666 * {@link #writeStringList} at the current dataPosition().
2667 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002668 * @see #writeStringList
2669 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002670 public final void readStringList(@NonNull List<String> list) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 int M = list.size();
2672 int N = readInt();
2673 int i = 0;
2674 for (; i < M && i < N; i++) {
2675 list.set(i, readString());
2676 }
2677 for (; i<N; i++) {
2678 list.add(readString());
2679 }
2680 for (; i<M; i++) {
2681 list.remove(N);
2682 }
2683 }
2684
2685 /**
2686 * Read into the given List items IBinder objects that were written with
2687 * {@link #writeBinderList} at the current dataPosition().
2688 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 * @see #writeBinderList
2690 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002691 public final void readBinderList(@NonNull List<IBinder> list) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 int M = list.size();
2693 int N = readInt();
2694 int i = 0;
2695 for (; i < M && i < N; i++) {
2696 list.set(i, readStrongBinder());
2697 }
2698 for (; i<N; i++) {
2699 list.add(readStrongBinder());
2700 }
2701 for (; i<M; i++) {
2702 list.remove(N);
2703 }
2704 }
2705
2706 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00002707 * Read the list of {@code Parcelable} objects at the current data position into the
2708 * given {@code list}. The contents of the {@code list} are replaced. If the serialized
2709 * list was {@code null}, {@code list} is cleared.
2710 *
2711 * @see #writeParcelableList(List, int)
Narayan Kamathbea48712016-12-01 15:38:28 +00002712 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002713 @NonNull
2714 public final <T extends Parcelable> List<T> readParcelableList(@NonNull List<T> list,
2715 @Nullable ClassLoader cl) {
Narayan Kamathbea48712016-12-01 15:38:28 +00002716 final int N = readInt();
2717 if (N == -1) {
2718 list.clear();
Eugene Susla36e866b2017-02-23 18:24:39 -08002719 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002720 }
2721
2722 final int M = list.size();
2723 int i = 0;
2724 for (; i < M && i < N; i++) {
2725 list.set(i, (T) readParcelable(cl));
2726 }
2727 for (; i<N; i++) {
2728 list.add((T) readParcelable(cl));
2729 }
2730 for (; i<M; i++) {
2731 list.remove(N);
2732 }
Eugene Susla36e866b2017-02-23 18:24:39 -08002733 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002734 }
2735
2736 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 * Read and return a new array containing a particular object type from
2738 * the parcel at the current dataPosition(). Returns null if the
2739 * previously written array was null. The array <em>must</em> have
2740 * previously been written via {@link #writeTypedArray} with the same
2741 * object type.
2742 *
2743 * @return A newly created array containing objects with the same data
2744 * as those that were previously written.
2745 *
2746 * @see #writeTypedArray
2747 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002748 @Nullable
2749 public final <T> T[] createTypedArray(@NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 int N = readInt();
2751 if (N < 0) {
2752 return null;
2753 }
2754 T[] l = c.newArray(N);
2755 for (int i=0; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002756 l[i] = readTypedObject(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 }
2758 return l;
2759 }
2760
Jake Whartonb1f474c2018-05-30 15:11:13 -04002761 public final <T> void readTypedArray(@NonNull T[] val, @NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 int N = readInt();
2763 if (N == val.length) {
2764 for (int i=0; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002765 val[i] = readTypedObject(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 }
2767 } else {
2768 throw new RuntimeException("bad array lengths");
2769 }
2770 }
2771
2772 /**
2773 * @deprecated
2774 * @hide
2775 */
2776 @Deprecated
2777 public final <T> T[] readTypedArray(Parcelable.Creator<T> c) {
2778 return createTypedArray(c);
2779 }
2780
2781 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002782 * Read and return a typed Parcelable object from a parcel.
2783 * Returns null if the previous written object was null.
2784 * The object <em>must</em> have previous been written via
2785 * {@link #writeTypedObject} with the same object type.
2786 *
2787 * @return A newly created object of the type that was previously
2788 * written.
2789 *
2790 * @see #writeTypedObject
2791 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002792 @Nullable
2793 public final <T> T readTypedObject(@NonNull Parcelable.Creator<T> c) {
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002794 if (readInt() != 0) {
2795 return c.createFromParcel(this);
2796 } else {
2797 return null;
2798 }
2799 }
2800
2801 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 * Write a heterogeneous array of Parcelable objects into the Parcel.
2803 * Each object in the array is written along with its class name, so
2804 * that the correct class can later be instantiated. As a result, this
2805 * has significantly more overhead than {@link #writeTypedArray}, but will
2806 * correctly handle an array containing more than one type of object.
2807 *
2808 * @param value The array of objects to be written.
2809 * @param parcelableFlags Contextual flags as per
2810 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
2811 *
2812 * @see #writeTypedArray
2813 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002814 public final <T extends Parcelable> void writeParcelableArray(@Nullable T[] value,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 int parcelableFlags) {
2816 if (value != null) {
2817 int N = value.length;
2818 writeInt(N);
2819 for (int i=0; i<N; i++) {
2820 writeParcelable(value[i], parcelableFlags);
2821 }
2822 } else {
2823 writeInt(-1);
2824 }
2825 }
2826
2827 /**
2828 * Read a typed object from a parcel. The given class loader will be
2829 * used to load any enclosed Parcelables. If it is null, the default class
2830 * loader will be used.
2831 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002832 @Nullable
2833 public final Object readValue(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 int type = readInt();
2835
2836 switch (type) {
2837 case VAL_NULL:
2838 return null;
2839
2840 case VAL_STRING:
2841 return readString();
2842
2843 case VAL_INTEGER:
2844 return readInt();
2845
2846 case VAL_MAP:
2847 return readHashMap(loader);
2848
2849 case VAL_PARCELABLE:
2850 return readParcelable(loader);
2851
2852 case VAL_SHORT:
2853 return (short) readInt();
2854
2855 case VAL_LONG:
2856 return readLong();
2857
2858 case VAL_FLOAT:
2859 return readFloat();
2860
2861 case VAL_DOUBLE:
2862 return readDouble();
2863
2864 case VAL_BOOLEAN:
2865 return readInt() == 1;
2866
2867 case VAL_CHARSEQUENCE:
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002868 return readCharSequence();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869
2870 case VAL_LIST:
2871 return readArrayList(loader);
2872
2873 case VAL_BOOLEANARRAY:
Samuel Tana8036662015-11-23 14:36:00 -08002874 return createBooleanArray();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875
2876 case VAL_BYTEARRAY:
2877 return createByteArray();
2878
2879 case VAL_STRINGARRAY:
2880 return readStringArray();
2881
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002882 case VAL_CHARSEQUENCEARRAY:
2883 return readCharSequenceArray();
2884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 case VAL_IBINDER:
2886 return readStrongBinder();
2887
2888 case VAL_OBJECTARRAY:
2889 return readArray(loader);
2890
2891 case VAL_INTARRAY:
2892 return createIntArray();
2893
2894 case VAL_LONGARRAY:
2895 return createLongArray();
2896
2897 case VAL_BYTE:
2898 return readByte();
2899
2900 case VAL_SERIALIZABLE:
John Spurlock5002b8c2014-01-10 13:32:12 -05002901 return readSerializable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902
2903 case VAL_PARCELABLEARRAY:
2904 return readParcelableArray(loader);
2905
2906 case VAL_SPARSEARRAY:
2907 return readSparseArray(loader);
2908
2909 case VAL_SPARSEBOOLEANARRAY:
2910 return readSparseBooleanArray();
2911
2912 case VAL_BUNDLE:
2913 return readBundle(loader); // loading will be deferred
2914
Craig Mautner719e6b12014-04-04 20:29:41 -07002915 case VAL_PERSISTABLEBUNDLE:
2916 return readPersistableBundle(loader);
2917
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002918 case VAL_SIZE:
2919 return readSize();
2920
2921 case VAL_SIZEF:
2922 return readSizeF();
2923
Samuel Tana8036662015-11-23 14:36:00 -08002924 case VAL_DOUBLEARRAY:
2925 return createDoubleArray();
2926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 default:
2928 int off = dataPosition() - 4;
2929 throw new RuntimeException(
2930 "Parcel " + this + ": Unmarshalling unknown type code " + type + " at offset " + off);
2931 }
2932 }
2933
2934 /**
2935 * Read and return a new Parcelable from the parcel. The given class loader
2936 * will be used to load any enclosed Parcelables. If it is null, the default
2937 * class loader will be used.
2938 * @param loader A ClassLoader from which to instantiate the Parcelable
2939 * object, or null for the default class loader.
2940 * @return Returns the newly created Parcelable, or null if a null
2941 * object has been written.
2942 * @throws BadParcelableException Throws BadParcelableException if there
2943 * was an error trying to instantiate the Parcelable.
2944 */
Neil Fuller44e440c2015-04-20 14:39:00 +01002945 @SuppressWarnings("unchecked")
Jake Whartonb1f474c2018-05-30 15:11:13 -04002946 @Nullable
2947 public final <T extends Parcelable> T readParcelable(@Nullable ClassLoader loader) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002948 Parcelable.Creator<?> creator = readParcelableCreator(loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002949 if (creator == null) {
2950 return null;
2951 }
2952 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002953 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2954 (Parcelable.ClassLoaderCreator<?>) creator;
2955 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002956 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002957 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002958 }
2959
2960 /** @hide */
Neil Fuller44e440c2015-04-20 14:39:00 +01002961 @SuppressWarnings("unchecked")
Jake Whartonb1f474c2018-05-30 15:11:13 -04002962 @Nullable
2963 public final <T extends Parcelable> T readCreator(@NonNull Parcelable.Creator<?> creator,
2964 @Nullable ClassLoader loader) {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002965 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002966 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2967 (Parcelable.ClassLoaderCreator<?>) creator;
2968 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002969 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002970 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002971 }
2972
2973 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002974 @Nullable
2975 public final Parcelable.Creator<?> readParcelableCreator(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 String name = readString();
2977 if (name == null) {
2978 return null;
2979 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002980 Parcelable.Creator<?> creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 synchronized (mCreators) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002982 HashMap<String,Parcelable.Creator<?>> map = mCreators.get(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983 if (map == null) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002984 map = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 mCreators.put(loader, map);
2986 }
2987 creator = map.get(name);
2988 if (creator == null) {
2989 try {
Neil Fuller44e440c2015-04-20 14:39:00 +01002990 // If loader == null, explicitly emulate Class.forName(String) "caller
2991 // classloader" behavior.
2992 ClassLoader parcelableClassLoader =
2993 (loader == null ? getClass().getClassLoader() : loader);
2994 // Avoid initializing the Parcelable class until we know it implements
2995 // Parcelable and has the necessary CREATOR field. http://b/1171613.
2996 Class<?> parcelableClass = Class.forName(name, false /* initialize */,
2997 parcelableClassLoader);
2998 if (!Parcelable.class.isAssignableFrom(parcelableClass)) {
Dianne Hackbornced54392018-02-26 13:07:42 -08002999 throw new BadParcelableException("Parcelable protocol requires subclassing "
3000 + "from Parcelable on class " + name);
Neil Fuller44e440c2015-04-20 14:39:00 +01003001 }
3002 Field f = parcelableClass.getField("CREATOR");
3003 if ((f.getModifiers() & Modifier.STATIC) == 0) {
3004 throw new BadParcelableException("Parcelable protocol requires "
3005 + "the CREATOR object to be static on class " + name);
3006 }
3007 Class<?> creatorType = f.getType();
3008 if (!Parcelable.Creator.class.isAssignableFrom(creatorType)) {
3009 // Fail before calling Field.get(), not after, to avoid initializing
3010 // parcelableClass unnecessarily.
3011 throw new BadParcelableException("Parcelable protocol requires a "
3012 + "Parcelable.Creator object called "
3013 + "CREATOR on class " + name);
3014 }
3015 creator = (Parcelable.Creator<?>) f.get(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 }
3017 catch (IllegalAccessException e) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003018 Log.e(TAG, "Illegal access when unmarshalling: " + name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 throw new BadParcelableException(
3020 "IllegalAccessException when unmarshalling: " + name);
3021 }
3022 catch (ClassNotFoundException e) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003023 Log.e(TAG, "Class not found when unmarshalling: " + name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 throw new BadParcelableException(
3025 "ClassNotFoundException when unmarshalling: " + name);
3026 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 catch (NoSuchFieldException e) {
3028 throw new BadParcelableException("Parcelable protocol requires a "
Neil Fuller44e440c2015-04-20 14:39:00 +01003029 + "Parcelable.Creator object called "
3030 + "CREATOR on class " + name);
Irfan Sheriff97a72f62013-01-11 10:45:51 -08003031 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 if (creator == null) {
3033 throw new BadParcelableException("Parcelable protocol requires a "
Neil Fuller44e440c2015-04-20 14:39:00 +01003034 + "non-null Parcelable.Creator object called "
3035 + "CREATOR on class " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 }
3037
3038 map.put(name, creator);
3039 }
3040 }
3041
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08003042 return creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003043 }
3044
3045 /**
3046 * Read and return a new Parcelable array from the parcel.
3047 * The given class loader will be used to load any enclosed
3048 * Parcelables.
3049 * @return the Parcelable array, or null if the array is null
3050 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003051 @Nullable
3052 public final Parcelable[] readParcelableArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003053 int N = readInt();
3054 if (N < 0) {
3055 return null;
3056 }
3057 Parcelable[] p = new Parcelable[N];
3058 for (int i = 0; i < N; i++) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003059 p[i] = readParcelable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 }
3061 return p;
3062 }
3063
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003064 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003065 @Nullable
3066 public final <T extends Parcelable> T[] readParcelableArray(@Nullable ClassLoader loader,
3067 @NonNull Class<T> clazz) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003068 int N = readInt();
3069 if (N < 0) {
3070 return null;
3071 }
3072 T[] p = (T[]) Array.newInstance(clazz, N);
3073 for (int i = 0; i < N; i++) {
3074 p[i] = readParcelable(loader);
3075 }
3076 return p;
3077 }
3078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 /**
3080 * Read and return a new Serializable object from the parcel.
3081 * @return the Serializable object, or null if the Serializable name
3082 * wasn't found in the parcel.
3083 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003084 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 public final Serializable readSerializable() {
John Spurlock5002b8c2014-01-10 13:32:12 -05003086 return readSerializable(null);
3087 }
3088
Jake Whartonb1f474c2018-05-30 15:11:13 -04003089 @Nullable
3090 private final Serializable readSerializable(@Nullable final ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 String name = readString();
3092 if (name == null) {
3093 // For some reason we were unable to read the name of the Serializable (either there
3094 // is nothing left in the Parcel to read, or the next value wasn't a String), so
3095 // return null, which indicates that the name wasn't found in the parcel.
3096 return null;
3097 }
3098
3099 byte[] serializedData = createByteArray();
3100 ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
3101 try {
John Spurlock5002b8c2014-01-10 13:32:12 -05003102 ObjectInputStream ois = new ObjectInputStream(bais) {
3103 @Override
3104 protected Class<?> resolveClass(ObjectStreamClass osClass)
3105 throws IOException, ClassNotFoundException {
3106 // try the custom classloader if provided
3107 if (loader != null) {
3108 Class<?> c = Class.forName(osClass.getName(), false, loader);
3109 if (c != null) {
3110 return c;
3111 }
3112 }
3113 return super.resolveClass(osClass);
3114 }
3115 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116 return (Serializable) ois.readObject();
3117 } catch (IOException ioe) {
3118 throw new RuntimeException("Parcelable encountered " +
3119 "IOException reading a Serializable object (name = " + name +
3120 ")", ioe);
3121 } catch (ClassNotFoundException cnfe) {
John Spurlock5002b8c2014-01-10 13:32:12 -05003122 throw new RuntimeException("Parcelable encountered " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 "ClassNotFoundException reading a Serializable object (name = "
3124 + name + ")", cnfe);
3125 }
3126 }
3127
3128 // Cache of previously looked up CREATOR.createFromParcel() methods for
3129 // particular classes. Keys are the names of the classes, values are
3130 // Method objects.
Neil Fuller44e440c2015-04-20 14:39:00 +01003131 private static final HashMap<ClassLoader,HashMap<String,Parcelable.Creator<?>>>
3132 mCreators = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133
Narayan Kamathb34a4612014-01-23 14:17:11 +00003134 /** @hide for internal use only. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 static protected final Parcel obtain(int obj) {
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003136 throw new UnsupportedOperationException();
3137 }
3138
3139 /** @hide */
3140 static protected final Parcel obtain(long obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 final Parcel[] pool = sHolderPool;
3142 synchronized (pool) {
3143 Parcel p;
3144 for (int i=0; i<POOL_SIZE; i++) {
3145 p = pool[i];
3146 if (p != null) {
3147 pool[i] = null;
3148 if (DEBUG_RECYCLE) {
3149 p.mStack = new RuntimeException();
3150 }
3151 p.init(obj);
3152 return p;
3153 }
3154 }
3155 }
3156 return new Parcel(obj);
3157 }
3158
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003159 private Parcel(long nativePtr) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003160 if (DEBUG_RECYCLE) {
3161 mStack = new RuntimeException();
3162 }
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003163 //Log.i(TAG, "Initializing obj=0x" + Integer.toHexString(obj), mStack);
Jeff Sharkey047238c2012-03-07 16:51:38 -08003164 init(nativePtr);
3165 }
3166
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003167 private void init(long nativePtr) {
Jeff Sharkey047238c2012-03-07 16:51:38 -08003168 if (nativePtr != 0) {
3169 mNativePtr = nativePtr;
3170 mOwnsNativeParcelObject = false;
3171 } else {
3172 mNativePtr = nativeCreate();
3173 mOwnsNativeParcelObject = true;
3174 }
3175 }
3176
3177 private void freeBuffer() {
3178 if (mOwnsNativeParcelObject) {
Adrian Roos04505652015-10-22 16:12:01 -07003179 updateNativeSize(nativeFreeBuffer(mNativePtr));
Jeff Sharkey047238c2012-03-07 16:51:38 -08003180 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -07003181 mReadWriteHelper = ReadWriteHelper.DEFAULT;
Jeff Sharkey047238c2012-03-07 16:51:38 -08003182 }
3183
3184 private void destroy() {
3185 if (mNativePtr != 0) {
3186 if (mOwnsNativeParcelObject) {
3187 nativeDestroy(mNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -07003188 updateNativeSize(0);
Jeff Sharkey047238c2012-03-07 16:51:38 -08003189 }
3190 mNativePtr = 0;
3191 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -07003192 mReadWriteHelper = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 }
3194
3195 @Override
3196 protected void finalize() throws Throwable {
3197 if (DEBUG_RECYCLE) {
3198 if (mStack != null) {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003199 Log.w(TAG, "Client did not call Parcel.recycle()", mStack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 }
3201 }
3202 destroy();
3203 }
3204
Jake Whartonb1f474c2018-05-30 15:11:13 -04003205 /* package */ void readMapInternal(@NonNull Map outVal, int N,
3206 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 while (N > 0) {
3208 Object key = readValue(loader);
3209 Object value = readValue(loader);
3210 outVal.put(key, value);
3211 N--;
3212 }
3213 }
3214
Jake Whartonb1f474c2018-05-30 15:11:13 -04003215 /* package */ void readArrayMapInternal(@NonNull ArrayMap outVal, int N,
3216 @Nullable ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003217 if (DEBUG_ARRAY_MAP) {
3218 RuntimeException here = new RuntimeException("here");
3219 here.fillInStackTrace();
3220 Log.d(TAG, "Reading " + N + " ArrayMap entries", here);
3221 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003222 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003223 while (N > 0) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003224 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003225 String key = readString();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003226 Object value = readValue(loader);
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003227 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " "
3228 + (dataPosition()-startPos) + " bytes: key=0x"
3229 + Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key);
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003230 outVal.append(key, value);
3231 N--;
3232 }
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003233 outVal.validate();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003234 }
3235
Jake Whartonb1f474c2018-05-30 15:11:13 -04003236 /* package */ void readArrayMapSafelyInternal(@NonNull ArrayMap outVal, int N,
3237 @Nullable ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003238 if (DEBUG_ARRAY_MAP) {
3239 RuntimeException here = new RuntimeException("here");
3240 here.fillInStackTrace();
3241 Log.d(TAG, "Reading safely " + N + " ArrayMap entries", here);
3242 }
3243 while (N > 0) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003244 String key = readString();
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003245 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read safe #" + (N-1) + ": key=0x"
3246 + (key != null ? key.hashCode() : 0) + " " + key);
3247 Object value = readValue(loader);
3248 outVal.put(key, value);
3249 N--;
3250 }
3251 }
3252
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003253 /**
3254 * @hide For testing only.
3255 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003256 public void readArrayMap(@NonNull ArrayMap outVal, @Nullable ClassLoader loader) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003257 final int N = readInt();
3258 if (N < 0) {
3259 return;
3260 }
3261 readArrayMapInternal(outVal, N, loader);
3262 }
3263
Svet Ganovddb94882016-06-23 19:55:24 -07003264 /**
3265 * Reads an array set.
3266 *
3267 * @param loader The class loader to use.
3268 *
3269 * @hide
3270 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003271 public @Nullable ArraySet<? extends Object> readArraySet(@Nullable ClassLoader loader) {
Svet Ganovddb94882016-06-23 19:55:24 -07003272 final int size = readInt();
3273 if (size < 0) {
3274 return null;
3275 }
3276 ArraySet<Object> result = new ArraySet<>(size);
3277 for (int i = 0; i < size; i++) {
3278 Object value = readValue(loader);
3279 result.append(value);
3280 }
3281 return result;
3282 }
3283
Jake Whartonb1f474c2018-05-30 15:11:13 -04003284 private void readListInternal(@NonNull List outVal, int N,
3285 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 while (N > 0) {
3287 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003288 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 outVal.add(value);
3290 N--;
3291 }
3292 }
3293
Jake Whartonb1f474c2018-05-30 15:11:13 -04003294 private void readArrayInternal(@NonNull Object[] outVal, int N,
3295 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 for (int i = 0; i < N; i++) {
3297 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003298 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 outVal[i] = value;
3300 }
3301 }
3302
Jake Whartonb1f474c2018-05-30 15:11:13 -04003303 private void readSparseArrayInternal(@NonNull SparseArray outVal, int N,
3304 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 while (N > 0) {
3306 int key = readInt();
3307 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003308 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 outVal.append(key, value);
3310 N--;
3311 }
3312 }
3313
3314
Jake Whartonb1f474c2018-05-30 15:11:13 -04003315 private void readSparseBooleanArrayInternal(@NonNull SparseBooleanArray outVal, int N) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 while (N > 0) {
3317 int key = readInt();
3318 boolean value = this.readByte() == 1;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003319 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 outVal.append(key, value);
3321 N--;
3322 }
3323 }
Dan Sandler5ce04302015-04-09 23:50:15 -04003324
Jake Whartonb1f474c2018-05-30 15:11:13 -04003325 private void readSparseIntArrayInternal(@NonNull SparseIntArray outVal, int N) {
Adam Lesinski4e862812016-11-21 16:02:24 -08003326 while (N > 0) {
3327 int key = readInt();
3328 int value = readInt();
3329 outVal.append(key, value);
3330 N--;
3331 }
3332 }
3333
Dan Sandler5ce04302015-04-09 23:50:15 -04003334 /**
3335 * @hide For testing
3336 */
3337 public long getBlobAshmemSize() {
3338 return nativeGetBlobAshmemSize(mNativePtr);
3339 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340}