blob: fe2e948bfcd911186f82fb661a9d73953c8a00b7 [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;
Howard Chen40abbb02019-05-24 02:04:50 +080021import android.annotation.TestApi;
Andrei Onea24ec3212019-03-15 17:35:05 +000022import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.text.TextUtils;
Dianne Hackbornb87655b2013-07-17 19:06:22 -070024import android.util.ArrayMap;
Svet Ganovddb94882016-06-23 19:55:24 -070025import android.util.ArraySet;
Fyodor Kupolov3b946f42017-11-27 10:40:46 -080026import android.util.ExceptionUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.util.Log;
Jeff Sharkey5ef33982014-09-04 18:13:39 -070028import android.util.Size;
29import android.util.SizeF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.util.SparseArray;
31import android.util.SparseBooleanArray;
Adam Lesinski4e862812016-11-21 16:02:24 -080032import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
Makoto Onukib148b6c2017-06-27 13:38:38 -070034import dalvik.annotation.optimization.CriticalNative;
Jeff Sharkey789a8fc2017-04-16 13:18:35 -060035import dalvik.annotation.optimization.FastNative;
36import dalvik.system.VMRuntime;
37
Pete Gillin60f55a252018-05-10 15:40:32 +010038import libcore.util.ArrayUtils;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -070039import libcore.util.SneakyThrow;
40
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import java.io.ByteArrayInputStream;
42import java.io.ByteArrayOutputStream;
43import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import java.io.IOException;
45import java.io.ObjectInputStream;
46import java.io.ObjectOutputStream;
John Spurlock5002b8c2014-01-10 13:32:12 -050047import java.io.ObjectStreamClass;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import java.io.Serializable;
Makoto Onuki440a1ea2016-07-20 14:21:18 -070049import java.lang.reflect.Array;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import java.lang.reflect.Field;
Neil Fuller44e440c2015-04-20 14:39:00 +010051import java.lang.reflect.Modifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import java.util.ArrayList;
53import java.util.HashMap;
54import java.util.List;
55import java.util.Map;
56import java.util.Set;
Adrian Roos04505652015-10-22 16:12:01 -070057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058/**
59 * Container for a message (data and object references) that can
60 * be sent through an IBinder. A Parcel can contain both flattened data
61 * that will be unflattened on the other side of the IPC (using the various
62 * methods here for writing specific types, or the general
63 * {@link Parcelable} interface), and references to live {@link IBinder}
64 * objects that will result in the other side receiving a proxy IBinder
65 * connected with the original IBinder in the Parcel.
66 *
67 * <p class="note">Parcel is <strong>not</strong> a general-purpose
68 * serialization mechanism. This class (and the corresponding
69 * {@link Parcelable} API for placing arbitrary objects into a Parcel) is
70 * designed as a high-performance IPC transport. As such, it is not
71 * appropriate to place any Parcel data in to persistent storage: changes
72 * in the underlying implementation of any of the data in the Parcel can
73 * render older data unreadable.</p>
Samuel Tana8036662015-11-23 14:36:00 -080074 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 * <p>The bulk of the Parcel API revolves around reading and writing data
76 * of various types. There are six major classes of such functions available.</p>
Samuel Tana8036662015-11-23 14:36:00 -080077 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 * <h3>Primitives</h3>
Samuel Tana8036662015-11-23 14:36:00 -080079 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 * <p>The most basic data functions are for writing and reading primitive
81 * data types: {@link #writeByte}, {@link #readByte}, {@link #writeDouble},
82 * {@link #readDouble}, {@link #writeFloat}, {@link #readFloat}, {@link #writeInt},
83 * {@link #readInt}, {@link #writeLong}, {@link #readLong},
84 * {@link #writeString}, {@link #readString}. Most other
85 * data operations are built on top of these. The given data is written and
86 * read using the endianess of the host CPU.</p>
Samuel Tana8036662015-11-23 14:36:00 -080087 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 * <h3>Primitive Arrays</h3>
Samuel Tana8036662015-11-23 14:36:00 -080089 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 * <p>There are a variety of methods for reading and writing raw arrays
91 * of primitive objects, which generally result in writing a 4-byte length
92 * followed by the primitive data items. The methods for reading can either
93 * read the data into an existing array, or create and return a new array.
94 * These available types are:</p>
Samuel Tana8036662015-11-23 14:36:00 -080095 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 * <ul>
97 * <li> {@link #writeBooleanArray(boolean[])},
98 * {@link #readBooleanArray(boolean[])}, {@link #createBooleanArray()}
99 * <li> {@link #writeByteArray(byte[])},
100 * {@link #writeByteArray(byte[], int, int)}, {@link #readByteArray(byte[])},
101 * {@link #createByteArray()}
102 * <li> {@link #writeCharArray(char[])}, {@link #readCharArray(char[])},
103 * {@link #createCharArray()}
104 * <li> {@link #writeDoubleArray(double[])}, {@link #readDoubleArray(double[])},
105 * {@link #createDoubleArray()}
106 * <li> {@link #writeFloatArray(float[])}, {@link #readFloatArray(float[])},
107 * {@link #createFloatArray()}
108 * <li> {@link #writeIntArray(int[])}, {@link #readIntArray(int[])},
109 * {@link #createIntArray()}
110 * <li> {@link #writeLongArray(long[])}, {@link #readLongArray(long[])},
111 * {@link #createLongArray()}
112 * <li> {@link #writeStringArray(String[])}, {@link #readStringArray(String[])},
113 * {@link #createStringArray()}.
114 * <li> {@link #writeSparseBooleanArray(SparseBooleanArray)},
115 * {@link #readSparseBooleanArray()}.
116 * </ul>
Samuel Tana8036662015-11-23 14:36:00 -0800117 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 * <h3>Parcelables</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800119 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 * <p>The {@link Parcelable} protocol provides an extremely efficient (but
121 * low-level) protocol for objects to write and read themselves from Parcels.
122 * You can use the direct methods {@link #writeParcelable(Parcelable, int)}
123 * and {@link #readParcelable(ClassLoader)} or
124 * {@link #writeParcelableArray} and
125 * {@link #readParcelableArray(ClassLoader)} to write or read. These
126 * methods write both the class type and its data to the Parcel, allowing
127 * that class to be reconstructed from the appropriate class loader when
128 * later reading.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800129 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 * <p>There are also some methods that provide a more efficient way to work
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -0700131 * with Parcelables: {@link #writeTypedObject}, {@link #writeTypedArray},
132 * {@link #writeTypedList}, {@link #readTypedObject},
133 * {@link #createTypedArray} and {@link #createTypedArrayList}. These methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * do not write the class information of the original object: instead, the
135 * caller of the read function must know what type to expect and pass in the
136 * appropriate {@link Parcelable.Creator Parcelable.Creator} instead to
137 * properly construct the new object and read its data. (To more efficient
Bin Chenb6b12b52017-07-11 11:01:44 +0800138 * write and read a single Parcelable object that is not null, you can directly
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -0700139 * call {@link Parcelable#writeToParcel Parcelable.writeToParcel} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * {@link Parcelable.Creator#createFromParcel Parcelable.Creator.createFromParcel}
141 * yourself.)</p>
Samuel Tana8036662015-11-23 14:36:00 -0800142 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 * <h3>Bundles</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800144 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 * <p>A special type-safe container, called {@link Bundle}, is available
146 * for key/value maps of heterogeneous values. This has many optimizations
147 * for improved performance when reading and writing data, and its type-safe
148 * API avoids difficult to debug type errors when finally marshalling the
149 * data contents into a Parcel. The methods to use are
150 * {@link #writeBundle(Bundle)}, {@link #readBundle()}, and
151 * {@link #readBundle(ClassLoader)}.
Samuel Tana8036662015-11-23 14:36:00 -0800152 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 * <h3>Active Objects</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800154 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 * <p>An unusual feature of Parcel is the ability to read and write active
156 * objects. For these objects the actual contents of the object is not
157 * written, rather a special token referencing the object is written. When
158 * reading the object back from the Parcel, you do not get a new instance of
159 * the object, but rather a handle that operates on the exact same object that
160 * was originally written. There are two forms of active objects available.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800161 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 * <p>{@link Binder} objects are a core facility of Android's general cross-process
163 * communication system. The {@link IBinder} interface describes an abstract
164 * protocol with a Binder object. Any such interface can be written in to
165 * a Parcel, and upon reading you will receive either the original object
166 * implementing that interface or a special proxy implementation
167 * that communicates calls back to the original object. The methods to use are
168 * {@link #writeStrongBinder(IBinder)},
169 * {@link #writeStrongInterface(IInterface)}, {@link #readStrongBinder()},
170 * {@link #writeBinderArray(IBinder[])}, {@link #readBinderArray(IBinder[])},
171 * {@link #createBinderArray()},
172 * {@link #writeBinderList(List)}, {@link #readBinderList(List)},
173 * {@link #createBinderArrayList()}.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800174 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 * <p>FileDescriptor objects, representing raw Linux file descriptor identifiers,
176 * can be written and {@link ParcelFileDescriptor} objects returned to operate
177 * on the original file descriptor. The returned file descriptor is a dup
178 * of the original file descriptor: the object and fd is different, but
179 * operating on the same underlying file stream, with the same position, etc.
180 * The methods to use are {@link #writeFileDescriptor(FileDescriptor)},
181 * {@link #readFileDescriptor()}.
Samuel Tana8036662015-11-23 14:36:00 -0800182 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 * <h3>Untyped Containers</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800184 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 * <p>A final class of methods are for writing and reading standard Java
186 * containers of arbitrary types. These all revolve around the
187 * {@link #writeValue(Object)} and {@link #readValue(ClassLoader)} methods
188 * which define the types of objects allowed. The container methods are
189 * {@link #writeArray(Object[])}, {@link #readArray(ClassLoader)},
190 * {@link #writeList(List)}, {@link #readList(List, ClassLoader)},
191 * {@link #readArrayList(ClassLoader)},
192 * {@link #writeMap(Map)}, {@link #readMap(Map, ClassLoader)},
193 * {@link #writeSparseArray(SparseArray)},
194 * {@link #readSparseArray(ClassLoader)}.
195 */
196public final class Parcel {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -0800197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 private static final boolean DEBUG_RECYCLE = false;
Dianne Hackborne784d1e2013-09-20 18:13:52 -0700199 private static final boolean DEBUG_ARRAY_MAP = false;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700200 private static final String TAG = "Parcel";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201
Andrei Onea24ec3212019-03-15 17:35:05 +0000202 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 @SuppressWarnings({"UnusedDeclaration"})
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000204 private long mNativePtr; // used by native code
Jeff Sharkey047238c2012-03-07 16:51:38 -0800205
206 /**
207 * Flag indicating if {@link #mNativePtr} was allocated by this object,
208 * indicating that we're responsible for its lifecycle.
209 */
210 private boolean mOwnsNativeParcelObject;
Adrian Roos04505652015-10-22 16:12:01 -0700211 private long mNativeSize;
Jeff Sharkey047238c2012-03-07 16:51:38 -0800212
Dianne Hackborn98305522017-05-05 17:53:53 -0700213 private ArrayMap<Class, Object> mClassCookies;
214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 private RuntimeException mStack;
216
Fyodor Kupolova81b8c02017-11-13 15:51:03 -0800217 /**
218 * Whether or not to parcel the stack trace of an exception. This has a performance
219 * impact, so should only be included in specific processes and only on debug builds.
220 */
221 private static boolean sParcelExceptionStackTrace;
222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 private static final int POOL_SIZE = 6;
224 private static final Parcel[] sOwnedPool = new Parcel[POOL_SIZE];
225 private static final Parcel[] sHolderPool = new Parcel[POOL_SIZE];
226
Robert Quattlebaum9cd7af32017-01-04 13:28:01 -0800227 // Keep in sync with frameworks/native/include/private/binder/ParcelValTypes.h.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 private static final int VAL_NULL = -1;
229 private static final int VAL_STRING = 0;
230 private static final int VAL_INTEGER = 1;
231 private static final int VAL_MAP = 2;
232 private static final int VAL_BUNDLE = 3;
233 private static final int VAL_PARCELABLE = 4;
234 private static final int VAL_SHORT = 5;
235 private static final int VAL_LONG = 6;
236 private static final int VAL_FLOAT = 7;
237 private static final int VAL_DOUBLE = 8;
238 private static final int VAL_BOOLEAN = 9;
239 private static final int VAL_CHARSEQUENCE = 10;
240 private static final int VAL_LIST = 11;
241 private static final int VAL_SPARSEARRAY = 12;
242 private static final int VAL_BYTEARRAY = 13;
243 private static final int VAL_STRINGARRAY = 14;
244 private static final int VAL_IBINDER = 15;
245 private static final int VAL_PARCELABLEARRAY = 16;
246 private static final int VAL_OBJECTARRAY = 17;
247 private static final int VAL_INTARRAY = 18;
248 private static final int VAL_LONGARRAY = 19;
249 private static final int VAL_BYTE = 20;
250 private static final int VAL_SERIALIZABLE = 21;
251 private static final int VAL_SPARSEBOOLEANARRAY = 22;
252 private static final int VAL_BOOLEANARRAY = 23;
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000253 private static final int VAL_CHARSEQUENCEARRAY = 24;
Craig Mautner719e6b12014-04-04 20:29:41 -0700254 private static final int VAL_PERSISTABLEBUNDLE = 25;
Jeff Sharkey5ef33982014-09-04 18:13:39 -0700255 private static final int VAL_SIZE = 26;
256 private static final int VAL_SIZEF = 27;
Samuel Tana8036662015-11-23 14:36:00 -0800257 private static final int VAL_DOUBLEARRAY = 28;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700259 // The initial int32 in a Binder call's reply Parcel header:
Christopher Wiley80fd1202015-11-22 17:12:37 -0800260 // Keep these in sync with libbinder's binder/Status.h.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 private static final int EX_SECURITY = -1;
262 private static final int EX_BAD_PARCELABLE = -2;
263 private static final int EX_ILLEGAL_ARGUMENT = -3;
264 private static final int EX_NULL_POINTER = -4;
265 private static final int EX_ILLEGAL_STATE = -5;
Dianne Hackborn7e714422013-09-13 17:32:57 -0700266 private static final int EX_NETWORK_MAIN_THREAD = -6;
Dianne Hackborn33d738a2014-09-12 14:23:58 -0700267 private static final int EX_UNSUPPORTED_OPERATION = -7;
Christopher Wiley80fd1202015-11-22 17:12:37 -0800268 private static final int EX_SERVICE_SPECIFIC = -8;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -0700269 private static final int EX_PARCELABLE = -9;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700270 private static final int EX_HAS_REPLY_HEADER = -128; // special; see below
Christopher Wiley80fd1202015-11-22 17:12:37 -0800271 // EX_TRANSACTION_FAILED is used exclusively in native code.
272 // see libbinder's binder/Status.h
273 private static final int EX_TRANSACTION_FAILED = -129;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700274
Makoto Onukib148b6c2017-06-27 13:38:38 -0700275 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000276 private static native int nativeDataSize(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700277 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000278 private static native int nativeDataAvail(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700279 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000280 private static native int nativeDataPosition(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700281 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000282 private static native int nativeDataCapacity(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700283 @FastNative
Adrian Roos04505652015-10-22 16:12:01 -0700284 private static native long nativeSetDataSize(long nativePtr, int size);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700285 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000286 private static native void nativeSetDataPosition(long nativePtr, int pos);
John Reck71207b52016-09-28 13:28:09 -0700287 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000288 private static native void nativeSetDataCapacity(long nativePtr, int size);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800289
Makoto Onukib148b6c2017-06-27 13:38:38 -0700290 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000291 private static native boolean nativePushAllowFds(long nativePtr, boolean allowFds);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700292 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000293 private static native void nativeRestoreAllowFds(long nativePtr, boolean lastValue);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800294
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000295 private static native void nativeWriteByteArray(long nativePtr, byte[] b, int offset, int len);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700296 private static native void nativeWriteBlob(long nativePtr, byte[] b, int offset, int len);
John Reck71207b52016-09-28 13:28:09 -0700297 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000298 private static native void nativeWriteInt(long nativePtr, int val);
John Reck71207b52016-09-28 13:28:09 -0700299 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000300 private static native void nativeWriteLong(long nativePtr, long val);
John Reck71207b52016-09-28 13:28:09 -0700301 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000302 private static native void nativeWriteFloat(long nativePtr, float val);
John Reck71207b52016-09-28 13:28:09 -0700303 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000304 private static native void nativeWriteDouble(long nativePtr, double val);
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700305 static native void nativeWriteString(long nativePtr, String val);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000306 private static native void nativeWriteStrongBinder(long nativePtr, IBinder val);
Adrian Roos04505652015-10-22 16:12:01 -0700307 private static native long nativeWriteFileDescriptor(long nativePtr, FileDescriptor val);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800308
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000309 private static native byte[] nativeCreateByteArray(long nativePtr);
Jocelyn Dang46100442017-05-05 15:40:49 -0700310 private static native boolean nativeReadByteArray(long nativePtr, byte[] dest, int destLen);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700311 private static native byte[] nativeReadBlob(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700312 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000313 private static native int nativeReadInt(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700314 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000315 private static native long nativeReadLong(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700316 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000317 private static native float nativeReadFloat(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700318 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000319 private static native double nativeReadDouble(long nativePtr);
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700320 static native String nativeReadString(long nativePtr);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000321 private static native IBinder nativeReadStrongBinder(long nativePtr);
322 private static native FileDescriptor nativeReadFileDescriptor(long nativePtr);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800323
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000324 private static native long nativeCreate();
Adrian Roos04505652015-10-22 16:12:01 -0700325 private static native long nativeFreeBuffer(long nativePtr);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000326 private static native void nativeDestroy(long nativePtr);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800327
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000328 private static native byte[] nativeMarshall(long nativePtr);
Adrian Roos04505652015-10-22 16:12:01 -0700329 private static native long nativeUnmarshall(
John Spurlocke0852362015-02-04 15:47:40 -0500330 long nativePtr, byte[] data, int offset, int length);
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700331 private static native int nativeCompareData(long thisNativePtr, long otherNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -0700332 private static native long nativeAppendFrom(
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000333 long thisNativePtr, long otherNativePtr, int offset, int length);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700334 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000335 private static native boolean nativeHasFileDescriptors(long nativePtr);
336 private static native void nativeWriteInterfaceToken(long nativePtr, String interfaceName);
337 private static native void nativeEnforceInterface(long nativePtr, String interfaceName);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800338
Olivier Gaillardbab444a2019-01-30 17:11:40 +0000339 @CriticalNative
340 private static native boolean nativeReplaceCallingWorkSourceUid(
341 long nativePtr, int workSourceUid);
342 @CriticalNative
343 private static native int nativeReadCallingWorkSourceUid(long nativePtr);
344
Fyodor Kupolova81b8c02017-11-13 15:51:03 -0800345 /** Last time exception with a stack trace was written */
346 private static volatile long sLastWriteExceptionStackTrace;
347 /** Used for throttling of writing stack trace, which is costly */
348 private static final int WRITE_EXCEPTION_STACK_TRACE_THRESHOLD_MS = 1000;
349
Makoto Onukib148b6c2017-06-27 13:38:38 -0700350 @CriticalNative
Dan Sandleraa861662015-04-21 10:24:32 -0400351 private static native long nativeGetBlobAshmemSize(long nativePtr);
Dan Sandler5ce04302015-04-09 23:50:15 -0400352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 public final static Parcelable.Creator<String> STRING_CREATOR
354 = new Parcelable.Creator<String>() {
355 public String createFromParcel(Parcel source) {
356 return source.readString();
357 }
358 public String[] newArray(int size) {
359 return new String[size];
360 }
361 };
362
363 /**
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700364 * @hide
365 */
366 public static class ReadWriteHelper {
367 public static final ReadWriteHelper DEFAULT = new ReadWriteHelper();
368
369 /**
370 * Called when writing a string to a parcel. Subclasses wanting to write a string
371 * must use {@link #writeStringNoHelper(String)} to avoid
372 * infinity recursive calls.
373 */
374 public void writeString(Parcel p, String s) {
375 nativeWriteString(p.mNativePtr, s);
376 }
377
378 /**
379 * Called when reading a string to a parcel. Subclasses wanting to read a string
380 * must use {@link #readStringNoHelper()} to avoid
381 * infinity recursive calls.
382 */
383 public String readString(Parcel p) {
384 return nativeReadString(p.mNativePtr);
385 }
386 }
387
388 private ReadWriteHelper mReadWriteHelper = ReadWriteHelper.DEFAULT;
389
390 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 * Retrieve a new Parcel object from the pool.
392 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400393 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 public static Parcel obtain() {
395 final Parcel[] pool = sOwnedPool;
396 synchronized (pool) {
397 Parcel p;
398 for (int i=0; i<POOL_SIZE; i++) {
399 p = pool[i];
400 if (p != null) {
401 pool[i] = null;
402 if (DEBUG_RECYCLE) {
403 p.mStack = new RuntimeException();
404 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700405 p.mReadWriteHelper = ReadWriteHelper.DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 return p;
407 }
408 }
409 }
410 return new Parcel(0);
411 }
412
413 /**
414 * Put a Parcel object back into the pool. You must not touch
415 * the object after this call.
416 */
417 public final void recycle() {
418 if (DEBUG_RECYCLE) mStack = null;
419 freeBuffer();
Jeff Sharkey047238c2012-03-07 16:51:38 -0800420
421 final Parcel[] pool;
422 if (mOwnsNativeParcelObject) {
423 pool = sOwnedPool;
424 } else {
425 mNativePtr = 0;
426 pool = sHolderPool;
427 }
428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 synchronized (pool) {
430 for (int i=0; i<POOL_SIZE; i++) {
431 if (pool[i] == null) {
432 pool[i] = this;
433 return;
434 }
435 }
436 }
437 }
438
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700439 /**
440 * Set a {@link ReadWriteHelper}, which can be used to avoid having duplicate strings, for
441 * example.
442 *
443 * @hide
444 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400445 public void setReadWriteHelper(@Nullable ReadWriteHelper helper) {
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700446 mReadWriteHelper = helper != null ? helper : ReadWriteHelper.DEFAULT;
447 }
448
449 /**
450 * @return whether this parcel has a {@link ReadWriteHelper}.
451 *
452 * @hide
453 */
454 public boolean hasReadWriteHelper() {
455 return (mReadWriteHelper != null) && (mReadWriteHelper != ReadWriteHelper.DEFAULT);
456 }
457
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800458 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000459 @UnsupportedAppUsage
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800460 public static native long getGlobalAllocSize();
461
462 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000463 @UnsupportedAppUsage
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800464 public static native long getGlobalAllocCount();
465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 /**
467 * Returns the total amount of data contained in the parcel.
468 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800469 public final int dataSize() {
470 return nativeDataSize(mNativePtr);
471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472
473 /**
474 * Returns the amount of data remaining to be read from the
475 * parcel. That is, {@link #dataSize}-{@link #dataPosition}.
476 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800477 public final int dataAvail() {
478 return nativeDataAvail(mNativePtr);
479 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480
481 /**
482 * Returns the current position in the parcel data. Never
483 * more than {@link #dataSize}.
484 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800485 public final int dataPosition() {
486 return nativeDataPosition(mNativePtr);
487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488
489 /**
490 * Returns the total amount of space in the parcel. This is always
491 * >= {@link #dataSize}. The difference between it and dataSize() is the
492 * amount of room left until the parcel needs to re-allocate its
493 * data buffer.
494 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800495 public final int dataCapacity() {
496 return nativeDataCapacity(mNativePtr);
497 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498
499 /**
500 * Change the amount of data in the parcel. Can be either smaller or
501 * larger than the current size. If larger than the current capacity,
502 * more memory will be allocated.
503 *
504 * @param size The new number of bytes in the Parcel.
505 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800506 public final void setDataSize(int size) {
Michael Wachenschwanz138bebf2017-05-18 22:09:18 +0000507 updateNativeSize(nativeSetDataSize(mNativePtr, size));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509
510 /**
511 * Move the current read/write position in the parcel.
512 * @param pos New offset in the parcel; must be between 0 and
513 * {@link #dataSize}.
514 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800515 public final void setDataPosition(int pos) {
516 nativeSetDataPosition(mNativePtr, pos);
517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518
519 /**
520 * Change the capacity (current available space) of the parcel.
521 *
522 * @param size The new capacity of the parcel, in bytes. Can not be
523 * less than {@link #dataSize} -- that is, you can not drop existing data
524 * with this method.
525 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800526 public final void setDataCapacity(int size) {
527 nativeSetDataCapacity(mNativePtr, size);
528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400530 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800531 public final boolean pushAllowFds(boolean allowFds) {
532 return nativePushAllowFds(mNativePtr, allowFds);
533 }
Dianne Hackbornc04db7e2011-10-03 21:09:35 -0700534
535 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800536 public final void restoreAllowFds(boolean lastValue) {
537 nativeRestoreAllowFds(mNativePtr, lastValue);
538 }
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 /**
541 * Returns the raw bytes of the parcel.
542 *
543 * <p class="note">The data you retrieve here <strong>must not</strong>
544 * be placed in any kind of persistent storage (on local disk, across
545 * a network, etc). For that, you should use standard serialization
546 * or another kind of general serialization mechanism. The Parcel
547 * marshalled representation is highly optimized for local IPC, and as
548 * such does not attempt to maintain compatibility with data created
549 * in different versions of the platform.
550 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800551 public final byte[] marshall() {
552 return nativeMarshall(mNativePtr);
553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554
555 /**
556 * Set the bytes in data to be the raw bytes of this Parcel.
557 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400558 public final void unmarshall(@NonNull byte[] data, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700559 updateNativeSize(nativeUnmarshall(mNativePtr, data, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561
Jeff Sharkey047238c2012-03-07 16:51:38 -0800562 public final void appendFrom(Parcel parcel, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700563 updateNativeSize(nativeAppendFrom(mNativePtr, parcel.mNativePtr, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700566 /** @hide */
567 public final int compareData(Parcel other) {
568 return nativeCompareData(mNativePtr, other.mNativePtr);
569 }
570
Dianne Hackborn98305522017-05-05 17:53:53 -0700571 /** @hide */
572 public final void setClassCookie(Class clz, Object cookie) {
573 if (mClassCookies == null) {
574 mClassCookies = new ArrayMap<>();
575 }
576 mClassCookies.put(clz, cookie);
577 }
578
579 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400580 @Nullable
Dianne Hackborn98305522017-05-05 17:53:53 -0700581 public final Object getClassCookie(Class clz) {
582 return mClassCookies != null ? mClassCookies.get(clz) : null;
583 }
584
585 /** @hide */
586 public final void adoptClassCookies(Parcel from) {
587 mClassCookies = from.mClassCookies;
588 }
589
Adrian Roosfb921842017-10-26 14:49:56 +0200590 /** @hide */
591 public Map<Class, Object> copyClassCookies() {
592 return new ArrayMap<>(mClassCookies);
593 }
594
595 /** @hide */
596 public void putClassCookies(Map<Class, Object> cookies) {
597 if (cookies == null) {
598 return;
599 }
600 if (mClassCookies == null) {
601 mClassCookies = new ArrayMap<>();
602 }
603 mClassCookies.putAll(cookies);
604 }
605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 /**
607 * Report whether the parcel contains any marshalled file descriptors.
608 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800609 public final boolean hasFileDescriptors() {
610 return nativeHasFileDescriptors(mNativePtr);
611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612
613 /**
614 * Store or read an IBinder interface token in the parcel at the current
615 * {@link #dataPosition}. This is used to validate that the marshalled
616 * transaction is intended for the target interface.
617 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800618 public final void writeInterfaceToken(String interfaceName) {
619 nativeWriteInterfaceToken(mNativePtr, interfaceName);
620 }
621
622 public final void enforceInterface(String interfaceName) {
623 nativeEnforceInterface(mNativePtr, interfaceName);
624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625
626 /**
Olivier Gaillardbab444a2019-01-30 17:11:40 +0000627 * Writes the work source uid to the request headers.
628 *
629 * <p>It requires the headers to have been written/read already to replace the work source.
630 *
631 * @return true if the request headers have been updated.
632 *
633 * @hide
634 */
635 public boolean replaceCallingWorkSourceUid(int workSourceUid) {
636 return nativeReplaceCallingWorkSourceUid(mNativePtr, workSourceUid);
637 }
638
639 /**
640 * Reads the work source uid from the request headers.
641 *
642 * <p>Unlike other read methods, this method does not read the parcel at the current
643 * {@link #dataPosition}. It will set the {@link #dataPosition} before the read and restore the
644 * position after reading the request header.
645 *
646 * @return the work source uid or {@link Binder#UNSET_WORKSOURCE} if headers have not been
647 * written/parsed yet.
648 *
649 * @hide
650 */
651 public int readCallingWorkSourceUid() {
652 return nativeReadCallingWorkSourceUid(mNativePtr);
653 }
654
655 /**
Elliott Hughesa28b83e2011-02-28 14:26:13 -0800656 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 * growing {@link #dataCapacity} if needed.
658 * @param b Bytes to place into the parcel.
659 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400660 public final void writeByteArray(@Nullable byte[] b) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 writeByteArray(b, 0, (b != null) ? b.length : 0);
662 }
663
664 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900665 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 * growing {@link #dataCapacity} if needed.
667 * @param b Bytes to place into the parcel.
668 * @param offset Index of first byte to be written.
669 * @param len Number of bytes to write.
670 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400671 public final void writeByteArray(@Nullable byte[] b, int offset, int len) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 if (b == null) {
673 writeInt(-1);
674 return;
675 }
Pete Gillin60f55a252018-05-10 15:40:32 +0100676 ArrayUtils.throwsIfOutOfBounds(b.length, offset, len);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800677 nativeWriteByteArray(mNativePtr, b, offset, len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 }
679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700681 * Write a blob of data into the parcel at the current {@link #dataPosition},
682 * growing {@link #dataCapacity} if needed.
683 * @param b Bytes to place into the parcel.
684 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -0700685 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700686 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000687 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -0400688 public final void writeBlob(@Nullable byte[] b) {
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500689 writeBlob(b, 0, (b != null) ? b.length : 0);
690 }
691
692 /**
693 * Write a blob of data into the parcel at the current {@link #dataPosition},
694 * growing {@link #dataCapacity} if needed.
695 * @param b Bytes to place into the parcel.
696 * @param offset Index of first byte to be written.
697 * @param len Number of bytes to write.
698 * {@hide}
699 * {@SystemApi}
700 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400701 public final void writeBlob(@Nullable byte[] b, int offset, int len) {
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500702 if (b == null) {
703 writeInt(-1);
704 return;
705 }
Pete Gillin60f55a252018-05-10 15:40:32 +0100706 ArrayUtils.throwsIfOutOfBounds(b.length, offset, len);
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500707 nativeWriteBlob(mNativePtr, b, offset, len);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700708 }
709
710 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 * Write an integer value into the parcel at the current dataPosition(),
712 * growing dataCapacity() if needed.
713 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800714 public final void writeInt(int val) {
715 nativeWriteInt(mNativePtr, val);
716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717
718 /**
719 * Write a long integer value into the parcel at the current dataPosition(),
720 * growing dataCapacity() if needed.
721 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800722 public final void writeLong(long val) {
723 nativeWriteLong(mNativePtr, val);
724 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725
726 /**
727 * Write a floating point value into the parcel at the current
728 * dataPosition(), growing dataCapacity() if needed.
729 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800730 public final void writeFloat(float val) {
731 nativeWriteFloat(mNativePtr, val);
732 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733
734 /**
735 * Write a double precision floating point value into the parcel at the
736 * current dataPosition(), growing dataCapacity() if needed.
737 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800738 public final void writeDouble(double val) {
739 nativeWriteDouble(mNativePtr, val);
740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741
742 /**
743 * Write a string value into the parcel at the current dataPosition(),
744 * growing dataCapacity() if needed.
745 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400746 public final void writeString(@Nullable String val) {
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700747 mReadWriteHelper.writeString(this, val);
748 }
749
750 /**
751 * Write a string without going though a {@link ReadWriteHelper}. Subclasses of
752 * {@link ReadWriteHelper} must use this method instead of {@link #writeString} to avoid
753 * infinity recursive calls.
754 *
755 * @hide
756 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400757 public void writeStringNoHelper(@Nullable String val) {
Jeff Sharkey047238c2012-03-07 16:51:38 -0800758 nativeWriteString(mNativePtr, val);
759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760
Jake Wharton2ffa4312018-04-23 17:47:14 -0400761 /**
762 * Write a boolean value into the parcel at the current dataPosition(),
763 * growing dataCapacity() if needed.
764 *
765 * <p>Note: This method currently delegates to writeInt with a value of 1 or 0
766 * for true or false, respectively, but may change in the future.
767 */
Eugene Susla36e866b2017-02-23 18:24:39 -0800768 public final void writeBoolean(boolean val) {
769 writeInt(val ? 1 : 0);
770 }
771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000773 * Write a CharSequence value into the parcel at the current dataPosition(),
774 * growing dataCapacity() if needed.
775 * @hide
776 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000777 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -0400778 public final void writeCharSequence(@Nullable CharSequence val) {
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000779 TextUtils.writeToParcel(val, this, 0);
780 }
781
782 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 * Write an object into the parcel at the current dataPosition(),
784 * growing dataCapacity() if needed.
785 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800786 public final void writeStrongBinder(IBinder val) {
787 nativeWriteStrongBinder(mNativePtr, val);
788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789
790 /**
791 * Write an object into the parcel at the current dataPosition(),
792 * growing dataCapacity() if needed.
793 */
794 public final void writeStrongInterface(IInterface val) {
795 writeStrongBinder(val == null ? null : val.asBinder());
796 }
797
798 /**
799 * Write a FileDescriptor into the parcel at the current dataPosition(),
800 * growing dataCapacity() if needed.
Dan Egnorb3e4ef32010-07-20 09:03:35 -0700801 *
802 * <p class="caution">The file descriptor will not be closed, which may
803 * result in file descriptor leaks when objects are returned from Binder
804 * calls. Use {@link ParcelFileDescriptor#writeToParcel} instead, which
805 * accepts contextual flags and will close the original file descriptor
806 * if {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} is set.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400808 public final void writeFileDescriptor(@NonNull FileDescriptor val) {
Adrian Roos04505652015-10-22 16:12:01 -0700809 updateNativeSize(nativeWriteFileDescriptor(mNativePtr, val));
810 }
811
812 private void updateNativeSize(long newNativeSize) {
813 if (mOwnsNativeParcelObject) {
814 if (newNativeSize > Integer.MAX_VALUE) {
815 newNativeSize = Integer.MAX_VALUE;
816 }
817 if (newNativeSize != mNativeSize) {
818 int delta = (int) (newNativeSize - mNativeSize);
819 if (delta > 0) {
820 VMRuntime.getRuntime().registerNativeAllocation(delta);
821 } else {
822 VMRuntime.getRuntime().registerNativeFree(-delta);
823 }
824 mNativeSize = newNativeSize;
825 }
826 }
Jeff Sharkey047238c2012-03-07 16:51:38 -0800827 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828
829 /**
Casey Dahlin2f974b22015-11-05 12:19:13 -0800830 * {@hide}
831 * This will be the new name for writeFileDescriptor, for consistency.
832 **/
Jake Whartonb1f474c2018-05-30 15:11:13 -0400833 public final void writeRawFileDescriptor(@NonNull FileDescriptor val) {
Casey Dahlin2f974b22015-11-05 12:19:13 -0800834 nativeWriteFileDescriptor(mNativePtr, val);
835 }
836
837 /**
838 * {@hide}
839 * Write an array of FileDescriptor objects into the Parcel.
840 *
841 * @param value The array of objects to be written.
842 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400843 public final void writeRawFileDescriptorArray(@Nullable FileDescriptor[] value) {
Casey Dahlin2f974b22015-11-05 12:19:13 -0800844 if (value != null) {
845 int N = value.length;
846 writeInt(N);
847 for (int i=0; i<N; i++) {
848 writeRawFileDescriptor(value[i]);
849 }
850 } else {
851 writeInt(-1);
852 }
853 }
854
855 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900856 * Write a byte value into the parcel at the current dataPosition(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 * growing dataCapacity() if needed.
Jake Wharton2ffa4312018-04-23 17:47:14 -0400858 *
859 * <p>Note: This method currently delegates to writeInt but may change in
860 * the future.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 */
862 public final void writeByte(byte val) {
863 writeInt(val);
864 }
865
866 /**
867 * Please use {@link #writeBundle} instead. Flattens a Map into the parcel
868 * at the current dataPosition(),
869 * growing dataCapacity() if needed. The Map keys must be String objects.
870 * The Map values are written using {@link #writeValue} and must follow
871 * the specification there.
Samuel Tana8036662015-11-23 14:36:00 -0800872 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 * <p>It is strongly recommended to use {@link #writeBundle} instead of
874 * this method, since the Bundle class provides a type-safe API that
875 * allows you to avoid mysterious type errors at the point of marshalling.
876 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400877 public final void writeMap(@Nullable Map val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700878 writeMapInternal((Map<String, Object>) val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 }
880
881 /**
882 * Flatten a Map into the parcel at the current dataPosition(),
883 * growing dataCapacity() if needed. The Map keys must be String objects.
884 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400885 /* package */ void writeMapInternal(@Nullable Map<String,Object> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 if (val == null) {
887 writeInt(-1);
888 return;
889 }
890 Set<Map.Entry<String,Object>> entries = val.entrySet();
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700891 int size = entries.size();
892 writeInt(size);
893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 for (Map.Entry<String,Object> e : entries) {
895 writeValue(e.getKey());
896 writeValue(e.getValue());
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700897 size--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 }
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700899
900 if (size != 0) {
901 throw new BadParcelableException("Map size does not match number of entries!");
902 }
903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 }
905
906 /**
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700907 * Flatten an ArrayMap into the parcel at the current dataPosition(),
908 * growing dataCapacity() if needed. The Map keys must be String objects.
909 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400910 /* package */ void writeArrayMapInternal(@Nullable ArrayMap<String, Object> val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700911 if (val == null) {
912 writeInt(-1);
913 return;
914 }
Samuel Tan3cefe6a2015-12-14 13:29:17 -0800915 // Keep the format of this Parcel in sync with writeToParcelInner() in
916 // frameworks/native/libs/binder/PersistableBundle.cpp.
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700917 final int N = val.size();
918 writeInt(N);
Dianne Hackborne784d1e2013-09-20 18:13:52 -0700919 if (DEBUG_ARRAY_MAP) {
920 RuntimeException here = new RuntimeException("here");
921 here.fillInStackTrace();
922 Log.d(TAG, "Writing " + N + " ArrayMap entries", here);
923 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700924 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700925 for (int i=0; i<N; i++) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700926 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700927 writeString(val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700928 writeValue(val.valueAt(i));
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700929 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Write #" + i + " "
930 + (dataPosition()-startPos) + " bytes: key=0x"
931 + Integer.toHexString(val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0)
932 + " " + val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700933 }
934 }
935
936 /**
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700937 * @hide For testing only.
938 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000939 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -0400940 public void writeArrayMap(@Nullable ArrayMap<String, Object> val) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700941 writeArrayMapInternal(val);
942 }
943
944 /**
Svet Ganov8455ba22019-01-02 13:05:56 -0800945 * Flatten an {@link ArrayMap} with string keys containing a particular object
946 * type into the parcel at the current dataPosition() and growing dataCapacity()
947 * if needed. The type of the objects in the array must be one that implements
948 * Parcelable. Only the raw data of the objects is written and not their type,
949 * so you must use the corresponding {@link #createTypedArrayMap(Parcelable.Creator)}
950 *
951 * @param val The map of objects to be written.
952 * @param parcelableFlags The parcelable flags to use.
953 *
954 * @see #createTypedArrayMap(Parcelable.Creator)
955 * @see Parcelable
956 */
957 public <T extends Parcelable> void writeTypedArrayMap(@Nullable ArrayMap<String, T> val,
958 int parcelableFlags) {
959 if (val == null) {
960 writeInt(-1);
961 return;
962 }
963 final int count = val.size();
964 writeInt(count);
965 for (int i = 0; i < count; i++) {
966 writeString(val.keyAt(i));
967 writeTypedObject(val.valueAt(i), parcelableFlags);
968 }
969 }
970
971 /**
Svet Ganovddb94882016-06-23 19:55:24 -0700972 * Write an array set to the parcel.
973 *
974 * @param val The array set to write.
975 *
976 * @hide
977 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000978 @UnsupportedAppUsage
Svet Ganovddb94882016-06-23 19:55:24 -0700979 public void writeArraySet(@Nullable ArraySet<? extends Object> val) {
980 final int size = (val != null) ? val.size() : -1;
981 writeInt(size);
982 for (int i = 0; i < size; i++) {
983 writeValue(val.valueAt(i));
984 }
985 }
986
987 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 * Flatten a Bundle into the parcel at the current dataPosition(),
989 * growing dataCapacity() if needed.
990 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400991 public final void writeBundle(@Nullable Bundle val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 if (val == null) {
993 writeInt(-1);
994 return;
995 }
996
Dianne Hackborn6aff9052009-05-22 13:20:23 -0700997 val.writeToParcel(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 }
999
1000 /**
Craig Mautner719e6b12014-04-04 20:29:41 -07001001 * Flatten a PersistableBundle into the parcel at the current dataPosition(),
1002 * growing dataCapacity() if needed.
1003 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001004 public final void writePersistableBundle(@Nullable PersistableBundle val) {
Craig Mautner719e6b12014-04-04 20:29:41 -07001005 if (val == null) {
1006 writeInt(-1);
1007 return;
1008 }
1009
1010 val.writeToParcel(this, 0);
1011 }
1012
1013 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001014 * Flatten a Size into the parcel at the current dataPosition(),
1015 * growing dataCapacity() if needed.
1016 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001017 public final void writeSize(@NonNull Size val) {
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001018 writeInt(val.getWidth());
1019 writeInt(val.getHeight());
1020 }
1021
1022 /**
1023 * Flatten a SizeF into the parcel at the current dataPosition(),
1024 * growing dataCapacity() if needed.
1025 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001026 public final void writeSizeF(@NonNull SizeF val) {
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001027 writeFloat(val.getWidth());
1028 writeFloat(val.getHeight());
1029 }
1030
1031 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 * Flatten a List into the parcel at the current dataPosition(), growing
1033 * dataCapacity() if needed. The List values are written using
1034 * {@link #writeValue} and must follow the specification there.
1035 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001036 public final void writeList(@Nullable List val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 if (val == null) {
1038 writeInt(-1);
1039 return;
1040 }
1041 int N = val.size();
1042 int i=0;
1043 writeInt(N);
1044 while (i < N) {
1045 writeValue(val.get(i));
1046 i++;
1047 }
1048 }
1049
1050 /**
1051 * Flatten an Object array into the parcel at the current dataPosition(),
1052 * growing dataCapacity() if needed. The array values are written using
1053 * {@link #writeValue} and must follow the specification there.
1054 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001055 public final void writeArray(@Nullable Object[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 if (val == null) {
1057 writeInt(-1);
1058 return;
1059 }
1060 int N = val.length;
1061 int i=0;
1062 writeInt(N);
1063 while (i < N) {
1064 writeValue(val[i]);
1065 i++;
1066 }
1067 }
1068
1069 /**
1070 * Flatten a generic SparseArray into the parcel at the current
1071 * dataPosition(), growing dataCapacity() if needed. The SparseArray
1072 * values are written using {@link #writeValue} and must follow the
1073 * specification there.
1074 */
Svet Ganov8455ba22019-01-02 13:05:56 -08001075 public final <T> void writeSparseArray(@Nullable SparseArray<T> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 if (val == null) {
1077 writeInt(-1);
1078 return;
1079 }
1080 int N = val.size();
1081 writeInt(N);
1082 int i=0;
1083 while (i < N) {
1084 writeInt(val.keyAt(i));
1085 writeValue(val.valueAt(i));
1086 i++;
1087 }
1088 }
1089
Jake Whartonb1f474c2018-05-30 15:11:13 -04001090 public final void writeSparseBooleanArray(@Nullable SparseBooleanArray val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 if (val == null) {
1092 writeInt(-1);
1093 return;
1094 }
1095 int N = val.size();
1096 writeInt(N);
1097 int i=0;
1098 while (i < N) {
1099 writeInt(val.keyAt(i));
1100 writeByte((byte)(val.valueAt(i) ? 1 : 0));
1101 i++;
1102 }
1103 }
1104
Adam Lesinski205656d2017-03-23 13:38:26 -07001105 /**
1106 * @hide
1107 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001108 public final void writeSparseIntArray(@Nullable SparseIntArray val) {
Adam Lesinski4e862812016-11-21 16:02:24 -08001109 if (val == null) {
1110 writeInt(-1);
1111 return;
1112 }
1113 int N = val.size();
1114 writeInt(N);
1115 int i=0;
1116 while (i < N) {
1117 writeInt(val.keyAt(i));
1118 writeInt(val.valueAt(i));
1119 i++;
1120 }
1121 }
1122
Jake Whartonb1f474c2018-05-30 15:11:13 -04001123 public final void writeBooleanArray(@Nullable boolean[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 if (val != null) {
1125 int N = val.length;
1126 writeInt(N);
1127 for (int i=0; i<N; i++) {
1128 writeInt(val[i] ? 1 : 0);
1129 }
1130 } else {
1131 writeInt(-1);
1132 }
1133 }
1134
Jake Whartonb1f474c2018-05-30 15:11:13 -04001135 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 public final boolean[] createBooleanArray() {
1137 int N = readInt();
1138 // >>2 as a fast divide-by-4 works in the create*Array() functions
1139 // because dataAvail() will never return a negative number. 4 is
1140 // the size of a stored boolean in the stream.
1141 if (N >= 0 && N <= (dataAvail() >> 2)) {
1142 boolean[] val = new boolean[N];
1143 for (int i=0; i<N; i++) {
1144 val[i] = readInt() != 0;
1145 }
1146 return val;
1147 } else {
1148 return null;
1149 }
1150 }
1151
Jake Whartonb1f474c2018-05-30 15:11:13 -04001152 public final void readBooleanArray(@NonNull boolean[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 int N = readInt();
1154 if (N == val.length) {
1155 for (int i=0; i<N; i++) {
1156 val[i] = readInt() != 0;
1157 }
1158 } else {
1159 throw new RuntimeException("bad array lengths");
1160 }
1161 }
1162
Jake Whartonb1f474c2018-05-30 15:11:13 -04001163 public final void writeCharArray(@Nullable char[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 if (val != null) {
1165 int N = val.length;
1166 writeInt(N);
1167 for (int i=0; i<N; i++) {
1168 writeInt((int)val[i]);
1169 }
1170 } else {
1171 writeInt(-1);
1172 }
1173 }
1174
Jake Whartonb1f474c2018-05-30 15:11:13 -04001175 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 public final char[] createCharArray() {
1177 int N = readInt();
1178 if (N >= 0 && N <= (dataAvail() >> 2)) {
1179 char[] val = new char[N];
1180 for (int i=0; i<N; i++) {
1181 val[i] = (char)readInt();
1182 }
1183 return val;
1184 } else {
1185 return null;
1186 }
1187 }
1188
Jake Whartonb1f474c2018-05-30 15:11:13 -04001189 public final void readCharArray(@NonNull char[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 int N = readInt();
1191 if (N == val.length) {
1192 for (int i=0; i<N; i++) {
1193 val[i] = (char)readInt();
1194 }
1195 } else {
1196 throw new RuntimeException("bad array lengths");
1197 }
1198 }
1199
Jake Whartonb1f474c2018-05-30 15:11:13 -04001200 public final void writeIntArray(@Nullable int[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 if (val != null) {
1202 int N = val.length;
1203 writeInt(N);
1204 for (int i=0; i<N; i++) {
1205 writeInt(val[i]);
1206 }
1207 } else {
1208 writeInt(-1);
1209 }
1210 }
1211
Jake Whartonb1f474c2018-05-30 15:11:13 -04001212 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 public final int[] createIntArray() {
1214 int N = readInt();
1215 if (N >= 0 && N <= (dataAvail() >> 2)) {
1216 int[] val = new int[N];
1217 for (int i=0; i<N; i++) {
1218 val[i] = readInt();
1219 }
1220 return val;
1221 } else {
1222 return null;
1223 }
1224 }
1225
Jake Whartonb1f474c2018-05-30 15:11:13 -04001226 public final void readIntArray(@NonNull int[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 int N = readInt();
1228 if (N == val.length) {
1229 for (int i=0; i<N; i++) {
1230 val[i] = readInt();
1231 }
1232 } else {
1233 throw new RuntimeException("bad array lengths");
1234 }
1235 }
1236
Jake Whartonb1f474c2018-05-30 15:11:13 -04001237 public final void writeLongArray(@Nullable long[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 if (val != null) {
1239 int N = val.length;
1240 writeInt(N);
1241 for (int i=0; i<N; i++) {
1242 writeLong(val[i]);
1243 }
1244 } else {
1245 writeInt(-1);
1246 }
1247 }
1248
Jake Whartonb1f474c2018-05-30 15:11:13 -04001249 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 public final long[] createLongArray() {
1251 int N = readInt();
1252 // >>3 because stored longs are 64 bits
1253 if (N >= 0 && N <= (dataAvail() >> 3)) {
1254 long[] val = new long[N];
1255 for (int i=0; i<N; i++) {
1256 val[i] = readLong();
1257 }
1258 return val;
1259 } else {
1260 return null;
1261 }
1262 }
1263
Jake Whartonb1f474c2018-05-30 15:11:13 -04001264 public final void readLongArray(@NonNull long[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 int N = readInt();
1266 if (N == val.length) {
1267 for (int i=0; i<N; i++) {
1268 val[i] = readLong();
1269 }
1270 } else {
1271 throw new RuntimeException("bad array lengths");
1272 }
1273 }
1274
Jake Whartonb1f474c2018-05-30 15:11:13 -04001275 public final void writeFloatArray(@Nullable float[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 if (val != null) {
1277 int N = val.length;
1278 writeInt(N);
1279 for (int i=0; i<N; i++) {
1280 writeFloat(val[i]);
1281 }
1282 } else {
1283 writeInt(-1);
1284 }
1285 }
1286
Jake Whartonb1f474c2018-05-30 15:11:13 -04001287 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 public final float[] createFloatArray() {
1289 int N = readInt();
1290 // >>2 because stored floats are 4 bytes
1291 if (N >= 0 && N <= (dataAvail() >> 2)) {
1292 float[] val = new float[N];
1293 for (int i=0; i<N; i++) {
1294 val[i] = readFloat();
1295 }
1296 return val;
1297 } else {
1298 return null;
1299 }
1300 }
1301
Jake Whartonb1f474c2018-05-30 15:11:13 -04001302 public final void readFloatArray(@NonNull float[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 int N = readInt();
1304 if (N == val.length) {
1305 for (int i=0; i<N; i++) {
1306 val[i] = readFloat();
1307 }
1308 } else {
1309 throw new RuntimeException("bad array lengths");
1310 }
1311 }
1312
Jake Whartonb1f474c2018-05-30 15:11:13 -04001313 public final void writeDoubleArray(@Nullable double[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 if (val != null) {
1315 int N = val.length;
1316 writeInt(N);
1317 for (int i=0; i<N; i++) {
1318 writeDouble(val[i]);
1319 }
1320 } else {
1321 writeInt(-1);
1322 }
1323 }
1324
Jake Whartonb1f474c2018-05-30 15:11:13 -04001325 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 public final double[] createDoubleArray() {
1327 int N = readInt();
1328 // >>3 because stored doubles are 8 bytes
1329 if (N >= 0 && N <= (dataAvail() >> 3)) {
1330 double[] val = new double[N];
1331 for (int i=0; i<N; i++) {
1332 val[i] = readDouble();
1333 }
1334 return val;
1335 } else {
1336 return null;
1337 }
1338 }
1339
Jake Whartonb1f474c2018-05-30 15:11:13 -04001340 public final void readDoubleArray(@NonNull double[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 int N = readInt();
1342 if (N == val.length) {
1343 for (int i=0; i<N; i++) {
1344 val[i] = readDouble();
1345 }
1346 } else {
1347 throw new RuntimeException("bad array lengths");
1348 }
1349 }
1350
Jake Whartonb1f474c2018-05-30 15:11:13 -04001351 public final void writeStringArray(@Nullable String[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 if (val != null) {
1353 int N = val.length;
1354 writeInt(N);
1355 for (int i=0; i<N; i++) {
1356 writeString(val[i]);
1357 }
1358 } else {
1359 writeInt(-1);
1360 }
1361 }
1362
Jake Whartonb1f474c2018-05-30 15:11:13 -04001363 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 public final String[] createStringArray() {
1365 int N = readInt();
1366 if (N >= 0) {
1367 String[] val = new String[N];
1368 for (int i=0; i<N; i++) {
1369 val[i] = readString();
1370 }
1371 return val;
1372 } else {
1373 return null;
1374 }
1375 }
1376
Jake Whartonb1f474c2018-05-30 15:11:13 -04001377 public final void readStringArray(@NonNull String[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 int N = readInt();
1379 if (N == val.length) {
1380 for (int i=0; i<N; i++) {
1381 val[i] = readString();
1382 }
1383 } else {
1384 throw new RuntimeException("bad array lengths");
1385 }
1386 }
1387
Jake Whartonb1f474c2018-05-30 15:11:13 -04001388 public final void writeBinderArray(@Nullable IBinder[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 if (val != null) {
1390 int N = val.length;
1391 writeInt(N);
1392 for (int i=0; i<N; i++) {
1393 writeStrongBinder(val[i]);
1394 }
1395 } else {
1396 writeInt(-1);
1397 }
1398 }
1399
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001400 /**
1401 * @hide
1402 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001403 public final void writeCharSequenceArray(@Nullable CharSequence[] val) {
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001404 if (val != null) {
1405 int N = val.length;
1406 writeInt(N);
1407 for (int i=0; i<N; i++) {
1408 writeCharSequence(val[i]);
1409 }
1410 } else {
1411 writeInt(-1);
1412 }
1413 }
1414
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001415 /**
1416 * @hide
1417 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001418 public final void writeCharSequenceList(@Nullable ArrayList<CharSequence> val) {
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001419 if (val != null) {
1420 int N = val.size();
1421 writeInt(N);
1422 for (int i=0; i<N; i++) {
1423 writeCharSequence(val.get(i));
1424 }
1425 } else {
1426 writeInt(-1);
1427 }
1428 }
1429
Jake Whartonb1f474c2018-05-30 15:11:13 -04001430 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 public final IBinder[] createBinderArray() {
1432 int N = readInt();
1433 if (N >= 0) {
1434 IBinder[] val = new IBinder[N];
1435 for (int i=0; i<N; i++) {
1436 val[i] = readStrongBinder();
1437 }
1438 return val;
1439 } else {
1440 return null;
1441 }
1442 }
1443
Jake Whartonb1f474c2018-05-30 15:11:13 -04001444 public final void readBinderArray(@NonNull IBinder[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 int N = readInt();
1446 if (N == val.length) {
1447 for (int i=0; i<N; i++) {
1448 val[i] = readStrongBinder();
1449 }
1450 } else {
1451 throw new RuntimeException("bad array lengths");
1452 }
1453 }
1454
1455 /**
1456 * Flatten a List containing a particular object type into the parcel, at
1457 * the current dataPosition() and growing dataCapacity() if needed. The
1458 * type of the objects in the list must be one that implements Parcelable.
1459 * Unlike the generic writeList() method, however, only the raw data of the
1460 * objects is written and not their type, so you must use the corresponding
1461 * readTypedList() to unmarshall them.
1462 *
1463 * @param val The list of objects to be written.
1464 *
1465 * @see #createTypedArrayList
1466 * @see #readTypedList
1467 * @see Parcelable
1468 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001469 public final <T extends Parcelable> void writeTypedList(@Nullable List<T> val) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001470 writeTypedList(val, 0);
1471 }
1472
1473 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08001474 * Flatten a {@link SparseArray} containing a particular object type into the parcel
1475 * at the current dataPosition() and growing dataCapacity() if needed. The
1476 * type of the objects in the array must be one that implements Parcelable.
1477 * Unlike the generic {@link #writeSparseArray(SparseArray)} method, however, only
1478 * the raw data of the objects is written and not their type, so you must use the
1479 * corresponding {@link #createTypedSparseArray(Parcelable.Creator)}.
1480 *
1481 * @param val The list of objects to be written.
1482 * @param parcelableFlags The parcelable flags to use.
1483 *
1484 * @see #createTypedSparseArray(Parcelable.Creator)
1485 * @see Parcelable
1486 */
1487 public final <T extends Parcelable> void writeTypedSparseArray(@Nullable SparseArray<T> val,
1488 int parcelableFlags) {
1489 if (val == null) {
1490 writeInt(-1);
1491 return;
1492 }
1493 final int count = val.size();
1494 writeInt(count);
1495 for (int i = 0; i < count; i++) {
1496 writeInt(val.keyAt(i));
1497 writeTypedObject(val.valueAt(i), parcelableFlags);
1498 }
1499 }
1500
1501 /**
Sunny Goyal0e60f222017-09-21 21:39:20 -07001502 * @hide
1503 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001504 public <T extends Parcelable> void writeTypedList(@Nullable List<T> val, int parcelableFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 if (val == null) {
1506 writeInt(-1);
1507 return;
1508 }
1509 int N = val.size();
1510 int i=0;
1511 writeInt(N);
1512 while (i < N) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001513 writeTypedObject(val.get(i), parcelableFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 i++;
1515 }
1516 }
1517
1518 /**
1519 * Flatten a List containing String objects into the parcel, at
1520 * the current dataPosition() and growing dataCapacity() if needed. They
1521 * can later be retrieved with {@link #createStringArrayList} or
1522 * {@link #readStringList}.
1523 *
1524 * @param val The list of strings to be written.
1525 *
1526 * @see #createStringArrayList
1527 * @see #readStringList
1528 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001529 public final void writeStringList(@Nullable List<String> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 if (val == null) {
1531 writeInt(-1);
1532 return;
1533 }
1534 int N = val.size();
1535 int i=0;
1536 writeInt(N);
1537 while (i < N) {
1538 writeString(val.get(i));
1539 i++;
1540 }
1541 }
1542
1543 /**
1544 * Flatten a List containing IBinder objects into the parcel, at
1545 * the current dataPosition() and growing dataCapacity() if needed. They
1546 * can later be retrieved with {@link #createBinderArrayList} or
1547 * {@link #readBinderList}.
1548 *
1549 * @param val The list of strings to be written.
1550 *
1551 * @see #createBinderArrayList
1552 * @see #readBinderList
1553 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001554 public final void writeBinderList(@Nullable List<IBinder> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 if (val == null) {
1556 writeInt(-1);
1557 return;
1558 }
1559 int N = val.size();
1560 int i=0;
1561 writeInt(N);
1562 while (i < N) {
1563 writeStrongBinder(val.get(i));
1564 i++;
1565 }
1566 }
1567
1568 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00001569 * Flatten a {@code List} containing arbitrary {@code Parcelable} objects into this parcel
1570 * at the current position. They can later be retrieved using
1571 * {@link #readParcelableList(List, ClassLoader)} if required.
1572 *
1573 * @see #readParcelableList(List, ClassLoader)
Narayan Kamathbea48712016-12-01 15:38:28 +00001574 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001575 public final <T extends Parcelable> void writeParcelableList(@Nullable List<T> val, int flags) {
Narayan Kamathbea48712016-12-01 15:38:28 +00001576 if (val == null) {
1577 writeInt(-1);
1578 return;
1579 }
1580
1581 int N = val.size();
1582 int i=0;
1583 writeInt(N);
1584 while (i < N) {
1585 writeParcelable(val.get(i), flags);
1586 i++;
1587 }
1588 }
1589
1590 /**
Svet Ganov0f4928f2017-02-02 20:02:51 -08001591 * Flatten a homogeneous array containing a particular object type into
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 * the parcel, at
1593 * the current dataPosition() and growing dataCapacity() if needed. The
1594 * type of the objects in the array must be one that implements Parcelable.
1595 * Unlike the {@link #writeParcelableArray} method, however, only the
1596 * raw data of the objects is written and not their type, so you must use
1597 * {@link #readTypedArray} with the correct corresponding
1598 * {@link Parcelable.Creator} implementation to unmarshall them.
1599 *
1600 * @param val The array of objects to be written.
1601 * @param parcelableFlags Contextual flags as per
1602 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1603 *
1604 * @see #readTypedArray
1605 * @see #writeParcelableArray
1606 * @see Parcelable.Creator
1607 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001608 public final <T extends Parcelable> void writeTypedArray(@Nullable T[] val,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 int parcelableFlags) {
1610 if (val != null) {
1611 int N = val.length;
1612 writeInt(N);
Svet Ganov0f4928f2017-02-02 20:02:51 -08001613 for (int i = 0; i < N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001614 writeTypedObject(val[i], parcelableFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 }
1616 } else {
1617 writeInt(-1);
1618 }
1619 }
1620
1621 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001622 * Flatten the Parcelable object into the parcel.
1623 *
1624 * @param val The Parcelable object to be written.
1625 * @param parcelableFlags Contextual flags as per
1626 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1627 *
1628 * @see #readTypedObject
1629 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001630 public final <T extends Parcelable> void writeTypedObject(@Nullable T val,
1631 int parcelableFlags) {
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001632 if (val != null) {
1633 writeInt(1);
1634 val.writeToParcel(this, parcelableFlags);
1635 } else {
1636 writeInt(0);
1637 }
1638 }
1639
1640 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 * Flatten a generic object in to a parcel. The given Object value may
1642 * currently be one of the following types:
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001643 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 * <ul>
1645 * <li> null
1646 * <li> String
1647 * <li> Byte
1648 * <li> Short
1649 * <li> Integer
1650 * <li> Long
1651 * <li> Float
1652 * <li> Double
1653 * <li> Boolean
1654 * <li> String[]
1655 * <li> boolean[]
1656 * <li> byte[]
1657 * <li> int[]
1658 * <li> long[]
1659 * <li> Object[] (supporting objects of the same type defined here).
1660 * <li> {@link Bundle}
1661 * <li> Map (as supported by {@link #writeMap}).
1662 * <li> Any object that implements the {@link Parcelable} protocol.
1663 * <li> Parcelable[]
1664 * <li> CharSequence (as supported by {@link TextUtils#writeToParcel}).
1665 * <li> List (as supported by {@link #writeList}).
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001666 * <li> {@link SparseArray} (as supported by {@link #writeSparseArray(SparseArray)}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 * <li> {@link IBinder}
1668 * <li> Any object that implements Serializable (but see
1669 * {@link #writeSerializable} for caveats). Note that all of the
1670 * previous types have relatively efficient implementations for
1671 * writing to a Parcel; having to rely on the generic serialization
1672 * approach is much less efficient and should be avoided whenever
1673 * possible.
1674 * </ul>
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001675 *
1676 * <p class="caution">{@link Parcelable} objects are written with
1677 * {@link Parcelable#writeToParcel} using contextual flags of 0. When
1678 * serializing objects containing {@link ParcelFileDescriptor}s,
1679 * this may result in file descriptor leaks when they are returned from
1680 * Binder calls (where {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE}
1681 * should be used).</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001683 public final void writeValue(@Nullable Object v) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 if (v == null) {
1685 writeInt(VAL_NULL);
1686 } else if (v instanceof String) {
1687 writeInt(VAL_STRING);
1688 writeString((String) v);
1689 } else if (v instanceof Integer) {
1690 writeInt(VAL_INTEGER);
1691 writeInt((Integer) v);
1692 } else if (v instanceof Map) {
1693 writeInt(VAL_MAP);
1694 writeMap((Map) v);
1695 } else if (v instanceof Bundle) {
1696 // Must be before Parcelable
1697 writeInt(VAL_BUNDLE);
1698 writeBundle((Bundle) v);
Samuel Tanceafe5e2015-12-11 16:50:58 -08001699 } else if (v instanceof PersistableBundle) {
1700 writeInt(VAL_PERSISTABLEBUNDLE);
1701 writePersistableBundle((PersistableBundle) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 } else if (v instanceof Parcelable) {
Samuel Tanceafe5e2015-12-11 16:50:58 -08001703 // IMPOTANT: cases for classes that implement Parcelable must
1704 // come before the Parcelable case, so that their specific VAL_*
1705 // types will be written.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 writeInt(VAL_PARCELABLE);
1707 writeParcelable((Parcelable) v, 0);
1708 } else if (v instanceof Short) {
1709 writeInt(VAL_SHORT);
1710 writeInt(((Short) v).intValue());
1711 } else if (v instanceof Long) {
1712 writeInt(VAL_LONG);
1713 writeLong((Long) v);
1714 } else if (v instanceof Float) {
1715 writeInt(VAL_FLOAT);
1716 writeFloat((Float) v);
1717 } else if (v instanceof Double) {
1718 writeInt(VAL_DOUBLE);
1719 writeDouble((Double) v);
1720 } else if (v instanceof Boolean) {
1721 writeInt(VAL_BOOLEAN);
1722 writeInt((Boolean) v ? 1 : 0);
1723 } else if (v instanceof CharSequence) {
1724 // Must be after String
1725 writeInt(VAL_CHARSEQUENCE);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001726 writeCharSequence((CharSequence) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 } else if (v instanceof List) {
1728 writeInt(VAL_LIST);
1729 writeList((List) v);
1730 } else if (v instanceof SparseArray) {
1731 writeInt(VAL_SPARSEARRAY);
1732 writeSparseArray((SparseArray) v);
1733 } else if (v instanceof boolean[]) {
1734 writeInt(VAL_BOOLEANARRAY);
1735 writeBooleanArray((boolean[]) v);
1736 } else if (v instanceof byte[]) {
1737 writeInt(VAL_BYTEARRAY);
1738 writeByteArray((byte[]) v);
1739 } else if (v instanceof String[]) {
1740 writeInt(VAL_STRINGARRAY);
1741 writeStringArray((String[]) v);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001742 } else if (v instanceof CharSequence[]) {
1743 // Must be after String[] and before Object[]
1744 writeInt(VAL_CHARSEQUENCEARRAY);
1745 writeCharSequenceArray((CharSequence[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 } else if (v instanceof IBinder) {
1747 writeInt(VAL_IBINDER);
1748 writeStrongBinder((IBinder) v);
1749 } else if (v instanceof Parcelable[]) {
1750 writeInt(VAL_PARCELABLEARRAY);
1751 writeParcelableArray((Parcelable[]) v, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 } else if (v instanceof int[]) {
1753 writeInt(VAL_INTARRAY);
1754 writeIntArray((int[]) v);
1755 } else if (v instanceof long[]) {
1756 writeInt(VAL_LONGARRAY);
1757 writeLongArray((long[]) v);
1758 } else if (v instanceof Byte) {
1759 writeInt(VAL_BYTE);
1760 writeInt((Byte) v);
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001761 } else if (v instanceof Size) {
1762 writeInt(VAL_SIZE);
1763 writeSize((Size) v);
1764 } else if (v instanceof SizeF) {
1765 writeInt(VAL_SIZEF);
1766 writeSizeF((SizeF) v);
Samuel Tana8036662015-11-23 14:36:00 -08001767 } else if (v instanceof double[]) {
1768 writeInt(VAL_DOUBLEARRAY);
1769 writeDoubleArray((double[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 } else {
Paul Duffinac5a0822014-02-03 15:02:17 +00001771 Class<?> clazz = v.getClass();
1772 if (clazz.isArray() && clazz.getComponentType() == Object.class) {
1773 // Only pure Object[] are written here, Other arrays of non-primitive types are
1774 // handled by serialization as this does not record the component type.
1775 writeInt(VAL_OBJECTARRAY);
1776 writeArray((Object[]) v);
1777 } else if (v instanceof Serializable) {
1778 // Must be last
1779 writeInt(VAL_SERIALIZABLE);
1780 writeSerializable((Serializable) v);
1781 } else {
1782 throw new RuntimeException("Parcel: unable to marshal value " + v);
1783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 }
1785 }
1786
1787 /**
1788 * Flatten the name of the class of the Parcelable and its contents
1789 * into the parcel.
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001790 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 * @param p The Parcelable object to be written.
1792 * @param parcelableFlags Contextual flags as per
1793 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1794 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001795 public final void writeParcelable(@Nullable Parcelable p, int parcelableFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 if (p == null) {
1797 writeString(null);
1798 return;
1799 }
Neil Fuller44e440c2015-04-20 14:39:00 +01001800 writeParcelableCreator(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 p.writeToParcel(this, parcelableFlags);
1802 }
1803
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001804 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001805 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04001806 public final void writeParcelableCreator(@NonNull Parcelable p) {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001807 String name = p.getClass().getName();
1808 writeString(name);
1809 }
1810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 /**
1812 * Write a generic serializable object in to a Parcel. It is strongly
1813 * recommended that this method be avoided, since the serialization
1814 * overhead is extremely large, and this approach will be much slower than
1815 * using the other approaches to writing data in to a Parcel.
1816 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001817 public final void writeSerializable(@Nullable Serializable s) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 if (s == null) {
1819 writeString(null);
1820 return;
1821 }
1822 String name = s.getClass().getName();
1823 writeString(name);
1824
1825 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1826 try {
1827 ObjectOutputStream oos = new ObjectOutputStream(baos);
1828 oos.writeObject(s);
1829 oos.close();
1830
1831 writeByteArray(baos.toByteArray());
1832 } catch (IOException ioe) {
1833 throw new RuntimeException("Parcelable encountered " +
1834 "IOException writing serializable object (name = " + name +
1835 ")", ioe);
1836 }
1837 }
1838
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001839 /** @hide For debugging purposes */
1840 public static void setStackTraceParceling(boolean enabled) {
1841 sParcelExceptionStackTrace = enabled;
1842 }
1843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 /**
1845 * Special function for writing an exception result at the header of
1846 * a parcel, to be used when returning an exception from a transaction.
1847 * Note that this currently only supports a few exception types; any other
1848 * exception will be re-thrown by this function as a RuntimeException
1849 * (to be caught by the system's last-resort exception handling when
1850 * dispatching a transaction).
Samuel Tana8036662015-11-23 14:36:00 -08001851 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 * <p>The supported exception types are:
1853 * <ul>
1854 * <li>{@link BadParcelableException}
1855 * <li>{@link IllegalArgumentException}
1856 * <li>{@link IllegalStateException}
1857 * <li>{@link NullPointerException}
1858 * <li>{@link SecurityException}
Dianne Hackborn18482ae2017-08-01 17:41:00 -07001859 * <li>{@link UnsupportedOperationException}
Dianne Hackborn7e714422013-09-13 17:32:57 -07001860 * <li>{@link NetworkOnMainThreadException}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 * </ul>
Samuel Tana8036662015-11-23 14:36:00 -08001862 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 * @param e The Exception to be written.
1864 *
1865 * @see #writeNoException
1866 * @see #readException
1867 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001868 public final void writeException(@NonNull Exception e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 int code = 0;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001870 if (e instanceof Parcelable
1871 && (e.getClass().getClassLoader() == Parcelable.class.getClassLoader())) {
1872 // We only send Parcelable exceptions that are in the
1873 // BootClassLoader to ensure that the receiver can unpack them
1874 code = EX_PARCELABLE;
1875 } else if (e instanceof SecurityException) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 code = EX_SECURITY;
1877 } else if (e instanceof BadParcelableException) {
1878 code = EX_BAD_PARCELABLE;
1879 } else if (e instanceof IllegalArgumentException) {
1880 code = EX_ILLEGAL_ARGUMENT;
1881 } else if (e instanceof NullPointerException) {
1882 code = EX_NULL_POINTER;
1883 } else if (e instanceof IllegalStateException) {
1884 code = EX_ILLEGAL_STATE;
Dianne Hackborn7e714422013-09-13 17:32:57 -07001885 } else if (e instanceof NetworkOnMainThreadException) {
1886 code = EX_NETWORK_MAIN_THREAD;
Dianne Hackborn33d738a2014-09-12 14:23:58 -07001887 } else if (e instanceof UnsupportedOperationException) {
1888 code = EX_UNSUPPORTED_OPERATION;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001889 } else if (e instanceof ServiceSpecificException) {
1890 code = EX_SERVICE_SPECIFIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 }
1892 writeInt(code);
Brad Fitzpatrick703e5d32010-07-15 13:16:41 -07001893 StrictMode.clearGatheredViolations();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 if (code == 0) {
1895 if (e instanceof RuntimeException) {
1896 throw (RuntimeException) e;
1897 }
1898 throw new RuntimeException(e);
1899 }
1900 writeString(e.getMessage());
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001901 final long timeNow = sParcelExceptionStackTrace ? SystemClock.elapsedRealtime() : 0;
1902 if (sParcelExceptionStackTrace && (timeNow - sLastWriteExceptionStackTrace
1903 > WRITE_EXCEPTION_STACK_TRACE_THRESHOLD_MS)) {
1904 sLastWriteExceptionStackTrace = timeNow;
1905 final int sizePosition = dataPosition();
1906 writeInt(0); // Header size will be filled in later
1907 StackTraceElement[] stackTrace = e.getStackTrace();
1908 final int truncatedSize = Math.min(stackTrace.length, 5);
1909 StringBuilder sb = new StringBuilder();
1910 for (int i = 0; i < truncatedSize; i++) {
Fyodor Kupolov679d9982017-11-21 10:42:23 -08001911 sb.append("\tat ").append(stackTrace[i]).append('\n');
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001912 }
1913 writeString(sb.toString());
1914 final int payloadPosition = dataPosition();
1915 setDataPosition(sizePosition);
1916 // Write stack trace header size. Used in native side to skip the header
1917 writeInt(payloadPosition - sizePosition);
1918 setDataPosition(payloadPosition);
1919 } else {
1920 writeInt(0);
1921 }
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001922 switch (code) {
1923 case EX_SERVICE_SPECIFIC:
1924 writeInt(((ServiceSpecificException) e).errorCode);
1925 break;
1926 case EX_PARCELABLE:
1927 // Write parceled exception prefixed by length
1928 final int sizePosition = dataPosition();
1929 writeInt(0);
1930 writeParcelable((Parcelable) e, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
1931 final int payloadPosition = dataPosition();
1932 setDataPosition(sizePosition);
1933 writeInt(payloadPosition - sizePosition);
1934 setDataPosition(payloadPosition);
1935 break;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 }
1938
1939 /**
1940 * Special function for writing information at the front of the Parcel
1941 * indicating that no exception occurred.
1942 *
1943 * @see #writeException
1944 * @see #readException
1945 */
1946 public final void writeNoException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001947 // Despite the name of this function ("write no exception"),
1948 // it should instead be thought of as "write the RPC response
1949 // header", but because this function name is written out by
1950 // the AIDL compiler, we're not going to rename it.
1951 //
1952 // The response header, in the non-exception case (see also
1953 // writeException above, also called by the AIDL compiler), is
1954 // either a 0 (the default case), or EX_HAS_REPLY_HEADER if
1955 // StrictMode has gathered up violations that have occurred
1956 // during a Binder call, in which case we write out the number
1957 // of violations and their details, serialized, before the
1958 // actual RPC respons data. The receiving end of this is
1959 // readException(), below.
1960 if (StrictMode.hasGatheredViolations()) {
1961 writeInt(EX_HAS_REPLY_HEADER);
1962 final int sizePosition = dataPosition();
1963 writeInt(0); // total size of fat header, to be filled in later
1964 StrictMode.writeGatheredViolationsToParcel(this);
1965 final int payloadPosition = dataPosition();
1966 setDataPosition(sizePosition);
1967 writeInt(payloadPosition - sizePosition); // header size
1968 setDataPosition(payloadPosition);
1969 } else {
1970 writeInt(0);
1971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 }
1973
1974 /**
1975 * Special function for reading an exception result from the header of
1976 * a parcel, to be used after receiving the result of a transaction. This
1977 * will throw the exception for you if it had been written to the Parcel,
1978 * otherwise return and let you read the normal result data from the Parcel.
1979 *
1980 * @see #writeException
1981 * @see #writeNoException
1982 */
1983 public final void readException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001984 int code = readExceptionCode();
1985 if (code != 0) {
1986 String msg = readString();
Takamasa Kuramitsu2214b822018-04-02 14:44:59 +09001987 readException(code, msg);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001988 }
1989 }
1990
1991 /**
1992 * Parses the header of a Binder call's response Parcel and
1993 * returns the exception code. Deals with lite or fat headers.
1994 * In the common successful case, this header is generally zero.
1995 * In less common cases, it's a small negative number and will be
1996 * followed by an error string.
1997 *
1998 * This exists purely for android.database.DatabaseUtils and
1999 * insulating it from having to handle fat headers as returned by
2000 * e.g. StrictMode-induced RPC responses.
2001 *
2002 * @hide
2003 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002004 @UnsupportedAppUsage
Howard Chen40abbb02019-05-24 02:04:50 +08002005 @TestApi
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002006 public final int readExceptionCode() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 int code = readInt();
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002008 if (code == EX_HAS_REPLY_HEADER) {
2009 int headerSize = readInt();
2010 if (headerSize == 0) {
2011 Log.e(TAG, "Unexpected zero-sized Parcel reply header.");
2012 } else {
2013 // Currently the only thing in the header is StrictMode stacks,
2014 // but discussions around event/RPC tracing suggest we might
2015 // put that here too. If so, switch on sub-header tags here.
2016 // But for now, just parse out the StrictMode stuff.
2017 StrictMode.readAndHandleBinderCallViolations(this);
2018 }
2019 // And fat response headers are currently only used when
2020 // there are no exceptions, so return no error:
2021 return 0;
2022 }
2023 return code;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 }
2025
2026 /**
Mark Doliner879ea452014-01-02 12:38:07 -08002027 * Throw an exception with the given message. Not intended for use
2028 * outside the Parcel class.
2029 *
2030 * @param code Used to determine which exception class to throw.
2031 * @param msg The exception message.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 */
2033 public final void readException(int code, String msg) {
Takamasa Kuramitsu2214b822018-04-02 14:44:59 +09002034 String remoteStackTrace = null;
2035 final int remoteStackPayloadSize = readInt();
2036 if (remoteStackPayloadSize > 0) {
2037 remoteStackTrace = readString();
2038 }
2039 Exception e = createException(code, msg);
2040 // Attach remote stack trace if availalble
2041 if (remoteStackTrace != null) {
2042 RemoteException cause = new RemoteException(
2043 "Remote stack trace:\n" + remoteStackTrace, null, false, false);
2044 try {
2045 Throwable rootCause = ExceptionUtils.getRootCause(e);
2046 if (rootCause != null) {
2047 rootCause.initCause(cause);
2048 }
2049 } catch (RuntimeException ex) {
2050 Log.e(TAG, "Cannot set cause " + cause + " for " + e, ex);
2051 }
2052 }
2053 SneakyThrow.sneakyThrow(e);
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002054 }
2055
2056 /**
2057 * Creates an exception with the given message.
2058 *
2059 * @param code Used to determine which exception class to throw.
2060 * @param msg The exception message.
2061 */
2062 private Exception createException(int code, String msg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 switch (code) {
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002064 case EX_PARCELABLE:
2065 if (readInt() > 0) {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002066 return (Exception) readParcelable(Parcelable.class.getClassLoader());
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002067 } else {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002068 return new RuntimeException(msg + " [missing Parcelable]");
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002069 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 case EX_SECURITY:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002071 return new SecurityException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 case EX_BAD_PARCELABLE:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002073 return new BadParcelableException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 case EX_ILLEGAL_ARGUMENT:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002075 return new IllegalArgumentException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 case EX_NULL_POINTER:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002077 return new NullPointerException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 case EX_ILLEGAL_STATE:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002079 return new IllegalStateException(msg);
Dianne Hackborn7e714422013-09-13 17:32:57 -07002080 case EX_NETWORK_MAIN_THREAD:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002081 return new NetworkOnMainThreadException();
Dianne Hackborn33d738a2014-09-12 14:23:58 -07002082 case EX_UNSUPPORTED_OPERATION:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002083 return new UnsupportedOperationException(msg);
Christopher Wiley80fd1202015-11-22 17:12:37 -08002084 case EX_SERVICE_SPECIFIC:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002085 return new ServiceSpecificException(readInt(), msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 }
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002087 return new RuntimeException("Unknown exception code: " + code
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 + " msg " + msg);
2089 }
2090
2091 /**
2092 * Read an integer value from the parcel at the current dataPosition().
2093 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002094 public final int readInt() {
2095 return nativeReadInt(mNativePtr);
2096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097
2098 /**
2099 * Read a long integer value from the parcel at the current dataPosition().
2100 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002101 public final long readLong() {
2102 return nativeReadLong(mNativePtr);
2103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104
2105 /**
2106 * Read a floating point value from the parcel at the current
2107 * dataPosition().
2108 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002109 public final float readFloat() {
2110 return nativeReadFloat(mNativePtr);
2111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112
2113 /**
2114 * Read a double precision floating point value from the parcel at the
2115 * current dataPosition().
2116 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002117 public final double readDouble() {
2118 return nativeReadDouble(mNativePtr);
2119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120
2121 /**
2122 * Read a string value from the parcel at the current dataPosition().
2123 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002124 @Nullable
Jeff Sharkey047238c2012-03-07 16:51:38 -08002125 public final String readString() {
Makoto Onuki4501c61d2017-07-27 15:56:40 -07002126 return mReadWriteHelper.readString(this);
2127 }
2128
2129 /**
2130 * Read a string without going though a {@link ReadWriteHelper}. Subclasses of
2131 * {@link ReadWriteHelper} must use this method instead of {@link #readString} to avoid
2132 * infinity recursive calls.
2133 *
2134 * @hide
2135 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002136 @Nullable
Makoto Onuki4501c61d2017-07-27 15:56:40 -07002137 public String readStringNoHelper() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002138 return nativeReadString(mNativePtr);
2139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140
Jake Wharton2ffa4312018-04-23 17:47:14 -04002141 /**
2142 * Read a boolean value from the parcel at the current dataPosition().
2143 */
Eugene Susla36e866b2017-02-23 18:24:39 -08002144 public final boolean readBoolean() {
2145 return readInt() != 0;
2146 }
2147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002149 * Read a CharSequence value from the parcel at the current dataPosition().
2150 * @hide
2151 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002152 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04002153 @Nullable
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002154 public final CharSequence readCharSequence() {
2155 return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(this);
2156 }
2157
2158 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 * Read an object from the parcel at the current dataPosition().
2160 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002161 public final IBinder readStrongBinder() {
2162 return nativeReadStrongBinder(mNativePtr);
2163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164
2165 /**
2166 * Read a FileDescriptor from the parcel at the current dataPosition().
2167 */
2168 public final ParcelFileDescriptor readFileDescriptor() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002169 FileDescriptor fd = nativeReadFileDescriptor(mNativePtr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 return fd != null ? new ParcelFileDescriptor(fd) : null;
2171 }
2172
Jeff Sharkeyda5a3e12013-08-11 12:54:42 -07002173 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +00002174 @UnsupportedAppUsage
Jeff Sharkeyda5a3e12013-08-11 12:54:42 -07002175 public final FileDescriptor readRawFileDescriptor() {
2176 return nativeReadFileDescriptor(mNativePtr);
2177 }
2178
Casey Dahlin2f974b22015-11-05 12:19:13 -08002179 /**
2180 * {@hide}
2181 * Read and return a new array of FileDescriptors from the parcel.
2182 * @return the FileDescriptor array, or null if the array is null.
2183 **/
Jake Whartonb1f474c2018-05-30 15:11:13 -04002184 @Nullable
Casey Dahlin2f974b22015-11-05 12:19:13 -08002185 public final FileDescriptor[] createRawFileDescriptorArray() {
2186 int N = readInt();
2187 if (N < 0) {
2188 return null;
2189 }
2190 FileDescriptor[] f = new FileDescriptor[N];
2191 for (int i = 0; i < N; i++) {
2192 f[i] = readRawFileDescriptor();
2193 }
2194 return f;
2195 }
2196
2197 /**
2198 * {@hide}
2199 * Read an array of FileDescriptors from a parcel.
2200 * The passed array must be exactly the length of the array in the parcel.
2201 * @return the FileDescriptor array, or null if the array is null.
2202 **/
2203 public final void readRawFileDescriptorArray(FileDescriptor[] val) {
2204 int N = readInt();
2205 if (N == val.length) {
2206 for (int i=0; i<N; i++) {
2207 val[i] = readRawFileDescriptor();
2208 }
2209 } else {
2210 throw new RuntimeException("bad array lengths");
2211 }
2212 }
2213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 /**
2215 * Read a byte value from the parcel at the current dataPosition().
2216 */
2217 public final byte readByte() {
2218 return (byte)(readInt() & 0xff);
2219 }
2220
2221 /**
2222 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2223 * been written with {@link #writeBundle}. Read into an existing Map object
2224 * from the parcel at the current dataPosition().
2225 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002226 public final void readMap(@NonNull Map outVal, @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 int N = readInt();
2228 readMapInternal(outVal, N, loader);
2229 }
2230
2231 /**
2232 * Read into an existing List object from the parcel at the current
2233 * dataPosition(), using the given class loader to load any enclosed
2234 * Parcelables. If it is null, the default class loader is used.
2235 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002236 public final void readList(@NonNull List outVal, @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 int N = readInt();
2238 readListInternal(outVal, N, loader);
2239 }
2240
2241 /**
2242 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2243 * been written with {@link #writeBundle}. Read and return a new HashMap
2244 * object from the parcel at the current dataPosition(), using the given
2245 * class loader to load any enclosed Parcelables. Returns null if
2246 * the previously written map object was null.
2247 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002248 @Nullable
2249 public final HashMap readHashMap(@Nullable ClassLoader loader)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 {
2251 int N = readInt();
2252 if (N < 0) {
2253 return null;
2254 }
2255 HashMap m = new HashMap(N);
2256 readMapInternal(m, N, loader);
2257 return m;
2258 }
2259
2260 /**
2261 * Read and return a new Bundle object from the parcel at the current
2262 * dataPosition(). Returns null if the previously written Bundle object was
2263 * null.
2264 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002265 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 public final Bundle readBundle() {
2267 return readBundle(null);
2268 }
2269
2270 /**
2271 * Read and return a new Bundle object from the parcel at the current
2272 * dataPosition(), using the given class loader to initialize the class
2273 * loader of the Bundle for later retrieval of Parcelable objects.
2274 * Returns null if the previously written Bundle object was null.
2275 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002276 @Nullable
2277 public final Bundle readBundle(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 int length = readInt();
2279 if (length < 0) {
Dianne Hackborn4a7d8242013-10-03 10:19:20 -07002280 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 return null;
2282 }
Samuel Tana8036662015-11-23 14:36:00 -08002283
Dianne Hackborn6aff9052009-05-22 13:20:23 -07002284 final Bundle bundle = new Bundle(this, length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 if (loader != null) {
2286 bundle.setClassLoader(loader);
2287 }
2288 return bundle;
2289 }
2290
2291 /**
Craig Mautner719e6b12014-04-04 20:29:41 -07002292 * Read and return a new Bundle object from the parcel at the current
2293 * dataPosition(). Returns null if the previously written Bundle object was
2294 * null.
2295 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002296 @Nullable
Craig Mautner719e6b12014-04-04 20:29:41 -07002297 public final PersistableBundle readPersistableBundle() {
2298 return readPersistableBundle(null);
2299 }
2300
2301 /**
2302 * Read and return a new Bundle object from the parcel at the current
2303 * dataPosition(), using the given class loader to initialize the class
2304 * loader of the Bundle for later retrieval of Parcelable objects.
2305 * Returns null if the previously written Bundle object was null.
2306 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002307 @Nullable
2308 public final PersistableBundle readPersistableBundle(@Nullable ClassLoader loader) {
Craig Mautner719e6b12014-04-04 20:29:41 -07002309 int length = readInt();
2310 if (length < 0) {
2311 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
2312 return null;
2313 }
2314
2315 final PersistableBundle bundle = new PersistableBundle(this, length);
2316 if (loader != null) {
2317 bundle.setClassLoader(loader);
2318 }
2319 return bundle;
2320 }
2321
2322 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002323 * Read a Size from the parcel at the current dataPosition().
2324 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002325 @NonNull
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002326 public final Size readSize() {
2327 final int width = readInt();
2328 final int height = readInt();
2329 return new Size(width, height);
2330 }
2331
2332 /**
2333 * Read a SizeF from the parcel at the current dataPosition().
2334 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002335 @NonNull
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002336 public final SizeF readSizeF() {
2337 final float width = readFloat();
2338 final float height = readFloat();
2339 return new SizeF(width, height);
2340 }
2341
2342 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 * Read and return a byte[] object from the parcel.
2344 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002345 @Nullable
Jeff Sharkey047238c2012-03-07 16:51:38 -08002346 public final byte[] createByteArray() {
2347 return nativeCreateByteArray(mNativePtr);
2348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349
2350 /**
2351 * Read a byte[] object from the parcel and copy it into the
2352 * given byte array.
2353 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002354 public final void readByteArray(@NonNull byte[] val) {
Jocelyn Dang46100442017-05-05 15:40:49 -07002355 boolean valid = nativeReadByteArray(mNativePtr, val, (val != null) ? val.length : 0);
2356 if (!valid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 throw new RuntimeException("bad array lengths");
2358 }
2359 }
2360
2361 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002362 * Read a blob of data from the parcel and return it as a byte array.
2363 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -07002364 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002365 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002366 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04002367 @Nullable
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002368 public final byte[] readBlob() {
2369 return nativeReadBlob(mNativePtr);
2370 }
2371
2372 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 * Read and return a String[] object from the parcel.
2374 * {@hide}
2375 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002376 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04002377 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 public final String[] readStringArray() {
2379 String[] array = null;
2380
2381 int length = readInt();
2382 if (length >= 0)
2383 {
2384 array = new String[length];
2385
2386 for (int i = 0 ; i < length ; i++)
2387 {
2388 array[i] = readString();
2389 }
2390 }
2391
2392 return array;
2393 }
2394
2395 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002396 * Read and return a CharSequence[] object from the parcel.
2397 * {@hide}
2398 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002399 @Nullable
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002400 public final CharSequence[] readCharSequenceArray() {
2401 CharSequence[] array = null;
2402
2403 int length = readInt();
2404 if (length >= 0)
2405 {
2406 array = new CharSequence[length];
2407
2408 for (int i = 0 ; i < length ; i++)
2409 {
2410 array[i] = readCharSequence();
2411 }
2412 }
2413
2414 return array;
2415 }
2416
2417 /**
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002418 * Read and return an ArrayList&lt;CharSequence&gt; object from the parcel.
2419 * {@hide}
2420 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002421 @Nullable
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002422 public final ArrayList<CharSequence> readCharSequenceList() {
2423 ArrayList<CharSequence> array = null;
2424
2425 int length = readInt();
2426 if (length >= 0) {
2427 array = new ArrayList<CharSequence>(length);
2428
2429 for (int i = 0 ; i < length ; i++) {
2430 array.add(readCharSequence());
2431 }
2432 }
2433
2434 return array;
2435 }
2436
2437 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 * Read and return a new ArrayList object from the parcel at the current
2439 * dataPosition(). Returns null if the previously written list object was
2440 * null. The given class loader will be used to load any enclosed
2441 * Parcelables.
2442 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002443 @Nullable
2444 public final ArrayList readArrayList(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 int N = readInt();
2446 if (N < 0) {
2447 return null;
2448 }
2449 ArrayList l = new ArrayList(N);
2450 readListInternal(l, N, loader);
2451 return l;
2452 }
2453
2454 /**
2455 * Read and return a new Object array from the parcel at the current
2456 * dataPosition(). Returns null if the previously written array was
2457 * null. The given class loader will be used to load any enclosed
2458 * Parcelables.
2459 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002460 @Nullable
2461 public final Object[] readArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 int N = readInt();
2463 if (N < 0) {
2464 return null;
2465 }
2466 Object[] l = new Object[N];
2467 readArrayInternal(l, N, loader);
2468 return l;
2469 }
2470
2471 /**
2472 * Read and return a new SparseArray object from the parcel at the current
2473 * dataPosition(). Returns null if the previously written list object was
2474 * null. The given class loader will be used to load any enclosed
2475 * Parcelables.
2476 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002477 @Nullable
Svet Ganov8455ba22019-01-02 13:05:56 -08002478 public final <T> SparseArray<T> readSparseArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 int N = readInt();
2480 if (N < 0) {
2481 return null;
2482 }
2483 SparseArray sa = new SparseArray(N);
2484 readSparseArrayInternal(sa, N, loader);
2485 return sa;
2486 }
2487
2488 /**
2489 * Read and return a new SparseBooleanArray object from the parcel at the current
2490 * dataPosition(). Returns null if the previously written list object was
2491 * null.
2492 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002493 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 public final SparseBooleanArray readSparseBooleanArray() {
2495 int N = readInt();
2496 if (N < 0) {
2497 return null;
2498 }
2499 SparseBooleanArray sa = new SparseBooleanArray(N);
2500 readSparseBooleanArrayInternal(sa, N);
2501 return sa;
2502 }
2503
2504 /**
Adam Lesinski4e862812016-11-21 16:02:24 -08002505 * Read and return a new SparseIntArray object from the parcel at the current
2506 * dataPosition(). Returns null if the previously written array object was null.
Adam Lesinski205656d2017-03-23 13:38:26 -07002507 * @hide
Adam Lesinski4e862812016-11-21 16:02:24 -08002508 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002509 @Nullable
Adam Lesinski4e862812016-11-21 16:02:24 -08002510 public final SparseIntArray readSparseIntArray() {
2511 int N = readInt();
2512 if (N < 0) {
2513 return null;
2514 }
2515 SparseIntArray sa = new SparseIntArray(N);
2516 readSparseIntArrayInternal(sa, N);
2517 return sa;
2518 }
2519
2520 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 * Read and return a new ArrayList containing a particular object type from
2522 * the parcel that was written with {@link #writeTypedList} at the
2523 * current dataPosition(). Returns null if the
2524 * previously written list object was null. The list <em>must</em> have
2525 * previously been written via {@link #writeTypedList} with the same object
2526 * type.
2527 *
2528 * @return A newly created ArrayList containing objects with the same data
2529 * as those that were previously written.
2530 *
2531 * @see #writeTypedList
2532 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002533 @Nullable
2534 public final <T> ArrayList<T> createTypedArrayList(@NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 int N = readInt();
2536 if (N < 0) {
2537 return null;
2538 }
2539 ArrayList<T> l = new ArrayList<T>(N);
2540 while (N > 0) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002541 l.add(readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 N--;
2543 }
2544 return l;
2545 }
2546
2547 /**
2548 * Read into the given List items containing a particular object type
2549 * that were written with {@link #writeTypedList} at the
2550 * current dataPosition(). The list <em>must</em> have
2551 * previously been written via {@link #writeTypedList} with the same object
2552 * type.
2553 *
2554 * @return A newly created ArrayList containing objects with the same data
2555 * as those that were previously written.
2556 *
2557 * @see #writeTypedList
2558 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002559 public final <T> void readTypedList(@NonNull List<T> list, @NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 int M = list.size();
2561 int N = readInt();
2562 int i = 0;
2563 for (; i < M && i < N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002564 list.set(i, readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 }
2566 for (; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002567 list.add(readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 }
2569 for (; i<M; i++) {
2570 list.remove(N);
2571 }
2572 }
2573
2574 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08002575 * Read into a new {@link SparseArray} items containing a particular object type
2576 * that were written with {@link #writeTypedSparseArray(SparseArray, int)} at the
2577 * current dataPosition(). The list <em>must</em> have previously been written
2578 * via {@link #writeTypedSparseArray(SparseArray, int)} with the same object type.
2579 *
2580 * @param creator The creator to use when for instantiation.
2581 *
2582 * @return A newly created {@link SparseArray} containing objects with the same data
2583 * as those that were previously written.
2584 *
2585 * @see #writeTypedSparseArray(SparseArray, int)
2586 */
2587 public final @Nullable <T extends Parcelable> SparseArray<T> createTypedSparseArray(
2588 @NonNull Parcelable.Creator<T> creator) {
2589 final int count = readInt();
2590 if (count < 0) {
2591 return null;
2592 }
2593 final SparseArray<T> array = new SparseArray<>(count);
2594 for (int i = 0; i < count; i++) {
2595 final int index = readInt();
2596 final T value = readTypedObject(creator);
2597 array.append(index, value);
2598 }
2599 return array;
2600 }
2601
2602 /**
2603 * Read into a new {@link ArrayMap} with string keys items containing a particular
2604 * object type that were written with {@link #writeTypedArrayMap(ArrayMap, int)} at the
2605 * current dataPosition(). The list <em>must</em> have previously been written
2606 * via {@link #writeTypedArrayMap(ArrayMap, int)} with the same object type.
2607 *
2608 * @param creator The creator to use when for instantiation.
2609 *
2610 * @return A newly created {@link ArrayMap} containing objects with the same data
2611 * as those that were previously written.
2612 *
2613 * @see #writeTypedArrayMap(ArrayMap, int)
2614 */
2615 public final @Nullable <T extends Parcelable> ArrayMap<String, T> createTypedArrayMap(
2616 @NonNull Parcelable.Creator<T> creator) {
2617 final int count = readInt();
2618 if (count < 0) {
2619 return null;
2620 }
2621 final ArrayMap<String, T> map = new ArrayMap<>(count);
2622 for (int i = 0; i < count; i++) {
2623 final String key = readString();
2624 final T value = readTypedObject(creator);
2625 map.append(key, value);
2626 }
2627 return map;
2628 }
2629
2630 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 * Read and return a new ArrayList containing String objects from
2632 * the parcel that was written with {@link #writeStringList} at the
2633 * current dataPosition(). Returns null if the
2634 * previously written list object was null.
2635 *
2636 * @return A newly created ArrayList containing strings with the same data
2637 * as those that were previously written.
2638 *
2639 * @see #writeStringList
2640 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002641 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 public final ArrayList<String> createStringArrayList() {
2643 int N = readInt();
2644 if (N < 0) {
2645 return null;
2646 }
2647 ArrayList<String> l = new ArrayList<String>(N);
2648 while (N > 0) {
2649 l.add(readString());
2650 N--;
2651 }
2652 return l;
2653 }
2654
2655 /**
2656 * Read and return a new ArrayList containing IBinder objects from
2657 * the parcel that was written with {@link #writeBinderList} at the
2658 * current dataPosition(). Returns null if the
2659 * previously written list object was null.
2660 *
2661 * @return A newly created ArrayList containing strings with the same data
2662 * as those that were previously written.
2663 *
2664 * @see #writeBinderList
2665 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002666 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 public final ArrayList<IBinder> createBinderArrayList() {
2668 int N = readInt();
2669 if (N < 0) {
2670 return null;
2671 }
2672 ArrayList<IBinder> l = new ArrayList<IBinder>(N);
2673 while (N > 0) {
2674 l.add(readStrongBinder());
2675 N--;
2676 }
2677 return l;
2678 }
2679
2680 /**
2681 * Read into the given List items String objects that were written with
2682 * {@link #writeStringList} at the current dataPosition().
2683 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 * @see #writeStringList
2685 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002686 public final void readStringList(@NonNull List<String> list) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 int M = list.size();
2688 int N = readInt();
2689 int i = 0;
2690 for (; i < M && i < N; i++) {
2691 list.set(i, readString());
2692 }
2693 for (; i<N; i++) {
2694 list.add(readString());
2695 }
2696 for (; i<M; i++) {
2697 list.remove(N);
2698 }
2699 }
2700
2701 /**
2702 * Read into the given List items IBinder objects that were written with
2703 * {@link #writeBinderList} at the current dataPosition().
2704 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 * @see #writeBinderList
2706 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002707 public final void readBinderList(@NonNull List<IBinder> list) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 int M = list.size();
2709 int N = readInt();
2710 int i = 0;
2711 for (; i < M && i < N; i++) {
2712 list.set(i, readStrongBinder());
2713 }
2714 for (; i<N; i++) {
2715 list.add(readStrongBinder());
2716 }
2717 for (; i<M; i++) {
2718 list.remove(N);
2719 }
2720 }
2721
2722 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00002723 * Read the list of {@code Parcelable} objects at the current data position into the
2724 * given {@code list}. The contents of the {@code list} are replaced. If the serialized
2725 * list was {@code null}, {@code list} is cleared.
2726 *
2727 * @see #writeParcelableList(List, int)
Narayan Kamathbea48712016-12-01 15:38:28 +00002728 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002729 @NonNull
2730 public final <T extends Parcelable> List<T> readParcelableList(@NonNull List<T> list,
2731 @Nullable ClassLoader cl) {
Narayan Kamathbea48712016-12-01 15:38:28 +00002732 final int N = readInt();
2733 if (N == -1) {
2734 list.clear();
Eugene Susla36e866b2017-02-23 18:24:39 -08002735 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002736 }
2737
2738 final int M = list.size();
2739 int i = 0;
2740 for (; i < M && i < N; i++) {
2741 list.set(i, (T) readParcelable(cl));
2742 }
2743 for (; i<N; i++) {
2744 list.add((T) readParcelable(cl));
2745 }
2746 for (; i<M; i++) {
2747 list.remove(N);
2748 }
Eugene Susla36e866b2017-02-23 18:24:39 -08002749 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002750 }
2751
2752 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 * Read and return a new array containing a particular object type from
2754 * the parcel at the current dataPosition(). Returns null if the
2755 * previously written array was null. The array <em>must</em> have
2756 * previously been written via {@link #writeTypedArray} with the same
2757 * object type.
2758 *
2759 * @return A newly created array containing objects with the same data
2760 * as those that were previously written.
2761 *
2762 * @see #writeTypedArray
2763 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002764 @Nullable
2765 public final <T> T[] createTypedArray(@NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 int N = readInt();
2767 if (N < 0) {
2768 return null;
2769 }
2770 T[] l = c.newArray(N);
2771 for (int i=0; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002772 l[i] = readTypedObject(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 }
2774 return l;
2775 }
2776
Jake Whartonb1f474c2018-05-30 15:11:13 -04002777 public final <T> void readTypedArray(@NonNull T[] val, @NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 int N = readInt();
2779 if (N == val.length) {
2780 for (int i=0; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002781 val[i] = readTypedObject(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 }
2783 } else {
2784 throw new RuntimeException("bad array lengths");
2785 }
2786 }
2787
2788 /**
2789 * @deprecated
2790 * @hide
2791 */
2792 @Deprecated
2793 public final <T> T[] readTypedArray(Parcelable.Creator<T> c) {
2794 return createTypedArray(c);
2795 }
2796
2797 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002798 * Read and return a typed Parcelable object from a parcel.
2799 * Returns null if the previous written object was null.
2800 * The object <em>must</em> have previous been written via
2801 * {@link #writeTypedObject} with the same object type.
2802 *
2803 * @return A newly created object of the type that was previously
2804 * written.
2805 *
2806 * @see #writeTypedObject
2807 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002808 @Nullable
2809 public final <T> T readTypedObject(@NonNull Parcelable.Creator<T> c) {
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002810 if (readInt() != 0) {
2811 return c.createFromParcel(this);
2812 } else {
2813 return null;
2814 }
2815 }
2816
2817 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 * Write a heterogeneous array of Parcelable objects into the Parcel.
2819 * Each object in the array is written along with its class name, so
2820 * that the correct class can later be instantiated. As a result, this
2821 * has significantly more overhead than {@link #writeTypedArray}, but will
2822 * correctly handle an array containing more than one type of object.
2823 *
2824 * @param value The array of objects to be written.
2825 * @param parcelableFlags Contextual flags as per
2826 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
2827 *
2828 * @see #writeTypedArray
2829 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002830 public final <T extends Parcelable> void writeParcelableArray(@Nullable T[] value,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 int parcelableFlags) {
2832 if (value != null) {
2833 int N = value.length;
2834 writeInt(N);
2835 for (int i=0; i<N; i++) {
2836 writeParcelable(value[i], parcelableFlags);
2837 }
2838 } else {
2839 writeInt(-1);
2840 }
2841 }
2842
2843 /**
2844 * Read a typed object from a parcel. The given class loader will be
2845 * used to load any enclosed Parcelables. If it is null, the default class
2846 * loader will be used.
2847 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002848 @Nullable
2849 public final Object readValue(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 int type = readInt();
2851
2852 switch (type) {
2853 case VAL_NULL:
2854 return null;
2855
2856 case VAL_STRING:
2857 return readString();
2858
2859 case VAL_INTEGER:
2860 return readInt();
2861
2862 case VAL_MAP:
2863 return readHashMap(loader);
2864
2865 case VAL_PARCELABLE:
2866 return readParcelable(loader);
2867
2868 case VAL_SHORT:
2869 return (short) readInt();
2870
2871 case VAL_LONG:
2872 return readLong();
2873
2874 case VAL_FLOAT:
2875 return readFloat();
2876
2877 case VAL_DOUBLE:
2878 return readDouble();
2879
2880 case VAL_BOOLEAN:
2881 return readInt() == 1;
2882
2883 case VAL_CHARSEQUENCE:
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002884 return readCharSequence();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885
2886 case VAL_LIST:
2887 return readArrayList(loader);
2888
2889 case VAL_BOOLEANARRAY:
Samuel Tana8036662015-11-23 14:36:00 -08002890 return createBooleanArray();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891
2892 case VAL_BYTEARRAY:
2893 return createByteArray();
2894
2895 case VAL_STRINGARRAY:
2896 return readStringArray();
2897
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002898 case VAL_CHARSEQUENCEARRAY:
2899 return readCharSequenceArray();
2900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 case VAL_IBINDER:
2902 return readStrongBinder();
2903
2904 case VAL_OBJECTARRAY:
2905 return readArray(loader);
2906
2907 case VAL_INTARRAY:
2908 return createIntArray();
2909
2910 case VAL_LONGARRAY:
2911 return createLongArray();
2912
2913 case VAL_BYTE:
2914 return readByte();
2915
2916 case VAL_SERIALIZABLE:
John Spurlock5002b8c2014-01-10 13:32:12 -05002917 return readSerializable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918
2919 case VAL_PARCELABLEARRAY:
2920 return readParcelableArray(loader);
2921
2922 case VAL_SPARSEARRAY:
2923 return readSparseArray(loader);
2924
2925 case VAL_SPARSEBOOLEANARRAY:
2926 return readSparseBooleanArray();
2927
2928 case VAL_BUNDLE:
2929 return readBundle(loader); // loading will be deferred
2930
Craig Mautner719e6b12014-04-04 20:29:41 -07002931 case VAL_PERSISTABLEBUNDLE:
2932 return readPersistableBundle(loader);
2933
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002934 case VAL_SIZE:
2935 return readSize();
2936
2937 case VAL_SIZEF:
2938 return readSizeF();
2939
Samuel Tana8036662015-11-23 14:36:00 -08002940 case VAL_DOUBLEARRAY:
2941 return createDoubleArray();
2942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943 default:
2944 int off = dataPosition() - 4;
2945 throw new RuntimeException(
2946 "Parcel " + this + ": Unmarshalling unknown type code " + type + " at offset " + off);
2947 }
2948 }
2949
2950 /**
2951 * Read and return a new Parcelable from the parcel. The given class loader
2952 * will be used to load any enclosed Parcelables. If it is null, the default
2953 * class loader will be used.
2954 * @param loader A ClassLoader from which to instantiate the Parcelable
2955 * object, or null for the default class loader.
2956 * @return Returns the newly created Parcelable, or null if a null
2957 * object has been written.
2958 * @throws BadParcelableException Throws BadParcelableException if there
2959 * was an error trying to instantiate the Parcelable.
2960 */
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 readParcelable(@Nullable ClassLoader loader) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002964 Parcelable.Creator<?> creator = readParcelableCreator(loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002965 if (creator == null) {
2966 return null;
2967 }
2968 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002969 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2970 (Parcelable.ClassLoaderCreator<?>) creator;
2971 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002972 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002973 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002974 }
2975
2976 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00002977 @UnsupportedAppUsage
Neil Fuller44e440c2015-04-20 14:39:00 +01002978 @SuppressWarnings("unchecked")
Jake Whartonb1f474c2018-05-30 15:11:13 -04002979 @Nullable
2980 public final <T extends Parcelable> T readCreator(@NonNull Parcelable.Creator<?> creator,
2981 @Nullable ClassLoader loader) {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002982 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002983 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2984 (Parcelable.ClassLoaderCreator<?>) creator;
2985 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002986 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002987 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002988 }
2989
2990 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00002991 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04002992 @Nullable
2993 public final Parcelable.Creator<?> readParcelableCreator(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002994 String name = readString();
2995 if (name == null) {
2996 return null;
2997 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002998 Parcelable.Creator<?> creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002999 synchronized (mCreators) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003000 HashMap<String,Parcelable.Creator<?>> map = mCreators.get(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 if (map == null) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003002 map = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 mCreators.put(loader, map);
3004 }
3005 creator = map.get(name);
3006 if (creator == null) {
3007 try {
Neil Fuller44e440c2015-04-20 14:39:00 +01003008 // If loader == null, explicitly emulate Class.forName(String) "caller
3009 // classloader" behavior.
3010 ClassLoader parcelableClassLoader =
3011 (loader == null ? getClass().getClassLoader() : loader);
3012 // Avoid initializing the Parcelable class until we know it implements
3013 // Parcelable and has the necessary CREATOR field. http://b/1171613.
3014 Class<?> parcelableClass = Class.forName(name, false /* initialize */,
3015 parcelableClassLoader);
3016 if (!Parcelable.class.isAssignableFrom(parcelableClass)) {
Dianne Hackbornced54392018-02-26 13:07:42 -08003017 throw new BadParcelableException("Parcelable protocol requires subclassing "
3018 + "from Parcelable on class " + name);
Neil Fuller44e440c2015-04-20 14:39:00 +01003019 }
3020 Field f = parcelableClass.getField("CREATOR");
3021 if ((f.getModifiers() & Modifier.STATIC) == 0) {
3022 throw new BadParcelableException("Parcelable protocol requires "
3023 + "the CREATOR object to be static on class " + name);
3024 }
3025 Class<?> creatorType = f.getType();
3026 if (!Parcelable.Creator.class.isAssignableFrom(creatorType)) {
3027 // Fail before calling Field.get(), not after, to avoid initializing
3028 // parcelableClass unnecessarily.
3029 throw new BadParcelableException("Parcelable protocol requires a "
3030 + "Parcelable.Creator object called "
3031 + "CREATOR on class " + name);
3032 }
3033 creator = (Parcelable.Creator<?>) f.get(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 }
3035 catch (IllegalAccessException e) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003036 Log.e(TAG, "Illegal access when unmarshalling: " + name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037 throw new BadParcelableException(
3038 "IllegalAccessException when unmarshalling: " + name);
3039 }
3040 catch (ClassNotFoundException e) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003041 Log.e(TAG, "Class not found when unmarshalling: " + name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 throw new BadParcelableException(
3043 "ClassNotFoundException when unmarshalling: " + name);
3044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003045 catch (NoSuchFieldException e) {
3046 throw new BadParcelableException("Parcelable protocol requires a "
Neil Fuller44e440c2015-04-20 14:39:00 +01003047 + "Parcelable.Creator object called "
3048 + "CREATOR on class " + name);
Irfan Sheriff97a72f62013-01-11 10:45:51 -08003049 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003050 if (creator == null) {
3051 throw new BadParcelableException("Parcelable protocol requires a "
Neil Fuller44e440c2015-04-20 14:39:00 +01003052 + "non-null Parcelable.Creator object called "
3053 + "CREATOR on class " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003054 }
3055
3056 map.put(name, creator);
3057 }
3058 }
3059
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08003060 return creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003061 }
3062
3063 /**
3064 * Read and return a new Parcelable array from the parcel.
3065 * The given class loader will be used to load any enclosed
3066 * Parcelables.
3067 * @return the Parcelable array, or null if the array is null
3068 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003069 @Nullable
3070 public final Parcelable[] readParcelableArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 int N = readInt();
3072 if (N < 0) {
3073 return null;
3074 }
3075 Parcelable[] p = new Parcelable[N];
3076 for (int i = 0; i < N; i++) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003077 p[i] = readParcelable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 }
3079 return p;
3080 }
3081
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003082 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003083 @Nullable
3084 public final <T extends Parcelable> T[] readParcelableArray(@Nullable ClassLoader loader,
3085 @NonNull Class<T> clazz) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003086 int N = readInt();
3087 if (N < 0) {
3088 return null;
3089 }
3090 T[] p = (T[]) Array.newInstance(clazz, N);
3091 for (int i = 0; i < N; i++) {
3092 p[i] = readParcelable(loader);
3093 }
3094 return p;
3095 }
3096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 /**
3098 * Read and return a new Serializable object from the parcel.
3099 * @return the Serializable object, or null if the Serializable name
3100 * wasn't found in the parcel.
3101 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003102 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 public final Serializable readSerializable() {
John Spurlock5002b8c2014-01-10 13:32:12 -05003104 return readSerializable(null);
3105 }
3106
Jake Whartonb1f474c2018-05-30 15:11:13 -04003107 @Nullable
3108 private final Serializable readSerializable(@Nullable final ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 String name = readString();
3110 if (name == null) {
3111 // For some reason we were unable to read the name of the Serializable (either there
3112 // is nothing left in the Parcel to read, or the next value wasn't a String), so
3113 // return null, which indicates that the name wasn't found in the parcel.
3114 return null;
3115 }
3116
3117 byte[] serializedData = createByteArray();
3118 ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
3119 try {
John Spurlock5002b8c2014-01-10 13:32:12 -05003120 ObjectInputStream ois = new ObjectInputStream(bais) {
3121 @Override
3122 protected Class<?> resolveClass(ObjectStreamClass osClass)
3123 throws IOException, ClassNotFoundException {
3124 // try the custom classloader if provided
3125 if (loader != null) {
3126 Class<?> c = Class.forName(osClass.getName(), false, loader);
3127 if (c != null) {
3128 return c;
3129 }
3130 }
3131 return super.resolveClass(osClass);
3132 }
3133 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003134 return (Serializable) ois.readObject();
3135 } catch (IOException ioe) {
3136 throw new RuntimeException("Parcelable encountered " +
3137 "IOException reading a Serializable object (name = " + name +
3138 ")", ioe);
3139 } catch (ClassNotFoundException cnfe) {
John Spurlock5002b8c2014-01-10 13:32:12 -05003140 throw new RuntimeException("Parcelable encountered " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 "ClassNotFoundException reading a Serializable object (name = "
3142 + name + ")", cnfe);
3143 }
3144 }
3145
3146 // Cache of previously looked up CREATOR.createFromParcel() methods for
3147 // particular classes. Keys are the names of the classes, values are
3148 // Method objects.
Neil Fuller44e440c2015-04-20 14:39:00 +01003149 private static final HashMap<ClassLoader,HashMap<String,Parcelable.Creator<?>>>
3150 mCreators = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151
Narayan Kamathb34a4612014-01-23 14:17:11 +00003152 /** @hide for internal use only. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003153 static protected final Parcel obtain(int obj) {
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003154 throw new UnsupportedOperationException();
3155 }
3156
3157 /** @hide */
3158 static protected final Parcel obtain(long obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003159 final Parcel[] pool = sHolderPool;
3160 synchronized (pool) {
3161 Parcel p;
3162 for (int i=0; i<POOL_SIZE; i++) {
3163 p = pool[i];
3164 if (p != null) {
3165 pool[i] = null;
3166 if (DEBUG_RECYCLE) {
3167 p.mStack = new RuntimeException();
3168 }
3169 p.init(obj);
3170 return p;
3171 }
3172 }
3173 }
3174 return new Parcel(obj);
3175 }
3176
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003177 private Parcel(long nativePtr) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 if (DEBUG_RECYCLE) {
3179 mStack = new RuntimeException();
3180 }
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003181 //Log.i(TAG, "Initializing obj=0x" + Integer.toHexString(obj), mStack);
Jeff Sharkey047238c2012-03-07 16:51:38 -08003182 init(nativePtr);
3183 }
3184
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003185 private void init(long nativePtr) {
Jeff Sharkey047238c2012-03-07 16:51:38 -08003186 if (nativePtr != 0) {
3187 mNativePtr = nativePtr;
3188 mOwnsNativeParcelObject = false;
3189 } else {
3190 mNativePtr = nativeCreate();
3191 mOwnsNativeParcelObject = true;
3192 }
3193 }
3194
3195 private void freeBuffer() {
3196 if (mOwnsNativeParcelObject) {
Adrian Roos04505652015-10-22 16:12:01 -07003197 updateNativeSize(nativeFreeBuffer(mNativePtr));
Jeff Sharkey047238c2012-03-07 16:51:38 -08003198 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -07003199 mReadWriteHelper = ReadWriteHelper.DEFAULT;
Jeff Sharkey047238c2012-03-07 16:51:38 -08003200 }
3201
3202 private void destroy() {
3203 if (mNativePtr != 0) {
3204 if (mOwnsNativeParcelObject) {
3205 nativeDestroy(mNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -07003206 updateNativeSize(0);
Jeff Sharkey047238c2012-03-07 16:51:38 -08003207 }
3208 mNativePtr = 0;
3209 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -07003210 mReadWriteHelper = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 }
3212
3213 @Override
3214 protected void finalize() throws Throwable {
3215 if (DEBUG_RECYCLE) {
3216 if (mStack != null) {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003217 Log.w(TAG, "Client did not call Parcel.recycle()", mStack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 }
3219 }
3220 destroy();
3221 }
3222
Jake Whartonb1f474c2018-05-30 15:11:13 -04003223 /* package */ void readMapInternal(@NonNull Map outVal, int N,
3224 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 while (N > 0) {
3226 Object key = readValue(loader);
3227 Object value = readValue(loader);
3228 outVal.put(key, value);
3229 N--;
3230 }
3231 }
3232
Jake Whartonb1f474c2018-05-30 15:11:13 -04003233 /* package */ void readArrayMapInternal(@NonNull ArrayMap outVal, int N,
3234 @Nullable ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003235 if (DEBUG_ARRAY_MAP) {
3236 RuntimeException here = new RuntimeException("here");
3237 here.fillInStackTrace();
3238 Log.d(TAG, "Reading " + N + " ArrayMap entries", here);
3239 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003240 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003241 while (N > 0) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003242 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003243 String key = readString();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003244 Object value = readValue(loader);
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003245 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " "
3246 + (dataPosition()-startPos) + " bytes: key=0x"
3247 + Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key);
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003248 outVal.append(key, value);
3249 N--;
3250 }
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003251 outVal.validate();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003252 }
3253
Jake Whartonb1f474c2018-05-30 15:11:13 -04003254 /* package */ void readArrayMapSafelyInternal(@NonNull ArrayMap outVal, int N,
3255 @Nullable ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003256 if (DEBUG_ARRAY_MAP) {
3257 RuntimeException here = new RuntimeException("here");
3258 here.fillInStackTrace();
3259 Log.d(TAG, "Reading safely " + N + " ArrayMap entries", here);
3260 }
3261 while (N > 0) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003262 String key = readString();
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003263 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read safe #" + (N-1) + ": key=0x"
3264 + (key != null ? key.hashCode() : 0) + " " + key);
3265 Object value = readValue(loader);
3266 outVal.put(key, value);
3267 N--;
3268 }
3269 }
3270
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003271 /**
3272 * @hide For testing only.
3273 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003274 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04003275 public void readArrayMap(@NonNull ArrayMap outVal, @Nullable ClassLoader loader) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003276 final int N = readInt();
3277 if (N < 0) {
3278 return;
3279 }
3280 readArrayMapInternal(outVal, N, loader);
3281 }
3282
Svet Ganovddb94882016-06-23 19:55:24 -07003283 /**
3284 * Reads an array set.
3285 *
3286 * @param loader The class loader to use.
3287 *
3288 * @hide
3289 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003290 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04003291 public @Nullable ArraySet<? extends Object> readArraySet(@Nullable ClassLoader loader) {
Svet Ganovddb94882016-06-23 19:55:24 -07003292 final int size = readInt();
3293 if (size < 0) {
3294 return null;
3295 }
3296 ArraySet<Object> result = new ArraySet<>(size);
3297 for (int i = 0; i < size; i++) {
3298 Object value = readValue(loader);
3299 result.append(value);
3300 }
3301 return result;
3302 }
3303
Jake Whartonb1f474c2018-05-30 15:11:13 -04003304 private void readListInternal(@NonNull List outVal, int N,
3305 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 while (N > 0) {
3307 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003308 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 outVal.add(value);
3310 N--;
3311 }
3312 }
3313
Jake Whartonb1f474c2018-05-30 15:11:13 -04003314 private void readArrayInternal(@NonNull Object[] outVal, int N,
3315 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 for (int i = 0; i < N; i++) {
3317 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003318 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 outVal[i] = value;
3320 }
3321 }
3322
Jake Whartonb1f474c2018-05-30 15:11:13 -04003323 private void readSparseArrayInternal(@NonNull SparseArray outVal, int N,
3324 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 while (N > 0) {
3326 int key = readInt();
3327 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003328 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003329 outVal.append(key, value);
3330 N--;
3331 }
3332 }
3333
3334
Jake Whartonb1f474c2018-05-30 15:11:13 -04003335 private void readSparseBooleanArrayInternal(@NonNull SparseBooleanArray outVal, int N) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 while (N > 0) {
3337 int key = readInt();
3338 boolean value = this.readByte() == 1;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003339 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 outVal.append(key, value);
3341 N--;
3342 }
3343 }
Dan Sandler5ce04302015-04-09 23:50:15 -04003344
Jake Whartonb1f474c2018-05-30 15:11:13 -04003345 private void readSparseIntArrayInternal(@NonNull SparseIntArray outVal, int N) {
Adam Lesinski4e862812016-11-21 16:02:24 -08003346 while (N > 0) {
3347 int key = readInt();
3348 int value = readInt();
3349 outVal.append(key, value);
3350 N--;
3351 }
3352 }
3353
Dan Sandler5ce04302015-04-09 23:50:15 -04003354 /**
3355 * @hide For testing
3356 */
3357 public long getBlobAshmemSize() {
3358 return nativeGetBlobAshmemSize(mNativePtr);
3359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360}