blob: de963c9927c278410410be0b09c44e5e626d75e0 [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;
Andrei Onea24ec3212019-03-15 17:35:05 +000021import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.text.TextUtils;
Dianne Hackbornb87655b2013-07-17 19:06:22 -070023import android.util.ArrayMap;
Svet Ganovddb94882016-06-23 19:55:24 -070024import android.util.ArraySet;
Fyodor Kupolov3b946f42017-11-27 10:40:46 -080025import android.util.ExceptionUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.util.Log;
Jeff Sharkey5ef33982014-09-04 18:13:39 -070027import android.util.Size;
28import android.util.SizeF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.util.SparseArray;
30import android.util.SparseBooleanArray;
Adam Lesinski4e862812016-11-21 16:02:24 -080031import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
Makoto Onukib148b6c2017-06-27 13:38:38 -070033import dalvik.annotation.optimization.CriticalNative;
Jeff Sharkey789a8fc2017-04-16 13:18:35 -060034import dalvik.annotation.optimization.FastNative;
35import dalvik.system.VMRuntime;
36
Pete Gillin60f55a252018-05-10 15:40:32 +010037import libcore.util.ArrayUtils;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -070038import libcore.util.SneakyThrow;
39
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import java.io.ByteArrayInputStream;
41import java.io.ByteArrayOutputStream;
42import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import java.io.IOException;
44import java.io.ObjectInputStream;
45import java.io.ObjectOutputStream;
John Spurlock5002b8c2014-01-10 13:32:12 -050046import java.io.ObjectStreamClass;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import java.io.Serializable;
Makoto Onuki440a1ea2016-07-20 14:21:18 -070048import java.lang.reflect.Array;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import java.lang.reflect.Field;
Neil Fuller44e440c2015-04-20 14:39:00 +010050import java.lang.reflect.Modifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import java.util.ArrayList;
52import java.util.HashMap;
53import java.util.List;
54import java.util.Map;
55import java.util.Set;
Adrian Roos04505652015-10-22 16:12:01 -070056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057/**
58 * Container for a message (data and object references) that can
59 * be sent through an IBinder. A Parcel can contain both flattened data
60 * that will be unflattened on the other side of the IPC (using the various
61 * methods here for writing specific types, or the general
62 * {@link Parcelable} interface), and references to live {@link IBinder}
63 * objects that will result in the other side receiving a proxy IBinder
64 * connected with the original IBinder in the Parcel.
65 *
66 * <p class="note">Parcel is <strong>not</strong> a general-purpose
67 * serialization mechanism. This class (and the corresponding
68 * {@link Parcelable} API for placing arbitrary objects into a Parcel) is
69 * designed as a high-performance IPC transport. As such, it is not
70 * appropriate to place any Parcel data in to persistent storage: changes
71 * in the underlying implementation of any of the data in the Parcel can
72 * render older data unreadable.</p>
Samuel Tana8036662015-11-23 14:36:00 -080073 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 * <p>The bulk of the Parcel API revolves around reading and writing data
75 * of various types. There are six major classes of such functions available.</p>
Samuel Tana8036662015-11-23 14:36:00 -080076 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 * <h3>Primitives</h3>
Samuel Tana8036662015-11-23 14:36:00 -080078 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 * <p>The most basic data functions are for writing and reading primitive
80 * data types: {@link #writeByte}, {@link #readByte}, {@link #writeDouble},
81 * {@link #readDouble}, {@link #writeFloat}, {@link #readFloat}, {@link #writeInt},
82 * {@link #readInt}, {@link #writeLong}, {@link #readLong},
83 * {@link #writeString}, {@link #readString}. Most other
84 * data operations are built on top of these. The given data is written and
85 * read using the endianess of the host CPU.</p>
Samuel Tana8036662015-11-23 14:36:00 -080086 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 * <h3>Primitive Arrays</h3>
Samuel Tana8036662015-11-23 14:36:00 -080088 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 * <p>There are a variety of methods for reading and writing raw arrays
90 * of primitive objects, which generally result in writing a 4-byte length
91 * followed by the primitive data items. The methods for reading can either
92 * read the data into an existing array, or create and return a new array.
93 * These available types are:</p>
Samuel Tana8036662015-11-23 14:36:00 -080094 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 * <ul>
96 * <li> {@link #writeBooleanArray(boolean[])},
97 * {@link #readBooleanArray(boolean[])}, {@link #createBooleanArray()}
98 * <li> {@link #writeByteArray(byte[])},
99 * {@link #writeByteArray(byte[], int, int)}, {@link #readByteArray(byte[])},
100 * {@link #createByteArray()}
101 * <li> {@link #writeCharArray(char[])}, {@link #readCharArray(char[])},
102 * {@link #createCharArray()}
103 * <li> {@link #writeDoubleArray(double[])}, {@link #readDoubleArray(double[])},
104 * {@link #createDoubleArray()}
105 * <li> {@link #writeFloatArray(float[])}, {@link #readFloatArray(float[])},
106 * {@link #createFloatArray()}
107 * <li> {@link #writeIntArray(int[])}, {@link #readIntArray(int[])},
108 * {@link #createIntArray()}
109 * <li> {@link #writeLongArray(long[])}, {@link #readLongArray(long[])},
110 * {@link #createLongArray()}
111 * <li> {@link #writeStringArray(String[])}, {@link #readStringArray(String[])},
112 * {@link #createStringArray()}.
113 * <li> {@link #writeSparseBooleanArray(SparseBooleanArray)},
114 * {@link #readSparseBooleanArray()}.
115 * </ul>
Samuel Tana8036662015-11-23 14:36:00 -0800116 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 * <h3>Parcelables</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800118 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 * <p>The {@link Parcelable} protocol provides an extremely efficient (but
120 * low-level) protocol for objects to write and read themselves from Parcels.
121 * You can use the direct methods {@link #writeParcelable(Parcelable, int)}
122 * and {@link #readParcelable(ClassLoader)} or
123 * {@link #writeParcelableArray} and
124 * {@link #readParcelableArray(ClassLoader)} to write or read. These
125 * methods write both the class type and its data to the Parcel, allowing
126 * that class to be reconstructed from the appropriate class loader when
127 * later reading.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800128 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 * <p>There are also some methods that provide a more efficient way to work
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -0700130 * with Parcelables: {@link #writeTypedObject}, {@link #writeTypedArray},
131 * {@link #writeTypedList}, {@link #readTypedObject},
132 * {@link #createTypedArray} and {@link #createTypedArrayList}. These methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 * do not write the class information of the original object: instead, the
134 * caller of the read function must know what type to expect and pass in the
135 * appropriate {@link Parcelable.Creator Parcelable.Creator} instead to
136 * properly construct the new object and read its data. (To more efficient
Bin Chenb6b12b52017-07-11 11:01:44 +0800137 * write and read a single Parcelable object that is not null, you can directly
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -0700138 * call {@link Parcelable#writeToParcel Parcelable.writeToParcel} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 * {@link Parcelable.Creator#createFromParcel Parcelable.Creator.createFromParcel}
140 * yourself.)</p>
Samuel Tana8036662015-11-23 14:36:00 -0800141 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 * <h3>Bundles</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800143 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 * <p>A special type-safe container, called {@link Bundle}, is available
145 * for key/value maps of heterogeneous values. This has many optimizations
146 * for improved performance when reading and writing data, and its type-safe
147 * API avoids difficult to debug type errors when finally marshalling the
148 * data contents into a Parcel. The methods to use are
149 * {@link #writeBundle(Bundle)}, {@link #readBundle()}, and
150 * {@link #readBundle(ClassLoader)}.
Samuel Tana8036662015-11-23 14:36:00 -0800151 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 * <h3>Active Objects</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800153 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 * <p>An unusual feature of Parcel is the ability to read and write active
155 * objects. For these objects the actual contents of the object is not
156 * written, rather a special token referencing the object is written. When
157 * reading the object back from the Parcel, you do not get a new instance of
158 * the object, but rather a handle that operates on the exact same object that
159 * was originally written. There are two forms of active objects available.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800160 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 * <p>{@link Binder} objects are a core facility of Android's general cross-process
162 * communication system. The {@link IBinder} interface describes an abstract
163 * protocol with a Binder object. Any such interface can be written in to
164 * a Parcel, and upon reading you will receive either the original object
165 * implementing that interface or a special proxy implementation
166 * that communicates calls back to the original object. The methods to use are
167 * {@link #writeStrongBinder(IBinder)},
168 * {@link #writeStrongInterface(IInterface)}, {@link #readStrongBinder()},
169 * {@link #writeBinderArray(IBinder[])}, {@link #readBinderArray(IBinder[])},
170 * {@link #createBinderArray()},
171 * {@link #writeBinderList(List)}, {@link #readBinderList(List)},
172 * {@link #createBinderArrayList()}.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800173 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 * <p>FileDescriptor objects, representing raw Linux file descriptor identifiers,
175 * can be written and {@link ParcelFileDescriptor} objects returned to operate
176 * on the original file descriptor. The returned file descriptor is a dup
177 * of the original file descriptor: the object and fd is different, but
178 * operating on the same underlying file stream, with the same position, etc.
179 * The methods to use are {@link #writeFileDescriptor(FileDescriptor)},
180 * {@link #readFileDescriptor()}.
Samuel Tana8036662015-11-23 14:36:00 -0800181 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 * <h3>Untyped Containers</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800183 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 * <p>A final class of methods are for writing and reading standard Java
185 * containers of arbitrary types. These all revolve around the
186 * {@link #writeValue(Object)} and {@link #readValue(ClassLoader)} methods
187 * which define the types of objects allowed. The container methods are
188 * {@link #writeArray(Object[])}, {@link #readArray(ClassLoader)},
189 * {@link #writeList(List)}, {@link #readList(List, ClassLoader)},
190 * {@link #readArrayList(ClassLoader)},
191 * {@link #writeMap(Map)}, {@link #readMap(Map, ClassLoader)},
192 * {@link #writeSparseArray(SparseArray)},
193 * {@link #readSparseArray(ClassLoader)}.
194 */
195public final class Parcel {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -0800196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 private static final boolean DEBUG_RECYCLE = false;
Dianne Hackborne784d1e2013-09-20 18:13:52 -0700198 private static final boolean DEBUG_ARRAY_MAP = false;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700199 private static final String TAG = "Parcel";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200
Andrei Onea24ec3212019-03-15 17:35:05 +0000201 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 @SuppressWarnings({"UnusedDeclaration"})
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000203 private long mNativePtr; // used by native code
Jeff Sharkey047238c2012-03-07 16:51:38 -0800204
205 /**
206 * Flag indicating if {@link #mNativePtr} was allocated by this object,
207 * indicating that we're responsible for its lifecycle.
208 */
209 private boolean mOwnsNativeParcelObject;
Adrian Roos04505652015-10-22 16:12:01 -0700210 private long mNativeSize;
Jeff Sharkey047238c2012-03-07 16:51:38 -0800211
Dianne Hackborn98305522017-05-05 17:53:53 -0700212 private ArrayMap<Class, Object> mClassCookies;
213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 private RuntimeException mStack;
215
Fyodor Kupolova81b8c02017-11-13 15:51:03 -0800216 /**
217 * Whether or not to parcel the stack trace of an exception. This has a performance
218 * impact, so should only be included in specific processes and only on debug builds.
219 */
220 private static boolean sParcelExceptionStackTrace;
221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 private static final int POOL_SIZE = 6;
223 private static final Parcel[] sOwnedPool = new Parcel[POOL_SIZE];
224 private static final Parcel[] sHolderPool = new Parcel[POOL_SIZE];
225
Robert Quattlebaum9cd7af32017-01-04 13:28:01 -0800226 // Keep in sync with frameworks/native/include/private/binder/ParcelValTypes.h.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 private static final int VAL_NULL = -1;
228 private static final int VAL_STRING = 0;
229 private static final int VAL_INTEGER = 1;
230 private static final int VAL_MAP = 2;
231 private static final int VAL_BUNDLE = 3;
232 private static final int VAL_PARCELABLE = 4;
233 private static final int VAL_SHORT = 5;
234 private static final int VAL_LONG = 6;
235 private static final int VAL_FLOAT = 7;
236 private static final int VAL_DOUBLE = 8;
237 private static final int VAL_BOOLEAN = 9;
238 private static final int VAL_CHARSEQUENCE = 10;
239 private static final int VAL_LIST = 11;
240 private static final int VAL_SPARSEARRAY = 12;
241 private static final int VAL_BYTEARRAY = 13;
242 private static final int VAL_STRINGARRAY = 14;
243 private static final int VAL_IBINDER = 15;
244 private static final int VAL_PARCELABLEARRAY = 16;
245 private static final int VAL_OBJECTARRAY = 17;
246 private static final int VAL_INTARRAY = 18;
247 private static final int VAL_LONGARRAY = 19;
248 private static final int VAL_BYTE = 20;
249 private static final int VAL_SERIALIZABLE = 21;
250 private static final int VAL_SPARSEBOOLEANARRAY = 22;
251 private static final int VAL_BOOLEANARRAY = 23;
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000252 private static final int VAL_CHARSEQUENCEARRAY = 24;
Craig Mautner719e6b12014-04-04 20:29:41 -0700253 private static final int VAL_PERSISTABLEBUNDLE = 25;
Jeff Sharkey5ef33982014-09-04 18:13:39 -0700254 private static final int VAL_SIZE = 26;
255 private static final int VAL_SIZEF = 27;
Samuel Tana8036662015-11-23 14:36:00 -0800256 private static final int VAL_DOUBLEARRAY = 28;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700258 // The initial int32 in a Binder call's reply Parcel header:
Christopher Wiley80fd1202015-11-22 17:12:37 -0800259 // Keep these in sync with libbinder's binder/Status.h.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 private static final int EX_SECURITY = -1;
261 private static final int EX_BAD_PARCELABLE = -2;
262 private static final int EX_ILLEGAL_ARGUMENT = -3;
263 private static final int EX_NULL_POINTER = -4;
264 private static final int EX_ILLEGAL_STATE = -5;
Dianne Hackborn7e714422013-09-13 17:32:57 -0700265 private static final int EX_NETWORK_MAIN_THREAD = -6;
Dianne Hackborn33d738a2014-09-12 14:23:58 -0700266 private static final int EX_UNSUPPORTED_OPERATION = -7;
Christopher Wiley80fd1202015-11-22 17:12:37 -0800267 private static final int EX_SERVICE_SPECIFIC = -8;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -0700268 private static final int EX_PARCELABLE = -9;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700269 private static final int EX_HAS_REPLY_HEADER = -128; // special; see below
Christopher Wiley80fd1202015-11-22 17:12:37 -0800270 // EX_TRANSACTION_FAILED is used exclusively in native code.
271 // see libbinder's binder/Status.h
272 private static final int EX_TRANSACTION_FAILED = -129;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700273
Makoto Onukib148b6c2017-06-27 13:38:38 -0700274 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000275 private static native int nativeDataSize(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700276 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000277 private static native int nativeDataAvail(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700278 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000279 private static native int nativeDataPosition(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700280 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000281 private static native int nativeDataCapacity(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700282 @FastNative
Adrian Roos04505652015-10-22 16:12:01 -0700283 private static native long nativeSetDataSize(long nativePtr, int size);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700284 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000285 private static native void nativeSetDataPosition(long nativePtr, int pos);
John Reck71207b52016-09-28 13:28:09 -0700286 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000287 private static native void nativeSetDataCapacity(long nativePtr, int size);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800288
Makoto Onukib148b6c2017-06-27 13:38:38 -0700289 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000290 private static native boolean nativePushAllowFds(long nativePtr, boolean allowFds);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700291 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000292 private static native void nativeRestoreAllowFds(long nativePtr, boolean lastValue);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800293
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000294 private static native void nativeWriteByteArray(long nativePtr, byte[] b, int offset, int len);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700295 private static native void nativeWriteBlob(long nativePtr, byte[] b, int offset, int len);
John Reck71207b52016-09-28 13:28:09 -0700296 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000297 private static native void nativeWriteInt(long nativePtr, int val);
John Reck71207b52016-09-28 13:28:09 -0700298 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000299 private static native void nativeWriteLong(long nativePtr, long val);
John Reck71207b52016-09-28 13:28:09 -0700300 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000301 private static native void nativeWriteFloat(long nativePtr, float val);
John Reck71207b52016-09-28 13:28:09 -0700302 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000303 private static native void nativeWriteDouble(long nativePtr, double val);
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700304 static native void nativeWriteString(long nativePtr, String val);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000305 private static native void nativeWriteStrongBinder(long nativePtr, IBinder val);
Adrian Roos04505652015-10-22 16:12:01 -0700306 private static native long nativeWriteFileDescriptor(long nativePtr, FileDescriptor val);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800307
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000308 private static native byte[] nativeCreateByteArray(long nativePtr);
Jocelyn Dang46100442017-05-05 15:40:49 -0700309 private static native boolean nativeReadByteArray(long nativePtr, byte[] dest, int destLen);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700310 private static native byte[] nativeReadBlob(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700311 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000312 private static native int nativeReadInt(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700313 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000314 private static native long nativeReadLong(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700315 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000316 private static native float nativeReadFloat(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700317 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000318 private static native double nativeReadDouble(long nativePtr);
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700319 static native String nativeReadString(long nativePtr);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000320 private static native IBinder nativeReadStrongBinder(long nativePtr);
321 private static native FileDescriptor nativeReadFileDescriptor(long nativePtr);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800322
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000323 private static native long nativeCreate();
Adrian Roos04505652015-10-22 16:12:01 -0700324 private static native long nativeFreeBuffer(long nativePtr);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000325 private static native void nativeDestroy(long nativePtr);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800326
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000327 private static native byte[] nativeMarshall(long nativePtr);
Adrian Roos04505652015-10-22 16:12:01 -0700328 private static native long nativeUnmarshall(
John Spurlocke0852362015-02-04 15:47:40 -0500329 long nativePtr, byte[] data, int offset, int length);
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700330 private static native int nativeCompareData(long thisNativePtr, long otherNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -0700331 private static native long nativeAppendFrom(
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000332 long thisNativePtr, long otherNativePtr, int offset, int length);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700333 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000334 private static native boolean nativeHasFileDescriptors(long nativePtr);
335 private static native void nativeWriteInterfaceToken(long nativePtr, String interfaceName);
336 private static native void nativeEnforceInterface(long nativePtr, String interfaceName);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800337
Olivier Gaillardbab444a2019-01-30 17:11:40 +0000338 @CriticalNative
339 private static native boolean nativeReplaceCallingWorkSourceUid(
340 long nativePtr, int workSourceUid);
341 @CriticalNative
342 private static native int nativeReadCallingWorkSourceUid(long nativePtr);
343
Fyodor Kupolova81b8c02017-11-13 15:51:03 -0800344 /** Last time exception with a stack trace was written */
345 private static volatile long sLastWriteExceptionStackTrace;
346 /** Used for throttling of writing stack trace, which is costly */
347 private static final int WRITE_EXCEPTION_STACK_TRACE_THRESHOLD_MS = 1000;
348
Makoto Onukib148b6c2017-06-27 13:38:38 -0700349 @CriticalNative
Dan Sandleraa861662015-04-21 10:24:32 -0400350 private static native long nativeGetBlobAshmemSize(long nativePtr);
Dan Sandler5ce04302015-04-09 23:50:15 -0400351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 public final static Parcelable.Creator<String> STRING_CREATOR
353 = new Parcelable.Creator<String>() {
354 public String createFromParcel(Parcel source) {
355 return source.readString();
356 }
357 public String[] newArray(int size) {
358 return new String[size];
359 }
360 };
361
362 /**
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700363 * @hide
364 */
365 public static class ReadWriteHelper {
366 public static final ReadWriteHelper DEFAULT = new ReadWriteHelper();
367
368 /**
369 * Called when writing a string to a parcel. Subclasses wanting to write a string
370 * must use {@link #writeStringNoHelper(String)} to avoid
371 * infinity recursive calls.
372 */
373 public void writeString(Parcel p, String s) {
374 nativeWriteString(p.mNativePtr, s);
375 }
376
377 /**
378 * Called when reading a string to a parcel. Subclasses wanting to read a string
379 * must use {@link #readStringNoHelper()} to avoid
380 * infinity recursive calls.
381 */
382 public String readString(Parcel p) {
383 return nativeReadString(p.mNativePtr);
384 }
385 }
386
387 private ReadWriteHelper mReadWriteHelper = ReadWriteHelper.DEFAULT;
388
389 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 * Retrieve a new Parcel object from the pool.
391 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400392 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 public static Parcel obtain() {
394 final Parcel[] pool = sOwnedPool;
395 synchronized (pool) {
396 Parcel p;
397 for (int i=0; i<POOL_SIZE; i++) {
398 p = pool[i];
399 if (p != null) {
400 pool[i] = null;
401 if (DEBUG_RECYCLE) {
402 p.mStack = new RuntimeException();
403 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700404 p.mReadWriteHelper = ReadWriteHelper.DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 return p;
406 }
407 }
408 }
409 return new Parcel(0);
410 }
411
412 /**
413 * Put a Parcel object back into the pool. You must not touch
414 * the object after this call.
415 */
416 public final void recycle() {
417 if (DEBUG_RECYCLE) mStack = null;
418 freeBuffer();
Jeff Sharkey047238c2012-03-07 16:51:38 -0800419
420 final Parcel[] pool;
421 if (mOwnsNativeParcelObject) {
422 pool = sOwnedPool;
423 } else {
424 mNativePtr = 0;
425 pool = sHolderPool;
426 }
427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 synchronized (pool) {
429 for (int i=0; i<POOL_SIZE; i++) {
430 if (pool[i] == null) {
431 pool[i] = this;
432 return;
433 }
434 }
435 }
436 }
437
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700438 /**
439 * Set a {@link ReadWriteHelper}, which can be used to avoid having duplicate strings, for
440 * example.
441 *
442 * @hide
443 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400444 public void setReadWriteHelper(@Nullable ReadWriteHelper helper) {
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700445 mReadWriteHelper = helper != null ? helper : ReadWriteHelper.DEFAULT;
446 }
447
448 /**
449 * @return whether this parcel has a {@link ReadWriteHelper}.
450 *
451 * @hide
452 */
453 public boolean hasReadWriteHelper() {
454 return (mReadWriteHelper != null) && (mReadWriteHelper != ReadWriteHelper.DEFAULT);
455 }
456
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800457 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000458 @UnsupportedAppUsage
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800459 public static native long getGlobalAllocSize();
460
461 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000462 @UnsupportedAppUsage
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800463 public static native long getGlobalAllocCount();
464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 /**
466 * Returns the total amount of data contained in the parcel.
467 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800468 public final int dataSize() {
469 return nativeDataSize(mNativePtr);
470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471
472 /**
473 * Returns the amount of data remaining to be read from the
474 * parcel. That is, {@link #dataSize}-{@link #dataPosition}.
475 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800476 public final int dataAvail() {
477 return nativeDataAvail(mNativePtr);
478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479
480 /**
481 * Returns the current position in the parcel data. Never
482 * more than {@link #dataSize}.
483 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800484 public final int dataPosition() {
485 return nativeDataPosition(mNativePtr);
486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487
488 /**
489 * Returns the total amount of space in the parcel. This is always
490 * >= {@link #dataSize}. The difference between it and dataSize() is the
491 * amount of room left until the parcel needs to re-allocate its
492 * data buffer.
493 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800494 public final int dataCapacity() {
495 return nativeDataCapacity(mNativePtr);
496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497
498 /**
499 * Change the amount of data in the parcel. Can be either smaller or
500 * larger than the current size. If larger than the current capacity,
501 * more memory will be allocated.
502 *
503 * @param size The new number of bytes in the Parcel.
504 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800505 public final void setDataSize(int size) {
Michael Wachenschwanz138bebf2017-05-18 22:09:18 +0000506 updateNativeSize(nativeSetDataSize(mNativePtr, size));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508
509 /**
510 * Move the current read/write position in the parcel.
511 * @param pos New offset in the parcel; must be between 0 and
512 * {@link #dataSize}.
513 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800514 public final void setDataPosition(int pos) {
515 nativeSetDataPosition(mNativePtr, pos);
516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517
518 /**
519 * Change the capacity (current available space) of the parcel.
520 *
521 * @param size The new capacity of the parcel, in bytes. Can not be
522 * less than {@link #dataSize} -- that is, you can not drop existing data
523 * with this method.
524 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800525 public final void setDataCapacity(int size) {
526 nativeSetDataCapacity(mNativePtr, size);
527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400529 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800530 public final boolean pushAllowFds(boolean allowFds) {
531 return nativePushAllowFds(mNativePtr, allowFds);
532 }
Dianne Hackbornc04db7e2011-10-03 21:09:35 -0700533
534 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800535 public final void restoreAllowFds(boolean lastValue) {
536 nativeRestoreAllowFds(mNativePtr, lastValue);
537 }
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 /**
540 * Returns the raw bytes of the parcel.
541 *
542 * <p class="note">The data you retrieve here <strong>must not</strong>
543 * be placed in any kind of persistent storage (on local disk, across
544 * a network, etc). For that, you should use standard serialization
545 * or another kind of general serialization mechanism. The Parcel
546 * marshalled representation is highly optimized for local IPC, and as
547 * such does not attempt to maintain compatibility with data created
548 * in different versions of the platform.
549 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800550 public final byte[] marshall() {
551 return nativeMarshall(mNativePtr);
552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553
554 /**
555 * Set the bytes in data to be the raw bytes of this Parcel.
556 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400557 public final void unmarshall(@NonNull byte[] data, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700558 updateNativeSize(nativeUnmarshall(mNativePtr, data, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800559 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560
Jeff Sharkey047238c2012-03-07 16:51:38 -0800561 public final void appendFrom(Parcel parcel, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700562 updateNativeSize(nativeAppendFrom(mNativePtr, parcel.mNativePtr, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800563 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700565 /** @hide */
566 public final int compareData(Parcel other) {
567 return nativeCompareData(mNativePtr, other.mNativePtr);
568 }
569
Dianne Hackborn98305522017-05-05 17:53:53 -0700570 /** @hide */
571 public final void setClassCookie(Class clz, Object cookie) {
572 if (mClassCookies == null) {
573 mClassCookies = new ArrayMap<>();
574 }
575 mClassCookies.put(clz, cookie);
576 }
577
578 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400579 @Nullable
Dianne Hackborn98305522017-05-05 17:53:53 -0700580 public final Object getClassCookie(Class clz) {
581 return mClassCookies != null ? mClassCookies.get(clz) : null;
582 }
583
584 /** @hide */
585 public final void adoptClassCookies(Parcel from) {
586 mClassCookies = from.mClassCookies;
587 }
588
Adrian Roosfb921842017-10-26 14:49:56 +0200589 /** @hide */
590 public Map<Class, Object> copyClassCookies() {
591 return new ArrayMap<>(mClassCookies);
592 }
593
594 /** @hide */
595 public void putClassCookies(Map<Class, Object> cookies) {
596 if (cookies == null) {
597 return;
598 }
599 if (mClassCookies == null) {
600 mClassCookies = new ArrayMap<>();
601 }
602 mClassCookies.putAll(cookies);
603 }
604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 /**
606 * Report whether the parcel contains any marshalled file descriptors.
607 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800608 public final boolean hasFileDescriptors() {
609 return nativeHasFileDescriptors(mNativePtr);
610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611
612 /**
613 * Store or read an IBinder interface token in the parcel at the current
614 * {@link #dataPosition}. This is used to validate that the marshalled
615 * transaction is intended for the target interface.
616 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800617 public final void writeInterfaceToken(String interfaceName) {
618 nativeWriteInterfaceToken(mNativePtr, interfaceName);
619 }
620
621 public final void enforceInterface(String interfaceName) {
622 nativeEnforceInterface(mNativePtr, interfaceName);
623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624
625 /**
Olivier Gaillardbab444a2019-01-30 17:11:40 +0000626 * Writes the work source uid to the request headers.
627 *
628 * <p>It requires the headers to have been written/read already to replace the work source.
629 *
630 * @return true if the request headers have been updated.
631 *
632 * @hide
633 */
634 public boolean replaceCallingWorkSourceUid(int workSourceUid) {
635 return nativeReplaceCallingWorkSourceUid(mNativePtr, workSourceUid);
636 }
637
638 /**
639 * Reads the work source uid from the request headers.
640 *
641 * <p>Unlike other read methods, this method does not read the parcel at the current
642 * {@link #dataPosition}. It will set the {@link #dataPosition} before the read and restore the
643 * position after reading the request header.
644 *
645 * @return the work source uid or {@link Binder#UNSET_WORKSOURCE} if headers have not been
646 * written/parsed yet.
647 *
648 * @hide
649 */
650 public int readCallingWorkSourceUid() {
651 return nativeReadCallingWorkSourceUid(mNativePtr);
652 }
653
654 /**
Elliott Hughesa28b83e2011-02-28 14:26:13 -0800655 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 * growing {@link #dataCapacity} if needed.
657 * @param b Bytes to place into the parcel.
658 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400659 public final void writeByteArray(@Nullable byte[] b) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 writeByteArray(b, 0, (b != null) ? b.length : 0);
661 }
662
663 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900664 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 * growing {@link #dataCapacity} if needed.
666 * @param b Bytes to place into the parcel.
667 * @param offset Index of first byte to be written.
668 * @param len Number of bytes to write.
669 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400670 public final void writeByteArray(@Nullable byte[] b, int offset, int len) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 if (b == null) {
672 writeInt(-1);
673 return;
674 }
Pete Gillin60f55a252018-05-10 15:40:32 +0100675 ArrayUtils.throwsIfOutOfBounds(b.length, offset, len);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800676 nativeWriteByteArray(mNativePtr, b, offset, len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 }
678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700680 * Write a blob of data into the parcel at the current {@link #dataPosition},
681 * growing {@link #dataCapacity} if needed.
682 * @param b Bytes to place into the parcel.
683 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -0700684 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700685 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000686 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -0400687 public final void writeBlob(@Nullable byte[] b) {
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500688 writeBlob(b, 0, (b != null) ? b.length : 0);
689 }
690
691 /**
692 * Write a blob of data into the parcel at the current {@link #dataPosition},
693 * growing {@link #dataCapacity} if needed.
694 * @param b Bytes to place into the parcel.
695 * @param offset Index of first byte to be written.
696 * @param len Number of bytes to write.
697 * {@hide}
698 * {@SystemApi}
699 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400700 public final void writeBlob(@Nullable byte[] b, int offset, int len) {
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500701 if (b == null) {
702 writeInt(-1);
703 return;
704 }
Pete Gillin60f55a252018-05-10 15:40:32 +0100705 ArrayUtils.throwsIfOutOfBounds(b.length, offset, len);
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500706 nativeWriteBlob(mNativePtr, b, offset, len);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700707 }
708
709 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 * Write an integer value into the parcel at the current dataPosition(),
711 * growing dataCapacity() if needed.
712 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800713 public final void writeInt(int val) {
714 nativeWriteInt(mNativePtr, val);
715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716
717 /**
718 * Write a long integer value into the parcel at the current dataPosition(),
719 * growing dataCapacity() if needed.
720 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800721 public final void writeLong(long val) {
722 nativeWriteLong(mNativePtr, val);
723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724
725 /**
726 * Write a floating point value into the parcel at the current
727 * dataPosition(), growing dataCapacity() if needed.
728 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800729 public final void writeFloat(float val) {
730 nativeWriteFloat(mNativePtr, val);
731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732
733 /**
734 * Write a double precision floating point value into the parcel at the
735 * current dataPosition(), growing dataCapacity() if needed.
736 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800737 public final void writeDouble(double val) {
738 nativeWriteDouble(mNativePtr, val);
739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740
741 /**
742 * Write a string value into the parcel at the current dataPosition(),
743 * growing dataCapacity() if needed.
744 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400745 public final void writeString(@Nullable String val) {
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700746 mReadWriteHelper.writeString(this, val);
747 }
748
749 /**
750 * Write a string without going though a {@link ReadWriteHelper}. Subclasses of
751 * {@link ReadWriteHelper} must use this method instead of {@link #writeString} to avoid
752 * infinity recursive calls.
753 *
754 * @hide
755 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400756 public void writeStringNoHelper(@Nullable String val) {
Jeff Sharkey047238c2012-03-07 16:51:38 -0800757 nativeWriteString(mNativePtr, val);
758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759
Jake Wharton2ffa4312018-04-23 17:47:14 -0400760 /**
761 * Write a boolean value into the parcel at the current dataPosition(),
762 * growing dataCapacity() if needed.
763 *
764 * <p>Note: This method currently delegates to writeInt with a value of 1 or 0
765 * for true or false, respectively, but may change in the future.
766 */
Eugene Susla36e866b2017-02-23 18:24:39 -0800767 public final void writeBoolean(boolean val) {
768 writeInt(val ? 1 : 0);
769 }
770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000772 * Write a CharSequence value into the parcel at the current dataPosition(),
773 * growing dataCapacity() if needed.
774 * @hide
775 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000776 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -0400777 public final void writeCharSequence(@Nullable CharSequence val) {
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000778 TextUtils.writeToParcel(val, this, 0);
779 }
780
781 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 * Write an object into the parcel at the current dataPosition(),
783 * growing dataCapacity() if needed.
784 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800785 public final void writeStrongBinder(IBinder val) {
786 nativeWriteStrongBinder(mNativePtr, val);
787 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788
789 /**
790 * Write an object into the parcel at the current dataPosition(),
791 * growing dataCapacity() if needed.
792 */
793 public final void writeStrongInterface(IInterface val) {
794 writeStrongBinder(val == null ? null : val.asBinder());
795 }
796
797 /**
798 * Write a FileDescriptor into the parcel at the current dataPosition(),
799 * growing dataCapacity() if needed.
Dan Egnorb3e4ef32010-07-20 09:03:35 -0700800 *
801 * <p class="caution">The file descriptor will not be closed, which may
802 * result in file descriptor leaks when objects are returned from Binder
803 * calls. Use {@link ParcelFileDescriptor#writeToParcel} instead, which
804 * accepts contextual flags and will close the original file descriptor
805 * if {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} is set.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400807 public final void writeFileDescriptor(@NonNull FileDescriptor val) {
Adrian Roos04505652015-10-22 16:12:01 -0700808 updateNativeSize(nativeWriteFileDescriptor(mNativePtr, val));
809 }
810
811 private void updateNativeSize(long newNativeSize) {
812 if (mOwnsNativeParcelObject) {
813 if (newNativeSize > Integer.MAX_VALUE) {
814 newNativeSize = Integer.MAX_VALUE;
815 }
816 if (newNativeSize != mNativeSize) {
817 int delta = (int) (newNativeSize - mNativeSize);
818 if (delta > 0) {
819 VMRuntime.getRuntime().registerNativeAllocation(delta);
820 } else {
821 VMRuntime.getRuntime().registerNativeFree(-delta);
822 }
823 mNativeSize = newNativeSize;
824 }
825 }
Jeff Sharkey047238c2012-03-07 16:51:38 -0800826 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827
828 /**
Casey Dahlin2f974b22015-11-05 12:19:13 -0800829 * {@hide}
830 * This will be the new name for writeFileDescriptor, for consistency.
831 **/
Jake Whartonb1f474c2018-05-30 15:11:13 -0400832 public final void writeRawFileDescriptor(@NonNull FileDescriptor val) {
Casey Dahlin2f974b22015-11-05 12:19:13 -0800833 nativeWriteFileDescriptor(mNativePtr, val);
834 }
835
836 /**
837 * {@hide}
838 * Write an array of FileDescriptor objects into the Parcel.
839 *
840 * @param value The array of objects to be written.
841 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400842 public final void writeRawFileDescriptorArray(@Nullable FileDescriptor[] value) {
Casey Dahlin2f974b22015-11-05 12:19:13 -0800843 if (value != null) {
844 int N = value.length;
845 writeInt(N);
846 for (int i=0; i<N; i++) {
847 writeRawFileDescriptor(value[i]);
848 }
849 } else {
850 writeInt(-1);
851 }
852 }
853
854 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900855 * Write a byte value into the parcel at the current dataPosition(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 * growing dataCapacity() if needed.
Jake Wharton2ffa4312018-04-23 17:47:14 -0400857 *
858 * <p>Note: This method currently delegates to writeInt but may change in
859 * the future.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 */
861 public final void writeByte(byte val) {
862 writeInt(val);
863 }
864
865 /**
866 * Please use {@link #writeBundle} instead. Flattens a Map into the parcel
867 * at the current dataPosition(),
868 * growing dataCapacity() if needed. The Map keys must be String objects.
869 * The Map values are written using {@link #writeValue} and must follow
870 * the specification there.
Samuel Tana8036662015-11-23 14:36:00 -0800871 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 * <p>It is strongly recommended to use {@link #writeBundle} instead of
873 * this method, since the Bundle class provides a type-safe API that
874 * allows you to avoid mysterious type errors at the point of marshalling.
875 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400876 public final void writeMap(@Nullable Map val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700877 writeMapInternal((Map<String, Object>) val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 }
879
880 /**
881 * Flatten a Map into the parcel at the current dataPosition(),
882 * growing dataCapacity() if needed. The Map keys must be String objects.
883 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400884 /* package */ void writeMapInternal(@Nullable Map<String,Object> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 if (val == null) {
886 writeInt(-1);
887 return;
888 }
889 Set<Map.Entry<String,Object>> entries = val.entrySet();
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700890 int size = entries.size();
891 writeInt(size);
892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 for (Map.Entry<String,Object> e : entries) {
894 writeValue(e.getKey());
895 writeValue(e.getValue());
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700896 size--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 }
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700898
899 if (size != 0) {
900 throw new BadParcelableException("Map size does not match number of entries!");
901 }
902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 }
904
905 /**
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700906 * Flatten an ArrayMap into the parcel at the current dataPosition(),
907 * growing dataCapacity() if needed. The Map keys must be String objects.
908 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400909 /* package */ void writeArrayMapInternal(@Nullable ArrayMap<String, Object> val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700910 if (val == null) {
911 writeInt(-1);
912 return;
913 }
Samuel Tan3cefe6a2015-12-14 13:29:17 -0800914 // Keep the format of this Parcel in sync with writeToParcelInner() in
915 // frameworks/native/libs/binder/PersistableBundle.cpp.
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700916 final int N = val.size();
917 writeInt(N);
Dianne Hackborne784d1e2013-09-20 18:13:52 -0700918 if (DEBUG_ARRAY_MAP) {
919 RuntimeException here = new RuntimeException("here");
920 here.fillInStackTrace();
921 Log.d(TAG, "Writing " + N + " ArrayMap entries", here);
922 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700923 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700924 for (int i=0; i<N; i++) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700925 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700926 writeString(val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700927 writeValue(val.valueAt(i));
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700928 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Write #" + i + " "
929 + (dataPosition()-startPos) + " bytes: key=0x"
930 + Integer.toHexString(val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0)
931 + " " + val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700932 }
933 }
934
935 /**
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700936 * @hide For testing only.
937 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000938 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -0400939 public void writeArrayMap(@Nullable ArrayMap<String, Object> val) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700940 writeArrayMapInternal(val);
941 }
942
943 /**
Svet Ganov8455ba22019-01-02 13:05:56 -0800944 * Flatten an {@link ArrayMap} with string keys containing a particular object
945 * type into the parcel at the current dataPosition() and growing dataCapacity()
946 * if needed. The type of the objects in the array must be one that implements
947 * Parcelable. Only the raw data of the objects is written and not their type,
948 * so you must use the corresponding {@link #createTypedArrayMap(Parcelable.Creator)}
949 *
950 * @param val The map of objects to be written.
951 * @param parcelableFlags The parcelable flags to use.
952 *
953 * @see #createTypedArrayMap(Parcelable.Creator)
954 * @see Parcelable
955 */
956 public <T extends Parcelable> void writeTypedArrayMap(@Nullable ArrayMap<String, T> val,
957 int parcelableFlags) {
958 if (val == null) {
959 writeInt(-1);
960 return;
961 }
962 final int count = val.size();
963 writeInt(count);
964 for (int i = 0; i < count; i++) {
965 writeString(val.keyAt(i));
966 writeTypedObject(val.valueAt(i), parcelableFlags);
967 }
968 }
969
970 /**
Svet Ganovddb94882016-06-23 19:55:24 -0700971 * Write an array set to the parcel.
972 *
973 * @param val The array set to write.
974 *
975 * @hide
976 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000977 @UnsupportedAppUsage
Svet Ganovddb94882016-06-23 19:55:24 -0700978 public void writeArraySet(@Nullable ArraySet<? extends Object> val) {
979 final int size = (val != null) ? val.size() : -1;
980 writeInt(size);
981 for (int i = 0; i < size; i++) {
982 writeValue(val.valueAt(i));
983 }
984 }
985
986 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 * Flatten a Bundle into the parcel at the current dataPosition(),
988 * growing dataCapacity() if needed.
989 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400990 public final void writeBundle(@Nullable Bundle val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 if (val == null) {
992 writeInt(-1);
993 return;
994 }
995
Dianne Hackborn6aff9052009-05-22 13:20:23 -0700996 val.writeToParcel(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 }
998
999 /**
Craig Mautner719e6b12014-04-04 20:29:41 -07001000 * Flatten a PersistableBundle into the parcel at the current dataPosition(),
1001 * growing dataCapacity() if needed.
1002 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001003 public final void writePersistableBundle(@Nullable PersistableBundle val) {
Craig Mautner719e6b12014-04-04 20:29:41 -07001004 if (val == null) {
1005 writeInt(-1);
1006 return;
1007 }
1008
1009 val.writeToParcel(this, 0);
1010 }
1011
1012 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001013 * Flatten a Size into the parcel at the current dataPosition(),
1014 * growing dataCapacity() if needed.
1015 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001016 public final void writeSize(@NonNull Size val) {
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001017 writeInt(val.getWidth());
1018 writeInt(val.getHeight());
1019 }
1020
1021 /**
1022 * Flatten a SizeF into the parcel at the current dataPosition(),
1023 * growing dataCapacity() if needed.
1024 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001025 public final void writeSizeF(@NonNull SizeF val) {
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001026 writeFloat(val.getWidth());
1027 writeFloat(val.getHeight());
1028 }
1029
1030 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 * Flatten a List into the parcel at the current dataPosition(), growing
1032 * dataCapacity() if needed. The List values are written using
1033 * {@link #writeValue} and must follow the specification there.
1034 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001035 public final void writeList(@Nullable List val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 if (val == null) {
1037 writeInt(-1);
1038 return;
1039 }
1040 int N = val.size();
1041 int i=0;
1042 writeInt(N);
1043 while (i < N) {
1044 writeValue(val.get(i));
1045 i++;
1046 }
1047 }
1048
1049 /**
1050 * Flatten an Object array into the parcel at the current dataPosition(),
1051 * growing dataCapacity() if needed. The array values are written using
1052 * {@link #writeValue} and must follow the specification there.
1053 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001054 public final void writeArray(@Nullable Object[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 if (val == null) {
1056 writeInt(-1);
1057 return;
1058 }
1059 int N = val.length;
1060 int i=0;
1061 writeInt(N);
1062 while (i < N) {
1063 writeValue(val[i]);
1064 i++;
1065 }
1066 }
1067
1068 /**
1069 * Flatten a generic SparseArray into the parcel at the current
1070 * dataPosition(), growing dataCapacity() if needed. The SparseArray
1071 * values are written using {@link #writeValue} and must follow the
1072 * specification there.
1073 */
Svet Ganov8455ba22019-01-02 13:05:56 -08001074 public final <T> void writeSparseArray(@Nullable SparseArray<T> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 if (val == null) {
1076 writeInt(-1);
1077 return;
1078 }
1079 int N = val.size();
1080 writeInt(N);
1081 int i=0;
1082 while (i < N) {
1083 writeInt(val.keyAt(i));
1084 writeValue(val.valueAt(i));
1085 i++;
1086 }
1087 }
1088
Jake Whartonb1f474c2018-05-30 15:11:13 -04001089 public final void writeSparseBooleanArray(@Nullable SparseBooleanArray val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 if (val == null) {
1091 writeInt(-1);
1092 return;
1093 }
1094 int N = val.size();
1095 writeInt(N);
1096 int i=0;
1097 while (i < N) {
1098 writeInt(val.keyAt(i));
1099 writeByte((byte)(val.valueAt(i) ? 1 : 0));
1100 i++;
1101 }
1102 }
1103
Adam Lesinski205656d2017-03-23 13:38:26 -07001104 /**
1105 * @hide
1106 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001107 public final void writeSparseIntArray(@Nullable SparseIntArray val) {
Adam Lesinski4e862812016-11-21 16:02:24 -08001108 if (val == null) {
1109 writeInt(-1);
1110 return;
1111 }
1112 int N = val.size();
1113 writeInt(N);
1114 int i=0;
1115 while (i < N) {
1116 writeInt(val.keyAt(i));
1117 writeInt(val.valueAt(i));
1118 i++;
1119 }
1120 }
1121
Jake Whartonb1f474c2018-05-30 15:11:13 -04001122 public final void writeBooleanArray(@Nullable boolean[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 if (val != null) {
1124 int N = val.length;
1125 writeInt(N);
1126 for (int i=0; i<N; i++) {
1127 writeInt(val[i] ? 1 : 0);
1128 }
1129 } else {
1130 writeInt(-1);
1131 }
1132 }
1133
Jake Whartonb1f474c2018-05-30 15:11:13 -04001134 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 public final boolean[] createBooleanArray() {
1136 int N = readInt();
1137 // >>2 as a fast divide-by-4 works in the create*Array() functions
1138 // because dataAvail() will never return a negative number. 4 is
1139 // the size of a stored boolean in the stream.
1140 if (N >= 0 && N <= (dataAvail() >> 2)) {
1141 boolean[] val = new boolean[N];
1142 for (int i=0; i<N; i++) {
1143 val[i] = readInt() != 0;
1144 }
1145 return val;
1146 } else {
1147 return null;
1148 }
1149 }
1150
Jake Whartonb1f474c2018-05-30 15:11:13 -04001151 public final void readBooleanArray(@NonNull boolean[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 int N = readInt();
1153 if (N == val.length) {
1154 for (int i=0; i<N; i++) {
1155 val[i] = readInt() != 0;
1156 }
1157 } else {
1158 throw new RuntimeException("bad array lengths");
1159 }
1160 }
1161
Jake Whartonb1f474c2018-05-30 15:11:13 -04001162 public final void writeCharArray(@Nullable char[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 if (val != null) {
1164 int N = val.length;
1165 writeInt(N);
1166 for (int i=0; i<N; i++) {
1167 writeInt((int)val[i]);
1168 }
1169 } else {
1170 writeInt(-1);
1171 }
1172 }
1173
Jake Whartonb1f474c2018-05-30 15:11:13 -04001174 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 public final char[] createCharArray() {
1176 int N = readInt();
1177 if (N >= 0 && N <= (dataAvail() >> 2)) {
1178 char[] val = new char[N];
1179 for (int i=0; i<N; i++) {
1180 val[i] = (char)readInt();
1181 }
1182 return val;
1183 } else {
1184 return null;
1185 }
1186 }
1187
Jake Whartonb1f474c2018-05-30 15:11:13 -04001188 public final void readCharArray(@NonNull char[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 int N = readInt();
1190 if (N == val.length) {
1191 for (int i=0; i<N; i++) {
1192 val[i] = (char)readInt();
1193 }
1194 } else {
1195 throw new RuntimeException("bad array lengths");
1196 }
1197 }
1198
Jake Whartonb1f474c2018-05-30 15:11:13 -04001199 public final void writeIntArray(@Nullable int[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 if (val != null) {
1201 int N = val.length;
1202 writeInt(N);
1203 for (int i=0; i<N; i++) {
1204 writeInt(val[i]);
1205 }
1206 } else {
1207 writeInt(-1);
1208 }
1209 }
1210
Jake Whartonb1f474c2018-05-30 15:11:13 -04001211 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 public final int[] createIntArray() {
1213 int N = readInt();
1214 if (N >= 0 && N <= (dataAvail() >> 2)) {
1215 int[] val = new int[N];
1216 for (int i=0; i<N; i++) {
1217 val[i] = readInt();
1218 }
1219 return val;
1220 } else {
1221 return null;
1222 }
1223 }
1224
Jake Whartonb1f474c2018-05-30 15:11:13 -04001225 public final void readIntArray(@NonNull int[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 int N = readInt();
1227 if (N == val.length) {
1228 for (int i=0; i<N; i++) {
1229 val[i] = readInt();
1230 }
1231 } else {
1232 throw new RuntimeException("bad array lengths");
1233 }
1234 }
1235
Jake Whartonb1f474c2018-05-30 15:11:13 -04001236 public final void writeLongArray(@Nullable long[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 if (val != null) {
1238 int N = val.length;
1239 writeInt(N);
1240 for (int i=0; i<N; i++) {
1241 writeLong(val[i]);
1242 }
1243 } else {
1244 writeInt(-1);
1245 }
1246 }
1247
Jake Whartonb1f474c2018-05-30 15:11:13 -04001248 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 public final long[] createLongArray() {
1250 int N = readInt();
1251 // >>3 because stored longs are 64 bits
1252 if (N >= 0 && N <= (dataAvail() >> 3)) {
1253 long[] val = new long[N];
1254 for (int i=0; i<N; i++) {
1255 val[i] = readLong();
1256 }
1257 return val;
1258 } else {
1259 return null;
1260 }
1261 }
1262
Jake Whartonb1f474c2018-05-30 15:11:13 -04001263 public final void readLongArray(@NonNull long[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 int N = readInt();
1265 if (N == val.length) {
1266 for (int i=0; i<N; i++) {
1267 val[i] = readLong();
1268 }
1269 } else {
1270 throw new RuntimeException("bad array lengths");
1271 }
1272 }
1273
Jake Whartonb1f474c2018-05-30 15:11:13 -04001274 public final void writeFloatArray(@Nullable float[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 if (val != null) {
1276 int N = val.length;
1277 writeInt(N);
1278 for (int i=0; i<N; i++) {
1279 writeFloat(val[i]);
1280 }
1281 } else {
1282 writeInt(-1);
1283 }
1284 }
1285
Jake Whartonb1f474c2018-05-30 15:11:13 -04001286 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 public final float[] createFloatArray() {
1288 int N = readInt();
1289 // >>2 because stored floats are 4 bytes
1290 if (N >= 0 && N <= (dataAvail() >> 2)) {
1291 float[] val = new float[N];
1292 for (int i=0; i<N; i++) {
1293 val[i] = readFloat();
1294 }
1295 return val;
1296 } else {
1297 return null;
1298 }
1299 }
1300
Jake Whartonb1f474c2018-05-30 15:11:13 -04001301 public final void readFloatArray(@NonNull float[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 int N = readInt();
1303 if (N == val.length) {
1304 for (int i=0; i<N; i++) {
1305 val[i] = readFloat();
1306 }
1307 } else {
1308 throw new RuntimeException("bad array lengths");
1309 }
1310 }
1311
Jake Whartonb1f474c2018-05-30 15:11:13 -04001312 public final void writeDoubleArray(@Nullable double[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 if (val != null) {
1314 int N = val.length;
1315 writeInt(N);
1316 for (int i=0; i<N; i++) {
1317 writeDouble(val[i]);
1318 }
1319 } else {
1320 writeInt(-1);
1321 }
1322 }
1323
Jake Whartonb1f474c2018-05-30 15:11:13 -04001324 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 public final double[] createDoubleArray() {
1326 int N = readInt();
1327 // >>3 because stored doubles are 8 bytes
1328 if (N >= 0 && N <= (dataAvail() >> 3)) {
1329 double[] val = new double[N];
1330 for (int i=0; i<N; i++) {
1331 val[i] = readDouble();
1332 }
1333 return val;
1334 } else {
1335 return null;
1336 }
1337 }
1338
Jake Whartonb1f474c2018-05-30 15:11:13 -04001339 public final void readDoubleArray(@NonNull double[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 int N = readInt();
1341 if (N == val.length) {
1342 for (int i=0; i<N; i++) {
1343 val[i] = readDouble();
1344 }
1345 } else {
1346 throw new RuntimeException("bad array lengths");
1347 }
1348 }
1349
Jake Whartonb1f474c2018-05-30 15:11:13 -04001350 public final void writeStringArray(@Nullable String[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 if (val != null) {
1352 int N = val.length;
1353 writeInt(N);
1354 for (int i=0; i<N; i++) {
1355 writeString(val[i]);
1356 }
1357 } else {
1358 writeInt(-1);
1359 }
1360 }
1361
Jake Whartonb1f474c2018-05-30 15:11:13 -04001362 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 public final String[] createStringArray() {
1364 int N = readInt();
1365 if (N >= 0) {
1366 String[] val = new String[N];
1367 for (int i=0; i<N; i++) {
1368 val[i] = readString();
1369 }
1370 return val;
1371 } else {
1372 return null;
1373 }
1374 }
1375
Jake Whartonb1f474c2018-05-30 15:11:13 -04001376 public final void readStringArray(@NonNull String[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 int N = readInt();
1378 if (N == val.length) {
1379 for (int i=0; i<N; i++) {
1380 val[i] = readString();
1381 }
1382 } else {
1383 throw new RuntimeException("bad array lengths");
1384 }
1385 }
1386
Jake Whartonb1f474c2018-05-30 15:11:13 -04001387 public final void writeBinderArray(@Nullable IBinder[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 if (val != null) {
1389 int N = val.length;
1390 writeInt(N);
1391 for (int i=0; i<N; i++) {
1392 writeStrongBinder(val[i]);
1393 }
1394 } else {
1395 writeInt(-1);
1396 }
1397 }
1398
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001399 /**
1400 * @hide
1401 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001402 public final void writeCharSequenceArray(@Nullable CharSequence[] val) {
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001403 if (val != null) {
1404 int N = val.length;
1405 writeInt(N);
1406 for (int i=0; i<N; i++) {
1407 writeCharSequence(val[i]);
1408 }
1409 } else {
1410 writeInt(-1);
1411 }
1412 }
1413
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001414 /**
1415 * @hide
1416 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001417 public final void writeCharSequenceList(@Nullable ArrayList<CharSequence> val) {
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001418 if (val != null) {
1419 int N = val.size();
1420 writeInt(N);
1421 for (int i=0; i<N; i++) {
1422 writeCharSequence(val.get(i));
1423 }
1424 } else {
1425 writeInt(-1);
1426 }
1427 }
1428
Jake Whartonb1f474c2018-05-30 15:11:13 -04001429 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 public final IBinder[] createBinderArray() {
1431 int N = readInt();
1432 if (N >= 0) {
1433 IBinder[] val = new IBinder[N];
1434 for (int i=0; i<N; i++) {
1435 val[i] = readStrongBinder();
1436 }
1437 return val;
1438 } else {
1439 return null;
1440 }
1441 }
1442
Jake Whartonb1f474c2018-05-30 15:11:13 -04001443 public final void readBinderArray(@NonNull IBinder[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 int N = readInt();
1445 if (N == val.length) {
1446 for (int i=0; i<N; i++) {
1447 val[i] = readStrongBinder();
1448 }
1449 } else {
1450 throw new RuntimeException("bad array lengths");
1451 }
1452 }
1453
1454 /**
1455 * Flatten a List containing a particular object type into the parcel, at
1456 * the current dataPosition() and growing dataCapacity() if needed. The
1457 * type of the objects in the list must be one that implements Parcelable.
1458 * Unlike the generic writeList() method, however, only the raw data of the
1459 * objects is written and not their type, so you must use the corresponding
1460 * readTypedList() to unmarshall them.
1461 *
1462 * @param val The list of objects to be written.
1463 *
1464 * @see #createTypedArrayList
1465 * @see #readTypedList
1466 * @see Parcelable
1467 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001468 public final <T extends Parcelable> void writeTypedList(@Nullable List<T> val) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001469 writeTypedList(val, 0);
1470 }
1471
1472 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08001473 * Flatten a {@link SparseArray} containing a particular object type into the parcel
1474 * at the current dataPosition() and growing dataCapacity() if needed. The
1475 * type of the objects in the array must be one that implements Parcelable.
1476 * Unlike the generic {@link #writeSparseArray(SparseArray)} method, however, only
1477 * the raw data of the objects is written and not their type, so you must use the
1478 * corresponding {@link #createTypedSparseArray(Parcelable.Creator)}.
1479 *
1480 * @param val The list of objects to be written.
1481 * @param parcelableFlags The parcelable flags to use.
1482 *
1483 * @see #createTypedSparseArray(Parcelable.Creator)
1484 * @see Parcelable
1485 */
1486 public final <T extends Parcelable> void writeTypedSparseArray(@Nullable SparseArray<T> val,
1487 int parcelableFlags) {
1488 if (val == null) {
1489 writeInt(-1);
1490 return;
1491 }
1492 final int count = val.size();
1493 writeInt(count);
1494 for (int i = 0; i < count; i++) {
1495 writeInt(val.keyAt(i));
1496 writeTypedObject(val.valueAt(i), parcelableFlags);
1497 }
1498 }
1499
1500 /**
Sunny Goyal0e60f222017-09-21 21:39:20 -07001501 * @hide
1502 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001503 public <T extends Parcelable> void writeTypedList(@Nullable List<T> val, int parcelableFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 if (val == null) {
1505 writeInt(-1);
1506 return;
1507 }
1508 int N = val.size();
1509 int i=0;
1510 writeInt(N);
1511 while (i < N) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001512 writeTypedObject(val.get(i), parcelableFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 i++;
1514 }
1515 }
1516
1517 /**
1518 * Flatten a List containing String objects into the parcel, at
1519 * the current dataPosition() and growing dataCapacity() if needed. They
1520 * can later be retrieved with {@link #createStringArrayList} or
1521 * {@link #readStringList}.
1522 *
1523 * @param val The list of strings to be written.
1524 *
1525 * @see #createStringArrayList
1526 * @see #readStringList
1527 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001528 public final void writeStringList(@Nullable List<String> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 if (val == null) {
1530 writeInt(-1);
1531 return;
1532 }
1533 int N = val.size();
1534 int i=0;
1535 writeInt(N);
1536 while (i < N) {
1537 writeString(val.get(i));
1538 i++;
1539 }
1540 }
1541
1542 /**
1543 * Flatten a List containing IBinder objects into the parcel, at
1544 * the current dataPosition() and growing dataCapacity() if needed. They
1545 * can later be retrieved with {@link #createBinderArrayList} or
1546 * {@link #readBinderList}.
1547 *
1548 * @param val The list of strings to be written.
1549 *
1550 * @see #createBinderArrayList
1551 * @see #readBinderList
1552 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001553 public final void writeBinderList(@Nullable List<IBinder> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 if (val == null) {
1555 writeInt(-1);
1556 return;
1557 }
1558 int N = val.size();
1559 int i=0;
1560 writeInt(N);
1561 while (i < N) {
1562 writeStrongBinder(val.get(i));
1563 i++;
1564 }
1565 }
1566
1567 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00001568 * Flatten a {@code List} containing arbitrary {@code Parcelable} objects into this parcel
1569 * at the current position. They can later be retrieved using
1570 * {@link #readParcelableList(List, ClassLoader)} if required.
1571 *
1572 * @see #readParcelableList(List, ClassLoader)
Narayan Kamathbea48712016-12-01 15:38:28 +00001573 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001574 public final <T extends Parcelable> void writeParcelableList(@Nullable List<T> val, int flags) {
Narayan Kamathbea48712016-12-01 15:38:28 +00001575 if (val == null) {
1576 writeInt(-1);
1577 return;
1578 }
1579
1580 int N = val.size();
1581 int i=0;
1582 writeInt(N);
1583 while (i < N) {
1584 writeParcelable(val.get(i), flags);
1585 i++;
1586 }
1587 }
1588
1589 /**
Svet Ganov0f4928f2017-02-02 20:02:51 -08001590 * Flatten a homogeneous array containing a particular object type into
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 * the parcel, at
1592 * the current dataPosition() and growing dataCapacity() if needed. The
1593 * type of the objects in the array must be one that implements Parcelable.
1594 * Unlike the {@link #writeParcelableArray} method, however, only the
1595 * raw data of the objects is written and not their type, so you must use
1596 * {@link #readTypedArray} with the correct corresponding
1597 * {@link Parcelable.Creator} implementation to unmarshall them.
1598 *
1599 * @param val The array of objects to be written.
1600 * @param parcelableFlags Contextual flags as per
1601 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1602 *
1603 * @see #readTypedArray
1604 * @see #writeParcelableArray
1605 * @see Parcelable.Creator
1606 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001607 public final <T extends Parcelable> void writeTypedArray(@Nullable T[] val,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 int parcelableFlags) {
1609 if (val != null) {
1610 int N = val.length;
1611 writeInt(N);
Svet Ganov0f4928f2017-02-02 20:02:51 -08001612 for (int i = 0; i < N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001613 writeTypedObject(val[i], parcelableFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 }
1615 } else {
1616 writeInt(-1);
1617 }
1618 }
1619
1620 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001621 * Flatten the Parcelable object into the parcel.
1622 *
1623 * @param val The Parcelable object to be written.
1624 * @param parcelableFlags Contextual flags as per
1625 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1626 *
1627 * @see #readTypedObject
1628 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001629 public final <T extends Parcelable> void writeTypedObject(@Nullable T val,
1630 int parcelableFlags) {
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001631 if (val != null) {
1632 writeInt(1);
1633 val.writeToParcel(this, parcelableFlags);
1634 } else {
1635 writeInt(0);
1636 }
1637 }
1638
1639 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 * Flatten a generic object in to a parcel. The given Object value may
1641 * currently be one of the following types:
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001642 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 * <ul>
1644 * <li> null
1645 * <li> String
1646 * <li> Byte
1647 * <li> Short
1648 * <li> Integer
1649 * <li> Long
1650 * <li> Float
1651 * <li> Double
1652 * <li> Boolean
1653 * <li> String[]
1654 * <li> boolean[]
1655 * <li> byte[]
1656 * <li> int[]
1657 * <li> long[]
1658 * <li> Object[] (supporting objects of the same type defined here).
1659 * <li> {@link Bundle}
1660 * <li> Map (as supported by {@link #writeMap}).
1661 * <li> Any object that implements the {@link Parcelable} protocol.
1662 * <li> Parcelable[]
1663 * <li> CharSequence (as supported by {@link TextUtils#writeToParcel}).
1664 * <li> List (as supported by {@link #writeList}).
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001665 * <li> {@link SparseArray} (as supported by {@link #writeSparseArray(SparseArray)}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 * <li> {@link IBinder}
1667 * <li> Any object that implements Serializable (but see
1668 * {@link #writeSerializable} for caveats). Note that all of the
1669 * previous types have relatively efficient implementations for
1670 * writing to a Parcel; having to rely on the generic serialization
1671 * approach is much less efficient and should be avoided whenever
1672 * possible.
1673 * </ul>
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001674 *
1675 * <p class="caution">{@link Parcelable} objects are written with
1676 * {@link Parcelable#writeToParcel} using contextual flags of 0. When
1677 * serializing objects containing {@link ParcelFileDescriptor}s,
1678 * this may result in file descriptor leaks when they are returned from
1679 * Binder calls (where {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE}
1680 * should be used).</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001682 public final void writeValue(@Nullable Object v) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 if (v == null) {
1684 writeInt(VAL_NULL);
1685 } else if (v instanceof String) {
1686 writeInt(VAL_STRING);
1687 writeString((String) v);
1688 } else if (v instanceof Integer) {
1689 writeInt(VAL_INTEGER);
1690 writeInt((Integer) v);
1691 } else if (v instanceof Map) {
1692 writeInt(VAL_MAP);
1693 writeMap((Map) v);
1694 } else if (v instanceof Bundle) {
1695 // Must be before Parcelable
1696 writeInt(VAL_BUNDLE);
1697 writeBundle((Bundle) v);
Samuel Tanceafe5e2015-12-11 16:50:58 -08001698 } else if (v instanceof PersistableBundle) {
1699 writeInt(VAL_PERSISTABLEBUNDLE);
1700 writePersistableBundle((PersistableBundle) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 } else if (v instanceof Parcelable) {
Samuel Tanceafe5e2015-12-11 16:50:58 -08001702 // IMPOTANT: cases for classes that implement Parcelable must
1703 // come before the Parcelable case, so that their specific VAL_*
1704 // types will be written.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 writeInt(VAL_PARCELABLE);
1706 writeParcelable((Parcelable) v, 0);
1707 } else if (v instanceof Short) {
1708 writeInt(VAL_SHORT);
1709 writeInt(((Short) v).intValue());
1710 } else if (v instanceof Long) {
1711 writeInt(VAL_LONG);
1712 writeLong((Long) v);
1713 } else if (v instanceof Float) {
1714 writeInt(VAL_FLOAT);
1715 writeFloat((Float) v);
1716 } else if (v instanceof Double) {
1717 writeInt(VAL_DOUBLE);
1718 writeDouble((Double) v);
1719 } else if (v instanceof Boolean) {
1720 writeInt(VAL_BOOLEAN);
1721 writeInt((Boolean) v ? 1 : 0);
1722 } else if (v instanceof CharSequence) {
1723 // Must be after String
1724 writeInt(VAL_CHARSEQUENCE);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001725 writeCharSequence((CharSequence) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 } else if (v instanceof List) {
1727 writeInt(VAL_LIST);
1728 writeList((List) v);
1729 } else if (v instanceof SparseArray) {
1730 writeInt(VAL_SPARSEARRAY);
1731 writeSparseArray((SparseArray) v);
1732 } else if (v instanceof boolean[]) {
1733 writeInt(VAL_BOOLEANARRAY);
1734 writeBooleanArray((boolean[]) v);
1735 } else if (v instanceof byte[]) {
1736 writeInt(VAL_BYTEARRAY);
1737 writeByteArray((byte[]) v);
1738 } else if (v instanceof String[]) {
1739 writeInt(VAL_STRINGARRAY);
1740 writeStringArray((String[]) v);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001741 } else if (v instanceof CharSequence[]) {
1742 // Must be after String[] and before Object[]
1743 writeInt(VAL_CHARSEQUENCEARRAY);
1744 writeCharSequenceArray((CharSequence[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 } else if (v instanceof IBinder) {
1746 writeInt(VAL_IBINDER);
1747 writeStrongBinder((IBinder) v);
1748 } else if (v instanceof Parcelable[]) {
1749 writeInt(VAL_PARCELABLEARRAY);
1750 writeParcelableArray((Parcelable[]) v, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 } else if (v instanceof int[]) {
1752 writeInt(VAL_INTARRAY);
1753 writeIntArray((int[]) v);
1754 } else if (v instanceof long[]) {
1755 writeInt(VAL_LONGARRAY);
1756 writeLongArray((long[]) v);
1757 } else if (v instanceof Byte) {
1758 writeInt(VAL_BYTE);
1759 writeInt((Byte) v);
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001760 } else if (v instanceof Size) {
1761 writeInt(VAL_SIZE);
1762 writeSize((Size) v);
1763 } else if (v instanceof SizeF) {
1764 writeInt(VAL_SIZEF);
1765 writeSizeF((SizeF) v);
Samuel Tana8036662015-11-23 14:36:00 -08001766 } else if (v instanceof double[]) {
1767 writeInt(VAL_DOUBLEARRAY);
1768 writeDoubleArray((double[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 } else {
Paul Duffinac5a0822014-02-03 15:02:17 +00001770 Class<?> clazz = v.getClass();
1771 if (clazz.isArray() && clazz.getComponentType() == Object.class) {
1772 // Only pure Object[] are written here, Other arrays of non-primitive types are
1773 // handled by serialization as this does not record the component type.
1774 writeInt(VAL_OBJECTARRAY);
1775 writeArray((Object[]) v);
1776 } else if (v instanceof Serializable) {
1777 // Must be last
1778 writeInt(VAL_SERIALIZABLE);
1779 writeSerializable((Serializable) v);
1780 } else {
1781 throw new RuntimeException("Parcel: unable to marshal value " + v);
1782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 }
1784 }
1785
1786 /**
1787 * Flatten the name of the class of the Parcelable and its contents
1788 * into the parcel.
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001789 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 * @param p The Parcelable object to be written.
1791 * @param parcelableFlags Contextual flags as per
1792 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1793 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001794 public final void writeParcelable(@Nullable Parcelable p, int parcelableFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 if (p == null) {
1796 writeString(null);
1797 return;
1798 }
Neil Fuller44e440c2015-04-20 14:39:00 +01001799 writeParcelableCreator(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 p.writeToParcel(this, parcelableFlags);
1801 }
1802
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001803 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001804 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04001805 public final void writeParcelableCreator(@NonNull Parcelable p) {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001806 String name = p.getClass().getName();
1807 writeString(name);
1808 }
1809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 /**
1811 * Write a generic serializable object in to a Parcel. It is strongly
1812 * recommended that this method be avoided, since the serialization
1813 * overhead is extremely large, and this approach will be much slower than
1814 * using the other approaches to writing data in to a Parcel.
1815 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001816 public final void writeSerializable(@Nullable Serializable s) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 if (s == null) {
1818 writeString(null);
1819 return;
1820 }
1821 String name = s.getClass().getName();
1822 writeString(name);
1823
1824 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1825 try {
1826 ObjectOutputStream oos = new ObjectOutputStream(baos);
1827 oos.writeObject(s);
1828 oos.close();
1829
1830 writeByteArray(baos.toByteArray());
1831 } catch (IOException ioe) {
1832 throw new RuntimeException("Parcelable encountered " +
1833 "IOException writing serializable object (name = " + name +
1834 ")", ioe);
1835 }
1836 }
1837
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001838 /** @hide For debugging purposes */
1839 public static void setStackTraceParceling(boolean enabled) {
1840 sParcelExceptionStackTrace = enabled;
1841 }
1842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 /**
1844 * Special function for writing an exception result at the header of
1845 * a parcel, to be used when returning an exception from a transaction.
1846 * Note that this currently only supports a few exception types; any other
1847 * exception will be re-thrown by this function as a RuntimeException
1848 * (to be caught by the system's last-resort exception handling when
1849 * dispatching a transaction).
Samuel Tana8036662015-11-23 14:36:00 -08001850 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 * <p>The supported exception types are:
1852 * <ul>
1853 * <li>{@link BadParcelableException}
1854 * <li>{@link IllegalArgumentException}
1855 * <li>{@link IllegalStateException}
1856 * <li>{@link NullPointerException}
1857 * <li>{@link SecurityException}
Dianne Hackborn18482ae2017-08-01 17:41:00 -07001858 * <li>{@link UnsupportedOperationException}
Dianne Hackborn7e714422013-09-13 17:32:57 -07001859 * <li>{@link NetworkOnMainThreadException}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 * </ul>
Samuel Tana8036662015-11-23 14:36:00 -08001861 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 * @param e The Exception to be written.
1863 *
1864 * @see #writeNoException
1865 * @see #readException
1866 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001867 public final void writeException(@NonNull Exception e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 int code = 0;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001869 if (e instanceof Parcelable
1870 && (e.getClass().getClassLoader() == Parcelable.class.getClassLoader())) {
1871 // We only send Parcelable exceptions that are in the
1872 // BootClassLoader to ensure that the receiver can unpack them
1873 code = EX_PARCELABLE;
1874 } else if (e instanceof SecurityException) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 code = EX_SECURITY;
1876 } else if (e instanceof BadParcelableException) {
1877 code = EX_BAD_PARCELABLE;
1878 } else if (e instanceof IllegalArgumentException) {
1879 code = EX_ILLEGAL_ARGUMENT;
1880 } else if (e instanceof NullPointerException) {
1881 code = EX_NULL_POINTER;
1882 } else if (e instanceof IllegalStateException) {
1883 code = EX_ILLEGAL_STATE;
Dianne Hackborn7e714422013-09-13 17:32:57 -07001884 } else if (e instanceof NetworkOnMainThreadException) {
1885 code = EX_NETWORK_MAIN_THREAD;
Dianne Hackborn33d738a2014-09-12 14:23:58 -07001886 } else if (e instanceof UnsupportedOperationException) {
1887 code = EX_UNSUPPORTED_OPERATION;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001888 } else if (e instanceof ServiceSpecificException) {
1889 code = EX_SERVICE_SPECIFIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 }
1891 writeInt(code);
Brad Fitzpatrick703e5d32010-07-15 13:16:41 -07001892 StrictMode.clearGatheredViolations();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 if (code == 0) {
1894 if (e instanceof RuntimeException) {
1895 throw (RuntimeException) e;
1896 }
1897 throw new RuntimeException(e);
1898 }
1899 writeString(e.getMessage());
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001900 final long timeNow = sParcelExceptionStackTrace ? SystemClock.elapsedRealtime() : 0;
1901 if (sParcelExceptionStackTrace && (timeNow - sLastWriteExceptionStackTrace
1902 > WRITE_EXCEPTION_STACK_TRACE_THRESHOLD_MS)) {
1903 sLastWriteExceptionStackTrace = timeNow;
1904 final int sizePosition = dataPosition();
1905 writeInt(0); // Header size will be filled in later
1906 StackTraceElement[] stackTrace = e.getStackTrace();
1907 final int truncatedSize = Math.min(stackTrace.length, 5);
1908 StringBuilder sb = new StringBuilder();
1909 for (int i = 0; i < truncatedSize; i++) {
Fyodor Kupolov679d9982017-11-21 10:42:23 -08001910 sb.append("\tat ").append(stackTrace[i]).append('\n');
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001911 }
1912 writeString(sb.toString());
1913 final int payloadPosition = dataPosition();
1914 setDataPosition(sizePosition);
1915 // Write stack trace header size. Used in native side to skip the header
1916 writeInt(payloadPosition - sizePosition);
1917 setDataPosition(payloadPosition);
1918 } else {
1919 writeInt(0);
1920 }
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001921 switch (code) {
1922 case EX_SERVICE_SPECIFIC:
1923 writeInt(((ServiceSpecificException) e).errorCode);
1924 break;
1925 case EX_PARCELABLE:
1926 // Write parceled exception prefixed by length
1927 final int sizePosition = dataPosition();
1928 writeInt(0);
1929 writeParcelable((Parcelable) e, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
1930 final int payloadPosition = dataPosition();
1931 setDataPosition(sizePosition);
1932 writeInt(payloadPosition - sizePosition);
1933 setDataPosition(payloadPosition);
1934 break;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 }
1937
1938 /**
1939 * Special function for writing information at the front of the Parcel
1940 * indicating that no exception occurred.
1941 *
1942 * @see #writeException
1943 * @see #readException
1944 */
1945 public final void writeNoException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001946 // Despite the name of this function ("write no exception"),
1947 // it should instead be thought of as "write the RPC response
1948 // header", but because this function name is written out by
1949 // the AIDL compiler, we're not going to rename it.
1950 //
1951 // The response header, in the non-exception case (see also
1952 // writeException above, also called by the AIDL compiler), is
1953 // either a 0 (the default case), or EX_HAS_REPLY_HEADER if
1954 // StrictMode has gathered up violations that have occurred
1955 // during a Binder call, in which case we write out the number
1956 // of violations and their details, serialized, before the
1957 // actual RPC respons data. The receiving end of this is
1958 // readException(), below.
1959 if (StrictMode.hasGatheredViolations()) {
1960 writeInt(EX_HAS_REPLY_HEADER);
1961 final int sizePosition = dataPosition();
1962 writeInt(0); // total size of fat header, to be filled in later
1963 StrictMode.writeGatheredViolationsToParcel(this);
1964 final int payloadPosition = dataPosition();
1965 setDataPosition(sizePosition);
1966 writeInt(payloadPosition - sizePosition); // header size
1967 setDataPosition(payloadPosition);
1968 } else {
1969 writeInt(0);
1970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 }
1972
1973 /**
1974 * Special function for reading an exception result from the header of
1975 * a parcel, to be used after receiving the result of a transaction. This
1976 * will throw the exception for you if it had been written to the Parcel,
1977 * otherwise return and let you read the normal result data from the Parcel.
1978 *
1979 * @see #writeException
1980 * @see #writeNoException
1981 */
1982 public final void readException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001983 int code = readExceptionCode();
1984 if (code != 0) {
1985 String msg = readString();
Takamasa Kuramitsu2214b822018-04-02 14:44:59 +09001986 readException(code, msg);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001987 }
1988 }
1989
1990 /**
1991 * Parses the header of a Binder call's response Parcel and
1992 * returns the exception code. Deals with lite or fat headers.
1993 * In the common successful case, this header is generally zero.
1994 * In less common cases, it's a small negative number and will be
1995 * followed by an error string.
1996 *
1997 * This exists purely for android.database.DatabaseUtils and
1998 * insulating it from having to handle fat headers as returned by
1999 * e.g. StrictMode-induced RPC responses.
2000 *
2001 * @hide
2002 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002003 @UnsupportedAppUsage
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002004 public final int readExceptionCode() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 int code = readInt();
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002006 if (code == EX_HAS_REPLY_HEADER) {
2007 int headerSize = readInt();
2008 if (headerSize == 0) {
2009 Log.e(TAG, "Unexpected zero-sized Parcel reply header.");
2010 } else {
2011 // Currently the only thing in the header is StrictMode stacks,
2012 // but discussions around event/RPC tracing suggest we might
2013 // put that here too. If so, switch on sub-header tags here.
2014 // But for now, just parse out the StrictMode stuff.
2015 StrictMode.readAndHandleBinderCallViolations(this);
2016 }
2017 // And fat response headers are currently only used when
2018 // there are no exceptions, so return no error:
2019 return 0;
2020 }
2021 return code;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 }
2023
2024 /**
Mark Doliner879ea452014-01-02 12:38:07 -08002025 * Throw an exception with the given message. Not intended for use
2026 * outside the Parcel class.
2027 *
2028 * @param code Used to determine which exception class to throw.
2029 * @param msg The exception message.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 */
2031 public final void readException(int code, String msg) {
Takamasa Kuramitsu2214b822018-04-02 14:44:59 +09002032 String remoteStackTrace = null;
2033 final int remoteStackPayloadSize = readInt();
2034 if (remoteStackPayloadSize > 0) {
2035 remoteStackTrace = readString();
2036 }
2037 Exception e = createException(code, msg);
2038 // Attach remote stack trace if availalble
2039 if (remoteStackTrace != null) {
2040 RemoteException cause = new RemoteException(
2041 "Remote stack trace:\n" + remoteStackTrace, null, false, false);
2042 try {
2043 Throwable rootCause = ExceptionUtils.getRootCause(e);
2044 if (rootCause != null) {
2045 rootCause.initCause(cause);
2046 }
2047 } catch (RuntimeException ex) {
2048 Log.e(TAG, "Cannot set cause " + cause + " for " + e, ex);
2049 }
2050 }
2051 SneakyThrow.sneakyThrow(e);
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002052 }
2053
2054 /**
2055 * Creates an exception with the given message.
2056 *
2057 * @param code Used to determine which exception class to throw.
2058 * @param msg The exception message.
2059 */
2060 private Exception createException(int code, String msg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 switch (code) {
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002062 case EX_PARCELABLE:
2063 if (readInt() > 0) {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002064 return (Exception) readParcelable(Parcelable.class.getClassLoader());
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002065 } else {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002066 return new RuntimeException(msg + " [missing Parcelable]");
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002067 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 case EX_SECURITY:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002069 return new SecurityException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 case EX_BAD_PARCELABLE:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002071 return new BadParcelableException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 case EX_ILLEGAL_ARGUMENT:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002073 return new IllegalArgumentException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 case EX_NULL_POINTER:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002075 return new NullPointerException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 case EX_ILLEGAL_STATE:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002077 return new IllegalStateException(msg);
Dianne Hackborn7e714422013-09-13 17:32:57 -07002078 case EX_NETWORK_MAIN_THREAD:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002079 return new NetworkOnMainThreadException();
Dianne Hackborn33d738a2014-09-12 14:23:58 -07002080 case EX_UNSUPPORTED_OPERATION:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002081 return new UnsupportedOperationException(msg);
Christopher Wiley80fd1202015-11-22 17:12:37 -08002082 case EX_SERVICE_SPECIFIC:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002083 return new ServiceSpecificException(readInt(), msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 }
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002085 return new RuntimeException("Unknown exception code: " + code
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 + " msg " + msg);
2087 }
2088
2089 /**
2090 * Read an integer value from the parcel at the current dataPosition().
2091 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002092 public final int readInt() {
2093 return nativeReadInt(mNativePtr);
2094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095
2096 /**
2097 * Read a long integer value from the parcel at the current dataPosition().
2098 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002099 public final long readLong() {
2100 return nativeReadLong(mNativePtr);
2101 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102
2103 /**
2104 * Read a floating point value from the parcel at the current
2105 * dataPosition().
2106 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002107 public final float readFloat() {
2108 return nativeReadFloat(mNativePtr);
2109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110
2111 /**
2112 * Read a double precision floating point value from the parcel at the
2113 * current dataPosition().
2114 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002115 public final double readDouble() {
2116 return nativeReadDouble(mNativePtr);
2117 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118
2119 /**
2120 * Read a string value from the parcel at the current dataPosition().
2121 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002122 @Nullable
Jeff Sharkey047238c2012-03-07 16:51:38 -08002123 public final String readString() {
Makoto Onuki4501c61d2017-07-27 15:56:40 -07002124 return mReadWriteHelper.readString(this);
2125 }
2126
2127 /**
2128 * Read a string without going though a {@link ReadWriteHelper}. Subclasses of
2129 * {@link ReadWriteHelper} must use this method instead of {@link #readString} to avoid
2130 * infinity recursive calls.
2131 *
2132 * @hide
2133 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002134 @Nullable
Makoto Onuki4501c61d2017-07-27 15:56:40 -07002135 public String readStringNoHelper() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002136 return nativeReadString(mNativePtr);
2137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138
Jake Wharton2ffa4312018-04-23 17:47:14 -04002139 /**
2140 * Read a boolean value from the parcel at the current dataPosition().
2141 */
Eugene Susla36e866b2017-02-23 18:24:39 -08002142 public final boolean readBoolean() {
2143 return readInt() != 0;
2144 }
2145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002147 * Read a CharSequence value from the parcel at the current dataPosition().
2148 * @hide
2149 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002150 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04002151 @Nullable
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002152 public final CharSequence readCharSequence() {
2153 return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(this);
2154 }
2155
2156 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 * Read an object from the parcel at the current dataPosition().
2158 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002159 public final IBinder readStrongBinder() {
2160 return nativeReadStrongBinder(mNativePtr);
2161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162
2163 /**
2164 * Read a FileDescriptor from the parcel at the current dataPosition().
2165 */
2166 public final ParcelFileDescriptor readFileDescriptor() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002167 FileDescriptor fd = nativeReadFileDescriptor(mNativePtr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 return fd != null ? new ParcelFileDescriptor(fd) : null;
2169 }
2170
Jeff Sharkeyda5a3e12013-08-11 12:54:42 -07002171 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +00002172 @UnsupportedAppUsage
Jeff Sharkeyda5a3e12013-08-11 12:54:42 -07002173 public final FileDescriptor readRawFileDescriptor() {
2174 return nativeReadFileDescriptor(mNativePtr);
2175 }
2176
Casey Dahlin2f974b22015-11-05 12:19:13 -08002177 /**
2178 * {@hide}
2179 * Read and return a new array of FileDescriptors from the parcel.
2180 * @return the FileDescriptor array, or null if the array is null.
2181 **/
Jake Whartonb1f474c2018-05-30 15:11:13 -04002182 @Nullable
Casey Dahlin2f974b22015-11-05 12:19:13 -08002183 public final FileDescriptor[] createRawFileDescriptorArray() {
2184 int N = readInt();
2185 if (N < 0) {
2186 return null;
2187 }
2188 FileDescriptor[] f = new FileDescriptor[N];
2189 for (int i = 0; i < N; i++) {
2190 f[i] = readRawFileDescriptor();
2191 }
2192 return f;
2193 }
2194
2195 /**
2196 * {@hide}
2197 * Read an array of FileDescriptors from a parcel.
2198 * The passed array must be exactly the length of the array in the parcel.
2199 * @return the FileDescriptor array, or null if the array is null.
2200 **/
2201 public final void readRawFileDescriptorArray(FileDescriptor[] val) {
2202 int N = readInt();
2203 if (N == val.length) {
2204 for (int i=0; i<N; i++) {
2205 val[i] = readRawFileDescriptor();
2206 }
2207 } else {
2208 throw new RuntimeException("bad array lengths");
2209 }
2210 }
2211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 /**
2213 * Read a byte value from the parcel at the current dataPosition().
2214 */
2215 public final byte readByte() {
2216 return (byte)(readInt() & 0xff);
2217 }
2218
2219 /**
2220 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2221 * been written with {@link #writeBundle}. Read into an existing Map object
2222 * from the parcel at the current dataPosition().
2223 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002224 public final void readMap(@NonNull Map outVal, @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 int N = readInt();
2226 readMapInternal(outVal, N, loader);
2227 }
2228
2229 /**
2230 * Read into an existing List object from the parcel at the current
2231 * dataPosition(), using the given class loader to load any enclosed
2232 * Parcelables. If it is null, the default class loader is used.
2233 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002234 public final void readList(@NonNull List outVal, @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 int N = readInt();
2236 readListInternal(outVal, N, loader);
2237 }
2238
2239 /**
2240 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2241 * been written with {@link #writeBundle}. Read and return a new HashMap
2242 * object from the parcel at the current dataPosition(), using the given
2243 * class loader to load any enclosed Parcelables. Returns null if
2244 * the previously written map object was null.
2245 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002246 @Nullable
2247 public final HashMap readHashMap(@Nullable ClassLoader loader)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 {
2249 int N = readInt();
2250 if (N < 0) {
2251 return null;
2252 }
2253 HashMap m = new HashMap(N);
2254 readMapInternal(m, N, loader);
2255 return m;
2256 }
2257
2258 /**
2259 * Read and return a new Bundle object from the parcel at the current
2260 * dataPosition(). Returns null if the previously written Bundle object was
2261 * null.
2262 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002263 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 public final Bundle readBundle() {
2265 return readBundle(null);
2266 }
2267
2268 /**
2269 * Read and return a new Bundle object from the parcel at the current
2270 * dataPosition(), using the given class loader to initialize the class
2271 * loader of the Bundle for later retrieval of Parcelable objects.
2272 * Returns null if the previously written Bundle object was null.
2273 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002274 @Nullable
2275 public final Bundle readBundle(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 int length = readInt();
2277 if (length < 0) {
Dianne Hackborn4a7d8242013-10-03 10:19:20 -07002278 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 return null;
2280 }
Samuel Tana8036662015-11-23 14:36:00 -08002281
Dianne Hackborn6aff9052009-05-22 13:20:23 -07002282 final Bundle bundle = new Bundle(this, length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 if (loader != null) {
2284 bundle.setClassLoader(loader);
2285 }
2286 return bundle;
2287 }
2288
2289 /**
Craig Mautner719e6b12014-04-04 20:29:41 -07002290 * Read and return a new Bundle object from the parcel at the current
2291 * dataPosition(). Returns null if the previously written Bundle object was
2292 * null.
2293 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002294 @Nullable
Craig Mautner719e6b12014-04-04 20:29:41 -07002295 public final PersistableBundle readPersistableBundle() {
2296 return readPersistableBundle(null);
2297 }
2298
2299 /**
2300 * Read and return a new Bundle object from the parcel at the current
2301 * dataPosition(), using the given class loader to initialize the class
2302 * loader of the Bundle for later retrieval of Parcelable objects.
2303 * Returns null if the previously written Bundle object was null.
2304 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002305 @Nullable
2306 public final PersistableBundle readPersistableBundle(@Nullable ClassLoader loader) {
Craig Mautner719e6b12014-04-04 20:29:41 -07002307 int length = readInt();
2308 if (length < 0) {
2309 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
2310 return null;
2311 }
2312
2313 final PersistableBundle bundle = new PersistableBundle(this, length);
2314 if (loader != null) {
2315 bundle.setClassLoader(loader);
2316 }
2317 return bundle;
2318 }
2319
2320 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002321 * Read a Size from the parcel at the current dataPosition().
2322 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002323 @NonNull
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002324 public final Size readSize() {
2325 final int width = readInt();
2326 final int height = readInt();
2327 return new Size(width, height);
2328 }
2329
2330 /**
2331 * Read a SizeF from the parcel at the current dataPosition().
2332 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002333 @NonNull
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002334 public final SizeF readSizeF() {
2335 final float width = readFloat();
2336 final float height = readFloat();
2337 return new SizeF(width, height);
2338 }
2339
2340 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 * Read and return a byte[] object from the parcel.
2342 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002343 @Nullable
Jeff Sharkey047238c2012-03-07 16:51:38 -08002344 public final byte[] createByteArray() {
2345 return nativeCreateByteArray(mNativePtr);
2346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347
2348 /**
2349 * Read a byte[] object from the parcel and copy it into the
2350 * given byte array.
2351 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002352 public final void readByteArray(@NonNull byte[] val) {
Jocelyn Dang46100442017-05-05 15:40:49 -07002353 boolean valid = nativeReadByteArray(mNativePtr, val, (val != null) ? val.length : 0);
2354 if (!valid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 throw new RuntimeException("bad array lengths");
2356 }
2357 }
2358
2359 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002360 * Read a blob of data from the parcel and return it as a byte array.
2361 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -07002362 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002363 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002364 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04002365 @Nullable
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002366 public final byte[] readBlob() {
2367 return nativeReadBlob(mNativePtr);
2368 }
2369
2370 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 * Read and return a String[] object from the parcel.
2372 * {@hide}
2373 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002374 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04002375 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 public final String[] readStringArray() {
2377 String[] array = null;
2378
2379 int length = readInt();
2380 if (length >= 0)
2381 {
2382 array = new String[length];
2383
2384 for (int i = 0 ; i < length ; i++)
2385 {
2386 array[i] = readString();
2387 }
2388 }
2389
2390 return array;
2391 }
2392
2393 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002394 * Read and return a CharSequence[] object from the parcel.
2395 * {@hide}
2396 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002397 @Nullable
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002398 public final CharSequence[] readCharSequenceArray() {
2399 CharSequence[] array = null;
2400
2401 int length = readInt();
2402 if (length >= 0)
2403 {
2404 array = new CharSequence[length];
2405
2406 for (int i = 0 ; i < length ; i++)
2407 {
2408 array[i] = readCharSequence();
2409 }
2410 }
2411
2412 return array;
2413 }
2414
2415 /**
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002416 * Read and return an ArrayList&lt;CharSequence&gt; object from the parcel.
2417 * {@hide}
2418 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002419 @Nullable
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002420 public final ArrayList<CharSequence> readCharSequenceList() {
2421 ArrayList<CharSequence> array = null;
2422
2423 int length = readInt();
2424 if (length >= 0) {
2425 array = new ArrayList<CharSequence>(length);
2426
2427 for (int i = 0 ; i < length ; i++) {
2428 array.add(readCharSequence());
2429 }
2430 }
2431
2432 return array;
2433 }
2434
2435 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 * Read and return a new ArrayList object from the parcel at the current
2437 * dataPosition(). Returns null if the previously written list object was
2438 * null. The given class loader will be used to load any enclosed
2439 * Parcelables.
2440 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002441 @Nullable
2442 public final ArrayList readArrayList(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 int N = readInt();
2444 if (N < 0) {
2445 return null;
2446 }
2447 ArrayList l = new ArrayList(N);
2448 readListInternal(l, N, loader);
2449 return l;
2450 }
2451
2452 /**
2453 * Read and return a new Object array from the parcel at the current
2454 * dataPosition(). Returns null if the previously written array was
2455 * null. The given class loader will be used to load any enclosed
2456 * Parcelables.
2457 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002458 @Nullable
2459 public final Object[] readArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 int N = readInt();
2461 if (N < 0) {
2462 return null;
2463 }
2464 Object[] l = new Object[N];
2465 readArrayInternal(l, N, loader);
2466 return l;
2467 }
2468
2469 /**
2470 * Read and return a new SparseArray object from the parcel at the current
2471 * dataPosition(). Returns null if the previously written list object was
2472 * null. The given class loader will be used to load any enclosed
2473 * Parcelables.
2474 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002475 @Nullable
Svet Ganov8455ba22019-01-02 13:05:56 -08002476 public final <T> SparseArray<T> readSparseArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 int N = readInt();
2478 if (N < 0) {
2479 return null;
2480 }
2481 SparseArray sa = new SparseArray(N);
2482 readSparseArrayInternal(sa, N, loader);
2483 return sa;
2484 }
2485
2486 /**
2487 * Read and return a new SparseBooleanArray object from the parcel at the current
2488 * dataPosition(). Returns null if the previously written list object was
2489 * null.
2490 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002491 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 public final SparseBooleanArray readSparseBooleanArray() {
2493 int N = readInt();
2494 if (N < 0) {
2495 return null;
2496 }
2497 SparseBooleanArray sa = new SparseBooleanArray(N);
2498 readSparseBooleanArrayInternal(sa, N);
2499 return sa;
2500 }
2501
2502 /**
Adam Lesinski4e862812016-11-21 16:02:24 -08002503 * Read and return a new SparseIntArray object from the parcel at the current
2504 * dataPosition(). Returns null if the previously written array object was null.
Adam Lesinski205656d2017-03-23 13:38:26 -07002505 * @hide
Adam Lesinski4e862812016-11-21 16:02:24 -08002506 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002507 @Nullable
Adam Lesinski4e862812016-11-21 16:02:24 -08002508 public final SparseIntArray readSparseIntArray() {
2509 int N = readInt();
2510 if (N < 0) {
2511 return null;
2512 }
2513 SparseIntArray sa = new SparseIntArray(N);
2514 readSparseIntArrayInternal(sa, N);
2515 return sa;
2516 }
2517
2518 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 * Read and return a new ArrayList containing a particular object type from
2520 * the parcel that was written with {@link #writeTypedList} at the
2521 * current dataPosition(). Returns null if the
2522 * previously written list object was null. The list <em>must</em> have
2523 * previously been written via {@link #writeTypedList} with the same object
2524 * type.
2525 *
2526 * @return A newly created ArrayList containing objects with the same data
2527 * as those that were previously written.
2528 *
2529 * @see #writeTypedList
2530 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002531 @Nullable
2532 public final <T> ArrayList<T> createTypedArrayList(@NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 int N = readInt();
2534 if (N < 0) {
2535 return null;
2536 }
2537 ArrayList<T> l = new ArrayList<T>(N);
2538 while (N > 0) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002539 l.add(readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 N--;
2541 }
2542 return l;
2543 }
2544
2545 /**
2546 * Read into the given List items containing a particular object type
2547 * that were written with {@link #writeTypedList} at the
2548 * current dataPosition(). The list <em>must</em> have
2549 * previously been written via {@link #writeTypedList} with the same object
2550 * type.
2551 *
2552 * @return A newly created ArrayList containing objects with the same data
2553 * as those that were previously written.
2554 *
2555 * @see #writeTypedList
2556 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002557 public final <T> void readTypedList(@NonNull List<T> list, @NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 int M = list.size();
2559 int N = readInt();
2560 int i = 0;
2561 for (; i < M && i < N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002562 list.set(i, readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563 }
2564 for (; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002565 list.add(readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 }
2567 for (; i<M; i++) {
2568 list.remove(N);
2569 }
2570 }
2571
2572 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08002573 * Read into a new {@link SparseArray} items containing a particular object type
2574 * that were written with {@link #writeTypedSparseArray(SparseArray, int)} at the
2575 * current dataPosition(). The list <em>must</em> have previously been written
2576 * via {@link #writeTypedSparseArray(SparseArray, int)} with the same object type.
2577 *
2578 * @param creator The creator to use when for instantiation.
2579 *
2580 * @return A newly created {@link SparseArray} containing objects with the same data
2581 * as those that were previously written.
2582 *
2583 * @see #writeTypedSparseArray(SparseArray, int)
2584 */
2585 public final @Nullable <T extends Parcelable> SparseArray<T> createTypedSparseArray(
2586 @NonNull Parcelable.Creator<T> creator) {
2587 final int count = readInt();
2588 if (count < 0) {
2589 return null;
2590 }
2591 final SparseArray<T> array = new SparseArray<>(count);
2592 for (int i = 0; i < count; i++) {
2593 final int index = readInt();
2594 final T value = readTypedObject(creator);
2595 array.append(index, value);
2596 }
2597 return array;
2598 }
2599
2600 /**
2601 * Read into a new {@link ArrayMap} with string keys items containing a particular
2602 * object type that were written with {@link #writeTypedArrayMap(ArrayMap, int)} at the
2603 * current dataPosition(). The list <em>must</em> have previously been written
2604 * via {@link #writeTypedArrayMap(ArrayMap, int)} with the same object type.
2605 *
2606 * @param creator The creator to use when for instantiation.
2607 *
2608 * @return A newly created {@link ArrayMap} containing objects with the same data
2609 * as those that were previously written.
2610 *
2611 * @see #writeTypedArrayMap(ArrayMap, int)
2612 */
2613 public final @Nullable <T extends Parcelable> ArrayMap<String, T> createTypedArrayMap(
2614 @NonNull Parcelable.Creator<T> creator) {
2615 final int count = readInt();
2616 if (count < 0) {
2617 return null;
2618 }
2619 final ArrayMap<String, T> map = new ArrayMap<>(count);
2620 for (int i = 0; i < count; i++) {
2621 final String key = readString();
2622 final T value = readTypedObject(creator);
2623 map.append(key, value);
2624 }
2625 return map;
2626 }
2627
2628 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 * Read and return a new ArrayList containing String objects from
2630 * the parcel that was written with {@link #writeStringList} at the
2631 * current dataPosition(). Returns null if the
2632 * previously written list object was null.
2633 *
2634 * @return A newly created ArrayList containing strings with the same data
2635 * as those that were previously written.
2636 *
2637 * @see #writeStringList
2638 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002639 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 public final ArrayList<String> createStringArrayList() {
2641 int N = readInt();
2642 if (N < 0) {
2643 return null;
2644 }
2645 ArrayList<String> l = new ArrayList<String>(N);
2646 while (N > 0) {
2647 l.add(readString());
2648 N--;
2649 }
2650 return l;
2651 }
2652
2653 /**
2654 * Read and return a new ArrayList containing IBinder objects from
2655 * the parcel that was written with {@link #writeBinderList} at the
2656 * current dataPosition(). Returns null if the
2657 * previously written list object was null.
2658 *
2659 * @return A newly created ArrayList containing strings with the same data
2660 * as those that were previously written.
2661 *
2662 * @see #writeBinderList
2663 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002664 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 public final ArrayList<IBinder> createBinderArrayList() {
2666 int N = readInt();
2667 if (N < 0) {
2668 return null;
2669 }
2670 ArrayList<IBinder> l = new ArrayList<IBinder>(N);
2671 while (N > 0) {
2672 l.add(readStrongBinder());
2673 N--;
2674 }
2675 return l;
2676 }
2677
2678 /**
2679 * Read into the given List items String objects that were written with
2680 * {@link #writeStringList} at the current dataPosition().
2681 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 * @see #writeStringList
2683 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002684 public final void readStringList(@NonNull List<String> list) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 int M = list.size();
2686 int N = readInt();
2687 int i = 0;
2688 for (; i < M && i < N; i++) {
2689 list.set(i, readString());
2690 }
2691 for (; i<N; i++) {
2692 list.add(readString());
2693 }
2694 for (; i<M; i++) {
2695 list.remove(N);
2696 }
2697 }
2698
2699 /**
2700 * Read into the given List items IBinder objects that were written with
2701 * {@link #writeBinderList} at the current dataPosition().
2702 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 * @see #writeBinderList
2704 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002705 public final void readBinderList(@NonNull List<IBinder> list) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 int M = list.size();
2707 int N = readInt();
2708 int i = 0;
2709 for (; i < M && i < N; i++) {
2710 list.set(i, readStrongBinder());
2711 }
2712 for (; i<N; i++) {
2713 list.add(readStrongBinder());
2714 }
2715 for (; i<M; i++) {
2716 list.remove(N);
2717 }
2718 }
2719
2720 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00002721 * Read the list of {@code Parcelable} objects at the current data position into the
2722 * given {@code list}. The contents of the {@code list} are replaced. If the serialized
2723 * list was {@code null}, {@code list} is cleared.
2724 *
2725 * @see #writeParcelableList(List, int)
Narayan Kamathbea48712016-12-01 15:38:28 +00002726 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002727 @NonNull
2728 public final <T extends Parcelable> List<T> readParcelableList(@NonNull List<T> list,
2729 @Nullable ClassLoader cl) {
Narayan Kamathbea48712016-12-01 15:38:28 +00002730 final int N = readInt();
2731 if (N == -1) {
2732 list.clear();
Eugene Susla36e866b2017-02-23 18:24:39 -08002733 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002734 }
2735
2736 final int M = list.size();
2737 int i = 0;
2738 for (; i < M && i < N; i++) {
2739 list.set(i, (T) readParcelable(cl));
2740 }
2741 for (; i<N; i++) {
2742 list.add((T) readParcelable(cl));
2743 }
2744 for (; i<M; i++) {
2745 list.remove(N);
2746 }
Eugene Susla36e866b2017-02-23 18:24:39 -08002747 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002748 }
2749
2750 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 * Read and return a new array containing a particular object type from
2752 * the parcel at the current dataPosition(). Returns null if the
2753 * previously written array was null. The array <em>must</em> have
2754 * previously been written via {@link #writeTypedArray} with the same
2755 * object type.
2756 *
2757 * @return A newly created array containing objects with the same data
2758 * as those that were previously written.
2759 *
2760 * @see #writeTypedArray
2761 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002762 @Nullable
2763 public final <T> T[] createTypedArray(@NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 int N = readInt();
2765 if (N < 0) {
2766 return null;
2767 }
2768 T[] l = c.newArray(N);
2769 for (int i=0; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002770 l[i] = readTypedObject(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 }
2772 return l;
2773 }
2774
Jake Whartonb1f474c2018-05-30 15:11:13 -04002775 public final <T> void readTypedArray(@NonNull T[] val, @NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 int N = readInt();
2777 if (N == val.length) {
2778 for (int i=0; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002779 val[i] = readTypedObject(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 }
2781 } else {
2782 throw new RuntimeException("bad array lengths");
2783 }
2784 }
2785
2786 /**
2787 * @deprecated
2788 * @hide
2789 */
2790 @Deprecated
2791 public final <T> T[] readTypedArray(Parcelable.Creator<T> c) {
2792 return createTypedArray(c);
2793 }
2794
2795 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002796 * Read and return a typed Parcelable object from a parcel.
2797 * Returns null if the previous written object was null.
2798 * The object <em>must</em> have previous been written via
2799 * {@link #writeTypedObject} with the same object type.
2800 *
2801 * @return A newly created object of the type that was previously
2802 * written.
2803 *
2804 * @see #writeTypedObject
2805 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002806 @Nullable
2807 public final <T> T readTypedObject(@NonNull Parcelable.Creator<T> c) {
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002808 if (readInt() != 0) {
2809 return c.createFromParcel(this);
2810 } else {
2811 return null;
2812 }
2813 }
2814
2815 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 * Write a heterogeneous array of Parcelable objects into the Parcel.
2817 * Each object in the array is written along with its class name, so
2818 * that the correct class can later be instantiated. As a result, this
2819 * has significantly more overhead than {@link #writeTypedArray}, but will
2820 * correctly handle an array containing more than one type of object.
2821 *
2822 * @param value The array of objects to be written.
2823 * @param parcelableFlags Contextual flags as per
2824 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
2825 *
2826 * @see #writeTypedArray
2827 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002828 public final <T extends Parcelable> void writeParcelableArray(@Nullable T[] value,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 int parcelableFlags) {
2830 if (value != null) {
2831 int N = value.length;
2832 writeInt(N);
2833 for (int i=0; i<N; i++) {
2834 writeParcelable(value[i], parcelableFlags);
2835 }
2836 } else {
2837 writeInt(-1);
2838 }
2839 }
2840
2841 /**
2842 * Read a typed object from a parcel. The given class loader will be
2843 * used to load any enclosed Parcelables. If it is null, the default class
2844 * loader will be used.
2845 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002846 @Nullable
2847 public final Object readValue(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 int type = readInt();
2849
2850 switch (type) {
2851 case VAL_NULL:
2852 return null;
2853
2854 case VAL_STRING:
2855 return readString();
2856
2857 case VAL_INTEGER:
2858 return readInt();
2859
2860 case VAL_MAP:
2861 return readHashMap(loader);
2862
2863 case VAL_PARCELABLE:
2864 return readParcelable(loader);
2865
2866 case VAL_SHORT:
2867 return (short) readInt();
2868
2869 case VAL_LONG:
2870 return readLong();
2871
2872 case VAL_FLOAT:
2873 return readFloat();
2874
2875 case VAL_DOUBLE:
2876 return readDouble();
2877
2878 case VAL_BOOLEAN:
2879 return readInt() == 1;
2880
2881 case VAL_CHARSEQUENCE:
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002882 return readCharSequence();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883
2884 case VAL_LIST:
2885 return readArrayList(loader);
2886
2887 case VAL_BOOLEANARRAY:
Samuel Tana8036662015-11-23 14:36:00 -08002888 return createBooleanArray();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889
2890 case VAL_BYTEARRAY:
2891 return createByteArray();
2892
2893 case VAL_STRINGARRAY:
2894 return readStringArray();
2895
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002896 case VAL_CHARSEQUENCEARRAY:
2897 return readCharSequenceArray();
2898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 case VAL_IBINDER:
2900 return readStrongBinder();
2901
2902 case VAL_OBJECTARRAY:
2903 return readArray(loader);
2904
2905 case VAL_INTARRAY:
2906 return createIntArray();
2907
2908 case VAL_LONGARRAY:
2909 return createLongArray();
2910
2911 case VAL_BYTE:
2912 return readByte();
2913
2914 case VAL_SERIALIZABLE:
John Spurlock5002b8c2014-01-10 13:32:12 -05002915 return readSerializable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916
2917 case VAL_PARCELABLEARRAY:
2918 return readParcelableArray(loader);
2919
2920 case VAL_SPARSEARRAY:
2921 return readSparseArray(loader);
2922
2923 case VAL_SPARSEBOOLEANARRAY:
2924 return readSparseBooleanArray();
2925
2926 case VAL_BUNDLE:
2927 return readBundle(loader); // loading will be deferred
2928
Craig Mautner719e6b12014-04-04 20:29:41 -07002929 case VAL_PERSISTABLEBUNDLE:
2930 return readPersistableBundle(loader);
2931
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002932 case VAL_SIZE:
2933 return readSize();
2934
2935 case VAL_SIZEF:
2936 return readSizeF();
2937
Samuel Tana8036662015-11-23 14:36:00 -08002938 case VAL_DOUBLEARRAY:
2939 return createDoubleArray();
2940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 default:
2942 int off = dataPosition() - 4;
2943 throw new RuntimeException(
2944 "Parcel " + this + ": Unmarshalling unknown type code " + type + " at offset " + off);
2945 }
2946 }
2947
2948 /**
2949 * Read and return a new Parcelable from the parcel. The given class loader
2950 * will be used to load any enclosed Parcelables. If it is null, the default
2951 * class loader will be used.
2952 * @param loader A ClassLoader from which to instantiate the Parcelable
2953 * object, or null for the default class loader.
2954 * @return Returns the newly created Parcelable, or null if a null
2955 * object has been written.
2956 * @throws BadParcelableException Throws BadParcelableException if there
2957 * was an error trying to instantiate the Parcelable.
2958 */
Neil Fuller44e440c2015-04-20 14:39:00 +01002959 @SuppressWarnings("unchecked")
Jake Whartonb1f474c2018-05-30 15:11:13 -04002960 @Nullable
2961 public final <T extends Parcelable> T readParcelable(@Nullable ClassLoader loader) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002962 Parcelable.Creator<?> creator = readParcelableCreator(loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002963 if (creator == null) {
2964 return null;
2965 }
2966 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002967 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2968 (Parcelable.ClassLoaderCreator<?>) creator;
2969 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002970 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002971 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002972 }
2973
2974 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00002975 @UnsupportedAppUsage
Neil Fuller44e440c2015-04-20 14:39:00 +01002976 @SuppressWarnings("unchecked")
Jake Whartonb1f474c2018-05-30 15:11:13 -04002977 @Nullable
2978 public final <T extends Parcelable> T readCreator(@NonNull Parcelable.Creator<?> creator,
2979 @Nullable ClassLoader loader) {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002980 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002981 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2982 (Parcelable.ClassLoaderCreator<?>) creator;
2983 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002984 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002985 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002986 }
2987
2988 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00002989 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04002990 @Nullable
2991 public final Parcelable.Creator<?> readParcelableCreator(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992 String name = readString();
2993 if (name == null) {
2994 return null;
2995 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002996 Parcelable.Creator<?> creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 synchronized (mCreators) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002998 HashMap<String,Parcelable.Creator<?>> map = mCreators.get(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002999 if (map == null) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003000 map = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 mCreators.put(loader, map);
3002 }
3003 creator = map.get(name);
3004 if (creator == null) {
3005 try {
Neil Fuller44e440c2015-04-20 14:39:00 +01003006 // If loader == null, explicitly emulate Class.forName(String) "caller
3007 // classloader" behavior.
3008 ClassLoader parcelableClassLoader =
3009 (loader == null ? getClass().getClassLoader() : loader);
3010 // Avoid initializing the Parcelable class until we know it implements
3011 // Parcelable and has the necessary CREATOR field. http://b/1171613.
3012 Class<?> parcelableClass = Class.forName(name, false /* initialize */,
3013 parcelableClassLoader);
3014 if (!Parcelable.class.isAssignableFrom(parcelableClass)) {
Dianne Hackbornced54392018-02-26 13:07:42 -08003015 throw new BadParcelableException("Parcelable protocol requires subclassing "
3016 + "from Parcelable on class " + name);
Neil Fuller44e440c2015-04-20 14:39:00 +01003017 }
3018 Field f = parcelableClass.getField("CREATOR");
3019 if ((f.getModifiers() & Modifier.STATIC) == 0) {
3020 throw new BadParcelableException("Parcelable protocol requires "
3021 + "the CREATOR object to be static on class " + name);
3022 }
3023 Class<?> creatorType = f.getType();
3024 if (!Parcelable.Creator.class.isAssignableFrom(creatorType)) {
3025 // Fail before calling Field.get(), not after, to avoid initializing
3026 // parcelableClass unnecessarily.
3027 throw new BadParcelableException("Parcelable protocol requires a "
3028 + "Parcelable.Creator object called "
3029 + "CREATOR on class " + name);
3030 }
3031 creator = (Parcelable.Creator<?>) f.get(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 }
3033 catch (IllegalAccessException e) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003034 Log.e(TAG, "Illegal access when unmarshalling: " + name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 throw new BadParcelableException(
3036 "IllegalAccessException when unmarshalling: " + name);
3037 }
3038 catch (ClassNotFoundException e) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003039 Log.e(TAG, "Class not found when unmarshalling: " + name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 throw new BadParcelableException(
3041 "ClassNotFoundException when unmarshalling: " + name);
3042 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003043 catch (NoSuchFieldException e) {
3044 throw new BadParcelableException("Parcelable protocol requires a "
Neil Fuller44e440c2015-04-20 14:39:00 +01003045 + "Parcelable.Creator object called "
3046 + "CREATOR on class " + name);
Irfan Sheriff97a72f62013-01-11 10:45:51 -08003047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 if (creator == null) {
3049 throw new BadParcelableException("Parcelable protocol requires a "
Neil Fuller44e440c2015-04-20 14:39:00 +01003050 + "non-null Parcelable.Creator object called "
3051 + "CREATOR on class " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052 }
3053
3054 map.put(name, creator);
3055 }
3056 }
3057
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08003058 return creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 }
3060
3061 /**
3062 * Read and return a new Parcelable array from the parcel.
3063 * The given class loader will be used to load any enclosed
3064 * Parcelables.
3065 * @return the Parcelable array, or null if the array is null
3066 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003067 @Nullable
3068 public final Parcelable[] readParcelableArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 int N = readInt();
3070 if (N < 0) {
3071 return null;
3072 }
3073 Parcelable[] p = new Parcelable[N];
3074 for (int i = 0; i < N; i++) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003075 p[i] = readParcelable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 }
3077 return p;
3078 }
3079
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003080 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003081 @Nullable
3082 public final <T extends Parcelable> T[] readParcelableArray(@Nullable ClassLoader loader,
3083 @NonNull Class<T> clazz) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003084 int N = readInt();
3085 if (N < 0) {
3086 return null;
3087 }
3088 T[] p = (T[]) Array.newInstance(clazz, N);
3089 for (int i = 0; i < N; i++) {
3090 p[i] = readParcelable(loader);
3091 }
3092 return p;
3093 }
3094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003095 /**
3096 * Read and return a new Serializable object from the parcel.
3097 * @return the Serializable object, or null if the Serializable name
3098 * wasn't found in the parcel.
3099 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003100 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 public final Serializable readSerializable() {
John Spurlock5002b8c2014-01-10 13:32:12 -05003102 return readSerializable(null);
3103 }
3104
Jake Whartonb1f474c2018-05-30 15:11:13 -04003105 @Nullable
3106 private final Serializable readSerializable(@Nullable final ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 String name = readString();
3108 if (name == null) {
3109 // For some reason we were unable to read the name of the Serializable (either there
3110 // is nothing left in the Parcel to read, or the next value wasn't a String), so
3111 // return null, which indicates that the name wasn't found in the parcel.
3112 return null;
3113 }
3114
3115 byte[] serializedData = createByteArray();
3116 ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
3117 try {
John Spurlock5002b8c2014-01-10 13:32:12 -05003118 ObjectInputStream ois = new ObjectInputStream(bais) {
3119 @Override
3120 protected Class<?> resolveClass(ObjectStreamClass osClass)
3121 throws IOException, ClassNotFoundException {
3122 // try the custom classloader if provided
3123 if (loader != null) {
3124 Class<?> c = Class.forName(osClass.getName(), false, loader);
3125 if (c != null) {
3126 return c;
3127 }
3128 }
3129 return super.resolveClass(osClass);
3130 }
3131 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 return (Serializable) ois.readObject();
3133 } catch (IOException ioe) {
3134 throw new RuntimeException("Parcelable encountered " +
3135 "IOException reading a Serializable object (name = " + name +
3136 ")", ioe);
3137 } catch (ClassNotFoundException cnfe) {
John Spurlock5002b8c2014-01-10 13:32:12 -05003138 throw new RuntimeException("Parcelable encountered " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 "ClassNotFoundException reading a Serializable object (name = "
3140 + name + ")", cnfe);
3141 }
3142 }
3143
3144 // Cache of previously looked up CREATOR.createFromParcel() methods for
3145 // particular classes. Keys are the names of the classes, values are
3146 // Method objects.
Neil Fuller44e440c2015-04-20 14:39:00 +01003147 private static final HashMap<ClassLoader,HashMap<String,Parcelable.Creator<?>>>
3148 mCreators = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149
Narayan Kamathb34a4612014-01-23 14:17:11 +00003150 /** @hide for internal use only. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 static protected final Parcel obtain(int obj) {
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003152 throw new UnsupportedOperationException();
3153 }
3154
3155 /** @hide */
3156 static protected final Parcel obtain(long obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157 final Parcel[] pool = sHolderPool;
3158 synchronized (pool) {
3159 Parcel p;
3160 for (int i=0; i<POOL_SIZE; i++) {
3161 p = pool[i];
3162 if (p != null) {
3163 pool[i] = null;
3164 if (DEBUG_RECYCLE) {
3165 p.mStack = new RuntimeException();
3166 }
3167 p.init(obj);
3168 return p;
3169 }
3170 }
3171 }
3172 return new Parcel(obj);
3173 }
3174
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003175 private Parcel(long nativePtr) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 if (DEBUG_RECYCLE) {
3177 mStack = new RuntimeException();
3178 }
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003179 //Log.i(TAG, "Initializing obj=0x" + Integer.toHexString(obj), mStack);
Jeff Sharkey047238c2012-03-07 16:51:38 -08003180 init(nativePtr);
3181 }
3182
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003183 private void init(long nativePtr) {
Jeff Sharkey047238c2012-03-07 16:51:38 -08003184 if (nativePtr != 0) {
3185 mNativePtr = nativePtr;
3186 mOwnsNativeParcelObject = false;
3187 } else {
3188 mNativePtr = nativeCreate();
3189 mOwnsNativeParcelObject = true;
3190 }
3191 }
3192
3193 private void freeBuffer() {
3194 if (mOwnsNativeParcelObject) {
Adrian Roos04505652015-10-22 16:12:01 -07003195 updateNativeSize(nativeFreeBuffer(mNativePtr));
Jeff Sharkey047238c2012-03-07 16:51:38 -08003196 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -07003197 mReadWriteHelper = ReadWriteHelper.DEFAULT;
Jeff Sharkey047238c2012-03-07 16:51:38 -08003198 }
3199
3200 private void destroy() {
3201 if (mNativePtr != 0) {
3202 if (mOwnsNativeParcelObject) {
3203 nativeDestroy(mNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -07003204 updateNativeSize(0);
Jeff Sharkey047238c2012-03-07 16:51:38 -08003205 }
3206 mNativePtr = 0;
3207 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -07003208 mReadWriteHelper = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 }
3210
3211 @Override
3212 protected void finalize() throws Throwable {
3213 if (DEBUG_RECYCLE) {
3214 if (mStack != null) {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003215 Log.w(TAG, "Client did not call Parcel.recycle()", mStack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 }
3217 }
3218 destroy();
3219 }
3220
Jake Whartonb1f474c2018-05-30 15:11:13 -04003221 /* package */ void readMapInternal(@NonNull Map outVal, int N,
3222 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 while (N > 0) {
3224 Object key = readValue(loader);
3225 Object value = readValue(loader);
3226 outVal.put(key, value);
3227 N--;
3228 }
3229 }
3230
Jake Whartonb1f474c2018-05-30 15:11:13 -04003231 /* package */ void readArrayMapInternal(@NonNull ArrayMap outVal, int N,
3232 @Nullable ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003233 if (DEBUG_ARRAY_MAP) {
3234 RuntimeException here = new RuntimeException("here");
3235 here.fillInStackTrace();
3236 Log.d(TAG, "Reading " + N + " ArrayMap entries", here);
3237 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003238 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003239 while (N > 0) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003240 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003241 String key = readString();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003242 Object value = readValue(loader);
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003243 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " "
3244 + (dataPosition()-startPos) + " bytes: key=0x"
3245 + Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key);
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003246 outVal.append(key, value);
3247 N--;
3248 }
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003249 outVal.validate();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003250 }
3251
Jake Whartonb1f474c2018-05-30 15:11:13 -04003252 /* package */ void readArrayMapSafelyInternal(@NonNull ArrayMap outVal, int N,
3253 @Nullable ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003254 if (DEBUG_ARRAY_MAP) {
3255 RuntimeException here = new RuntimeException("here");
3256 here.fillInStackTrace();
3257 Log.d(TAG, "Reading safely " + N + " ArrayMap entries", here);
3258 }
3259 while (N > 0) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003260 String key = readString();
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003261 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read safe #" + (N-1) + ": key=0x"
3262 + (key != null ? key.hashCode() : 0) + " " + key);
3263 Object value = readValue(loader);
3264 outVal.put(key, value);
3265 N--;
3266 }
3267 }
3268
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003269 /**
3270 * @hide For testing only.
3271 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003272 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04003273 public void readArrayMap(@NonNull ArrayMap outVal, @Nullable ClassLoader loader) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003274 final int N = readInt();
3275 if (N < 0) {
3276 return;
3277 }
3278 readArrayMapInternal(outVal, N, loader);
3279 }
3280
Svet Ganovddb94882016-06-23 19:55:24 -07003281 /**
3282 * Reads an array set.
3283 *
3284 * @param loader The class loader to use.
3285 *
3286 * @hide
3287 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003288 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04003289 public @Nullable ArraySet<? extends Object> readArraySet(@Nullable ClassLoader loader) {
Svet Ganovddb94882016-06-23 19:55:24 -07003290 final int size = readInt();
3291 if (size < 0) {
3292 return null;
3293 }
3294 ArraySet<Object> result = new ArraySet<>(size);
3295 for (int i = 0; i < size; i++) {
3296 Object value = readValue(loader);
3297 result.append(value);
3298 }
3299 return result;
3300 }
3301
Jake Whartonb1f474c2018-05-30 15:11:13 -04003302 private void readListInternal(@NonNull List outVal, int N,
3303 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003304 while (N > 0) {
3305 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003306 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003307 outVal.add(value);
3308 N--;
3309 }
3310 }
3311
Jake Whartonb1f474c2018-05-30 15:11:13 -04003312 private void readArrayInternal(@NonNull Object[] outVal, int N,
3313 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003314 for (int i = 0; i < N; i++) {
3315 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003316 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 outVal[i] = value;
3318 }
3319 }
3320
Jake Whartonb1f474c2018-05-30 15:11:13 -04003321 private void readSparseArrayInternal(@NonNull SparseArray outVal, int N,
3322 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 while (N > 0) {
3324 int key = readInt();
3325 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003326 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 outVal.append(key, value);
3328 N--;
3329 }
3330 }
3331
3332
Jake Whartonb1f474c2018-05-30 15:11:13 -04003333 private void readSparseBooleanArrayInternal(@NonNull SparseBooleanArray outVal, int N) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334 while (N > 0) {
3335 int key = readInt();
3336 boolean value = this.readByte() == 1;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003337 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 outVal.append(key, value);
3339 N--;
3340 }
3341 }
Dan Sandler5ce04302015-04-09 23:50:15 -04003342
Jake Whartonb1f474c2018-05-30 15:11:13 -04003343 private void readSparseIntArrayInternal(@NonNull SparseIntArray outVal, int N) {
Adam Lesinski4e862812016-11-21 16:02:24 -08003344 while (N > 0) {
3345 int key = readInt();
3346 int value = readInt();
3347 outVal.append(key, value);
3348 N--;
3349 }
3350 }
3351
Dan Sandler5ce04302015-04-09 23:50:15 -04003352 /**
3353 * @hide For testing
3354 */
3355 public long getBlobAshmemSize() {
3356 return nativeGetBlobAshmemSize(mNativePtr);
3357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358}