blob: 7e7666adfeedff17f4bfcf0b30916cca3998258a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.os;
18
Jake Whartonb1f474c2018-05-30 15:11:13 -040019import android.annotation.NonNull;
Svet Ganovddb94882016-06-23 19:55:24 -070020import android.annotation.Nullable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.text.TextUtils;
Dianne Hackbornb87655b2013-07-17 19:06:22 -070022import android.util.ArrayMap;
Svet Ganovddb94882016-06-23 19:55:24 -070023import android.util.ArraySet;
Fyodor Kupolov3b946f42017-11-27 10:40:46 -080024import android.util.ExceptionUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.util.Log;
Jeff Sharkey5ef33982014-09-04 18:13:39 -070026import android.util.Size;
27import android.util.SizeF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.util.SparseArray;
29import android.util.SparseBooleanArray;
Adam Lesinski4e862812016-11-21 16:02:24 -080030import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
Makoto Onukib148b6c2017-06-27 13:38:38 -070032import dalvik.annotation.optimization.CriticalNative;
Jeff Sharkey789a8fc2017-04-16 13:18:35 -060033import dalvik.annotation.optimization.FastNative;
34import dalvik.system.VMRuntime;
35
Pete Gillin60f55a252018-05-10 15:40:32 +010036import libcore.util.ArrayUtils;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -070037import libcore.util.SneakyThrow;
38
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import java.io.ByteArrayInputStream;
40import java.io.ByteArrayOutputStream;
41import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import java.io.IOException;
43import java.io.ObjectInputStream;
44import java.io.ObjectOutputStream;
John Spurlock5002b8c2014-01-10 13:32:12 -050045import java.io.ObjectStreamClass;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import java.io.Serializable;
Makoto Onuki440a1ea2016-07-20 14:21:18 -070047import java.lang.reflect.Array;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import java.lang.reflect.Field;
Neil Fuller44e440c2015-04-20 14:39:00 +010049import java.lang.reflect.Modifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import java.util.ArrayList;
51import java.util.HashMap;
52import java.util.List;
53import java.util.Map;
54import java.util.Set;
Adrian Roos04505652015-10-22 16:12:01 -070055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056/**
57 * Container for a message (data and object references) that can
58 * be sent through an IBinder. A Parcel can contain both flattened data
59 * that will be unflattened on the other side of the IPC (using the various
60 * methods here for writing specific types, or the general
61 * {@link Parcelable} interface), and references to live {@link IBinder}
62 * objects that will result in the other side receiving a proxy IBinder
63 * connected with the original IBinder in the Parcel.
64 *
65 * <p class="note">Parcel is <strong>not</strong> a general-purpose
66 * serialization mechanism. This class (and the corresponding
67 * {@link Parcelable} API for placing arbitrary objects into a Parcel) is
68 * designed as a high-performance IPC transport. As such, it is not
69 * appropriate to place any Parcel data in to persistent storage: changes
70 * in the underlying implementation of any of the data in the Parcel can
71 * render older data unreadable.</p>
Samuel Tana8036662015-11-23 14:36:00 -080072 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 * <p>The bulk of the Parcel API revolves around reading and writing data
74 * of various types. There are six major classes of such functions available.</p>
Samuel Tana8036662015-11-23 14:36:00 -080075 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 * <h3>Primitives</h3>
Samuel Tana8036662015-11-23 14:36:00 -080077 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 * <p>The most basic data functions are for writing and reading primitive
79 * data types: {@link #writeByte}, {@link #readByte}, {@link #writeDouble},
80 * {@link #readDouble}, {@link #writeFloat}, {@link #readFloat}, {@link #writeInt},
81 * {@link #readInt}, {@link #writeLong}, {@link #readLong},
82 * {@link #writeString}, {@link #readString}. Most other
83 * data operations are built on top of these. The given data is written and
84 * read using the endianess of the host CPU.</p>
Samuel Tana8036662015-11-23 14:36:00 -080085 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 * <h3>Primitive Arrays</h3>
Samuel Tana8036662015-11-23 14:36:00 -080087 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 * <p>There are a variety of methods for reading and writing raw arrays
89 * of primitive objects, which generally result in writing a 4-byte length
90 * followed by the primitive data items. The methods for reading can either
91 * read the data into an existing array, or create and return a new array.
92 * These available types are:</p>
Samuel Tana8036662015-11-23 14:36:00 -080093 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * <ul>
95 * <li> {@link #writeBooleanArray(boolean[])},
96 * {@link #readBooleanArray(boolean[])}, {@link #createBooleanArray()}
97 * <li> {@link #writeByteArray(byte[])},
98 * {@link #writeByteArray(byte[], int, int)}, {@link #readByteArray(byte[])},
99 * {@link #createByteArray()}
100 * <li> {@link #writeCharArray(char[])}, {@link #readCharArray(char[])},
101 * {@link #createCharArray()}
102 * <li> {@link #writeDoubleArray(double[])}, {@link #readDoubleArray(double[])},
103 * {@link #createDoubleArray()}
104 * <li> {@link #writeFloatArray(float[])}, {@link #readFloatArray(float[])},
105 * {@link #createFloatArray()}
106 * <li> {@link #writeIntArray(int[])}, {@link #readIntArray(int[])},
107 * {@link #createIntArray()}
108 * <li> {@link #writeLongArray(long[])}, {@link #readLongArray(long[])},
109 * {@link #createLongArray()}
110 * <li> {@link #writeStringArray(String[])}, {@link #readStringArray(String[])},
111 * {@link #createStringArray()}.
112 * <li> {@link #writeSparseBooleanArray(SparseBooleanArray)},
113 * {@link #readSparseBooleanArray()}.
114 * </ul>
Samuel Tana8036662015-11-23 14:36:00 -0800115 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 * <h3>Parcelables</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800117 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 * <p>The {@link Parcelable} protocol provides an extremely efficient (but
119 * low-level) protocol for objects to write and read themselves from Parcels.
120 * You can use the direct methods {@link #writeParcelable(Parcelable, int)}
121 * and {@link #readParcelable(ClassLoader)} or
122 * {@link #writeParcelableArray} and
123 * {@link #readParcelableArray(ClassLoader)} to write or read. These
124 * methods write both the class type and its data to the Parcel, allowing
125 * that class to be reconstructed from the appropriate class loader when
126 * later reading.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800127 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 * <p>There are also some methods that provide a more efficient way to work
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -0700129 * with Parcelables: {@link #writeTypedObject}, {@link #writeTypedArray},
130 * {@link #writeTypedList}, {@link #readTypedObject},
131 * {@link #createTypedArray} and {@link #createTypedArrayList}. These methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 * do not write the class information of the original object: instead, the
133 * caller of the read function must know what type to expect and pass in the
134 * appropriate {@link Parcelable.Creator Parcelable.Creator} instead to
135 * properly construct the new object and read its data. (To more efficient
Bin Chenb6b12b52017-07-11 11:01:44 +0800136 * write and read a single Parcelable object that is not null, you can directly
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -0700137 * call {@link Parcelable#writeToParcel Parcelable.writeToParcel} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * {@link Parcelable.Creator#createFromParcel Parcelable.Creator.createFromParcel}
139 * yourself.)</p>
Samuel Tana8036662015-11-23 14:36:00 -0800140 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 * <h3>Bundles</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800142 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 * <p>A special type-safe container, called {@link Bundle}, is available
144 * for key/value maps of heterogeneous values. This has many optimizations
145 * for improved performance when reading and writing data, and its type-safe
146 * API avoids difficult to debug type errors when finally marshalling the
147 * data contents into a Parcel. The methods to use are
148 * {@link #writeBundle(Bundle)}, {@link #readBundle()}, and
149 * {@link #readBundle(ClassLoader)}.
Samuel Tana8036662015-11-23 14:36:00 -0800150 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 * <h3>Active Objects</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800152 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 * <p>An unusual feature of Parcel is the ability to read and write active
154 * objects. For these objects the actual contents of the object is not
155 * written, rather a special token referencing the object is written. When
156 * reading the object back from the Parcel, you do not get a new instance of
157 * the object, but rather a handle that operates on the exact same object that
158 * was originally written. There are two forms of active objects available.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800159 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 * <p>{@link Binder} objects are a core facility of Android's general cross-process
161 * communication system. The {@link IBinder} interface describes an abstract
162 * protocol with a Binder object. Any such interface can be written in to
163 * a Parcel, and upon reading you will receive either the original object
164 * implementing that interface or a special proxy implementation
165 * that communicates calls back to the original object. The methods to use are
166 * {@link #writeStrongBinder(IBinder)},
167 * {@link #writeStrongInterface(IInterface)}, {@link #readStrongBinder()},
168 * {@link #writeBinderArray(IBinder[])}, {@link #readBinderArray(IBinder[])},
169 * {@link #createBinderArray()},
170 * {@link #writeBinderList(List)}, {@link #readBinderList(List)},
171 * {@link #createBinderArrayList()}.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800172 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 * <p>FileDescriptor objects, representing raw Linux file descriptor identifiers,
174 * can be written and {@link ParcelFileDescriptor} objects returned to operate
175 * on the original file descriptor. The returned file descriptor is a dup
176 * of the original file descriptor: the object and fd is different, but
177 * operating on the same underlying file stream, with the same position, etc.
178 * The methods to use are {@link #writeFileDescriptor(FileDescriptor)},
179 * {@link #readFileDescriptor()}.
Samuel Tana8036662015-11-23 14:36:00 -0800180 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <h3>Untyped Containers</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800182 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 * <p>A final class of methods are for writing and reading standard Java
184 * containers of arbitrary types. These all revolve around the
185 * {@link #writeValue(Object)} and {@link #readValue(ClassLoader)} methods
186 * which define the types of objects allowed. The container methods are
187 * {@link #writeArray(Object[])}, {@link #readArray(ClassLoader)},
188 * {@link #writeList(List)}, {@link #readList(List, ClassLoader)},
189 * {@link #readArrayList(ClassLoader)},
190 * {@link #writeMap(Map)}, {@link #readMap(Map, ClassLoader)},
191 * {@link #writeSparseArray(SparseArray)},
192 * {@link #readSparseArray(ClassLoader)}.
193 */
194public final class Parcel {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -0800195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 private static final boolean DEBUG_RECYCLE = false;
Dianne Hackborne784d1e2013-09-20 18:13:52 -0700197 private static final boolean DEBUG_ARRAY_MAP = false;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700198 private static final String TAG = "Parcel";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199
200 @SuppressWarnings({"UnusedDeclaration"})
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000201 private long mNativePtr; // used by native code
Jeff Sharkey047238c2012-03-07 16:51:38 -0800202
203 /**
204 * Flag indicating if {@link #mNativePtr} was allocated by this object,
205 * indicating that we're responsible for its lifecycle.
206 */
207 private boolean mOwnsNativeParcelObject;
Adrian Roos04505652015-10-22 16:12:01 -0700208 private long mNativeSize;
Jeff Sharkey047238c2012-03-07 16:51:38 -0800209
Dianne Hackborn98305522017-05-05 17:53:53 -0700210 private ArrayMap<Class, Object> mClassCookies;
211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 private RuntimeException mStack;
213
Fyodor Kupolova81b8c02017-11-13 15:51:03 -0800214 /**
215 * Whether or not to parcel the stack trace of an exception. This has a performance
216 * impact, so should only be included in specific processes and only on debug builds.
217 */
218 private static boolean sParcelExceptionStackTrace;
219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 private static final int POOL_SIZE = 6;
221 private static final Parcel[] sOwnedPool = new Parcel[POOL_SIZE];
222 private static final Parcel[] sHolderPool = new Parcel[POOL_SIZE];
223
Robert Quattlebaum9cd7af32017-01-04 13:28:01 -0800224 // Keep in sync with frameworks/native/include/private/binder/ParcelValTypes.h.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 private static final int VAL_NULL = -1;
226 private static final int VAL_STRING = 0;
227 private static final int VAL_INTEGER = 1;
228 private static final int VAL_MAP = 2;
229 private static final int VAL_BUNDLE = 3;
230 private static final int VAL_PARCELABLE = 4;
231 private static final int VAL_SHORT = 5;
232 private static final int VAL_LONG = 6;
233 private static final int VAL_FLOAT = 7;
234 private static final int VAL_DOUBLE = 8;
235 private static final int VAL_BOOLEAN = 9;
236 private static final int VAL_CHARSEQUENCE = 10;
237 private static final int VAL_LIST = 11;
238 private static final int VAL_SPARSEARRAY = 12;
239 private static final int VAL_BYTEARRAY = 13;
240 private static final int VAL_STRINGARRAY = 14;
241 private static final int VAL_IBINDER = 15;
242 private static final int VAL_PARCELABLEARRAY = 16;
243 private static final int VAL_OBJECTARRAY = 17;
244 private static final int VAL_INTARRAY = 18;
245 private static final int VAL_LONGARRAY = 19;
246 private static final int VAL_BYTE = 20;
247 private static final int VAL_SERIALIZABLE = 21;
248 private static final int VAL_SPARSEBOOLEANARRAY = 22;
249 private static final int VAL_BOOLEANARRAY = 23;
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000250 private static final int VAL_CHARSEQUENCEARRAY = 24;
Craig Mautner719e6b12014-04-04 20:29:41 -0700251 private static final int VAL_PERSISTABLEBUNDLE = 25;
Jeff Sharkey5ef33982014-09-04 18:13:39 -0700252 private static final int VAL_SIZE = 26;
253 private static final int VAL_SIZEF = 27;
Samuel Tana8036662015-11-23 14:36:00 -0800254 private static final int VAL_DOUBLEARRAY = 28;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700256 // The initial int32 in a Binder call's reply Parcel header:
Christopher Wiley80fd1202015-11-22 17:12:37 -0800257 // Keep these in sync with libbinder's binder/Status.h.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 private static final int EX_SECURITY = -1;
259 private static final int EX_BAD_PARCELABLE = -2;
260 private static final int EX_ILLEGAL_ARGUMENT = -3;
261 private static final int EX_NULL_POINTER = -4;
262 private static final int EX_ILLEGAL_STATE = -5;
Dianne Hackborn7e714422013-09-13 17:32:57 -0700263 private static final int EX_NETWORK_MAIN_THREAD = -6;
Dianne Hackborn33d738a2014-09-12 14:23:58 -0700264 private static final int EX_UNSUPPORTED_OPERATION = -7;
Christopher Wiley80fd1202015-11-22 17:12:37 -0800265 private static final int EX_SERVICE_SPECIFIC = -8;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -0700266 private static final int EX_PARCELABLE = -9;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700267 private static final int EX_HAS_REPLY_HEADER = -128; // special; see below
Christopher Wiley80fd1202015-11-22 17:12:37 -0800268 // EX_TRANSACTION_FAILED is used exclusively in native code.
269 // see libbinder's binder/Status.h
270 private static final int EX_TRANSACTION_FAILED = -129;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700271
Makoto Onukib148b6c2017-06-27 13:38:38 -0700272 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000273 private static native int nativeDataSize(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700274 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000275 private static native int nativeDataAvail(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700276 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000277 private static native int nativeDataPosition(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700278 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000279 private static native int nativeDataCapacity(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700280 @FastNative
Adrian Roos04505652015-10-22 16:12:01 -0700281 private static native long nativeSetDataSize(long nativePtr, int size);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700282 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000283 private static native void nativeSetDataPosition(long nativePtr, int pos);
John Reck71207b52016-09-28 13:28:09 -0700284 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000285 private static native void nativeSetDataCapacity(long nativePtr, int size);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800286
Makoto Onukib148b6c2017-06-27 13:38:38 -0700287 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000288 private static native boolean nativePushAllowFds(long nativePtr, boolean allowFds);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700289 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000290 private static native void nativeRestoreAllowFds(long nativePtr, boolean lastValue);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800291
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000292 private static native void nativeWriteByteArray(long nativePtr, byte[] b, int offset, int len);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700293 private static native void nativeWriteBlob(long nativePtr, byte[] b, int offset, int len);
John Reck71207b52016-09-28 13:28:09 -0700294 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000295 private static native void nativeWriteInt(long nativePtr, int val);
John Reck71207b52016-09-28 13:28:09 -0700296 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000297 private static native void nativeWriteLong(long nativePtr, long val);
John Reck71207b52016-09-28 13:28:09 -0700298 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000299 private static native void nativeWriteFloat(long nativePtr, float val);
John Reck71207b52016-09-28 13:28:09 -0700300 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000301 private static native void nativeWriteDouble(long nativePtr, double val);
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700302 static native void nativeWriteString(long nativePtr, String val);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000303 private static native void nativeWriteStrongBinder(long nativePtr, IBinder val);
Adrian Roos04505652015-10-22 16:12:01 -0700304 private static native long nativeWriteFileDescriptor(long nativePtr, FileDescriptor val);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800305
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000306 private static native byte[] nativeCreateByteArray(long nativePtr);
Jocelyn Dang46100442017-05-05 15:40:49 -0700307 private static native boolean nativeReadByteArray(long nativePtr, byte[] dest, int destLen);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700308 private static native byte[] nativeReadBlob(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700309 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000310 private static native int nativeReadInt(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700311 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000312 private static native long nativeReadLong(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700313 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000314 private static native float nativeReadFloat(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700315 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000316 private static native double nativeReadDouble(long nativePtr);
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700317 static native String nativeReadString(long nativePtr);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000318 private static native IBinder nativeReadStrongBinder(long nativePtr);
319 private static native FileDescriptor nativeReadFileDescriptor(long nativePtr);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800320
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000321 private static native long nativeCreate();
Adrian Roos04505652015-10-22 16:12:01 -0700322 private static native long nativeFreeBuffer(long nativePtr);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000323 private static native void nativeDestroy(long nativePtr);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800324
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000325 private static native byte[] nativeMarshall(long nativePtr);
Adrian Roos04505652015-10-22 16:12:01 -0700326 private static native long nativeUnmarshall(
John Spurlocke0852362015-02-04 15:47:40 -0500327 long nativePtr, byte[] data, int offset, int length);
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700328 private static native int nativeCompareData(long thisNativePtr, long otherNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -0700329 private static native long nativeAppendFrom(
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000330 long thisNativePtr, long otherNativePtr, int offset, int length);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700331 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000332 private static native boolean nativeHasFileDescriptors(long nativePtr);
333 private static native void nativeWriteInterfaceToken(long nativePtr, String interfaceName);
334 private static native void nativeEnforceInterface(long nativePtr, String interfaceName);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800335
Fyodor Kupolova81b8c02017-11-13 15:51:03 -0800336 /** Last time exception with a stack trace was written */
337 private static volatile long sLastWriteExceptionStackTrace;
338 /** Used for throttling of writing stack trace, which is costly */
339 private static final int WRITE_EXCEPTION_STACK_TRACE_THRESHOLD_MS = 1000;
340
Makoto Onukib148b6c2017-06-27 13:38:38 -0700341 @CriticalNative
Dan Sandleraa861662015-04-21 10:24:32 -0400342 private static native long nativeGetBlobAshmemSize(long nativePtr);
Dan Sandler5ce04302015-04-09 23:50:15 -0400343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 public final static Parcelable.Creator<String> STRING_CREATOR
345 = new Parcelable.Creator<String>() {
346 public String createFromParcel(Parcel source) {
347 return source.readString();
348 }
349 public String[] newArray(int size) {
350 return new String[size];
351 }
352 };
353
354 /**
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700355 * @hide
356 */
357 public static class ReadWriteHelper {
358 public static final ReadWriteHelper DEFAULT = new ReadWriteHelper();
359
360 /**
361 * Called when writing a string to a parcel. Subclasses wanting to write a string
362 * must use {@link #writeStringNoHelper(String)} to avoid
363 * infinity recursive calls.
364 */
365 public void writeString(Parcel p, String s) {
366 nativeWriteString(p.mNativePtr, s);
367 }
368
369 /**
370 * Called when reading a string to a parcel. Subclasses wanting to read a string
371 * must use {@link #readStringNoHelper()} to avoid
372 * infinity recursive calls.
373 */
374 public String readString(Parcel p) {
375 return nativeReadString(p.mNativePtr);
376 }
377 }
378
379 private ReadWriteHelper mReadWriteHelper = ReadWriteHelper.DEFAULT;
380
381 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 * Retrieve a new Parcel object from the pool.
383 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400384 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 public static Parcel obtain() {
386 final Parcel[] pool = sOwnedPool;
387 synchronized (pool) {
388 Parcel p;
389 for (int i=0; i<POOL_SIZE; i++) {
390 p = pool[i];
391 if (p != null) {
392 pool[i] = null;
393 if (DEBUG_RECYCLE) {
394 p.mStack = new RuntimeException();
395 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700396 p.mReadWriteHelper = ReadWriteHelper.DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 return p;
398 }
399 }
400 }
401 return new Parcel(0);
402 }
403
404 /**
405 * Put a Parcel object back into the pool. You must not touch
406 * the object after this call.
407 */
408 public final void recycle() {
409 if (DEBUG_RECYCLE) mStack = null;
410 freeBuffer();
Jeff Sharkey047238c2012-03-07 16:51:38 -0800411
412 final Parcel[] pool;
413 if (mOwnsNativeParcelObject) {
414 pool = sOwnedPool;
415 } else {
416 mNativePtr = 0;
417 pool = sHolderPool;
418 }
419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 synchronized (pool) {
421 for (int i=0; i<POOL_SIZE; i++) {
422 if (pool[i] == null) {
423 pool[i] = this;
424 return;
425 }
426 }
427 }
428 }
429
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700430 /**
431 * Set a {@link ReadWriteHelper}, which can be used to avoid having duplicate strings, for
432 * example.
433 *
434 * @hide
435 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400436 public void setReadWriteHelper(@Nullable ReadWriteHelper helper) {
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700437 mReadWriteHelper = helper != null ? helper : ReadWriteHelper.DEFAULT;
438 }
439
440 /**
441 * @return whether this parcel has a {@link ReadWriteHelper}.
442 *
443 * @hide
444 */
445 public boolean hasReadWriteHelper() {
446 return (mReadWriteHelper != null) && (mReadWriteHelper != ReadWriteHelper.DEFAULT);
447 }
448
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800449 /** @hide */
450 public static native long getGlobalAllocSize();
451
452 /** @hide */
453 public static native long getGlobalAllocCount();
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 /**
456 * Returns the total amount of data contained in the parcel.
457 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800458 public final int dataSize() {
459 return nativeDataSize(mNativePtr);
460 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461
462 /**
463 * Returns the amount of data remaining to be read from the
464 * parcel. That is, {@link #dataSize}-{@link #dataPosition}.
465 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800466 public final int dataAvail() {
467 return nativeDataAvail(mNativePtr);
468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469
470 /**
471 * Returns the current position in the parcel data. Never
472 * more than {@link #dataSize}.
473 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800474 public final int dataPosition() {
475 return nativeDataPosition(mNativePtr);
476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477
478 /**
479 * Returns the total amount of space in the parcel. This is always
480 * >= {@link #dataSize}. The difference between it and dataSize() is the
481 * amount of room left until the parcel needs to re-allocate its
482 * data buffer.
483 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800484 public final int dataCapacity() {
485 return nativeDataCapacity(mNativePtr);
486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487
488 /**
489 * Change the amount of data in the parcel. Can be either smaller or
490 * larger than the current size. If larger than the current capacity,
491 * more memory will be allocated.
492 *
493 * @param size The new number of bytes in the Parcel.
494 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800495 public final void setDataSize(int size) {
Michael Wachenschwanz138bebf2017-05-18 22:09:18 +0000496 updateNativeSize(nativeSetDataSize(mNativePtr, size));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800497 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498
499 /**
500 * Move the current read/write position in the parcel.
501 * @param pos New offset in the parcel; must be between 0 and
502 * {@link #dataSize}.
503 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800504 public final void setDataPosition(int pos) {
505 nativeSetDataPosition(mNativePtr, pos);
506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507
508 /**
509 * Change the capacity (current available space) of the parcel.
510 *
511 * @param size The new capacity of the parcel, in bytes. Can not be
512 * less than {@link #dataSize} -- that is, you can not drop existing data
513 * with this method.
514 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800515 public final void setDataCapacity(int size) {
516 nativeSetDataCapacity(mNativePtr, size);
517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400519 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800520 public final boolean pushAllowFds(boolean allowFds) {
521 return nativePushAllowFds(mNativePtr, allowFds);
522 }
Dianne Hackbornc04db7e2011-10-03 21:09:35 -0700523
524 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800525 public final void restoreAllowFds(boolean lastValue) {
526 nativeRestoreAllowFds(mNativePtr, lastValue);
527 }
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 /**
530 * Returns the raw bytes of the parcel.
531 *
532 * <p class="note">The data you retrieve here <strong>must not</strong>
533 * be placed in any kind of persistent storage (on local disk, across
534 * a network, etc). For that, you should use standard serialization
535 * or another kind of general serialization mechanism. The Parcel
536 * marshalled representation is highly optimized for local IPC, and as
537 * such does not attempt to maintain compatibility with data created
538 * in different versions of the platform.
539 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800540 public final byte[] marshall() {
541 return nativeMarshall(mNativePtr);
542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543
544 /**
545 * Set the bytes in data to be the raw bytes of this Parcel.
546 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400547 public final void unmarshall(@NonNull byte[] data, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700548 updateNativeSize(nativeUnmarshall(mNativePtr, data, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550
Jeff Sharkey047238c2012-03-07 16:51:38 -0800551 public final void appendFrom(Parcel parcel, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700552 updateNativeSize(nativeAppendFrom(mNativePtr, parcel.mNativePtr, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700555 /** @hide */
556 public final int compareData(Parcel other) {
557 return nativeCompareData(mNativePtr, other.mNativePtr);
558 }
559
Dianne Hackborn98305522017-05-05 17:53:53 -0700560 /** @hide */
561 public final void setClassCookie(Class clz, Object cookie) {
562 if (mClassCookies == null) {
563 mClassCookies = new ArrayMap<>();
564 }
565 mClassCookies.put(clz, cookie);
566 }
567
568 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400569 @Nullable
Dianne Hackborn98305522017-05-05 17:53:53 -0700570 public final Object getClassCookie(Class clz) {
571 return mClassCookies != null ? mClassCookies.get(clz) : null;
572 }
573
574 /** @hide */
575 public final void adoptClassCookies(Parcel from) {
576 mClassCookies = from.mClassCookies;
577 }
578
Adrian Roosfb921842017-10-26 14:49:56 +0200579 /** @hide */
580 public Map<Class, Object> copyClassCookies() {
581 return new ArrayMap<>(mClassCookies);
582 }
583
584 /** @hide */
585 public void putClassCookies(Map<Class, Object> cookies) {
586 if (cookies == null) {
587 return;
588 }
589 if (mClassCookies == null) {
590 mClassCookies = new ArrayMap<>();
591 }
592 mClassCookies.putAll(cookies);
593 }
594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 /**
596 * Report whether the parcel contains any marshalled file descriptors.
597 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800598 public final boolean hasFileDescriptors() {
599 return nativeHasFileDescriptors(mNativePtr);
600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601
602 /**
603 * Store or read an IBinder interface token in the parcel at the current
604 * {@link #dataPosition}. This is used to validate that the marshalled
605 * transaction is intended for the target interface.
606 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800607 public final void writeInterfaceToken(String interfaceName) {
608 nativeWriteInterfaceToken(mNativePtr, interfaceName);
609 }
610
611 public final void enforceInterface(String interfaceName) {
612 nativeEnforceInterface(mNativePtr, interfaceName);
613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614
615 /**
Elliott Hughesa28b83e2011-02-28 14:26:13 -0800616 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 * growing {@link #dataCapacity} if needed.
618 * @param b Bytes to place into the parcel.
619 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400620 public final void writeByteArray(@Nullable byte[] b) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 writeByteArray(b, 0, (b != null) ? b.length : 0);
622 }
623
624 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900625 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 * growing {@link #dataCapacity} if needed.
627 * @param b Bytes to place into the parcel.
628 * @param offset Index of first byte to be written.
629 * @param len Number of bytes to write.
630 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400631 public final void writeByteArray(@Nullable byte[] b, int offset, int len) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 if (b == null) {
633 writeInt(-1);
634 return;
635 }
Pete Gillin60f55a252018-05-10 15:40:32 +0100636 ArrayUtils.throwsIfOutOfBounds(b.length, offset, len);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800637 nativeWriteByteArray(mNativePtr, b, offset, len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 }
639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700641 * Write a blob of data into the parcel at the current {@link #dataPosition},
642 * growing {@link #dataCapacity} if needed.
643 * @param b Bytes to place into the parcel.
644 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -0700645 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700646 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400647 public final void writeBlob(@Nullable byte[] b) {
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500648 writeBlob(b, 0, (b != null) ? b.length : 0);
649 }
650
651 /**
652 * Write a blob of data into the parcel at the current {@link #dataPosition},
653 * growing {@link #dataCapacity} if needed.
654 * @param b Bytes to place into the parcel.
655 * @param offset Index of first byte to be written.
656 * @param len Number of bytes to write.
657 * {@hide}
658 * {@SystemApi}
659 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400660 public final void writeBlob(@Nullable byte[] b, int offset, int len) {
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500661 if (b == null) {
662 writeInt(-1);
663 return;
664 }
Pete Gillin60f55a252018-05-10 15:40:32 +0100665 ArrayUtils.throwsIfOutOfBounds(b.length, offset, len);
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500666 nativeWriteBlob(mNativePtr, b, offset, len);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700667 }
668
669 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 * Write an integer value into the parcel at the current dataPosition(),
671 * growing dataCapacity() if needed.
672 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800673 public final void writeInt(int val) {
674 nativeWriteInt(mNativePtr, val);
675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676
677 /**
678 * Write a long integer value into the parcel at the current dataPosition(),
679 * growing dataCapacity() if needed.
680 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800681 public final void writeLong(long val) {
682 nativeWriteLong(mNativePtr, val);
683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684
685 /**
686 * Write a floating point value into the parcel at the current
687 * dataPosition(), growing dataCapacity() if needed.
688 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800689 public final void writeFloat(float val) {
690 nativeWriteFloat(mNativePtr, val);
691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692
693 /**
694 * Write a double precision floating point value into the parcel at the
695 * current dataPosition(), growing dataCapacity() if needed.
696 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800697 public final void writeDouble(double val) {
698 nativeWriteDouble(mNativePtr, val);
699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700
701 /**
702 * Write a string value into the parcel at the current dataPosition(),
703 * growing dataCapacity() if needed.
704 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400705 public final void writeString(@Nullable String val) {
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700706 mReadWriteHelper.writeString(this, val);
707 }
708
709 /**
710 * Write a string without going though a {@link ReadWriteHelper}. Subclasses of
711 * {@link ReadWriteHelper} must use this method instead of {@link #writeString} to avoid
712 * infinity recursive calls.
713 *
714 * @hide
715 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400716 public void writeStringNoHelper(@Nullable String val) {
Jeff Sharkey047238c2012-03-07 16:51:38 -0800717 nativeWriteString(mNativePtr, val);
718 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719
Jake Wharton2ffa4312018-04-23 17:47:14 -0400720 /**
721 * Write a boolean value into the parcel at the current dataPosition(),
722 * growing dataCapacity() if needed.
723 *
724 * <p>Note: This method currently delegates to writeInt with a value of 1 or 0
725 * for true or false, respectively, but may change in the future.
726 */
Eugene Susla36e866b2017-02-23 18:24:39 -0800727 public final void writeBoolean(boolean val) {
728 writeInt(val ? 1 : 0);
729 }
730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000732 * Write a CharSequence value into the parcel at the current dataPosition(),
733 * growing dataCapacity() if needed.
734 * @hide
735 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400736 public final void writeCharSequence(@Nullable CharSequence val) {
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000737 TextUtils.writeToParcel(val, this, 0);
738 }
739
740 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 * Write an object into the parcel at the current dataPosition(),
742 * growing dataCapacity() if needed.
743 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800744 public final void writeStrongBinder(IBinder val) {
745 nativeWriteStrongBinder(mNativePtr, val);
746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747
748 /**
749 * Write an object into the parcel at the current dataPosition(),
750 * growing dataCapacity() if needed.
751 */
752 public final void writeStrongInterface(IInterface val) {
753 writeStrongBinder(val == null ? null : val.asBinder());
754 }
755
756 /**
757 * Write a FileDescriptor into the parcel at the current dataPosition(),
758 * growing dataCapacity() if needed.
Dan Egnorb3e4ef32010-07-20 09:03:35 -0700759 *
760 * <p class="caution">The file descriptor will not be closed, which may
761 * result in file descriptor leaks when objects are returned from Binder
762 * calls. Use {@link ParcelFileDescriptor#writeToParcel} instead, which
763 * accepts contextual flags and will close the original file descriptor
764 * if {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} is set.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400766 public final void writeFileDescriptor(@NonNull FileDescriptor val) {
Adrian Roos04505652015-10-22 16:12:01 -0700767 updateNativeSize(nativeWriteFileDescriptor(mNativePtr, val));
768 }
769
770 private void updateNativeSize(long newNativeSize) {
771 if (mOwnsNativeParcelObject) {
772 if (newNativeSize > Integer.MAX_VALUE) {
773 newNativeSize = Integer.MAX_VALUE;
774 }
775 if (newNativeSize != mNativeSize) {
776 int delta = (int) (newNativeSize - mNativeSize);
777 if (delta > 0) {
778 VMRuntime.getRuntime().registerNativeAllocation(delta);
779 } else {
780 VMRuntime.getRuntime().registerNativeFree(-delta);
781 }
782 mNativeSize = newNativeSize;
783 }
784 }
Jeff Sharkey047238c2012-03-07 16:51:38 -0800785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786
787 /**
Casey Dahlin2f974b22015-11-05 12:19:13 -0800788 * {@hide}
789 * This will be the new name for writeFileDescriptor, for consistency.
790 **/
Jake Whartonb1f474c2018-05-30 15:11:13 -0400791 public final void writeRawFileDescriptor(@NonNull FileDescriptor val) {
Casey Dahlin2f974b22015-11-05 12:19:13 -0800792 nativeWriteFileDescriptor(mNativePtr, val);
793 }
794
795 /**
796 * {@hide}
797 * Write an array of FileDescriptor objects into the Parcel.
798 *
799 * @param value The array of objects to be written.
800 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400801 public final void writeRawFileDescriptorArray(@Nullable FileDescriptor[] value) {
Casey Dahlin2f974b22015-11-05 12:19:13 -0800802 if (value != null) {
803 int N = value.length;
804 writeInt(N);
805 for (int i=0; i<N; i++) {
806 writeRawFileDescriptor(value[i]);
807 }
808 } else {
809 writeInt(-1);
810 }
811 }
812
813 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900814 * Write a byte value into the parcel at the current dataPosition(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 * growing dataCapacity() if needed.
Jake Wharton2ffa4312018-04-23 17:47:14 -0400816 *
817 * <p>Note: This method currently delegates to writeInt but may change in
818 * the future.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 */
820 public final void writeByte(byte val) {
821 writeInt(val);
822 }
823
824 /**
825 * Please use {@link #writeBundle} instead. Flattens a Map into the parcel
826 * at the current dataPosition(),
827 * growing dataCapacity() if needed. The Map keys must be String objects.
828 * The Map values are written using {@link #writeValue} and must follow
829 * the specification there.
Samuel Tana8036662015-11-23 14:36:00 -0800830 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 * <p>It is strongly recommended to use {@link #writeBundle} instead of
832 * this method, since the Bundle class provides a type-safe API that
833 * allows you to avoid mysterious type errors at the point of marshalling.
834 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400835 public final void writeMap(@Nullable Map val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700836 writeMapInternal((Map<String, Object>) val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 }
838
839 /**
840 * Flatten a Map into the parcel at the current dataPosition(),
841 * growing dataCapacity() if needed. The Map keys must be String objects.
842 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400843 /* package */ void writeMapInternal(@Nullable Map<String,Object> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 if (val == null) {
845 writeInt(-1);
846 return;
847 }
848 Set<Map.Entry<String,Object>> entries = val.entrySet();
849 writeInt(entries.size());
850 for (Map.Entry<String,Object> e : entries) {
851 writeValue(e.getKey());
852 writeValue(e.getValue());
853 }
854 }
855
856 /**
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700857 * Flatten an ArrayMap into the parcel at the current dataPosition(),
858 * growing dataCapacity() if needed. The Map keys must be String objects.
859 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400860 /* package */ void writeArrayMapInternal(@Nullable ArrayMap<String, Object> val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700861 if (val == null) {
862 writeInt(-1);
863 return;
864 }
Samuel Tan3cefe6a2015-12-14 13:29:17 -0800865 // Keep the format of this Parcel in sync with writeToParcelInner() in
866 // frameworks/native/libs/binder/PersistableBundle.cpp.
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700867 final int N = val.size();
868 writeInt(N);
Dianne Hackborne784d1e2013-09-20 18:13:52 -0700869 if (DEBUG_ARRAY_MAP) {
870 RuntimeException here = new RuntimeException("here");
871 here.fillInStackTrace();
872 Log.d(TAG, "Writing " + N + " ArrayMap entries", here);
873 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700874 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700875 for (int i=0; i<N; i++) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700876 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700877 writeString(val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700878 writeValue(val.valueAt(i));
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700879 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Write #" + i + " "
880 + (dataPosition()-startPos) + " bytes: key=0x"
881 + Integer.toHexString(val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0)
882 + " " + val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700883 }
884 }
885
886 /**
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700887 * @hide For testing only.
888 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400889 public void writeArrayMap(@Nullable ArrayMap<String, Object> val) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700890 writeArrayMapInternal(val);
891 }
892
893 /**
Svet Ganovddb94882016-06-23 19:55:24 -0700894 * Write an array set to the parcel.
895 *
896 * @param val The array set to write.
897 *
898 * @hide
899 */
900 public void writeArraySet(@Nullable ArraySet<? extends Object> val) {
901 final int size = (val != null) ? val.size() : -1;
902 writeInt(size);
903 for (int i = 0; i < size; i++) {
904 writeValue(val.valueAt(i));
905 }
906 }
907
908 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 * Flatten a Bundle into the parcel at the current dataPosition(),
910 * growing dataCapacity() if needed.
911 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400912 public final void writeBundle(@Nullable Bundle val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 if (val == null) {
914 writeInt(-1);
915 return;
916 }
917
Dianne Hackborn6aff9052009-05-22 13:20:23 -0700918 val.writeToParcel(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 }
920
921 /**
Craig Mautner719e6b12014-04-04 20:29:41 -0700922 * Flatten a PersistableBundle into the parcel at the current dataPosition(),
923 * growing dataCapacity() if needed.
924 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400925 public final void writePersistableBundle(@Nullable PersistableBundle val) {
Craig Mautner719e6b12014-04-04 20:29:41 -0700926 if (val == null) {
927 writeInt(-1);
928 return;
929 }
930
931 val.writeToParcel(this, 0);
932 }
933
934 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -0700935 * Flatten a Size into the parcel at the current dataPosition(),
936 * growing dataCapacity() if needed.
937 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400938 public final void writeSize(@NonNull Size val) {
Jeff Sharkey5ef33982014-09-04 18:13:39 -0700939 writeInt(val.getWidth());
940 writeInt(val.getHeight());
941 }
942
943 /**
944 * Flatten a SizeF into the parcel at the current dataPosition(),
945 * growing dataCapacity() if needed.
946 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400947 public final void writeSizeF(@NonNull SizeF val) {
Jeff Sharkey5ef33982014-09-04 18:13:39 -0700948 writeFloat(val.getWidth());
949 writeFloat(val.getHeight());
950 }
951
952 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 * Flatten a List into the parcel at the current dataPosition(), growing
954 * dataCapacity() if needed. The List values are written using
955 * {@link #writeValue} and must follow the specification there.
956 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400957 public final void writeList(@Nullable List val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 if (val == null) {
959 writeInt(-1);
960 return;
961 }
962 int N = val.size();
963 int i=0;
964 writeInt(N);
965 while (i < N) {
966 writeValue(val.get(i));
967 i++;
968 }
969 }
970
971 /**
972 * Flatten an Object array into the parcel at the current dataPosition(),
973 * growing dataCapacity() if needed. The array values are written using
974 * {@link #writeValue} and must follow the specification there.
975 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400976 public final void writeArray(@Nullable Object[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 if (val == null) {
978 writeInt(-1);
979 return;
980 }
981 int N = val.length;
982 int i=0;
983 writeInt(N);
984 while (i < N) {
985 writeValue(val[i]);
986 i++;
987 }
988 }
989
990 /**
991 * Flatten a generic SparseArray into the parcel at the current
992 * dataPosition(), growing dataCapacity() if needed. The SparseArray
993 * values are written using {@link #writeValue} and must follow the
994 * specification there.
995 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400996 public final void writeSparseArray(@Nullable SparseArray<Object> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 if (val == null) {
998 writeInt(-1);
999 return;
1000 }
1001 int N = val.size();
1002 writeInt(N);
1003 int i=0;
1004 while (i < N) {
1005 writeInt(val.keyAt(i));
1006 writeValue(val.valueAt(i));
1007 i++;
1008 }
1009 }
1010
Jake Whartonb1f474c2018-05-30 15:11:13 -04001011 public final void writeSparseBooleanArray(@Nullable SparseBooleanArray val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 if (val == null) {
1013 writeInt(-1);
1014 return;
1015 }
1016 int N = val.size();
1017 writeInt(N);
1018 int i=0;
1019 while (i < N) {
1020 writeInt(val.keyAt(i));
1021 writeByte((byte)(val.valueAt(i) ? 1 : 0));
1022 i++;
1023 }
1024 }
1025
Adam Lesinski205656d2017-03-23 13:38:26 -07001026 /**
1027 * @hide
1028 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001029 public final void writeSparseIntArray(@Nullable SparseIntArray val) {
Adam Lesinski4e862812016-11-21 16:02:24 -08001030 if (val == null) {
1031 writeInt(-1);
1032 return;
1033 }
1034 int N = val.size();
1035 writeInt(N);
1036 int i=0;
1037 while (i < N) {
1038 writeInt(val.keyAt(i));
1039 writeInt(val.valueAt(i));
1040 i++;
1041 }
1042 }
1043
Jake Whartonb1f474c2018-05-30 15:11:13 -04001044 public final void writeBooleanArray(@Nullable boolean[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 if (val != null) {
1046 int N = val.length;
1047 writeInt(N);
1048 for (int i=0; i<N; i++) {
1049 writeInt(val[i] ? 1 : 0);
1050 }
1051 } else {
1052 writeInt(-1);
1053 }
1054 }
1055
Jake Whartonb1f474c2018-05-30 15:11:13 -04001056 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 public final boolean[] createBooleanArray() {
1058 int N = readInt();
1059 // >>2 as a fast divide-by-4 works in the create*Array() functions
1060 // because dataAvail() will never return a negative number. 4 is
1061 // the size of a stored boolean in the stream.
1062 if (N >= 0 && N <= (dataAvail() >> 2)) {
1063 boolean[] val = new boolean[N];
1064 for (int i=0; i<N; i++) {
1065 val[i] = readInt() != 0;
1066 }
1067 return val;
1068 } else {
1069 return null;
1070 }
1071 }
1072
Jake Whartonb1f474c2018-05-30 15:11:13 -04001073 public final void readBooleanArray(@NonNull boolean[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 int N = readInt();
1075 if (N == val.length) {
1076 for (int i=0; i<N; i++) {
1077 val[i] = readInt() != 0;
1078 }
1079 } else {
1080 throw new RuntimeException("bad array lengths");
1081 }
1082 }
1083
Jake Whartonb1f474c2018-05-30 15:11:13 -04001084 public final void writeCharArray(@Nullable char[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 if (val != null) {
1086 int N = val.length;
1087 writeInt(N);
1088 for (int i=0; i<N; i++) {
1089 writeInt((int)val[i]);
1090 }
1091 } else {
1092 writeInt(-1);
1093 }
1094 }
1095
Jake Whartonb1f474c2018-05-30 15:11:13 -04001096 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 public final char[] createCharArray() {
1098 int N = readInt();
1099 if (N >= 0 && N <= (dataAvail() >> 2)) {
1100 char[] val = new char[N];
1101 for (int i=0; i<N; i++) {
1102 val[i] = (char)readInt();
1103 }
1104 return val;
1105 } else {
1106 return null;
1107 }
1108 }
1109
Jake Whartonb1f474c2018-05-30 15:11:13 -04001110 public final void readCharArray(@NonNull char[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 int N = readInt();
1112 if (N == val.length) {
1113 for (int i=0; i<N; i++) {
1114 val[i] = (char)readInt();
1115 }
1116 } else {
1117 throw new RuntimeException("bad array lengths");
1118 }
1119 }
1120
Jake Whartonb1f474c2018-05-30 15:11:13 -04001121 public final void writeIntArray(@Nullable int[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 if (val != null) {
1123 int N = val.length;
1124 writeInt(N);
1125 for (int i=0; i<N; i++) {
1126 writeInt(val[i]);
1127 }
1128 } else {
1129 writeInt(-1);
1130 }
1131 }
1132
Jake Whartonb1f474c2018-05-30 15:11:13 -04001133 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 public final int[] createIntArray() {
1135 int N = readInt();
1136 if (N >= 0 && N <= (dataAvail() >> 2)) {
1137 int[] val = new int[N];
1138 for (int i=0; i<N; i++) {
1139 val[i] = readInt();
1140 }
1141 return val;
1142 } else {
1143 return null;
1144 }
1145 }
1146
Jake Whartonb1f474c2018-05-30 15:11:13 -04001147 public final void readIntArray(@NonNull int[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 int N = readInt();
1149 if (N == val.length) {
1150 for (int i=0; i<N; i++) {
1151 val[i] = readInt();
1152 }
1153 } else {
1154 throw new RuntimeException("bad array lengths");
1155 }
1156 }
1157
Jake Whartonb1f474c2018-05-30 15:11:13 -04001158 public final void writeLongArray(@Nullable long[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 if (val != null) {
1160 int N = val.length;
1161 writeInt(N);
1162 for (int i=0; i<N; i++) {
1163 writeLong(val[i]);
1164 }
1165 } else {
1166 writeInt(-1);
1167 }
1168 }
1169
Jake Whartonb1f474c2018-05-30 15:11:13 -04001170 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 public final long[] createLongArray() {
1172 int N = readInt();
1173 // >>3 because stored longs are 64 bits
1174 if (N >= 0 && N <= (dataAvail() >> 3)) {
1175 long[] val = new long[N];
1176 for (int i=0; i<N; i++) {
1177 val[i] = readLong();
1178 }
1179 return val;
1180 } else {
1181 return null;
1182 }
1183 }
1184
Jake Whartonb1f474c2018-05-30 15:11:13 -04001185 public final void readLongArray(@NonNull long[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 int N = readInt();
1187 if (N == val.length) {
1188 for (int i=0; i<N; i++) {
1189 val[i] = readLong();
1190 }
1191 } else {
1192 throw new RuntimeException("bad array lengths");
1193 }
1194 }
1195
Jake Whartonb1f474c2018-05-30 15:11:13 -04001196 public final void writeFloatArray(@Nullable float[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 if (val != null) {
1198 int N = val.length;
1199 writeInt(N);
1200 for (int i=0; i<N; i++) {
1201 writeFloat(val[i]);
1202 }
1203 } else {
1204 writeInt(-1);
1205 }
1206 }
1207
Jake Whartonb1f474c2018-05-30 15:11:13 -04001208 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 public final float[] createFloatArray() {
1210 int N = readInt();
1211 // >>2 because stored floats are 4 bytes
1212 if (N >= 0 && N <= (dataAvail() >> 2)) {
1213 float[] val = new float[N];
1214 for (int i=0; i<N; i++) {
1215 val[i] = readFloat();
1216 }
1217 return val;
1218 } else {
1219 return null;
1220 }
1221 }
1222
Jake Whartonb1f474c2018-05-30 15:11:13 -04001223 public final void readFloatArray(@NonNull float[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 int N = readInt();
1225 if (N == val.length) {
1226 for (int i=0; i<N; i++) {
1227 val[i] = readFloat();
1228 }
1229 } else {
1230 throw new RuntimeException("bad array lengths");
1231 }
1232 }
1233
Jake Whartonb1f474c2018-05-30 15:11:13 -04001234 public final void writeDoubleArray(@Nullable double[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 if (val != null) {
1236 int N = val.length;
1237 writeInt(N);
1238 for (int i=0; i<N; i++) {
1239 writeDouble(val[i]);
1240 }
1241 } else {
1242 writeInt(-1);
1243 }
1244 }
1245
Jake Whartonb1f474c2018-05-30 15:11:13 -04001246 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 public final double[] createDoubleArray() {
1248 int N = readInt();
1249 // >>3 because stored doubles are 8 bytes
1250 if (N >= 0 && N <= (dataAvail() >> 3)) {
1251 double[] val = new double[N];
1252 for (int i=0; i<N; i++) {
1253 val[i] = readDouble();
1254 }
1255 return val;
1256 } else {
1257 return null;
1258 }
1259 }
1260
Jake Whartonb1f474c2018-05-30 15:11:13 -04001261 public final void readDoubleArray(@NonNull double[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 int N = readInt();
1263 if (N == val.length) {
1264 for (int i=0; i<N; i++) {
1265 val[i] = readDouble();
1266 }
1267 } else {
1268 throw new RuntimeException("bad array lengths");
1269 }
1270 }
1271
Jake Whartonb1f474c2018-05-30 15:11:13 -04001272 public final void writeStringArray(@Nullable String[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 if (val != null) {
1274 int N = val.length;
1275 writeInt(N);
1276 for (int i=0; i<N; i++) {
1277 writeString(val[i]);
1278 }
1279 } else {
1280 writeInt(-1);
1281 }
1282 }
1283
Jake Whartonb1f474c2018-05-30 15:11:13 -04001284 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 public final String[] createStringArray() {
1286 int N = readInt();
1287 if (N >= 0) {
1288 String[] val = new String[N];
1289 for (int i=0; i<N; i++) {
1290 val[i] = readString();
1291 }
1292 return val;
1293 } else {
1294 return null;
1295 }
1296 }
1297
Jake Whartonb1f474c2018-05-30 15:11:13 -04001298 public final void readStringArray(@NonNull String[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 int N = readInt();
1300 if (N == val.length) {
1301 for (int i=0; i<N; i++) {
1302 val[i] = readString();
1303 }
1304 } else {
1305 throw new RuntimeException("bad array lengths");
1306 }
1307 }
1308
Jake Whartonb1f474c2018-05-30 15:11:13 -04001309 public final void writeBinderArray(@Nullable IBinder[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 if (val != null) {
1311 int N = val.length;
1312 writeInt(N);
1313 for (int i=0; i<N; i++) {
1314 writeStrongBinder(val[i]);
1315 }
1316 } else {
1317 writeInt(-1);
1318 }
1319 }
1320
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001321 /**
1322 * @hide
1323 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001324 public final void writeCharSequenceArray(@Nullable CharSequence[] val) {
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001325 if (val != null) {
1326 int N = val.length;
1327 writeInt(N);
1328 for (int i=0; i<N; i++) {
1329 writeCharSequence(val[i]);
1330 }
1331 } else {
1332 writeInt(-1);
1333 }
1334 }
1335
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001336 /**
1337 * @hide
1338 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001339 public final void writeCharSequenceList(@Nullable ArrayList<CharSequence> val) {
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001340 if (val != null) {
1341 int N = val.size();
1342 writeInt(N);
1343 for (int i=0; i<N; i++) {
1344 writeCharSequence(val.get(i));
1345 }
1346 } else {
1347 writeInt(-1);
1348 }
1349 }
1350
Jake Whartonb1f474c2018-05-30 15:11:13 -04001351 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 public final IBinder[] createBinderArray() {
1353 int N = readInt();
1354 if (N >= 0) {
1355 IBinder[] val = new IBinder[N];
1356 for (int i=0; i<N; i++) {
1357 val[i] = readStrongBinder();
1358 }
1359 return val;
1360 } else {
1361 return null;
1362 }
1363 }
1364
Jake Whartonb1f474c2018-05-30 15:11:13 -04001365 public final void readBinderArray(@NonNull IBinder[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 int N = readInt();
1367 if (N == val.length) {
1368 for (int i=0; i<N; i++) {
1369 val[i] = readStrongBinder();
1370 }
1371 } else {
1372 throw new RuntimeException("bad array lengths");
1373 }
1374 }
1375
1376 /**
1377 * Flatten a List containing a particular object type into the parcel, at
1378 * the current dataPosition() and growing dataCapacity() if needed. The
1379 * type of the objects in the list must be one that implements Parcelable.
1380 * Unlike the generic writeList() method, however, only the raw data of the
1381 * objects is written and not their type, so you must use the corresponding
1382 * readTypedList() to unmarshall them.
1383 *
1384 * @param val The list of objects to be written.
1385 *
1386 * @see #createTypedArrayList
1387 * @see #readTypedList
1388 * @see Parcelable
1389 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001390 public final <T extends Parcelable> void writeTypedList(@Nullable List<T> val) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001391 writeTypedList(val, 0);
1392 }
1393
1394 /**
1395 * @hide
1396 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001397 public <T extends Parcelable> void writeTypedList(@Nullable List<T> val, int parcelableFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 if (val == null) {
1399 writeInt(-1);
1400 return;
1401 }
1402 int N = val.size();
1403 int i=0;
1404 writeInt(N);
1405 while (i < N) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001406 writeTypedObject(val.get(i), parcelableFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 i++;
1408 }
1409 }
1410
1411 /**
1412 * Flatten a List containing String objects into the parcel, at
1413 * the current dataPosition() and growing dataCapacity() if needed. They
1414 * can later be retrieved with {@link #createStringArrayList} or
1415 * {@link #readStringList}.
1416 *
1417 * @param val The list of strings to be written.
1418 *
1419 * @see #createStringArrayList
1420 * @see #readStringList
1421 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001422 public final void writeStringList(@Nullable List<String> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 if (val == null) {
1424 writeInt(-1);
1425 return;
1426 }
1427 int N = val.size();
1428 int i=0;
1429 writeInt(N);
1430 while (i < N) {
1431 writeString(val.get(i));
1432 i++;
1433 }
1434 }
1435
1436 /**
1437 * Flatten a List containing IBinder objects into the parcel, at
1438 * the current dataPosition() and growing dataCapacity() if needed. They
1439 * can later be retrieved with {@link #createBinderArrayList} or
1440 * {@link #readBinderList}.
1441 *
1442 * @param val The list of strings to be written.
1443 *
1444 * @see #createBinderArrayList
1445 * @see #readBinderList
1446 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001447 public final void writeBinderList(@Nullable List<IBinder> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 if (val == null) {
1449 writeInt(-1);
1450 return;
1451 }
1452 int N = val.size();
1453 int i=0;
1454 writeInt(N);
1455 while (i < N) {
1456 writeStrongBinder(val.get(i));
1457 i++;
1458 }
1459 }
1460
1461 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00001462 * Flatten a {@code List} containing arbitrary {@code Parcelable} objects into this parcel
1463 * at the current position. They can later be retrieved using
1464 * {@link #readParcelableList(List, ClassLoader)} if required.
1465 *
1466 * @see #readParcelableList(List, ClassLoader)
Narayan Kamathbea48712016-12-01 15:38:28 +00001467 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001468 public final <T extends Parcelable> void writeParcelableList(@Nullable List<T> val, int flags) {
Narayan Kamathbea48712016-12-01 15:38:28 +00001469 if (val == null) {
1470 writeInt(-1);
1471 return;
1472 }
1473
1474 int N = val.size();
1475 int i=0;
1476 writeInt(N);
1477 while (i < N) {
1478 writeParcelable(val.get(i), flags);
1479 i++;
1480 }
1481 }
1482
1483 /**
Svet Ganov0f4928f2017-02-02 20:02:51 -08001484 * Flatten a homogeneous array containing a particular object type into
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 * the parcel, at
1486 * the current dataPosition() and growing dataCapacity() if needed. The
1487 * type of the objects in the array must be one that implements Parcelable.
1488 * Unlike the {@link #writeParcelableArray} method, however, only the
1489 * raw data of the objects is written and not their type, so you must use
1490 * {@link #readTypedArray} with the correct corresponding
1491 * {@link Parcelable.Creator} implementation to unmarshall them.
1492 *
1493 * @param val The array of objects to be written.
1494 * @param parcelableFlags Contextual flags as per
1495 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1496 *
1497 * @see #readTypedArray
1498 * @see #writeParcelableArray
1499 * @see Parcelable.Creator
1500 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001501 public final <T extends Parcelable> void writeTypedArray(@Nullable T[] val,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 int parcelableFlags) {
1503 if (val != null) {
1504 int N = val.length;
1505 writeInt(N);
Svet Ganov0f4928f2017-02-02 20:02:51 -08001506 for (int i = 0; i < N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001507 writeTypedObject(val[i], parcelableFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 }
1509 } else {
1510 writeInt(-1);
1511 }
1512 }
1513
1514 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001515 * Flatten the Parcelable object into the parcel.
1516 *
1517 * @param val The Parcelable object to be written.
1518 * @param parcelableFlags Contextual flags as per
1519 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1520 *
1521 * @see #readTypedObject
1522 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001523 public final <T extends Parcelable> void writeTypedObject(@Nullable T val,
1524 int parcelableFlags) {
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001525 if (val != null) {
1526 writeInt(1);
1527 val.writeToParcel(this, parcelableFlags);
1528 } else {
1529 writeInt(0);
1530 }
1531 }
1532
1533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 * Flatten a generic object in to a parcel. The given Object value may
1535 * currently be one of the following types:
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001536 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 * <ul>
1538 * <li> null
1539 * <li> String
1540 * <li> Byte
1541 * <li> Short
1542 * <li> Integer
1543 * <li> Long
1544 * <li> Float
1545 * <li> Double
1546 * <li> Boolean
1547 * <li> String[]
1548 * <li> boolean[]
1549 * <li> byte[]
1550 * <li> int[]
1551 * <li> long[]
1552 * <li> Object[] (supporting objects of the same type defined here).
1553 * <li> {@link Bundle}
1554 * <li> Map (as supported by {@link #writeMap}).
1555 * <li> Any object that implements the {@link Parcelable} protocol.
1556 * <li> Parcelable[]
1557 * <li> CharSequence (as supported by {@link TextUtils#writeToParcel}).
1558 * <li> List (as supported by {@link #writeList}).
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001559 * <li> {@link SparseArray} (as supported by {@link #writeSparseArray(SparseArray)}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 * <li> {@link IBinder}
1561 * <li> Any object that implements Serializable (but see
1562 * {@link #writeSerializable} for caveats). Note that all of the
1563 * previous types have relatively efficient implementations for
1564 * writing to a Parcel; having to rely on the generic serialization
1565 * approach is much less efficient and should be avoided whenever
1566 * possible.
1567 * </ul>
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001568 *
1569 * <p class="caution">{@link Parcelable} objects are written with
1570 * {@link Parcelable#writeToParcel} using contextual flags of 0. When
1571 * serializing objects containing {@link ParcelFileDescriptor}s,
1572 * this may result in file descriptor leaks when they are returned from
1573 * Binder calls (where {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE}
1574 * should be used).</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001576 public final void writeValue(@Nullable Object v) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 if (v == null) {
1578 writeInt(VAL_NULL);
1579 } else if (v instanceof String) {
1580 writeInt(VAL_STRING);
1581 writeString((String) v);
1582 } else if (v instanceof Integer) {
1583 writeInt(VAL_INTEGER);
1584 writeInt((Integer) v);
1585 } else if (v instanceof Map) {
1586 writeInt(VAL_MAP);
1587 writeMap((Map) v);
1588 } else if (v instanceof Bundle) {
1589 // Must be before Parcelable
1590 writeInt(VAL_BUNDLE);
1591 writeBundle((Bundle) v);
Samuel Tanceafe5e2015-12-11 16:50:58 -08001592 } else if (v instanceof PersistableBundle) {
1593 writeInt(VAL_PERSISTABLEBUNDLE);
1594 writePersistableBundle((PersistableBundle) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 } else if (v instanceof Parcelable) {
Samuel Tanceafe5e2015-12-11 16:50:58 -08001596 // IMPOTANT: cases for classes that implement Parcelable must
1597 // come before the Parcelable case, so that their specific VAL_*
1598 // types will be written.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 writeInt(VAL_PARCELABLE);
1600 writeParcelable((Parcelable) v, 0);
1601 } else if (v instanceof Short) {
1602 writeInt(VAL_SHORT);
1603 writeInt(((Short) v).intValue());
1604 } else if (v instanceof Long) {
1605 writeInt(VAL_LONG);
1606 writeLong((Long) v);
1607 } else if (v instanceof Float) {
1608 writeInt(VAL_FLOAT);
1609 writeFloat((Float) v);
1610 } else if (v instanceof Double) {
1611 writeInt(VAL_DOUBLE);
1612 writeDouble((Double) v);
1613 } else if (v instanceof Boolean) {
1614 writeInt(VAL_BOOLEAN);
1615 writeInt((Boolean) v ? 1 : 0);
1616 } else if (v instanceof CharSequence) {
1617 // Must be after String
1618 writeInt(VAL_CHARSEQUENCE);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001619 writeCharSequence((CharSequence) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 } else if (v instanceof List) {
1621 writeInt(VAL_LIST);
1622 writeList((List) v);
1623 } else if (v instanceof SparseArray) {
1624 writeInt(VAL_SPARSEARRAY);
1625 writeSparseArray((SparseArray) v);
1626 } else if (v instanceof boolean[]) {
1627 writeInt(VAL_BOOLEANARRAY);
1628 writeBooleanArray((boolean[]) v);
1629 } else if (v instanceof byte[]) {
1630 writeInt(VAL_BYTEARRAY);
1631 writeByteArray((byte[]) v);
1632 } else if (v instanceof String[]) {
1633 writeInt(VAL_STRINGARRAY);
1634 writeStringArray((String[]) v);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001635 } else if (v instanceof CharSequence[]) {
1636 // Must be after String[] and before Object[]
1637 writeInt(VAL_CHARSEQUENCEARRAY);
1638 writeCharSequenceArray((CharSequence[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 } else if (v instanceof IBinder) {
1640 writeInt(VAL_IBINDER);
1641 writeStrongBinder((IBinder) v);
1642 } else if (v instanceof Parcelable[]) {
1643 writeInt(VAL_PARCELABLEARRAY);
1644 writeParcelableArray((Parcelable[]) v, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 } else if (v instanceof int[]) {
1646 writeInt(VAL_INTARRAY);
1647 writeIntArray((int[]) v);
1648 } else if (v instanceof long[]) {
1649 writeInt(VAL_LONGARRAY);
1650 writeLongArray((long[]) v);
1651 } else if (v instanceof Byte) {
1652 writeInt(VAL_BYTE);
1653 writeInt((Byte) v);
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001654 } else if (v instanceof Size) {
1655 writeInt(VAL_SIZE);
1656 writeSize((Size) v);
1657 } else if (v instanceof SizeF) {
1658 writeInt(VAL_SIZEF);
1659 writeSizeF((SizeF) v);
Samuel Tana8036662015-11-23 14:36:00 -08001660 } else if (v instanceof double[]) {
1661 writeInt(VAL_DOUBLEARRAY);
1662 writeDoubleArray((double[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 } else {
Paul Duffinac5a0822014-02-03 15:02:17 +00001664 Class<?> clazz = v.getClass();
1665 if (clazz.isArray() && clazz.getComponentType() == Object.class) {
1666 // Only pure Object[] are written here, Other arrays of non-primitive types are
1667 // handled by serialization as this does not record the component type.
1668 writeInt(VAL_OBJECTARRAY);
1669 writeArray((Object[]) v);
1670 } else if (v instanceof Serializable) {
1671 // Must be last
1672 writeInt(VAL_SERIALIZABLE);
1673 writeSerializable((Serializable) v);
1674 } else {
1675 throw new RuntimeException("Parcel: unable to marshal value " + v);
1676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 }
1678 }
1679
1680 /**
1681 * Flatten the name of the class of the Parcelable and its contents
1682 * into the parcel.
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001683 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 * @param p The Parcelable object to be written.
1685 * @param parcelableFlags Contextual flags as per
1686 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1687 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001688 public final void writeParcelable(@Nullable Parcelable p, int parcelableFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 if (p == null) {
1690 writeString(null);
1691 return;
1692 }
Neil Fuller44e440c2015-04-20 14:39:00 +01001693 writeParcelableCreator(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 p.writeToParcel(this, parcelableFlags);
1695 }
1696
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001697 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001698 public final void writeParcelableCreator(@NonNull Parcelable p) {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001699 String name = p.getClass().getName();
1700 writeString(name);
1701 }
1702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 /**
1704 * Write a generic serializable object in to a Parcel. It is strongly
1705 * recommended that this method be avoided, since the serialization
1706 * overhead is extremely large, and this approach will be much slower than
1707 * using the other approaches to writing data in to a Parcel.
1708 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001709 public final void writeSerializable(@Nullable Serializable s) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 if (s == null) {
1711 writeString(null);
1712 return;
1713 }
1714 String name = s.getClass().getName();
1715 writeString(name);
1716
1717 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1718 try {
1719 ObjectOutputStream oos = new ObjectOutputStream(baos);
1720 oos.writeObject(s);
1721 oos.close();
1722
1723 writeByteArray(baos.toByteArray());
1724 } catch (IOException ioe) {
1725 throw new RuntimeException("Parcelable encountered " +
1726 "IOException writing serializable object (name = " + name +
1727 ")", ioe);
1728 }
1729 }
1730
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001731 /** @hide For debugging purposes */
1732 public static void setStackTraceParceling(boolean enabled) {
1733 sParcelExceptionStackTrace = enabled;
1734 }
1735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 /**
1737 * Special function for writing an exception result at the header of
1738 * a parcel, to be used when returning an exception from a transaction.
1739 * Note that this currently only supports a few exception types; any other
1740 * exception will be re-thrown by this function as a RuntimeException
1741 * (to be caught by the system's last-resort exception handling when
1742 * dispatching a transaction).
Samuel Tana8036662015-11-23 14:36:00 -08001743 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 * <p>The supported exception types are:
1745 * <ul>
1746 * <li>{@link BadParcelableException}
1747 * <li>{@link IllegalArgumentException}
1748 * <li>{@link IllegalStateException}
1749 * <li>{@link NullPointerException}
1750 * <li>{@link SecurityException}
Dianne Hackborn18482ae2017-08-01 17:41:00 -07001751 * <li>{@link UnsupportedOperationException}
Dianne Hackborn7e714422013-09-13 17:32:57 -07001752 * <li>{@link NetworkOnMainThreadException}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 * </ul>
Samuel Tana8036662015-11-23 14:36:00 -08001754 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 * @param e The Exception to be written.
1756 *
1757 * @see #writeNoException
1758 * @see #readException
1759 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001760 public final void writeException(@NonNull Exception e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 int code = 0;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001762 if (e instanceof Parcelable
1763 && (e.getClass().getClassLoader() == Parcelable.class.getClassLoader())) {
1764 // We only send Parcelable exceptions that are in the
1765 // BootClassLoader to ensure that the receiver can unpack them
1766 code = EX_PARCELABLE;
1767 } else if (e instanceof SecurityException) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 code = EX_SECURITY;
1769 } else if (e instanceof BadParcelableException) {
1770 code = EX_BAD_PARCELABLE;
1771 } else if (e instanceof IllegalArgumentException) {
1772 code = EX_ILLEGAL_ARGUMENT;
1773 } else if (e instanceof NullPointerException) {
1774 code = EX_NULL_POINTER;
1775 } else if (e instanceof IllegalStateException) {
1776 code = EX_ILLEGAL_STATE;
Dianne Hackborn7e714422013-09-13 17:32:57 -07001777 } else if (e instanceof NetworkOnMainThreadException) {
1778 code = EX_NETWORK_MAIN_THREAD;
Dianne Hackborn33d738a2014-09-12 14:23:58 -07001779 } else if (e instanceof UnsupportedOperationException) {
1780 code = EX_UNSUPPORTED_OPERATION;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001781 } else if (e instanceof ServiceSpecificException) {
1782 code = EX_SERVICE_SPECIFIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 }
1784 writeInt(code);
Brad Fitzpatrick703e5d32010-07-15 13:16:41 -07001785 StrictMode.clearGatheredViolations();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 if (code == 0) {
1787 if (e instanceof RuntimeException) {
1788 throw (RuntimeException) e;
1789 }
1790 throw new RuntimeException(e);
1791 }
1792 writeString(e.getMessage());
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001793 final long timeNow = sParcelExceptionStackTrace ? SystemClock.elapsedRealtime() : 0;
1794 if (sParcelExceptionStackTrace && (timeNow - sLastWriteExceptionStackTrace
1795 > WRITE_EXCEPTION_STACK_TRACE_THRESHOLD_MS)) {
1796 sLastWriteExceptionStackTrace = timeNow;
1797 final int sizePosition = dataPosition();
1798 writeInt(0); // Header size will be filled in later
1799 StackTraceElement[] stackTrace = e.getStackTrace();
1800 final int truncatedSize = Math.min(stackTrace.length, 5);
1801 StringBuilder sb = new StringBuilder();
1802 for (int i = 0; i < truncatedSize; i++) {
Fyodor Kupolov679d9982017-11-21 10:42:23 -08001803 sb.append("\tat ").append(stackTrace[i]).append('\n');
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001804 }
1805 writeString(sb.toString());
1806 final int payloadPosition = dataPosition();
1807 setDataPosition(sizePosition);
1808 // Write stack trace header size. Used in native side to skip the header
1809 writeInt(payloadPosition - sizePosition);
1810 setDataPosition(payloadPosition);
1811 } else {
1812 writeInt(0);
1813 }
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001814 switch (code) {
1815 case EX_SERVICE_SPECIFIC:
1816 writeInt(((ServiceSpecificException) e).errorCode);
1817 break;
1818 case EX_PARCELABLE:
1819 // Write parceled exception prefixed by length
1820 final int sizePosition = dataPosition();
1821 writeInt(0);
1822 writeParcelable((Parcelable) e, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
1823 final int payloadPosition = dataPosition();
1824 setDataPosition(sizePosition);
1825 writeInt(payloadPosition - sizePosition);
1826 setDataPosition(payloadPosition);
1827 break;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001828 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 }
1830
1831 /**
1832 * Special function for writing information at the front of the Parcel
1833 * indicating that no exception occurred.
1834 *
1835 * @see #writeException
1836 * @see #readException
1837 */
1838 public final void writeNoException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001839 // Despite the name of this function ("write no exception"),
1840 // it should instead be thought of as "write the RPC response
1841 // header", but because this function name is written out by
1842 // the AIDL compiler, we're not going to rename it.
1843 //
1844 // The response header, in the non-exception case (see also
1845 // writeException above, also called by the AIDL compiler), is
1846 // either a 0 (the default case), or EX_HAS_REPLY_HEADER if
1847 // StrictMode has gathered up violations that have occurred
1848 // during a Binder call, in which case we write out the number
1849 // of violations and their details, serialized, before the
1850 // actual RPC respons data. The receiving end of this is
1851 // readException(), below.
1852 if (StrictMode.hasGatheredViolations()) {
1853 writeInt(EX_HAS_REPLY_HEADER);
1854 final int sizePosition = dataPosition();
1855 writeInt(0); // total size of fat header, to be filled in later
1856 StrictMode.writeGatheredViolationsToParcel(this);
1857 final int payloadPosition = dataPosition();
1858 setDataPosition(sizePosition);
1859 writeInt(payloadPosition - sizePosition); // header size
1860 setDataPosition(payloadPosition);
1861 } else {
1862 writeInt(0);
1863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 }
1865
1866 /**
1867 * Special function for reading an exception result from the header of
1868 * a parcel, to be used after receiving the result of a transaction. This
1869 * will throw the exception for you if it had been written to the Parcel,
1870 * otherwise return and let you read the normal result data from the Parcel.
1871 *
1872 * @see #writeException
1873 * @see #writeNoException
1874 */
1875 public final void readException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001876 int code = readExceptionCode();
1877 if (code != 0) {
1878 String msg = readString();
Takamasa Kuramitsu2214b822018-04-02 14:44:59 +09001879 readException(code, msg);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001880 }
1881 }
1882
1883 /**
1884 * Parses the header of a Binder call's response Parcel and
1885 * returns the exception code. Deals with lite or fat headers.
1886 * In the common successful case, this header is generally zero.
1887 * In less common cases, it's a small negative number and will be
1888 * followed by an error string.
1889 *
1890 * This exists purely for android.database.DatabaseUtils and
1891 * insulating it from having to handle fat headers as returned by
1892 * e.g. StrictMode-induced RPC responses.
1893 *
1894 * @hide
1895 */
1896 public final int readExceptionCode() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 int code = readInt();
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001898 if (code == EX_HAS_REPLY_HEADER) {
1899 int headerSize = readInt();
1900 if (headerSize == 0) {
1901 Log.e(TAG, "Unexpected zero-sized Parcel reply header.");
1902 } else {
1903 // Currently the only thing in the header is StrictMode stacks,
1904 // but discussions around event/RPC tracing suggest we might
1905 // put that here too. If so, switch on sub-header tags here.
1906 // But for now, just parse out the StrictMode stuff.
1907 StrictMode.readAndHandleBinderCallViolations(this);
1908 }
1909 // And fat response headers are currently only used when
1910 // there are no exceptions, so return no error:
1911 return 0;
1912 }
1913 return code;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 }
1915
1916 /**
Mark Doliner879ea452014-01-02 12:38:07 -08001917 * Throw an exception with the given message. Not intended for use
1918 * outside the Parcel class.
1919 *
1920 * @param code Used to determine which exception class to throw.
1921 * @param msg The exception message.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 */
1923 public final void readException(int code, String msg) {
Takamasa Kuramitsu2214b822018-04-02 14:44:59 +09001924 String remoteStackTrace = null;
1925 final int remoteStackPayloadSize = readInt();
1926 if (remoteStackPayloadSize > 0) {
1927 remoteStackTrace = readString();
1928 }
1929 Exception e = createException(code, msg);
1930 // Attach remote stack trace if availalble
1931 if (remoteStackTrace != null) {
1932 RemoteException cause = new RemoteException(
1933 "Remote stack trace:\n" + remoteStackTrace, null, false, false);
1934 try {
1935 Throwable rootCause = ExceptionUtils.getRootCause(e);
1936 if (rootCause != null) {
1937 rootCause.initCause(cause);
1938 }
1939 } catch (RuntimeException ex) {
1940 Log.e(TAG, "Cannot set cause " + cause + " for " + e, ex);
1941 }
1942 }
1943 SneakyThrow.sneakyThrow(e);
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001944 }
1945
1946 /**
1947 * Creates an exception with the given message.
1948 *
1949 * @param code Used to determine which exception class to throw.
1950 * @param msg The exception message.
1951 */
1952 private Exception createException(int code, String msg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 switch (code) {
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001954 case EX_PARCELABLE:
1955 if (readInt() > 0) {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001956 return (Exception) readParcelable(Parcelable.class.getClassLoader());
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001957 } else {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001958 return new RuntimeException(msg + " [missing Parcelable]");
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 case EX_SECURITY:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001961 return new SecurityException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 case EX_BAD_PARCELABLE:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001963 return new BadParcelableException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 case EX_ILLEGAL_ARGUMENT:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001965 return new IllegalArgumentException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 case EX_NULL_POINTER:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001967 return new NullPointerException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 case EX_ILLEGAL_STATE:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001969 return new IllegalStateException(msg);
Dianne Hackborn7e714422013-09-13 17:32:57 -07001970 case EX_NETWORK_MAIN_THREAD:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001971 return new NetworkOnMainThreadException();
Dianne Hackborn33d738a2014-09-12 14:23:58 -07001972 case EX_UNSUPPORTED_OPERATION:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001973 return new UnsupportedOperationException(msg);
Christopher Wiley80fd1202015-11-22 17:12:37 -08001974 case EX_SERVICE_SPECIFIC:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001975 return new ServiceSpecificException(readInt(), msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 }
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001977 return new RuntimeException("Unknown exception code: " + code
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 + " msg " + msg);
1979 }
1980
1981 /**
1982 * Read an integer value from the parcel at the current dataPosition().
1983 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001984 public final int readInt() {
1985 return nativeReadInt(mNativePtr);
1986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987
1988 /**
1989 * Read a long integer value from the parcel at the current dataPosition().
1990 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001991 public final long readLong() {
1992 return nativeReadLong(mNativePtr);
1993 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994
1995 /**
1996 * Read a floating point value from the parcel at the current
1997 * dataPosition().
1998 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001999 public final float readFloat() {
2000 return nativeReadFloat(mNativePtr);
2001 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002
2003 /**
2004 * Read a double precision floating point value from the parcel at the
2005 * current dataPosition().
2006 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002007 public final double readDouble() {
2008 return nativeReadDouble(mNativePtr);
2009 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010
2011 /**
2012 * Read a string value from the parcel at the current dataPosition().
2013 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002014 @Nullable
Jeff Sharkey047238c2012-03-07 16:51:38 -08002015 public final String readString() {
Makoto Onuki4501c61d2017-07-27 15:56:40 -07002016 return mReadWriteHelper.readString(this);
2017 }
2018
2019 /**
2020 * Read a string without going though a {@link ReadWriteHelper}. Subclasses of
2021 * {@link ReadWriteHelper} must use this method instead of {@link #readString} to avoid
2022 * infinity recursive calls.
2023 *
2024 * @hide
2025 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002026 @Nullable
Makoto Onuki4501c61d2017-07-27 15:56:40 -07002027 public String readStringNoHelper() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002028 return nativeReadString(mNativePtr);
2029 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030
Jake Wharton2ffa4312018-04-23 17:47:14 -04002031 /**
2032 * Read a boolean value from the parcel at the current dataPosition().
2033 */
Eugene Susla36e866b2017-02-23 18:24:39 -08002034 public final boolean readBoolean() {
2035 return readInt() != 0;
2036 }
2037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002039 * Read a CharSequence value from the parcel at the current dataPosition().
2040 * @hide
2041 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002042 @Nullable
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002043 public final CharSequence readCharSequence() {
2044 return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(this);
2045 }
2046
2047 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 * Read an object from the parcel at the current dataPosition().
2049 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002050 public final IBinder readStrongBinder() {
2051 return nativeReadStrongBinder(mNativePtr);
2052 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053
2054 /**
2055 * Read a FileDescriptor from the parcel at the current dataPosition().
2056 */
2057 public final ParcelFileDescriptor readFileDescriptor() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002058 FileDescriptor fd = nativeReadFileDescriptor(mNativePtr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 return fd != null ? new ParcelFileDescriptor(fd) : null;
2060 }
2061
Jeff Sharkeyda5a3e12013-08-11 12:54:42 -07002062 /** {@hide} */
2063 public final FileDescriptor readRawFileDescriptor() {
2064 return nativeReadFileDescriptor(mNativePtr);
2065 }
2066
Casey Dahlin2f974b22015-11-05 12:19:13 -08002067 /**
2068 * {@hide}
2069 * Read and return a new array of FileDescriptors from the parcel.
2070 * @return the FileDescriptor array, or null if the array is null.
2071 **/
Jake Whartonb1f474c2018-05-30 15:11:13 -04002072 @Nullable
Casey Dahlin2f974b22015-11-05 12:19:13 -08002073 public final FileDescriptor[] createRawFileDescriptorArray() {
2074 int N = readInt();
2075 if (N < 0) {
2076 return null;
2077 }
2078 FileDescriptor[] f = new FileDescriptor[N];
2079 for (int i = 0; i < N; i++) {
2080 f[i] = readRawFileDescriptor();
2081 }
2082 return f;
2083 }
2084
2085 /**
2086 * {@hide}
2087 * Read an array of FileDescriptors from a parcel.
2088 * The passed array must be exactly the length of the array in the parcel.
2089 * @return the FileDescriptor array, or null if the array is null.
2090 **/
2091 public final void readRawFileDescriptorArray(FileDescriptor[] val) {
2092 int N = readInt();
2093 if (N == val.length) {
2094 for (int i=0; i<N; i++) {
2095 val[i] = readRawFileDescriptor();
2096 }
2097 } else {
2098 throw new RuntimeException("bad array lengths");
2099 }
2100 }
2101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 /**
2103 * Read a byte value from the parcel at the current dataPosition().
2104 */
2105 public final byte readByte() {
2106 return (byte)(readInt() & 0xff);
2107 }
2108
2109 /**
2110 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2111 * been written with {@link #writeBundle}. Read into an existing Map object
2112 * from the parcel at the current dataPosition().
2113 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002114 public final void readMap(@NonNull Map outVal, @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 int N = readInt();
2116 readMapInternal(outVal, N, loader);
2117 }
2118
2119 /**
2120 * Read into an existing List object from the parcel at the current
2121 * dataPosition(), using the given class loader to load any enclosed
2122 * Parcelables. If it is null, the default class loader is used.
2123 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002124 public final void readList(@NonNull List outVal, @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 int N = readInt();
2126 readListInternal(outVal, N, loader);
2127 }
2128
2129 /**
2130 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2131 * been written with {@link #writeBundle}. Read and return a new HashMap
2132 * object from the parcel at the current dataPosition(), using the given
2133 * class loader to load any enclosed Parcelables. Returns null if
2134 * the previously written map object was null.
2135 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002136 @Nullable
2137 public final HashMap readHashMap(@Nullable ClassLoader loader)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 {
2139 int N = readInt();
2140 if (N < 0) {
2141 return null;
2142 }
2143 HashMap m = new HashMap(N);
2144 readMapInternal(m, N, loader);
2145 return m;
2146 }
2147
2148 /**
2149 * Read and return a new Bundle object from the parcel at the current
2150 * dataPosition(). Returns null if the previously written Bundle object was
2151 * null.
2152 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002153 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 public final Bundle readBundle() {
2155 return readBundle(null);
2156 }
2157
2158 /**
2159 * Read and return a new Bundle object from the parcel at the current
2160 * dataPosition(), using the given class loader to initialize the class
2161 * loader of the Bundle for later retrieval of Parcelable objects.
2162 * Returns null if the previously written Bundle object was null.
2163 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002164 @Nullable
2165 public final Bundle readBundle(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 int length = readInt();
2167 if (length < 0) {
Dianne Hackborn4a7d8242013-10-03 10:19:20 -07002168 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 return null;
2170 }
Samuel Tana8036662015-11-23 14:36:00 -08002171
Dianne Hackborn6aff9052009-05-22 13:20:23 -07002172 final Bundle bundle = new Bundle(this, length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 if (loader != null) {
2174 bundle.setClassLoader(loader);
2175 }
2176 return bundle;
2177 }
2178
2179 /**
Craig Mautner719e6b12014-04-04 20:29:41 -07002180 * Read and return a new Bundle object from the parcel at the current
2181 * dataPosition(). Returns null if the previously written Bundle object was
2182 * null.
2183 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002184 @Nullable
Craig Mautner719e6b12014-04-04 20:29:41 -07002185 public final PersistableBundle readPersistableBundle() {
2186 return readPersistableBundle(null);
2187 }
2188
2189 /**
2190 * Read and return a new Bundle object from the parcel at the current
2191 * dataPosition(), using the given class loader to initialize the class
2192 * loader of the Bundle for later retrieval of Parcelable objects.
2193 * Returns null if the previously written Bundle object was null.
2194 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002195 @Nullable
2196 public final PersistableBundle readPersistableBundle(@Nullable ClassLoader loader) {
Craig Mautner719e6b12014-04-04 20:29:41 -07002197 int length = readInt();
2198 if (length < 0) {
2199 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
2200 return null;
2201 }
2202
2203 final PersistableBundle bundle = new PersistableBundle(this, length);
2204 if (loader != null) {
2205 bundle.setClassLoader(loader);
2206 }
2207 return bundle;
2208 }
2209
2210 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002211 * Read a Size from the parcel at the current dataPosition().
2212 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002213 @NonNull
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002214 public final Size readSize() {
2215 final int width = readInt();
2216 final int height = readInt();
2217 return new Size(width, height);
2218 }
2219
2220 /**
2221 * Read a SizeF from the parcel at the current dataPosition().
2222 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002223 @NonNull
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002224 public final SizeF readSizeF() {
2225 final float width = readFloat();
2226 final float height = readFloat();
2227 return new SizeF(width, height);
2228 }
2229
2230 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 * Read and return a byte[] object from the parcel.
2232 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002233 @Nullable
Jeff Sharkey047238c2012-03-07 16:51:38 -08002234 public final byte[] createByteArray() {
2235 return nativeCreateByteArray(mNativePtr);
2236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237
2238 /**
2239 * Read a byte[] object from the parcel and copy it into the
2240 * given byte array.
2241 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002242 public final void readByteArray(@NonNull byte[] val) {
Jocelyn Dang46100442017-05-05 15:40:49 -07002243 boolean valid = nativeReadByteArray(mNativePtr, val, (val != null) ? val.length : 0);
2244 if (!valid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 throw new RuntimeException("bad array lengths");
2246 }
2247 }
2248
2249 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002250 * Read a blob of data from the parcel and return it as a byte array.
2251 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -07002252 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002253 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002254 @Nullable
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002255 public final byte[] readBlob() {
2256 return nativeReadBlob(mNativePtr);
2257 }
2258
2259 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 * Read and return a String[] object from the parcel.
2261 * {@hide}
2262 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002263 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 public final String[] readStringArray() {
2265 String[] array = null;
2266
2267 int length = readInt();
2268 if (length >= 0)
2269 {
2270 array = new String[length];
2271
2272 for (int i = 0 ; i < length ; i++)
2273 {
2274 array[i] = readString();
2275 }
2276 }
2277
2278 return array;
2279 }
2280
2281 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002282 * Read and return a CharSequence[] object from the parcel.
2283 * {@hide}
2284 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002285 @Nullable
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002286 public final CharSequence[] readCharSequenceArray() {
2287 CharSequence[] array = null;
2288
2289 int length = readInt();
2290 if (length >= 0)
2291 {
2292 array = new CharSequence[length];
2293
2294 for (int i = 0 ; i < length ; i++)
2295 {
2296 array[i] = readCharSequence();
2297 }
2298 }
2299
2300 return array;
2301 }
2302
2303 /**
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002304 * Read and return an ArrayList&lt;CharSequence&gt; object from the parcel.
2305 * {@hide}
2306 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002307 @Nullable
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002308 public final ArrayList<CharSequence> readCharSequenceList() {
2309 ArrayList<CharSequence> array = null;
2310
2311 int length = readInt();
2312 if (length >= 0) {
2313 array = new ArrayList<CharSequence>(length);
2314
2315 for (int i = 0 ; i < length ; i++) {
2316 array.add(readCharSequence());
2317 }
2318 }
2319
2320 return array;
2321 }
2322
2323 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 * Read and return a new ArrayList object from the parcel at the current
2325 * dataPosition(). Returns null if the previously written list object was
2326 * null. The given class loader will be used to load any enclosed
2327 * Parcelables.
2328 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002329 @Nullable
2330 public final ArrayList readArrayList(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331 int N = readInt();
2332 if (N < 0) {
2333 return null;
2334 }
2335 ArrayList l = new ArrayList(N);
2336 readListInternal(l, N, loader);
2337 return l;
2338 }
2339
2340 /**
2341 * Read and return a new Object array from the parcel at the current
2342 * dataPosition(). Returns null if the previously written array was
2343 * null. The given class loader will be used to load any enclosed
2344 * Parcelables.
2345 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002346 @Nullable
2347 public final Object[] readArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 int N = readInt();
2349 if (N < 0) {
2350 return null;
2351 }
2352 Object[] l = new Object[N];
2353 readArrayInternal(l, N, loader);
2354 return l;
2355 }
2356
2357 /**
2358 * Read and return a new SparseArray object from the parcel at the current
2359 * dataPosition(). Returns null if the previously written list object was
2360 * null. The given class loader will be used to load any enclosed
2361 * Parcelables.
2362 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002363 @Nullable
2364 public final SparseArray readSparseArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 int N = readInt();
2366 if (N < 0) {
2367 return null;
2368 }
2369 SparseArray sa = new SparseArray(N);
2370 readSparseArrayInternal(sa, N, loader);
2371 return sa;
2372 }
2373
2374 /**
2375 * Read and return a new SparseBooleanArray object from the parcel at the current
2376 * dataPosition(). Returns null if the previously written list object was
2377 * null.
2378 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002379 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 public final SparseBooleanArray readSparseBooleanArray() {
2381 int N = readInt();
2382 if (N < 0) {
2383 return null;
2384 }
2385 SparseBooleanArray sa = new SparseBooleanArray(N);
2386 readSparseBooleanArrayInternal(sa, N);
2387 return sa;
2388 }
2389
2390 /**
Adam Lesinski4e862812016-11-21 16:02:24 -08002391 * Read and return a new SparseIntArray object from the parcel at the current
2392 * dataPosition(). Returns null if the previously written array object was null.
Adam Lesinski205656d2017-03-23 13:38:26 -07002393 * @hide
Adam Lesinski4e862812016-11-21 16:02:24 -08002394 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002395 @Nullable
Adam Lesinski4e862812016-11-21 16:02:24 -08002396 public final SparseIntArray readSparseIntArray() {
2397 int N = readInt();
2398 if (N < 0) {
2399 return null;
2400 }
2401 SparseIntArray sa = new SparseIntArray(N);
2402 readSparseIntArrayInternal(sa, N);
2403 return sa;
2404 }
2405
2406 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 * Read and return a new ArrayList containing a particular object type from
2408 * the parcel that was written with {@link #writeTypedList} at the
2409 * current dataPosition(). Returns null if the
2410 * previously written list object was null. The list <em>must</em> have
2411 * previously been written via {@link #writeTypedList} with the same object
2412 * type.
2413 *
2414 * @return A newly created ArrayList containing objects with the same data
2415 * as those that were previously written.
2416 *
2417 * @see #writeTypedList
2418 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002419 @Nullable
2420 public final <T> ArrayList<T> createTypedArrayList(@NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 int N = readInt();
2422 if (N < 0) {
2423 return null;
2424 }
2425 ArrayList<T> l = new ArrayList<T>(N);
2426 while (N > 0) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002427 l.add(readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 N--;
2429 }
2430 return l;
2431 }
2432
2433 /**
2434 * Read into the given List items containing a particular object type
2435 * that were written with {@link #writeTypedList} at the
2436 * current dataPosition(). The list <em>must</em> have
2437 * previously been written via {@link #writeTypedList} with the same object
2438 * type.
2439 *
2440 * @return A newly created ArrayList containing objects with the same data
2441 * as those that were previously written.
2442 *
2443 * @see #writeTypedList
2444 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002445 public final <T> void readTypedList(@NonNull List<T> list, @NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 int M = list.size();
2447 int N = readInt();
2448 int i = 0;
2449 for (; i < M && i < N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002450 list.set(i, readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 }
2452 for (; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002453 list.add(readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002454 }
2455 for (; i<M; i++) {
2456 list.remove(N);
2457 }
2458 }
2459
2460 /**
2461 * Read and return a new ArrayList containing String objects from
2462 * the parcel that was written with {@link #writeStringList} at the
2463 * current dataPosition(). Returns null if the
2464 * previously written list object was null.
2465 *
2466 * @return A newly created ArrayList containing strings with the same data
2467 * as those that were previously written.
2468 *
2469 * @see #writeStringList
2470 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002471 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 public final ArrayList<String> createStringArrayList() {
2473 int N = readInt();
2474 if (N < 0) {
2475 return null;
2476 }
2477 ArrayList<String> l = new ArrayList<String>(N);
2478 while (N > 0) {
2479 l.add(readString());
2480 N--;
2481 }
2482 return l;
2483 }
2484
2485 /**
2486 * Read and return a new ArrayList containing IBinder objects from
2487 * the parcel that was written with {@link #writeBinderList} at the
2488 * current dataPosition(). Returns null if the
2489 * previously written list object was null.
2490 *
2491 * @return A newly created ArrayList containing strings with the same data
2492 * as those that were previously written.
2493 *
2494 * @see #writeBinderList
2495 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002496 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 public final ArrayList<IBinder> createBinderArrayList() {
2498 int N = readInt();
2499 if (N < 0) {
2500 return null;
2501 }
2502 ArrayList<IBinder> l = new ArrayList<IBinder>(N);
2503 while (N > 0) {
2504 l.add(readStrongBinder());
2505 N--;
2506 }
2507 return l;
2508 }
2509
2510 /**
2511 * Read into the given List items String objects that were written with
2512 * {@link #writeStringList} at the current dataPosition().
2513 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 * @see #writeStringList
2515 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002516 public final void readStringList(@NonNull List<String> list) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 int M = list.size();
2518 int N = readInt();
2519 int i = 0;
2520 for (; i < M && i < N; i++) {
2521 list.set(i, readString());
2522 }
2523 for (; i<N; i++) {
2524 list.add(readString());
2525 }
2526 for (; i<M; i++) {
2527 list.remove(N);
2528 }
2529 }
2530
2531 /**
2532 * Read into the given List items IBinder objects that were written with
2533 * {@link #writeBinderList} at the current dataPosition().
2534 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 * @see #writeBinderList
2536 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002537 public final void readBinderList(@NonNull List<IBinder> list) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 int M = list.size();
2539 int N = readInt();
2540 int i = 0;
2541 for (; i < M && i < N; i++) {
2542 list.set(i, readStrongBinder());
2543 }
2544 for (; i<N; i++) {
2545 list.add(readStrongBinder());
2546 }
2547 for (; i<M; i++) {
2548 list.remove(N);
2549 }
2550 }
2551
2552 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00002553 * Read the list of {@code Parcelable} objects at the current data position into the
2554 * given {@code list}. The contents of the {@code list} are replaced. If the serialized
2555 * list was {@code null}, {@code list} is cleared.
2556 *
2557 * @see #writeParcelableList(List, int)
Narayan Kamathbea48712016-12-01 15:38:28 +00002558 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002559 @NonNull
2560 public final <T extends Parcelable> List<T> readParcelableList(@NonNull List<T> list,
2561 @Nullable ClassLoader cl) {
Narayan Kamathbea48712016-12-01 15:38:28 +00002562 final int N = readInt();
2563 if (N == -1) {
2564 list.clear();
Eugene Susla36e866b2017-02-23 18:24:39 -08002565 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002566 }
2567
2568 final int M = list.size();
2569 int i = 0;
2570 for (; i < M && i < N; i++) {
2571 list.set(i, (T) readParcelable(cl));
2572 }
2573 for (; i<N; i++) {
2574 list.add((T) readParcelable(cl));
2575 }
2576 for (; i<M; i++) {
2577 list.remove(N);
2578 }
Eugene Susla36e866b2017-02-23 18:24:39 -08002579 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002580 }
2581
2582 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 * Read and return a new array containing a particular object type from
2584 * the parcel at the current dataPosition(). Returns null if the
2585 * previously written array was null. The array <em>must</em> have
2586 * previously been written via {@link #writeTypedArray} with the same
2587 * object type.
2588 *
2589 * @return A newly created array containing objects with the same data
2590 * as those that were previously written.
2591 *
2592 * @see #writeTypedArray
2593 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002594 @Nullable
2595 public final <T> T[] createTypedArray(@NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 int N = readInt();
2597 if (N < 0) {
2598 return null;
2599 }
2600 T[] l = c.newArray(N);
2601 for (int i=0; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002602 l[i] = readTypedObject(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 }
2604 return l;
2605 }
2606
Jake Whartonb1f474c2018-05-30 15:11:13 -04002607 public final <T> void readTypedArray(@NonNull T[] val, @NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 int N = readInt();
2609 if (N == val.length) {
2610 for (int i=0; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002611 val[i] = readTypedObject(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612 }
2613 } else {
2614 throw new RuntimeException("bad array lengths");
2615 }
2616 }
2617
2618 /**
2619 * @deprecated
2620 * @hide
2621 */
2622 @Deprecated
2623 public final <T> T[] readTypedArray(Parcelable.Creator<T> c) {
2624 return createTypedArray(c);
2625 }
2626
2627 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002628 * Read and return a typed Parcelable object from a parcel.
2629 * Returns null if the previous written object was null.
2630 * The object <em>must</em> have previous been written via
2631 * {@link #writeTypedObject} with the same object type.
2632 *
2633 * @return A newly created object of the type that was previously
2634 * written.
2635 *
2636 * @see #writeTypedObject
2637 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002638 @Nullable
2639 public final <T> T readTypedObject(@NonNull Parcelable.Creator<T> c) {
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002640 if (readInt() != 0) {
2641 return c.createFromParcel(this);
2642 } else {
2643 return null;
2644 }
2645 }
2646
2647 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 * Write a heterogeneous array of Parcelable objects into the Parcel.
2649 * Each object in the array is written along with its class name, so
2650 * that the correct class can later be instantiated. As a result, this
2651 * has significantly more overhead than {@link #writeTypedArray}, but will
2652 * correctly handle an array containing more than one type of object.
2653 *
2654 * @param value The array of objects to be written.
2655 * @param parcelableFlags Contextual flags as per
2656 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
2657 *
2658 * @see #writeTypedArray
2659 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002660 public final <T extends Parcelable> void writeParcelableArray(@Nullable T[] value,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 int parcelableFlags) {
2662 if (value != null) {
2663 int N = value.length;
2664 writeInt(N);
2665 for (int i=0; i<N; i++) {
2666 writeParcelable(value[i], parcelableFlags);
2667 }
2668 } else {
2669 writeInt(-1);
2670 }
2671 }
2672
2673 /**
2674 * Read a typed object from a parcel. The given class loader will be
2675 * used to load any enclosed Parcelables. If it is null, the default class
2676 * loader will be used.
2677 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002678 @Nullable
2679 public final Object readValue(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 int type = readInt();
2681
2682 switch (type) {
2683 case VAL_NULL:
2684 return null;
2685
2686 case VAL_STRING:
2687 return readString();
2688
2689 case VAL_INTEGER:
2690 return readInt();
2691
2692 case VAL_MAP:
2693 return readHashMap(loader);
2694
2695 case VAL_PARCELABLE:
2696 return readParcelable(loader);
2697
2698 case VAL_SHORT:
2699 return (short) readInt();
2700
2701 case VAL_LONG:
2702 return readLong();
2703
2704 case VAL_FLOAT:
2705 return readFloat();
2706
2707 case VAL_DOUBLE:
2708 return readDouble();
2709
2710 case VAL_BOOLEAN:
2711 return readInt() == 1;
2712
2713 case VAL_CHARSEQUENCE:
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002714 return readCharSequence();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715
2716 case VAL_LIST:
2717 return readArrayList(loader);
2718
2719 case VAL_BOOLEANARRAY:
Samuel Tana8036662015-11-23 14:36:00 -08002720 return createBooleanArray();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721
2722 case VAL_BYTEARRAY:
2723 return createByteArray();
2724
2725 case VAL_STRINGARRAY:
2726 return readStringArray();
2727
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002728 case VAL_CHARSEQUENCEARRAY:
2729 return readCharSequenceArray();
2730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 case VAL_IBINDER:
2732 return readStrongBinder();
2733
2734 case VAL_OBJECTARRAY:
2735 return readArray(loader);
2736
2737 case VAL_INTARRAY:
2738 return createIntArray();
2739
2740 case VAL_LONGARRAY:
2741 return createLongArray();
2742
2743 case VAL_BYTE:
2744 return readByte();
2745
2746 case VAL_SERIALIZABLE:
John Spurlock5002b8c2014-01-10 13:32:12 -05002747 return readSerializable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748
2749 case VAL_PARCELABLEARRAY:
2750 return readParcelableArray(loader);
2751
2752 case VAL_SPARSEARRAY:
2753 return readSparseArray(loader);
2754
2755 case VAL_SPARSEBOOLEANARRAY:
2756 return readSparseBooleanArray();
2757
2758 case VAL_BUNDLE:
2759 return readBundle(loader); // loading will be deferred
2760
Craig Mautner719e6b12014-04-04 20:29:41 -07002761 case VAL_PERSISTABLEBUNDLE:
2762 return readPersistableBundle(loader);
2763
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002764 case VAL_SIZE:
2765 return readSize();
2766
2767 case VAL_SIZEF:
2768 return readSizeF();
2769
Samuel Tana8036662015-11-23 14:36:00 -08002770 case VAL_DOUBLEARRAY:
2771 return createDoubleArray();
2772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 default:
2774 int off = dataPosition() - 4;
2775 throw new RuntimeException(
2776 "Parcel " + this + ": Unmarshalling unknown type code " + type + " at offset " + off);
2777 }
2778 }
2779
2780 /**
2781 * Read and return a new Parcelable from the parcel. The given class loader
2782 * will be used to load any enclosed Parcelables. If it is null, the default
2783 * class loader will be used.
2784 * @param loader A ClassLoader from which to instantiate the Parcelable
2785 * object, or null for the default class loader.
2786 * @return Returns the newly created Parcelable, or null if a null
2787 * object has been written.
2788 * @throws BadParcelableException Throws BadParcelableException if there
2789 * was an error trying to instantiate the Parcelable.
2790 */
Neil Fuller44e440c2015-04-20 14:39:00 +01002791 @SuppressWarnings("unchecked")
Jake Whartonb1f474c2018-05-30 15:11:13 -04002792 @Nullable
2793 public final <T extends Parcelable> T readParcelable(@Nullable ClassLoader loader) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002794 Parcelable.Creator<?> creator = readParcelableCreator(loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002795 if (creator == null) {
2796 return null;
2797 }
2798 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002799 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2800 (Parcelable.ClassLoaderCreator<?>) creator;
2801 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002802 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002803 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002804 }
2805
2806 /** @hide */
Neil Fuller44e440c2015-04-20 14:39:00 +01002807 @SuppressWarnings("unchecked")
Jake Whartonb1f474c2018-05-30 15:11:13 -04002808 @Nullable
2809 public final <T extends Parcelable> T readCreator(@NonNull Parcelable.Creator<?> creator,
2810 @Nullable ClassLoader loader) {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002811 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002812 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2813 (Parcelable.ClassLoaderCreator<?>) creator;
2814 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002815 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002816 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002817 }
2818
2819 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002820 @Nullable
2821 public final Parcelable.Creator<?> readParcelableCreator(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 String name = readString();
2823 if (name == null) {
2824 return null;
2825 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002826 Parcelable.Creator<?> creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 synchronized (mCreators) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002828 HashMap<String,Parcelable.Creator<?>> map = mCreators.get(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 if (map == null) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002830 map = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 mCreators.put(loader, map);
2832 }
2833 creator = map.get(name);
2834 if (creator == null) {
2835 try {
Neil Fuller44e440c2015-04-20 14:39:00 +01002836 // If loader == null, explicitly emulate Class.forName(String) "caller
2837 // classloader" behavior.
2838 ClassLoader parcelableClassLoader =
2839 (loader == null ? getClass().getClassLoader() : loader);
2840 // Avoid initializing the Parcelable class until we know it implements
2841 // Parcelable and has the necessary CREATOR field. http://b/1171613.
2842 Class<?> parcelableClass = Class.forName(name, false /* initialize */,
2843 parcelableClassLoader);
2844 if (!Parcelable.class.isAssignableFrom(parcelableClass)) {
Dianne Hackbornced54392018-02-26 13:07:42 -08002845 throw new BadParcelableException("Parcelable protocol requires subclassing "
2846 + "from Parcelable on class " + name);
Neil Fuller44e440c2015-04-20 14:39:00 +01002847 }
2848 Field f = parcelableClass.getField("CREATOR");
2849 if ((f.getModifiers() & Modifier.STATIC) == 0) {
2850 throw new BadParcelableException("Parcelable protocol requires "
2851 + "the CREATOR object to be static on class " + name);
2852 }
2853 Class<?> creatorType = f.getType();
2854 if (!Parcelable.Creator.class.isAssignableFrom(creatorType)) {
2855 // Fail before calling Field.get(), not after, to avoid initializing
2856 // parcelableClass unnecessarily.
2857 throw new BadParcelableException("Parcelable protocol requires a "
2858 + "Parcelable.Creator object called "
2859 + "CREATOR on class " + name);
2860 }
2861 creator = (Parcelable.Creator<?>) f.get(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 }
2863 catch (IllegalAccessException e) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002864 Log.e(TAG, "Illegal access when unmarshalling: " + name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 throw new BadParcelableException(
2866 "IllegalAccessException when unmarshalling: " + name);
2867 }
2868 catch (ClassNotFoundException e) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002869 Log.e(TAG, "Class not found when unmarshalling: " + name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 throw new BadParcelableException(
2871 "ClassNotFoundException when unmarshalling: " + name);
2872 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 catch (NoSuchFieldException e) {
2874 throw new BadParcelableException("Parcelable protocol requires a "
Neil Fuller44e440c2015-04-20 14:39:00 +01002875 + "Parcelable.Creator object called "
2876 + "CREATOR on class " + name);
Irfan Sheriff97a72f62013-01-11 10:45:51 -08002877 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 if (creator == null) {
2879 throw new BadParcelableException("Parcelable protocol requires a "
Neil Fuller44e440c2015-04-20 14:39:00 +01002880 + "non-null Parcelable.Creator object called "
2881 + "CREATOR on class " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 }
2883
2884 map.put(name, creator);
2885 }
2886 }
2887
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002888 return creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 }
2890
2891 /**
2892 * Read and return a new Parcelable array from the parcel.
2893 * The given class loader will be used to load any enclosed
2894 * Parcelables.
2895 * @return the Parcelable array, or null if the array is null
2896 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002897 @Nullable
2898 public final Parcelable[] readParcelableArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 int N = readInt();
2900 if (N < 0) {
2901 return null;
2902 }
2903 Parcelable[] p = new Parcelable[N];
2904 for (int i = 0; i < N; i++) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002905 p[i] = readParcelable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 }
2907 return p;
2908 }
2909
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002910 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002911 @Nullable
2912 public final <T extends Parcelable> T[] readParcelableArray(@Nullable ClassLoader loader,
2913 @NonNull Class<T> clazz) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002914 int N = readInt();
2915 if (N < 0) {
2916 return null;
2917 }
2918 T[] p = (T[]) Array.newInstance(clazz, N);
2919 for (int i = 0; i < N; i++) {
2920 p[i] = readParcelable(loader);
2921 }
2922 return p;
2923 }
2924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 /**
2926 * Read and return a new Serializable object from the parcel.
2927 * @return the Serializable object, or null if the Serializable name
2928 * wasn't found in the parcel.
2929 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002930 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002931 public final Serializable readSerializable() {
John Spurlock5002b8c2014-01-10 13:32:12 -05002932 return readSerializable(null);
2933 }
2934
Jake Whartonb1f474c2018-05-30 15:11:13 -04002935 @Nullable
2936 private final Serializable readSerializable(@Nullable final ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 String name = readString();
2938 if (name == null) {
2939 // For some reason we were unable to read the name of the Serializable (either there
2940 // is nothing left in the Parcel to read, or the next value wasn't a String), so
2941 // return null, which indicates that the name wasn't found in the parcel.
2942 return null;
2943 }
2944
2945 byte[] serializedData = createByteArray();
2946 ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
2947 try {
John Spurlock5002b8c2014-01-10 13:32:12 -05002948 ObjectInputStream ois = new ObjectInputStream(bais) {
2949 @Override
2950 protected Class<?> resolveClass(ObjectStreamClass osClass)
2951 throws IOException, ClassNotFoundException {
2952 // try the custom classloader if provided
2953 if (loader != null) {
2954 Class<?> c = Class.forName(osClass.getName(), false, loader);
2955 if (c != null) {
2956 return c;
2957 }
2958 }
2959 return super.resolveClass(osClass);
2960 }
2961 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 return (Serializable) ois.readObject();
2963 } catch (IOException ioe) {
2964 throw new RuntimeException("Parcelable encountered " +
2965 "IOException reading a Serializable object (name = " + name +
2966 ")", ioe);
2967 } catch (ClassNotFoundException cnfe) {
John Spurlock5002b8c2014-01-10 13:32:12 -05002968 throw new RuntimeException("Parcelable encountered " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 "ClassNotFoundException reading a Serializable object (name = "
2970 + name + ")", cnfe);
2971 }
2972 }
2973
2974 // Cache of previously looked up CREATOR.createFromParcel() methods for
2975 // particular classes. Keys are the names of the classes, values are
2976 // Method objects.
Neil Fuller44e440c2015-04-20 14:39:00 +01002977 private static final HashMap<ClassLoader,HashMap<String,Parcelable.Creator<?>>>
2978 mCreators = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979
Narayan Kamathb34a4612014-01-23 14:17:11 +00002980 /** @hide for internal use only. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 static protected final Parcel obtain(int obj) {
Ashok Bhat8ab665d2014-01-22 16:00:20 +00002982 throw new UnsupportedOperationException();
2983 }
2984
2985 /** @hide */
2986 static protected final Parcel obtain(long obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002987 final Parcel[] pool = sHolderPool;
2988 synchronized (pool) {
2989 Parcel p;
2990 for (int i=0; i<POOL_SIZE; i++) {
2991 p = pool[i];
2992 if (p != null) {
2993 pool[i] = null;
2994 if (DEBUG_RECYCLE) {
2995 p.mStack = new RuntimeException();
2996 }
2997 p.init(obj);
2998 return p;
2999 }
3000 }
3001 }
3002 return new Parcel(obj);
3003 }
3004
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003005 private Parcel(long nativePtr) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006 if (DEBUG_RECYCLE) {
3007 mStack = new RuntimeException();
3008 }
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003009 //Log.i(TAG, "Initializing obj=0x" + Integer.toHexString(obj), mStack);
Jeff Sharkey047238c2012-03-07 16:51:38 -08003010 init(nativePtr);
3011 }
3012
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003013 private void init(long nativePtr) {
Jeff Sharkey047238c2012-03-07 16:51:38 -08003014 if (nativePtr != 0) {
3015 mNativePtr = nativePtr;
3016 mOwnsNativeParcelObject = false;
3017 } else {
3018 mNativePtr = nativeCreate();
3019 mOwnsNativeParcelObject = true;
3020 }
3021 }
3022
3023 private void freeBuffer() {
3024 if (mOwnsNativeParcelObject) {
Adrian Roos04505652015-10-22 16:12:01 -07003025 updateNativeSize(nativeFreeBuffer(mNativePtr));
Jeff Sharkey047238c2012-03-07 16:51:38 -08003026 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -07003027 mReadWriteHelper = ReadWriteHelper.DEFAULT;
Jeff Sharkey047238c2012-03-07 16:51:38 -08003028 }
3029
3030 private void destroy() {
3031 if (mNativePtr != 0) {
3032 if (mOwnsNativeParcelObject) {
3033 nativeDestroy(mNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -07003034 updateNativeSize(0);
Jeff Sharkey047238c2012-03-07 16:51:38 -08003035 }
3036 mNativePtr = 0;
3037 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -07003038 mReadWriteHelper = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003039 }
3040
3041 @Override
3042 protected void finalize() throws Throwable {
3043 if (DEBUG_RECYCLE) {
3044 if (mStack != null) {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003045 Log.w(TAG, "Client did not call Parcel.recycle()", mStack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003046 }
3047 }
3048 destroy();
3049 }
3050
Jake Whartonb1f474c2018-05-30 15:11:13 -04003051 /* package */ void readMapInternal(@NonNull Map outVal, int N,
3052 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003053 while (N > 0) {
3054 Object key = readValue(loader);
3055 Object value = readValue(loader);
3056 outVal.put(key, value);
3057 N--;
3058 }
3059 }
3060
Jake Whartonb1f474c2018-05-30 15:11:13 -04003061 /* package */ void readArrayMapInternal(@NonNull ArrayMap outVal, int N,
3062 @Nullable ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003063 if (DEBUG_ARRAY_MAP) {
3064 RuntimeException here = new RuntimeException("here");
3065 here.fillInStackTrace();
3066 Log.d(TAG, "Reading " + N + " ArrayMap entries", here);
3067 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003068 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003069 while (N > 0) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003070 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003071 String key = readString();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003072 Object value = readValue(loader);
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003073 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " "
3074 + (dataPosition()-startPos) + " bytes: key=0x"
3075 + Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key);
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003076 outVal.append(key, value);
3077 N--;
3078 }
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003079 outVal.validate();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003080 }
3081
Jake Whartonb1f474c2018-05-30 15:11:13 -04003082 /* package */ void readArrayMapSafelyInternal(@NonNull ArrayMap outVal, int N,
3083 @Nullable ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003084 if (DEBUG_ARRAY_MAP) {
3085 RuntimeException here = new RuntimeException("here");
3086 here.fillInStackTrace();
3087 Log.d(TAG, "Reading safely " + N + " ArrayMap entries", here);
3088 }
3089 while (N > 0) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003090 String key = readString();
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003091 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read safe #" + (N-1) + ": key=0x"
3092 + (key != null ? key.hashCode() : 0) + " " + key);
3093 Object value = readValue(loader);
3094 outVal.put(key, value);
3095 N--;
3096 }
3097 }
3098
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003099 /**
3100 * @hide For testing only.
3101 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003102 public void readArrayMap(@NonNull ArrayMap outVal, @Nullable ClassLoader loader) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003103 final int N = readInt();
3104 if (N < 0) {
3105 return;
3106 }
3107 readArrayMapInternal(outVal, N, loader);
3108 }
3109
Svet Ganovddb94882016-06-23 19:55:24 -07003110 /**
3111 * Reads an array set.
3112 *
3113 * @param loader The class loader to use.
3114 *
3115 * @hide
3116 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003117 public @Nullable ArraySet<? extends Object> readArraySet(@Nullable ClassLoader loader) {
Svet Ganovddb94882016-06-23 19:55:24 -07003118 final int size = readInt();
3119 if (size < 0) {
3120 return null;
3121 }
3122 ArraySet<Object> result = new ArraySet<>(size);
3123 for (int i = 0; i < size; i++) {
3124 Object value = readValue(loader);
3125 result.append(value);
3126 }
3127 return result;
3128 }
3129
Jake Whartonb1f474c2018-05-30 15:11:13 -04003130 private void readListInternal(@NonNull List outVal, int N,
3131 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 while (N > 0) {
3133 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003134 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 outVal.add(value);
3136 N--;
3137 }
3138 }
3139
Jake Whartonb1f474c2018-05-30 15:11:13 -04003140 private void readArrayInternal(@NonNull Object[] outVal, int N,
3141 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 for (int i = 0; i < N; i++) {
3143 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003144 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 outVal[i] = value;
3146 }
3147 }
3148
Jake Whartonb1f474c2018-05-30 15:11:13 -04003149 private void readSparseArrayInternal(@NonNull SparseArray outVal, int N,
3150 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 while (N > 0) {
3152 int key = readInt();
3153 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003154 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003155 outVal.append(key, value);
3156 N--;
3157 }
3158 }
3159
3160
Jake Whartonb1f474c2018-05-30 15:11:13 -04003161 private void readSparseBooleanArrayInternal(@NonNull SparseBooleanArray outVal, int N) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 while (N > 0) {
3163 int key = readInt();
3164 boolean value = this.readByte() == 1;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003165 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166 outVal.append(key, value);
3167 N--;
3168 }
3169 }
Dan Sandler5ce04302015-04-09 23:50:15 -04003170
Jake Whartonb1f474c2018-05-30 15:11:13 -04003171 private void readSparseIntArrayInternal(@NonNull SparseIntArray outVal, int N) {
Adam Lesinski4e862812016-11-21 16:02:24 -08003172 while (N > 0) {
3173 int key = readInt();
3174 int value = readInt();
3175 outVal.append(key, value);
3176 N--;
3177 }
3178 }
3179
Dan Sandler5ce04302015-04-09 23:50:15 -04003180 /**
3181 * @hide For testing
3182 */
3183 public long getBlobAshmemSize() {
3184 return nativeGetBlobAshmemSize(mNativePtr);
3185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186}