blob: 2efb0f5e419d79b0c9a577ca5d9a22c1acc1395b [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
Svet Ganovddb94882016-06-23 19:55:24 -070019import android.annotation.Nullable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.text.TextUtils;
Dianne Hackbornb87655b2013-07-17 19:06:22 -070021import android.util.ArrayMap;
Svet Ganovddb94882016-06-23 19:55:24 -070022import android.util.ArraySet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.util.Log;
Jeff Sharkey5ef33982014-09-04 18:13:39 -070024import android.util.Size;
25import android.util.SizeF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.util.SparseArray;
27import android.util.SparseBooleanArray;
Adam Lesinski4e862812016-11-21 16:02:24 -080028import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
Makoto Onukib148b6c2017-06-27 13:38:38 -070030import dalvik.annotation.optimization.CriticalNative;
Jeff Sharkey789a8fc2017-04-16 13:18:35 -060031import dalvik.annotation.optimization.FastNative;
32import dalvik.system.VMRuntime;
33
Jeff Sharkeye628b7d2017-01-17 13:50:20 -070034import libcore.util.SneakyThrow;
35
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import java.io.ByteArrayInputStream;
37import java.io.ByteArrayOutputStream;
38import java.io.FileDescriptor;
39import java.io.FileNotFoundException;
40import java.io.IOException;
41import java.io.ObjectInputStream;
42import java.io.ObjectOutputStream;
John Spurlock5002b8c2014-01-10 13:32:12 -050043import java.io.ObjectStreamClass;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import java.io.Serializable;
Makoto Onuki440a1ea2016-07-20 14:21:18 -070045import java.lang.reflect.Array;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import java.lang.reflect.Field;
Neil Fuller44e440c2015-04-20 14:39:00 +010047import java.lang.reflect.Modifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import java.util.ArrayList;
Elliott Hughesa28b83e2011-02-28 14:26:13 -080049import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import java.util.HashMap;
51import java.util.List;
52import java.util.Map;
53import java.util.Set;
Adrian Roos04505652015-10-22 16:12:01 -070054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055/**
56 * Container for a message (data and object references) that can
57 * be sent through an IBinder. A Parcel can contain both flattened data
58 * that will be unflattened on the other side of the IPC (using the various
59 * methods here for writing specific types, or the general
60 * {@link Parcelable} interface), and references to live {@link IBinder}
61 * objects that will result in the other side receiving a proxy IBinder
62 * connected with the original IBinder in the Parcel.
63 *
64 * <p class="note">Parcel is <strong>not</strong> a general-purpose
65 * serialization mechanism. This class (and the corresponding
66 * {@link Parcelable} API for placing arbitrary objects into a Parcel) is
67 * designed as a high-performance IPC transport. As such, it is not
68 * appropriate to place any Parcel data in to persistent storage: changes
69 * in the underlying implementation of any of the data in the Parcel can
70 * render older data unreadable.</p>
Samuel Tana8036662015-11-23 14:36:00 -080071 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 * <p>The bulk of the Parcel API revolves around reading and writing data
73 * of various types. There are six major classes of such functions available.</p>
Samuel Tana8036662015-11-23 14:36:00 -080074 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 * <h3>Primitives</h3>
Samuel Tana8036662015-11-23 14:36:00 -080076 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 * <p>The most basic data functions are for writing and reading primitive
78 * data types: {@link #writeByte}, {@link #readByte}, {@link #writeDouble},
79 * {@link #readDouble}, {@link #writeFloat}, {@link #readFloat}, {@link #writeInt},
80 * {@link #readInt}, {@link #writeLong}, {@link #readLong},
81 * {@link #writeString}, {@link #readString}. Most other
82 * data operations are built on top of these. The given data is written and
83 * read using the endianess of the host CPU.</p>
Samuel Tana8036662015-11-23 14:36:00 -080084 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 * <h3>Primitive Arrays</h3>
Samuel Tana8036662015-11-23 14:36:00 -080086 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 * <p>There are a variety of methods for reading and writing raw arrays
88 * of primitive objects, which generally result in writing a 4-byte length
89 * followed by the primitive data items. The methods for reading can either
90 * read the data into an existing array, or create and return a new array.
91 * These available types are:</p>
Samuel Tana8036662015-11-23 14:36:00 -080092 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 * <ul>
94 * <li> {@link #writeBooleanArray(boolean[])},
95 * {@link #readBooleanArray(boolean[])}, {@link #createBooleanArray()}
96 * <li> {@link #writeByteArray(byte[])},
97 * {@link #writeByteArray(byte[], int, int)}, {@link #readByteArray(byte[])},
98 * {@link #createByteArray()}
99 * <li> {@link #writeCharArray(char[])}, {@link #readCharArray(char[])},
100 * {@link #createCharArray()}
101 * <li> {@link #writeDoubleArray(double[])}, {@link #readDoubleArray(double[])},
102 * {@link #createDoubleArray()}
103 * <li> {@link #writeFloatArray(float[])}, {@link #readFloatArray(float[])},
104 * {@link #createFloatArray()}
105 * <li> {@link #writeIntArray(int[])}, {@link #readIntArray(int[])},
106 * {@link #createIntArray()}
107 * <li> {@link #writeLongArray(long[])}, {@link #readLongArray(long[])},
108 * {@link #createLongArray()}
109 * <li> {@link #writeStringArray(String[])}, {@link #readStringArray(String[])},
110 * {@link #createStringArray()}.
111 * <li> {@link #writeSparseBooleanArray(SparseBooleanArray)},
112 * {@link #readSparseBooleanArray()}.
113 * </ul>
Samuel Tana8036662015-11-23 14:36:00 -0800114 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 * <h3>Parcelables</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800116 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 * <p>The {@link Parcelable} protocol provides an extremely efficient (but
118 * low-level) protocol for objects to write and read themselves from Parcels.
119 * You can use the direct methods {@link #writeParcelable(Parcelable, int)}
120 * and {@link #readParcelable(ClassLoader)} or
121 * {@link #writeParcelableArray} and
122 * {@link #readParcelableArray(ClassLoader)} to write or read. These
123 * methods write both the class type and its data to the Parcel, allowing
124 * that class to be reconstructed from the appropriate class loader when
125 * later reading.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800126 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 * <p>There are also some methods that provide a more efficient way to work
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -0700128 * with Parcelables: {@link #writeTypedObject}, {@link #writeTypedArray},
129 * {@link #writeTypedList}, {@link #readTypedObject},
130 * {@link #createTypedArray} and {@link #createTypedArrayList}. These methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 * do not write the class information of the original object: instead, the
132 * caller of the read function must know what type to expect and pass in the
133 * appropriate {@link Parcelable.Creator Parcelable.Creator} instead to
134 * properly construct the new object and read its data. (To more efficient
Bin Chenb6b12b52017-07-11 11:01:44 +0800135 * write and read a single Parcelable object that is not null, you can directly
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -0700136 * call {@link Parcelable#writeToParcel Parcelable.writeToParcel} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 * {@link Parcelable.Creator#createFromParcel Parcelable.Creator.createFromParcel}
138 * yourself.)</p>
Samuel Tana8036662015-11-23 14:36:00 -0800139 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * <h3>Bundles</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800141 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 * <p>A special type-safe container, called {@link Bundle}, is available
143 * for key/value maps of heterogeneous values. This has many optimizations
144 * for improved performance when reading and writing data, and its type-safe
145 * API avoids difficult to debug type errors when finally marshalling the
146 * data contents into a Parcel. The methods to use are
147 * {@link #writeBundle(Bundle)}, {@link #readBundle()}, and
148 * {@link #readBundle(ClassLoader)}.
Samuel Tana8036662015-11-23 14:36:00 -0800149 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 * <h3>Active Objects</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800151 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 * <p>An unusual feature of Parcel is the ability to read and write active
153 * objects. For these objects the actual contents of the object is not
154 * written, rather a special token referencing the object is written. When
155 * reading the object back from the Parcel, you do not get a new instance of
156 * the object, but rather a handle that operates on the exact same object that
157 * was originally written. There are two forms of active objects available.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800158 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 * <p>{@link Binder} objects are a core facility of Android's general cross-process
160 * communication system. The {@link IBinder} interface describes an abstract
161 * protocol with a Binder object. Any such interface can be written in to
162 * a Parcel, and upon reading you will receive either the original object
163 * implementing that interface or a special proxy implementation
164 * that communicates calls back to the original object. The methods to use are
165 * {@link #writeStrongBinder(IBinder)},
166 * {@link #writeStrongInterface(IInterface)}, {@link #readStrongBinder()},
167 * {@link #writeBinderArray(IBinder[])}, {@link #readBinderArray(IBinder[])},
168 * {@link #createBinderArray()},
169 * {@link #writeBinderList(List)}, {@link #readBinderList(List)},
170 * {@link #createBinderArrayList()}.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800171 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 * <p>FileDescriptor objects, representing raw Linux file descriptor identifiers,
173 * can be written and {@link ParcelFileDescriptor} objects returned to operate
174 * on the original file descriptor. The returned file descriptor is a dup
175 * of the original file descriptor: the object and fd is different, but
176 * operating on the same underlying file stream, with the same position, etc.
177 * The methods to use are {@link #writeFileDescriptor(FileDescriptor)},
178 * {@link #readFileDescriptor()}.
Samuel Tana8036662015-11-23 14:36:00 -0800179 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 * <h3>Untyped Containers</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800181 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 * <p>A final class of methods are for writing and reading standard Java
183 * containers of arbitrary types. These all revolve around the
184 * {@link #writeValue(Object)} and {@link #readValue(ClassLoader)} methods
185 * which define the types of objects allowed. The container methods are
186 * {@link #writeArray(Object[])}, {@link #readArray(ClassLoader)},
187 * {@link #writeList(List)}, {@link #readList(List, ClassLoader)},
188 * {@link #readArrayList(ClassLoader)},
189 * {@link #writeMap(Map)}, {@link #readMap(Map, ClassLoader)},
190 * {@link #writeSparseArray(SparseArray)},
191 * {@link #readSparseArray(ClassLoader)}.
192 */
193public final class Parcel {
194 private static final boolean DEBUG_RECYCLE = false;
Dianne Hackborne784d1e2013-09-20 18:13:52 -0700195 private static final boolean DEBUG_ARRAY_MAP = false;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700196 private static final String TAG = "Parcel";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197
198 @SuppressWarnings({"UnusedDeclaration"})
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000199 private long mNativePtr; // used by native code
Jeff Sharkey047238c2012-03-07 16:51:38 -0800200
201 /**
202 * Flag indicating if {@link #mNativePtr} was allocated by this object,
203 * indicating that we're responsible for its lifecycle.
204 */
205 private boolean mOwnsNativeParcelObject;
Adrian Roos04505652015-10-22 16:12:01 -0700206 private long mNativeSize;
Jeff Sharkey047238c2012-03-07 16:51:38 -0800207
Dianne Hackborn98305522017-05-05 17:53:53 -0700208 private ArrayMap<Class, Object> mClassCookies;
209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 private RuntimeException mStack;
211
212 private static final int POOL_SIZE = 6;
213 private static final Parcel[] sOwnedPool = new Parcel[POOL_SIZE];
214 private static final Parcel[] sHolderPool = new Parcel[POOL_SIZE];
215
Robert Quattlebaum9cd7af32017-01-04 13:28:01 -0800216 // Keep in sync with frameworks/native/include/private/binder/ParcelValTypes.h.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 private static final int VAL_NULL = -1;
218 private static final int VAL_STRING = 0;
219 private static final int VAL_INTEGER = 1;
220 private static final int VAL_MAP = 2;
221 private static final int VAL_BUNDLE = 3;
222 private static final int VAL_PARCELABLE = 4;
223 private static final int VAL_SHORT = 5;
224 private static final int VAL_LONG = 6;
225 private static final int VAL_FLOAT = 7;
226 private static final int VAL_DOUBLE = 8;
227 private static final int VAL_BOOLEAN = 9;
228 private static final int VAL_CHARSEQUENCE = 10;
229 private static final int VAL_LIST = 11;
230 private static final int VAL_SPARSEARRAY = 12;
231 private static final int VAL_BYTEARRAY = 13;
232 private static final int VAL_STRINGARRAY = 14;
233 private static final int VAL_IBINDER = 15;
234 private static final int VAL_PARCELABLEARRAY = 16;
235 private static final int VAL_OBJECTARRAY = 17;
236 private static final int VAL_INTARRAY = 18;
237 private static final int VAL_LONGARRAY = 19;
238 private static final int VAL_BYTE = 20;
239 private static final int VAL_SERIALIZABLE = 21;
240 private static final int VAL_SPARSEBOOLEANARRAY = 22;
241 private static final int VAL_BOOLEANARRAY = 23;
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000242 private static final int VAL_CHARSEQUENCEARRAY = 24;
Craig Mautner719e6b12014-04-04 20:29:41 -0700243 private static final int VAL_PERSISTABLEBUNDLE = 25;
Jeff Sharkey5ef33982014-09-04 18:13:39 -0700244 private static final int VAL_SIZE = 26;
245 private static final int VAL_SIZEF = 27;
Samuel Tana8036662015-11-23 14:36:00 -0800246 private static final int VAL_DOUBLEARRAY = 28;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700248 // The initial int32 in a Binder call's reply Parcel header:
Christopher Wiley80fd1202015-11-22 17:12:37 -0800249 // Keep these in sync with libbinder's binder/Status.h.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 private static final int EX_SECURITY = -1;
251 private static final int EX_BAD_PARCELABLE = -2;
252 private static final int EX_ILLEGAL_ARGUMENT = -3;
253 private static final int EX_NULL_POINTER = -4;
254 private static final int EX_ILLEGAL_STATE = -5;
Dianne Hackborn7e714422013-09-13 17:32:57 -0700255 private static final int EX_NETWORK_MAIN_THREAD = -6;
Dianne Hackborn33d738a2014-09-12 14:23:58 -0700256 private static final int EX_UNSUPPORTED_OPERATION = -7;
Christopher Wiley80fd1202015-11-22 17:12:37 -0800257 private static final int EX_SERVICE_SPECIFIC = -8;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -0700258 private static final int EX_PARCELABLE = -9;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700259 private static final int EX_HAS_REPLY_HEADER = -128; // special; see below
Christopher Wiley80fd1202015-11-22 17:12:37 -0800260 // EX_TRANSACTION_FAILED is used exclusively in native code.
261 // see libbinder's binder/Status.h
262 private static final int EX_TRANSACTION_FAILED = -129;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700263
Makoto Onukib148b6c2017-06-27 13:38:38 -0700264 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000265 private static native int nativeDataSize(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700266 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000267 private static native int nativeDataAvail(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700268 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000269 private static native int nativeDataPosition(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700270 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000271 private static native int nativeDataCapacity(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700272 @FastNative
Adrian Roos04505652015-10-22 16:12:01 -0700273 private static native long nativeSetDataSize(long nativePtr, int size);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700274 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000275 private static native void nativeSetDataPosition(long nativePtr, int pos);
John Reck71207b52016-09-28 13:28:09 -0700276 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000277 private static native void nativeSetDataCapacity(long nativePtr, int size);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800278
Makoto Onukib148b6c2017-06-27 13:38:38 -0700279 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000280 private static native boolean nativePushAllowFds(long nativePtr, boolean allowFds);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700281 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000282 private static native void nativeRestoreAllowFds(long nativePtr, boolean lastValue);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800283
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000284 private static native void nativeWriteByteArray(long nativePtr, byte[] b, int offset, int len);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700285 private static native void nativeWriteBlob(long nativePtr, byte[] b, int offset, int len);
John Reck71207b52016-09-28 13:28:09 -0700286 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000287 private static native void nativeWriteInt(long nativePtr, int val);
John Reck71207b52016-09-28 13:28:09 -0700288 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000289 private static native void nativeWriteLong(long nativePtr, long val);
John Reck71207b52016-09-28 13:28:09 -0700290 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000291 private static native void nativeWriteFloat(long nativePtr, float val);
John Reck71207b52016-09-28 13:28:09 -0700292 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000293 private static native void nativeWriteDouble(long nativePtr, double val);
Makoto Onuki63e624a2017-08-01 00:42:33 +0000294 private static native void nativeWriteString(long nativePtr, String val);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000295 private static native void nativeWriteStrongBinder(long nativePtr, IBinder val);
Adrian Roos04505652015-10-22 16:12:01 -0700296 private static native long nativeWriteFileDescriptor(long nativePtr, FileDescriptor val);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800297
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000298 private static native byte[] nativeCreateByteArray(long nativePtr);
Jocelyn Dang46100442017-05-05 15:40:49 -0700299 private static native boolean nativeReadByteArray(long nativePtr, byte[] dest, int destLen);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700300 private static native byte[] nativeReadBlob(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700301 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000302 private static native int nativeReadInt(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700303 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000304 private static native long nativeReadLong(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700305 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000306 private static native float nativeReadFloat(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700307 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000308 private static native double nativeReadDouble(long nativePtr);
Makoto Onuki63e624a2017-08-01 00:42:33 +0000309 private static native String nativeReadString(long nativePtr);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000310 private static native IBinder nativeReadStrongBinder(long nativePtr);
311 private static native FileDescriptor nativeReadFileDescriptor(long nativePtr);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800312
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000313 private static native long nativeCreate();
Adrian Roos04505652015-10-22 16:12:01 -0700314 private static native long nativeFreeBuffer(long nativePtr);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000315 private static native void nativeDestroy(long nativePtr);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800316
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000317 private static native byte[] nativeMarshall(long nativePtr);
Adrian Roos04505652015-10-22 16:12:01 -0700318 private static native long nativeUnmarshall(
John Spurlocke0852362015-02-04 15:47:40 -0500319 long nativePtr, byte[] data, int offset, int length);
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700320 private static native int nativeCompareData(long thisNativePtr, long otherNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -0700321 private static native long nativeAppendFrom(
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000322 long thisNativePtr, long otherNativePtr, int offset, int length);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700323 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000324 private static native boolean nativeHasFileDescriptors(long nativePtr);
325 private static native void nativeWriteInterfaceToken(long nativePtr, String interfaceName);
326 private static native void nativeEnforceInterface(long nativePtr, String interfaceName);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800327
Makoto Onukib148b6c2017-06-27 13:38:38 -0700328 @CriticalNative
Dan Sandleraa861662015-04-21 10:24:32 -0400329 private static native long nativeGetBlobAshmemSize(long nativePtr);
Dan Sandler5ce04302015-04-09 23:50:15 -0400330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 public final static Parcelable.Creator<String> STRING_CREATOR
332 = new Parcelable.Creator<String>() {
333 public String createFromParcel(Parcel source) {
334 return source.readString();
335 }
336 public String[] newArray(int size) {
337 return new String[size];
338 }
339 };
340
341 /**
342 * Retrieve a new Parcel object from the pool.
343 */
344 public static Parcel obtain() {
345 final Parcel[] pool = sOwnedPool;
346 synchronized (pool) {
347 Parcel p;
348 for (int i=0; i<POOL_SIZE; i++) {
349 p = pool[i];
350 if (p != null) {
351 pool[i] = null;
352 if (DEBUG_RECYCLE) {
353 p.mStack = new RuntimeException();
354 }
355 return p;
356 }
357 }
358 }
359 return new Parcel(0);
360 }
361
362 /**
363 * Put a Parcel object back into the pool. You must not touch
364 * the object after this call.
365 */
366 public final void recycle() {
367 if (DEBUG_RECYCLE) mStack = null;
368 freeBuffer();
Jeff Sharkey047238c2012-03-07 16:51:38 -0800369
370 final Parcel[] pool;
371 if (mOwnsNativeParcelObject) {
372 pool = sOwnedPool;
373 } else {
374 mNativePtr = 0;
375 pool = sHolderPool;
376 }
377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 synchronized (pool) {
379 for (int i=0; i<POOL_SIZE; i++) {
380 if (pool[i] == null) {
381 pool[i] = this;
382 return;
383 }
384 }
385 }
386 }
387
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800388 /** @hide */
389 public static native long getGlobalAllocSize();
390
391 /** @hide */
392 public static native long getGlobalAllocCount();
393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 /**
395 * Returns the total amount of data contained in the parcel.
396 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800397 public final int dataSize() {
398 return nativeDataSize(mNativePtr);
399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400
401 /**
402 * Returns the amount of data remaining to be read from the
403 * parcel. That is, {@link #dataSize}-{@link #dataPosition}.
404 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800405 public final int dataAvail() {
406 return nativeDataAvail(mNativePtr);
407 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408
409 /**
410 * Returns the current position in the parcel data. Never
411 * more than {@link #dataSize}.
412 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800413 public final int dataPosition() {
414 return nativeDataPosition(mNativePtr);
415 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416
417 /**
418 * Returns the total amount of space in the parcel. This is always
419 * >= {@link #dataSize}. The difference between it and dataSize() is the
420 * amount of room left until the parcel needs to re-allocate its
421 * data buffer.
422 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800423 public final int dataCapacity() {
424 return nativeDataCapacity(mNativePtr);
425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426
427 /**
428 * Change the amount of data in the parcel. Can be either smaller or
429 * larger than the current size. If larger than the current capacity,
430 * more memory will be allocated.
431 *
432 * @param size The new number of bytes in the Parcel.
433 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800434 public final void setDataSize(int size) {
Michael Wachenschwanz138bebf2017-05-18 22:09:18 +0000435 updateNativeSize(nativeSetDataSize(mNativePtr, size));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437
438 /**
439 * Move the current read/write position in the parcel.
440 * @param pos New offset in the parcel; must be between 0 and
441 * {@link #dataSize}.
442 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800443 public final void setDataPosition(int pos) {
444 nativeSetDataPosition(mNativePtr, pos);
445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446
447 /**
448 * Change the capacity (current available space) of the parcel.
449 *
450 * @param size The new capacity of the parcel, in bytes. Can not be
451 * less than {@link #dataSize} -- that is, you can not drop existing data
452 * with this method.
453 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800454 public final void setDataCapacity(int size) {
455 nativeSetDataCapacity(mNativePtr, size);
456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400458 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800459 public final boolean pushAllowFds(boolean allowFds) {
460 return nativePushAllowFds(mNativePtr, allowFds);
461 }
Dianne Hackbornc04db7e2011-10-03 21:09:35 -0700462
463 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800464 public final void restoreAllowFds(boolean lastValue) {
465 nativeRestoreAllowFds(mNativePtr, lastValue);
466 }
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 /**
469 * Returns the raw bytes of the parcel.
470 *
471 * <p class="note">The data you retrieve here <strong>must not</strong>
472 * be placed in any kind of persistent storage (on local disk, across
473 * a network, etc). For that, you should use standard serialization
474 * or another kind of general serialization mechanism. The Parcel
475 * marshalled representation is highly optimized for local IPC, and as
476 * such does not attempt to maintain compatibility with data created
477 * in different versions of the platform.
478 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800479 public final byte[] marshall() {
480 return nativeMarshall(mNativePtr);
481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482
483 /**
484 * Set the bytes in data to be the raw bytes of this Parcel.
485 */
John Spurlocke0852362015-02-04 15:47:40 -0500486 public final void unmarshall(byte[] data, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700487 updateNativeSize(nativeUnmarshall(mNativePtr, data, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489
Jeff Sharkey047238c2012-03-07 16:51:38 -0800490 public final void appendFrom(Parcel parcel, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700491 updateNativeSize(nativeAppendFrom(mNativePtr, parcel.mNativePtr, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700494 /** @hide */
495 public final int compareData(Parcel other) {
496 return nativeCompareData(mNativePtr, other.mNativePtr);
497 }
498
Dianne Hackborn98305522017-05-05 17:53:53 -0700499 /** @hide */
500 public final void setClassCookie(Class clz, Object cookie) {
501 if (mClassCookies == null) {
502 mClassCookies = new ArrayMap<>();
503 }
504 mClassCookies.put(clz, cookie);
505 }
506
507 /** @hide */
508 public final Object getClassCookie(Class clz) {
509 return mClassCookies != null ? mClassCookies.get(clz) : null;
510 }
511
512 /** @hide */
513 public final void adoptClassCookies(Parcel from) {
514 mClassCookies = from.mClassCookies;
515 }
516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 /**
518 * Report whether the parcel contains any marshalled file descriptors.
519 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800520 public final boolean hasFileDescriptors() {
521 return nativeHasFileDescriptors(mNativePtr);
522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523
524 /**
525 * Store or read an IBinder interface token in the parcel at the current
526 * {@link #dataPosition}. This is used to validate that the marshalled
527 * transaction is intended for the target interface.
528 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800529 public final void writeInterfaceToken(String interfaceName) {
530 nativeWriteInterfaceToken(mNativePtr, interfaceName);
531 }
532
533 public final void enforceInterface(String interfaceName) {
534 nativeEnforceInterface(mNativePtr, interfaceName);
535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536
537 /**
Elliott Hughesa28b83e2011-02-28 14:26:13 -0800538 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 * growing {@link #dataCapacity} if needed.
540 * @param b Bytes to place into the parcel.
541 */
542 public final void writeByteArray(byte[] b) {
543 writeByteArray(b, 0, (b != null) ? b.length : 0);
544 }
545
546 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900547 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 * growing {@link #dataCapacity} if needed.
549 * @param b Bytes to place into the parcel.
550 * @param offset Index of first byte to be written.
551 * @param len Number of bytes to write.
552 */
553 public final void writeByteArray(byte[] b, int offset, int len) {
554 if (b == null) {
555 writeInt(-1);
556 return;
557 }
Elliott Hughesa28b83e2011-02-28 14:26:13 -0800558 Arrays.checkOffsetAndCount(b.length, offset, len);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800559 nativeWriteByteArray(mNativePtr, b, offset, len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 }
561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700563 * Write a blob of data into the parcel at the current {@link #dataPosition},
564 * growing {@link #dataCapacity} if needed.
565 * @param b Bytes to place into the parcel.
566 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -0700567 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700568 */
569 public final void writeBlob(byte[] b) {
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500570 writeBlob(b, 0, (b != null) ? b.length : 0);
571 }
572
573 /**
574 * Write a blob of data into the parcel at the current {@link #dataPosition},
575 * growing {@link #dataCapacity} if needed.
576 * @param b Bytes to place into the parcel.
577 * @param offset Index of first byte to be written.
578 * @param len Number of bytes to write.
579 * {@hide}
580 * {@SystemApi}
581 */
582 public final void writeBlob(byte[] b, int offset, int len) {
583 if (b == null) {
584 writeInt(-1);
585 return;
586 }
587 Arrays.checkOffsetAndCount(b.length, offset, len);
588 nativeWriteBlob(mNativePtr, b, offset, len);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700589 }
590
591 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 * Write an integer value into the parcel at the current dataPosition(),
593 * growing dataCapacity() if needed.
594 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800595 public final void writeInt(int val) {
596 nativeWriteInt(mNativePtr, val);
597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598
599 /**
600 * Write a long integer value into the parcel at the current dataPosition(),
601 * growing dataCapacity() if needed.
602 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800603 public final void writeLong(long val) {
604 nativeWriteLong(mNativePtr, val);
605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606
607 /**
608 * Write a floating point value into the parcel at the current
609 * dataPosition(), growing dataCapacity() if needed.
610 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800611 public final void writeFloat(float val) {
612 nativeWriteFloat(mNativePtr, val);
613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614
615 /**
616 * Write a double precision floating point value into the parcel at the
617 * current dataPosition(), growing dataCapacity() if needed.
618 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800619 public final void writeDouble(double val) {
620 nativeWriteDouble(mNativePtr, val);
621 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622
623 /**
624 * Write a string value into the parcel at the current dataPosition(),
625 * growing dataCapacity() if needed.
626 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800627 public final void writeString(String val) {
628 nativeWriteString(mNativePtr, val);
629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630
Eugene Susla36e866b2017-02-23 18:24:39 -0800631 /** @hide */
632 public final void writeBoolean(boolean val) {
633 writeInt(val ? 1 : 0);
634 }
635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000637 * Write a CharSequence value into the parcel at the current dataPosition(),
638 * growing dataCapacity() if needed.
639 * @hide
640 */
641 public final void writeCharSequence(CharSequence val) {
642 TextUtils.writeToParcel(val, this, 0);
643 }
644
645 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 * Write an object into the parcel at the current dataPosition(),
647 * growing dataCapacity() if needed.
648 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800649 public final void writeStrongBinder(IBinder val) {
650 nativeWriteStrongBinder(mNativePtr, val);
651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652
653 /**
654 * Write an object into the parcel at the current dataPosition(),
655 * growing dataCapacity() if needed.
656 */
657 public final void writeStrongInterface(IInterface val) {
658 writeStrongBinder(val == null ? null : val.asBinder());
659 }
660
661 /**
662 * Write a FileDescriptor into the parcel at the current dataPosition(),
663 * growing dataCapacity() if needed.
Dan Egnorb3e4ef32010-07-20 09:03:35 -0700664 *
665 * <p class="caution">The file descriptor will not be closed, which may
666 * result in file descriptor leaks when objects are returned from Binder
667 * calls. Use {@link ParcelFileDescriptor#writeToParcel} instead, which
668 * accepts contextual flags and will close the original file descriptor
669 * if {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} is set.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800671 public final void writeFileDescriptor(FileDescriptor val) {
Adrian Roos04505652015-10-22 16:12:01 -0700672 updateNativeSize(nativeWriteFileDescriptor(mNativePtr, val));
673 }
674
675 private void updateNativeSize(long newNativeSize) {
676 if (mOwnsNativeParcelObject) {
677 if (newNativeSize > Integer.MAX_VALUE) {
678 newNativeSize = Integer.MAX_VALUE;
679 }
680 if (newNativeSize != mNativeSize) {
681 int delta = (int) (newNativeSize - mNativeSize);
682 if (delta > 0) {
683 VMRuntime.getRuntime().registerNativeAllocation(delta);
684 } else {
685 VMRuntime.getRuntime().registerNativeFree(-delta);
686 }
687 mNativeSize = newNativeSize;
688 }
689 }
Jeff Sharkey047238c2012-03-07 16:51:38 -0800690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691
692 /**
Casey Dahlin2f974b22015-11-05 12:19:13 -0800693 * {@hide}
694 * This will be the new name for writeFileDescriptor, for consistency.
695 **/
696 public final void writeRawFileDescriptor(FileDescriptor val) {
697 nativeWriteFileDescriptor(mNativePtr, val);
698 }
699
700 /**
701 * {@hide}
702 * Write an array of FileDescriptor objects into the Parcel.
703 *
704 * @param value The array of objects to be written.
705 */
706 public final void writeRawFileDescriptorArray(FileDescriptor[] value) {
707 if (value != null) {
708 int N = value.length;
709 writeInt(N);
710 for (int i=0; i<N; i++) {
711 writeRawFileDescriptor(value[i]);
712 }
713 } else {
714 writeInt(-1);
715 }
716 }
717
718 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900719 * Write a byte value into the parcel at the current dataPosition(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 * growing dataCapacity() if needed.
721 */
722 public final void writeByte(byte val) {
723 writeInt(val);
724 }
725
726 /**
727 * Please use {@link #writeBundle} instead. Flattens a Map into the parcel
728 * at the current dataPosition(),
729 * growing dataCapacity() if needed. The Map keys must be String objects.
730 * The Map values are written using {@link #writeValue} and must follow
731 * the specification there.
Samuel Tana8036662015-11-23 14:36:00 -0800732 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 * <p>It is strongly recommended to use {@link #writeBundle} instead of
734 * this method, since the Bundle class provides a type-safe API that
735 * allows you to avoid mysterious type errors at the point of marshalling.
736 */
737 public final void writeMap(Map val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700738 writeMapInternal((Map<String, Object>) val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 }
740
741 /**
742 * Flatten a Map into the parcel at the current dataPosition(),
743 * growing dataCapacity() if needed. The Map keys must be String objects.
744 */
Dianne Hackborn6aff9052009-05-22 13:20:23 -0700745 /* package */ void writeMapInternal(Map<String,Object> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 if (val == null) {
747 writeInt(-1);
748 return;
749 }
750 Set<Map.Entry<String,Object>> entries = val.entrySet();
751 writeInt(entries.size());
752 for (Map.Entry<String,Object> e : entries) {
753 writeValue(e.getKey());
754 writeValue(e.getValue());
755 }
756 }
757
758 /**
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700759 * Flatten an ArrayMap into the parcel at the current dataPosition(),
760 * growing dataCapacity() if needed. The Map keys must be String objects.
761 */
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700762 /* package */ void writeArrayMapInternal(ArrayMap<String, Object> val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700763 if (val == null) {
764 writeInt(-1);
765 return;
766 }
Samuel Tan3cefe6a2015-12-14 13:29:17 -0800767 // Keep the format of this Parcel in sync with writeToParcelInner() in
768 // frameworks/native/libs/binder/PersistableBundle.cpp.
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700769 final int N = val.size();
770 writeInt(N);
Dianne Hackborne784d1e2013-09-20 18:13:52 -0700771 if (DEBUG_ARRAY_MAP) {
772 RuntimeException here = new RuntimeException("here");
773 here.fillInStackTrace();
774 Log.d(TAG, "Writing " + N + " ArrayMap entries", here);
775 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700776 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700777 for (int i=0; i<N; i++) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700778 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700779 writeString(val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700780 writeValue(val.valueAt(i));
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700781 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Write #" + i + " "
782 + (dataPosition()-startPos) + " bytes: key=0x"
783 + Integer.toHexString(val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0)
784 + " " + val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700785 }
786 }
787
788 /**
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700789 * @hide For testing only.
790 */
791 public void writeArrayMap(ArrayMap<String, Object> val) {
792 writeArrayMapInternal(val);
793 }
794
795 /**
Svet Ganovddb94882016-06-23 19:55:24 -0700796 * Write an array set to the parcel.
797 *
798 * @param val The array set to write.
799 *
800 * @hide
801 */
802 public void writeArraySet(@Nullable ArraySet<? extends Object> val) {
803 final int size = (val != null) ? val.size() : -1;
804 writeInt(size);
805 for (int i = 0; i < size; i++) {
806 writeValue(val.valueAt(i));
807 }
808 }
809
810 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 * Flatten a Bundle into the parcel at the current dataPosition(),
812 * growing dataCapacity() if needed.
813 */
814 public final void writeBundle(Bundle val) {
815 if (val == null) {
816 writeInt(-1);
817 return;
818 }
819
Dianne Hackborn6aff9052009-05-22 13:20:23 -0700820 val.writeToParcel(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 }
822
823 /**
Craig Mautner719e6b12014-04-04 20:29:41 -0700824 * Flatten a PersistableBundle into the parcel at the current dataPosition(),
825 * growing dataCapacity() if needed.
826 */
827 public final void writePersistableBundle(PersistableBundle val) {
828 if (val == null) {
829 writeInt(-1);
830 return;
831 }
832
833 val.writeToParcel(this, 0);
834 }
835
836 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -0700837 * Flatten a Size into the parcel at the current dataPosition(),
838 * growing dataCapacity() if needed.
839 */
840 public final void writeSize(Size val) {
841 writeInt(val.getWidth());
842 writeInt(val.getHeight());
843 }
844
845 /**
846 * Flatten a SizeF into the parcel at the current dataPosition(),
847 * growing dataCapacity() if needed.
848 */
849 public final void writeSizeF(SizeF val) {
850 writeFloat(val.getWidth());
851 writeFloat(val.getHeight());
852 }
853
854 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 * Flatten a List into the parcel at the current dataPosition(), growing
856 * dataCapacity() if needed. The List values are written using
857 * {@link #writeValue} and must follow the specification there.
858 */
859 public final void writeList(List val) {
860 if (val == null) {
861 writeInt(-1);
862 return;
863 }
864 int N = val.size();
865 int i=0;
866 writeInt(N);
867 while (i < N) {
868 writeValue(val.get(i));
869 i++;
870 }
871 }
872
873 /**
874 * Flatten an Object array into the parcel at the current dataPosition(),
875 * growing dataCapacity() if needed. The array values are written using
876 * {@link #writeValue} and must follow the specification there.
877 */
878 public final void writeArray(Object[] val) {
879 if (val == null) {
880 writeInt(-1);
881 return;
882 }
883 int N = val.length;
884 int i=0;
885 writeInt(N);
886 while (i < N) {
887 writeValue(val[i]);
888 i++;
889 }
890 }
891
892 /**
893 * Flatten a generic SparseArray into the parcel at the current
894 * dataPosition(), growing dataCapacity() if needed. The SparseArray
895 * values are written using {@link #writeValue} and must follow the
896 * specification there.
897 */
898 public final void writeSparseArray(SparseArray<Object> val) {
899 if (val == null) {
900 writeInt(-1);
901 return;
902 }
903 int N = val.size();
904 writeInt(N);
905 int i=0;
906 while (i < N) {
907 writeInt(val.keyAt(i));
908 writeValue(val.valueAt(i));
909 i++;
910 }
911 }
912
913 public final void writeSparseBooleanArray(SparseBooleanArray val) {
914 if (val == null) {
915 writeInt(-1);
916 return;
917 }
918 int N = val.size();
919 writeInt(N);
920 int i=0;
921 while (i < N) {
922 writeInt(val.keyAt(i));
923 writeByte((byte)(val.valueAt(i) ? 1 : 0));
924 i++;
925 }
926 }
927
Adam Lesinski205656d2017-03-23 13:38:26 -0700928 /**
929 * @hide
930 */
Adam Lesinski4e862812016-11-21 16:02:24 -0800931 public final void writeSparseIntArray(SparseIntArray val) {
932 if (val == null) {
933 writeInt(-1);
934 return;
935 }
936 int N = val.size();
937 writeInt(N);
938 int i=0;
939 while (i < N) {
940 writeInt(val.keyAt(i));
941 writeInt(val.valueAt(i));
942 i++;
943 }
944 }
945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 public final void writeBooleanArray(boolean[] val) {
947 if (val != null) {
948 int N = val.length;
949 writeInt(N);
950 for (int i=0; i<N; i++) {
951 writeInt(val[i] ? 1 : 0);
952 }
953 } else {
954 writeInt(-1);
955 }
956 }
957
958 public final boolean[] createBooleanArray() {
959 int N = readInt();
960 // >>2 as a fast divide-by-4 works in the create*Array() functions
961 // because dataAvail() will never return a negative number. 4 is
962 // the size of a stored boolean in the stream.
963 if (N >= 0 && N <= (dataAvail() >> 2)) {
964 boolean[] val = new boolean[N];
965 for (int i=0; i<N; i++) {
966 val[i] = readInt() != 0;
967 }
968 return val;
969 } else {
970 return null;
971 }
972 }
973
974 public final void readBooleanArray(boolean[] val) {
975 int N = readInt();
976 if (N == val.length) {
977 for (int i=0; i<N; i++) {
978 val[i] = readInt() != 0;
979 }
980 } else {
981 throw new RuntimeException("bad array lengths");
982 }
983 }
984
985 public final void writeCharArray(char[] val) {
986 if (val != null) {
987 int N = val.length;
988 writeInt(N);
989 for (int i=0; i<N; i++) {
990 writeInt((int)val[i]);
991 }
992 } else {
993 writeInt(-1);
994 }
995 }
996
997 public final char[] createCharArray() {
998 int N = readInt();
999 if (N >= 0 && N <= (dataAvail() >> 2)) {
1000 char[] val = new char[N];
1001 for (int i=0; i<N; i++) {
1002 val[i] = (char)readInt();
1003 }
1004 return val;
1005 } else {
1006 return null;
1007 }
1008 }
1009
1010 public final void readCharArray(char[] val) {
1011 int N = readInt();
1012 if (N == val.length) {
1013 for (int i=0; i<N; i++) {
1014 val[i] = (char)readInt();
1015 }
1016 } else {
1017 throw new RuntimeException("bad array lengths");
1018 }
1019 }
1020
1021 public final void writeIntArray(int[] val) {
1022 if (val != null) {
1023 int N = val.length;
1024 writeInt(N);
1025 for (int i=0; i<N; i++) {
1026 writeInt(val[i]);
1027 }
1028 } else {
1029 writeInt(-1);
1030 }
1031 }
1032
1033 public final int[] createIntArray() {
1034 int N = readInt();
1035 if (N >= 0 && N <= (dataAvail() >> 2)) {
1036 int[] val = new int[N];
1037 for (int i=0; i<N; i++) {
1038 val[i] = readInt();
1039 }
1040 return val;
1041 } else {
1042 return null;
1043 }
1044 }
1045
1046 public final void readIntArray(int[] val) {
1047 int N = readInt();
1048 if (N == val.length) {
1049 for (int i=0; i<N; i++) {
1050 val[i] = readInt();
1051 }
1052 } else {
1053 throw new RuntimeException("bad array lengths");
1054 }
1055 }
1056
1057 public final void writeLongArray(long[] val) {
1058 if (val != null) {
1059 int N = val.length;
1060 writeInt(N);
1061 for (int i=0; i<N; i++) {
1062 writeLong(val[i]);
1063 }
1064 } else {
1065 writeInt(-1);
1066 }
1067 }
1068
1069 public final long[] createLongArray() {
1070 int N = readInt();
1071 // >>3 because stored longs are 64 bits
1072 if (N >= 0 && N <= (dataAvail() >> 3)) {
1073 long[] val = new long[N];
1074 for (int i=0; i<N; i++) {
1075 val[i] = readLong();
1076 }
1077 return val;
1078 } else {
1079 return null;
1080 }
1081 }
1082
1083 public final void readLongArray(long[] val) {
1084 int N = readInt();
1085 if (N == val.length) {
1086 for (int i=0; i<N; i++) {
1087 val[i] = readLong();
1088 }
1089 } else {
1090 throw new RuntimeException("bad array lengths");
1091 }
1092 }
1093
1094 public final void writeFloatArray(float[] val) {
1095 if (val != null) {
1096 int N = val.length;
1097 writeInt(N);
1098 for (int i=0; i<N; i++) {
1099 writeFloat(val[i]);
1100 }
1101 } else {
1102 writeInt(-1);
1103 }
1104 }
1105
1106 public final float[] createFloatArray() {
1107 int N = readInt();
1108 // >>2 because stored floats are 4 bytes
1109 if (N >= 0 && N <= (dataAvail() >> 2)) {
1110 float[] val = new float[N];
1111 for (int i=0; i<N; i++) {
1112 val[i] = readFloat();
1113 }
1114 return val;
1115 } else {
1116 return null;
1117 }
1118 }
1119
1120 public final void readFloatArray(float[] val) {
1121 int N = readInt();
1122 if (N == val.length) {
1123 for (int i=0; i<N; i++) {
1124 val[i] = readFloat();
1125 }
1126 } else {
1127 throw new RuntimeException("bad array lengths");
1128 }
1129 }
1130
1131 public final void writeDoubleArray(double[] val) {
1132 if (val != null) {
1133 int N = val.length;
1134 writeInt(N);
1135 for (int i=0; i<N; i++) {
1136 writeDouble(val[i]);
1137 }
1138 } else {
1139 writeInt(-1);
1140 }
1141 }
1142
1143 public final double[] createDoubleArray() {
1144 int N = readInt();
1145 // >>3 because stored doubles are 8 bytes
1146 if (N >= 0 && N <= (dataAvail() >> 3)) {
1147 double[] val = new double[N];
1148 for (int i=0; i<N; i++) {
1149 val[i] = readDouble();
1150 }
1151 return val;
1152 } else {
1153 return null;
1154 }
1155 }
1156
1157 public final void readDoubleArray(double[] val) {
1158 int N = readInt();
1159 if (N == val.length) {
1160 for (int i=0; i<N; i++) {
1161 val[i] = readDouble();
1162 }
1163 } else {
1164 throw new RuntimeException("bad array lengths");
1165 }
1166 }
1167
1168 public final void writeStringArray(String[] val) {
1169 if (val != null) {
1170 int N = val.length;
1171 writeInt(N);
1172 for (int i=0; i<N; i++) {
1173 writeString(val[i]);
1174 }
1175 } else {
1176 writeInt(-1);
1177 }
1178 }
1179
1180 public final String[] createStringArray() {
1181 int N = readInt();
1182 if (N >= 0) {
1183 String[] val = new String[N];
1184 for (int i=0; i<N; i++) {
1185 val[i] = readString();
1186 }
1187 return val;
1188 } else {
1189 return null;
1190 }
1191 }
1192
1193 public final void readStringArray(String[] val) {
1194 int N = readInt();
1195 if (N == val.length) {
1196 for (int i=0; i<N; i++) {
1197 val[i] = readString();
1198 }
1199 } else {
1200 throw new RuntimeException("bad array lengths");
1201 }
1202 }
1203
1204 public final void writeBinderArray(IBinder[] val) {
1205 if (val != null) {
1206 int N = val.length;
1207 writeInt(N);
1208 for (int i=0; i<N; i++) {
1209 writeStrongBinder(val[i]);
1210 }
1211 } else {
1212 writeInt(-1);
1213 }
1214 }
1215
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001216 /**
1217 * @hide
1218 */
1219 public final void writeCharSequenceArray(CharSequence[] val) {
1220 if (val != null) {
1221 int N = val.length;
1222 writeInt(N);
1223 for (int i=0; i<N; i++) {
1224 writeCharSequence(val[i]);
1225 }
1226 } else {
1227 writeInt(-1);
1228 }
1229 }
1230
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001231 /**
1232 * @hide
1233 */
1234 public final void writeCharSequenceList(ArrayList<CharSequence> val) {
1235 if (val != null) {
1236 int N = val.size();
1237 writeInt(N);
1238 for (int i=0; i<N; i++) {
1239 writeCharSequence(val.get(i));
1240 }
1241 } else {
1242 writeInt(-1);
1243 }
1244 }
1245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 public final IBinder[] createBinderArray() {
1247 int N = readInt();
1248 if (N >= 0) {
1249 IBinder[] val = new IBinder[N];
1250 for (int i=0; i<N; i++) {
1251 val[i] = readStrongBinder();
1252 }
1253 return val;
1254 } else {
1255 return null;
1256 }
1257 }
1258
1259 public final void readBinderArray(IBinder[] val) {
1260 int N = readInt();
1261 if (N == val.length) {
1262 for (int i=0; i<N; i++) {
1263 val[i] = readStrongBinder();
1264 }
1265 } else {
1266 throw new RuntimeException("bad array lengths");
1267 }
1268 }
1269
1270 /**
1271 * Flatten a List containing a particular object type into the parcel, at
1272 * the current dataPosition() and growing dataCapacity() if needed. The
1273 * type of the objects in the list must be one that implements Parcelable.
1274 * Unlike the generic writeList() method, however, only the raw data of the
1275 * objects is written and not their type, so you must use the corresponding
1276 * readTypedList() to unmarshall them.
1277 *
1278 * @param val The list of objects to be written.
1279 *
1280 * @see #createTypedArrayList
1281 * @see #readTypedList
1282 * @see Parcelable
1283 */
1284 public final <T extends Parcelable> void writeTypedList(List<T> val) {
1285 if (val == null) {
1286 writeInt(-1);
1287 return;
1288 }
1289 int N = val.size();
1290 int i=0;
1291 writeInt(N);
1292 while (i < N) {
1293 T item = val.get(i);
1294 if (item != null) {
1295 writeInt(1);
1296 item.writeToParcel(this, 0);
1297 } else {
1298 writeInt(0);
1299 }
1300 i++;
1301 }
1302 }
1303
1304 /**
1305 * Flatten a List containing String objects into the parcel, at
1306 * the current dataPosition() and growing dataCapacity() if needed. They
1307 * can later be retrieved with {@link #createStringArrayList} or
1308 * {@link #readStringList}.
1309 *
1310 * @param val The list of strings to be written.
1311 *
1312 * @see #createStringArrayList
1313 * @see #readStringList
1314 */
1315 public final void writeStringList(List<String> val) {
1316 if (val == null) {
1317 writeInt(-1);
1318 return;
1319 }
1320 int N = val.size();
1321 int i=0;
1322 writeInt(N);
1323 while (i < N) {
1324 writeString(val.get(i));
1325 i++;
1326 }
1327 }
1328
1329 /**
1330 * Flatten a List containing IBinder objects into the parcel, at
1331 * the current dataPosition() and growing dataCapacity() if needed. They
1332 * can later be retrieved with {@link #createBinderArrayList} or
1333 * {@link #readBinderList}.
1334 *
1335 * @param val The list of strings to be written.
1336 *
1337 * @see #createBinderArrayList
1338 * @see #readBinderList
1339 */
1340 public final void writeBinderList(List<IBinder> val) {
1341 if (val == null) {
1342 writeInt(-1);
1343 return;
1344 }
1345 int N = val.size();
1346 int i=0;
1347 writeInt(N);
1348 while (i < N) {
1349 writeStrongBinder(val.get(i));
1350 i++;
1351 }
1352 }
1353
1354 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00001355 * Flatten a {@code List} containing arbitrary {@code Parcelable} objects into this parcel
1356 * at the current position. They can later be retrieved using
1357 * {@link #readParcelableList(List, ClassLoader)} if required.
1358 *
1359 * @see #readParcelableList(List, ClassLoader)
1360 * @hide
1361 */
1362 public final <T extends Parcelable> void writeParcelableList(List<T> val, int flags) {
1363 if (val == null) {
1364 writeInt(-1);
1365 return;
1366 }
1367
1368 int N = val.size();
1369 int i=0;
1370 writeInt(N);
1371 while (i < N) {
1372 writeParcelable(val.get(i), flags);
1373 i++;
1374 }
1375 }
1376
1377 /**
Svet Ganov0f4928f2017-02-02 20:02:51 -08001378 * Flatten a homogeneous array containing a particular object type into
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 * the parcel, at
1380 * the current dataPosition() and growing dataCapacity() if needed. The
1381 * type of the objects in the array must be one that implements Parcelable.
1382 * Unlike the {@link #writeParcelableArray} method, however, only the
1383 * raw data of the objects is written and not their type, so you must use
1384 * {@link #readTypedArray} with the correct corresponding
1385 * {@link Parcelable.Creator} implementation to unmarshall them.
1386 *
1387 * @param val The array of objects to be written.
1388 * @param parcelableFlags Contextual flags as per
1389 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1390 *
1391 * @see #readTypedArray
1392 * @see #writeParcelableArray
1393 * @see Parcelable.Creator
1394 */
1395 public final <T extends Parcelable> void writeTypedArray(T[] val,
1396 int parcelableFlags) {
1397 if (val != null) {
1398 int N = val.length;
1399 writeInt(N);
Svet Ganov0f4928f2017-02-02 20:02:51 -08001400 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 T item = val[i];
1402 if (item != null) {
1403 writeInt(1);
1404 item.writeToParcel(this, parcelableFlags);
1405 } else {
1406 writeInt(0);
1407 }
1408 }
1409 } else {
1410 writeInt(-1);
1411 }
1412 }
1413
1414 /**
Svet Ganov0f4928f2017-02-02 20:02:51 -08001415 * Write a uniform (all items are null or the same class) array list of
1416 * parcelables.
1417 *
1418 * @param list The list to write.
1419 *
1420 * @hide
1421 */
1422 public final <T extends Parcelable> void writeTypedArrayList(@Nullable ArrayList<T> list,
1423 int parcelableFlags) {
1424 if (list != null) {
1425 int N = list.size();
1426 writeInt(N);
1427 boolean wroteCreator = false;
1428 for (int i = 0; i < N; i++) {
1429 T item = list.get(i);
1430 if (item != null) {
1431 writeInt(1);
1432 if (!wroteCreator) {
1433 writeParcelableCreator(item);
1434 wroteCreator = true;
1435 }
1436 item.writeToParcel(this, parcelableFlags);
1437 } else {
1438 writeInt(0);
1439 }
1440 }
1441 } else {
1442 writeInt(-1);
1443 }
1444 }
1445
1446 /**
1447 * Reads a uniform (all items are null or the same class) array list of
1448 * parcelables.
1449 *
1450 * @return The list or null.
1451 *
1452 * @hide
1453 */
1454 public final @Nullable <T> ArrayList<T> readTypedArrayList(@Nullable ClassLoader loader) {
1455 int N = readInt();
1456 if (N <= 0) {
1457 return null;
1458 }
1459 Parcelable.Creator<?> creator = null;
1460 ArrayList<T> result = new ArrayList<T>(N);
1461 for (int i = 0; i < N; i++) {
1462 if (readInt() != 0) {
1463 if (creator == null) {
1464 creator = readParcelableCreator(loader);
1465 if (creator == null) {
1466 return null;
1467 }
1468 }
1469 final T parcelable;
1470 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
1471 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
1472 (Parcelable.ClassLoaderCreator<?>) creator;
1473 parcelable = (T) classLoaderCreator.createFromParcel(this, loader);
1474 } else {
1475 parcelable = (T) creator.createFromParcel(this);
1476 }
1477 result.add(parcelable);
1478 } else {
1479 result.add(null);
1480 }
1481 }
1482 return result;
1483 }
1484
1485 /**
1486 * Write a uniform (all items are null or the same class) array set of
1487 * parcelables.
1488 *
1489 * @param set The set to write.
1490 *
1491 * @hide
1492 */
1493 public final <T extends Parcelable> void writeTypedArraySet(@Nullable ArraySet<T> set,
1494 int parcelableFlags) {
1495 if (set != null) {
1496 int N = set.size();
1497 writeInt(N);
1498 boolean wroteCreator = false;
1499 for (int i = 0; i < N; i++) {
1500 T item = set.valueAt(i);
1501 if (item != null) {
1502 writeInt(1);
1503 if (!wroteCreator) {
1504 writeParcelableCreator(item);
1505 wroteCreator = true;
1506 }
1507 item.writeToParcel(this, parcelableFlags);
1508 } else {
1509 writeInt(0);
1510 }
1511 }
1512 } else {
1513 writeInt(-1);
1514 }
1515 }
1516
1517 /**
1518 * Reads a uniform (all items are null or the same class) array set of
1519 * parcelables.
1520 *
1521 * @return The set or null.
1522 *
1523 * @hide
1524 */
1525 public final @Nullable <T> ArraySet<T> readTypedArraySet(@Nullable ClassLoader loader) {
1526 int N = readInt();
1527 if (N <= 0) {
1528 return null;
1529 }
1530 Parcelable.Creator<?> creator = null;
1531 ArraySet<T> result = new ArraySet<T>(N);
1532 for (int i = 0; i < N; i++) {
1533 T parcelable = null;
1534 if (readInt() != 0) {
1535 if (creator == null) {
1536 creator = readParcelableCreator(loader);
1537 if (creator == null) {
1538 return null;
1539 }
1540 }
1541 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
1542 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
1543 (Parcelable.ClassLoaderCreator<?>) creator;
1544 parcelable = (T) classLoaderCreator.createFromParcel(this, loader);
1545 } else {
1546 parcelable = (T) creator.createFromParcel(this);
1547 }
1548 }
1549 result.append(parcelable);
1550 }
1551 return result;
1552 }
1553
1554 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001555 * Flatten the Parcelable object into the parcel.
1556 *
1557 * @param val The Parcelable object to be written.
1558 * @param parcelableFlags Contextual flags as per
1559 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1560 *
1561 * @see #readTypedObject
1562 */
1563 public final <T extends Parcelable> void writeTypedObject(T val, int parcelableFlags) {
1564 if (val != null) {
1565 writeInt(1);
1566 val.writeToParcel(this, parcelableFlags);
1567 } else {
1568 writeInt(0);
1569 }
1570 }
1571
1572 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 * Flatten a generic object in to a parcel. The given Object value may
1574 * currently be one of the following types:
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001575 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 * <ul>
1577 * <li> null
1578 * <li> String
1579 * <li> Byte
1580 * <li> Short
1581 * <li> Integer
1582 * <li> Long
1583 * <li> Float
1584 * <li> Double
1585 * <li> Boolean
1586 * <li> String[]
1587 * <li> boolean[]
1588 * <li> byte[]
1589 * <li> int[]
1590 * <li> long[]
1591 * <li> Object[] (supporting objects of the same type defined here).
1592 * <li> {@link Bundle}
1593 * <li> Map (as supported by {@link #writeMap}).
1594 * <li> Any object that implements the {@link Parcelable} protocol.
1595 * <li> Parcelable[]
1596 * <li> CharSequence (as supported by {@link TextUtils#writeToParcel}).
1597 * <li> List (as supported by {@link #writeList}).
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001598 * <li> {@link SparseArray} (as supported by {@link #writeSparseArray(SparseArray)}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 * <li> {@link IBinder}
1600 * <li> Any object that implements Serializable (but see
1601 * {@link #writeSerializable} for caveats). Note that all of the
1602 * previous types have relatively efficient implementations for
1603 * writing to a Parcel; having to rely on the generic serialization
1604 * approach is much less efficient and should be avoided whenever
1605 * possible.
1606 * </ul>
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001607 *
1608 * <p class="caution">{@link Parcelable} objects are written with
1609 * {@link Parcelable#writeToParcel} using contextual flags of 0. When
1610 * serializing objects containing {@link ParcelFileDescriptor}s,
1611 * this may result in file descriptor leaks when they are returned from
1612 * Binder calls (where {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE}
1613 * should be used).</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 */
1615 public final void writeValue(Object v) {
1616 if (v == null) {
1617 writeInt(VAL_NULL);
1618 } else if (v instanceof String) {
1619 writeInt(VAL_STRING);
1620 writeString((String) v);
1621 } else if (v instanceof Integer) {
1622 writeInt(VAL_INTEGER);
1623 writeInt((Integer) v);
1624 } else if (v instanceof Map) {
1625 writeInt(VAL_MAP);
1626 writeMap((Map) v);
1627 } else if (v instanceof Bundle) {
1628 // Must be before Parcelable
1629 writeInt(VAL_BUNDLE);
1630 writeBundle((Bundle) v);
Samuel Tanceafe5e2015-12-11 16:50:58 -08001631 } else if (v instanceof PersistableBundle) {
1632 writeInt(VAL_PERSISTABLEBUNDLE);
1633 writePersistableBundle((PersistableBundle) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 } else if (v instanceof Parcelable) {
Samuel Tanceafe5e2015-12-11 16:50:58 -08001635 // IMPOTANT: cases for classes that implement Parcelable must
1636 // come before the Parcelable case, so that their specific VAL_*
1637 // types will be written.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 writeInt(VAL_PARCELABLE);
1639 writeParcelable((Parcelable) v, 0);
1640 } else if (v instanceof Short) {
1641 writeInt(VAL_SHORT);
1642 writeInt(((Short) v).intValue());
1643 } else if (v instanceof Long) {
1644 writeInt(VAL_LONG);
1645 writeLong((Long) v);
1646 } else if (v instanceof Float) {
1647 writeInt(VAL_FLOAT);
1648 writeFloat((Float) v);
1649 } else if (v instanceof Double) {
1650 writeInt(VAL_DOUBLE);
1651 writeDouble((Double) v);
1652 } else if (v instanceof Boolean) {
1653 writeInt(VAL_BOOLEAN);
1654 writeInt((Boolean) v ? 1 : 0);
1655 } else if (v instanceof CharSequence) {
1656 // Must be after String
1657 writeInt(VAL_CHARSEQUENCE);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001658 writeCharSequence((CharSequence) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 } else if (v instanceof List) {
1660 writeInt(VAL_LIST);
1661 writeList((List) v);
1662 } else if (v instanceof SparseArray) {
1663 writeInt(VAL_SPARSEARRAY);
1664 writeSparseArray((SparseArray) v);
1665 } else if (v instanceof boolean[]) {
1666 writeInt(VAL_BOOLEANARRAY);
1667 writeBooleanArray((boolean[]) v);
1668 } else if (v instanceof byte[]) {
1669 writeInt(VAL_BYTEARRAY);
1670 writeByteArray((byte[]) v);
1671 } else if (v instanceof String[]) {
1672 writeInt(VAL_STRINGARRAY);
1673 writeStringArray((String[]) v);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001674 } else if (v instanceof CharSequence[]) {
1675 // Must be after String[] and before Object[]
1676 writeInt(VAL_CHARSEQUENCEARRAY);
1677 writeCharSequenceArray((CharSequence[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 } else if (v instanceof IBinder) {
1679 writeInt(VAL_IBINDER);
1680 writeStrongBinder((IBinder) v);
1681 } else if (v instanceof Parcelable[]) {
1682 writeInt(VAL_PARCELABLEARRAY);
1683 writeParcelableArray((Parcelable[]) v, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 } else if (v instanceof int[]) {
1685 writeInt(VAL_INTARRAY);
1686 writeIntArray((int[]) v);
1687 } else if (v instanceof long[]) {
1688 writeInt(VAL_LONGARRAY);
1689 writeLongArray((long[]) v);
1690 } else if (v instanceof Byte) {
1691 writeInt(VAL_BYTE);
1692 writeInt((Byte) v);
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001693 } else if (v instanceof Size) {
1694 writeInt(VAL_SIZE);
1695 writeSize((Size) v);
1696 } else if (v instanceof SizeF) {
1697 writeInt(VAL_SIZEF);
1698 writeSizeF((SizeF) v);
Samuel Tana8036662015-11-23 14:36:00 -08001699 } else if (v instanceof double[]) {
1700 writeInt(VAL_DOUBLEARRAY);
1701 writeDoubleArray((double[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 } else {
Paul Duffinac5a0822014-02-03 15:02:17 +00001703 Class<?> clazz = v.getClass();
1704 if (clazz.isArray() && clazz.getComponentType() == Object.class) {
1705 // Only pure Object[] are written here, Other arrays of non-primitive types are
1706 // handled by serialization as this does not record the component type.
1707 writeInt(VAL_OBJECTARRAY);
1708 writeArray((Object[]) v);
1709 } else if (v instanceof Serializable) {
1710 // Must be last
1711 writeInt(VAL_SERIALIZABLE);
1712 writeSerializable((Serializable) v);
1713 } else {
1714 throw new RuntimeException("Parcel: unable to marshal value " + v);
1715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 }
1717 }
1718
1719 /**
1720 * Flatten the name of the class of the Parcelable and its contents
1721 * into the parcel.
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001722 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 * @param p The Parcelable object to be written.
1724 * @param parcelableFlags Contextual flags as per
1725 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1726 */
1727 public final void writeParcelable(Parcelable p, int parcelableFlags) {
1728 if (p == null) {
1729 writeString(null);
1730 return;
1731 }
Neil Fuller44e440c2015-04-20 14:39:00 +01001732 writeParcelableCreator(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 p.writeToParcel(this, parcelableFlags);
1734 }
1735
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001736 /** @hide */
1737 public final void writeParcelableCreator(Parcelable p) {
1738 String name = p.getClass().getName();
1739 writeString(name);
1740 }
1741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 /**
1743 * Write a generic serializable object in to a Parcel. It is strongly
1744 * recommended that this method be avoided, since the serialization
1745 * overhead is extremely large, and this approach will be much slower than
1746 * using the other approaches to writing data in to a Parcel.
1747 */
1748 public final void writeSerializable(Serializable s) {
1749 if (s == null) {
1750 writeString(null);
1751 return;
1752 }
1753 String name = s.getClass().getName();
1754 writeString(name);
1755
1756 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1757 try {
1758 ObjectOutputStream oos = new ObjectOutputStream(baos);
1759 oos.writeObject(s);
1760 oos.close();
1761
1762 writeByteArray(baos.toByteArray());
1763 } catch (IOException ioe) {
1764 throw new RuntimeException("Parcelable encountered " +
1765 "IOException writing serializable object (name = " + name +
1766 ")", ioe);
1767 }
1768 }
1769
1770 /**
1771 * Special function for writing an exception result at the header of
1772 * a parcel, to be used when returning an exception from a transaction.
1773 * Note that this currently only supports a few exception types; any other
1774 * exception will be re-thrown by this function as a RuntimeException
1775 * (to be caught by the system's last-resort exception handling when
1776 * dispatching a transaction).
Samuel Tana8036662015-11-23 14:36:00 -08001777 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 * <p>The supported exception types are:
1779 * <ul>
1780 * <li>{@link BadParcelableException}
1781 * <li>{@link IllegalArgumentException}
1782 * <li>{@link IllegalStateException}
1783 * <li>{@link NullPointerException}
1784 * <li>{@link SecurityException}
Dianne Hackborn18482ae2017-08-01 17:41:00 -07001785 * <li>{@link UnsupportedOperationException}
Dianne Hackborn7e714422013-09-13 17:32:57 -07001786 * <li>{@link NetworkOnMainThreadException}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 * </ul>
Samuel Tana8036662015-11-23 14:36:00 -08001788 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 * @param e The Exception to be written.
1790 *
1791 * @see #writeNoException
1792 * @see #readException
1793 */
1794 public final void writeException(Exception e) {
1795 int code = 0;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001796 if (e instanceof Parcelable
1797 && (e.getClass().getClassLoader() == Parcelable.class.getClassLoader())) {
1798 // We only send Parcelable exceptions that are in the
1799 // BootClassLoader to ensure that the receiver can unpack them
1800 code = EX_PARCELABLE;
1801 } else if (e instanceof SecurityException) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 code = EX_SECURITY;
1803 } else if (e instanceof BadParcelableException) {
1804 code = EX_BAD_PARCELABLE;
1805 } else if (e instanceof IllegalArgumentException) {
1806 code = EX_ILLEGAL_ARGUMENT;
1807 } else if (e instanceof NullPointerException) {
1808 code = EX_NULL_POINTER;
1809 } else if (e instanceof IllegalStateException) {
1810 code = EX_ILLEGAL_STATE;
Dianne Hackborn7e714422013-09-13 17:32:57 -07001811 } else if (e instanceof NetworkOnMainThreadException) {
1812 code = EX_NETWORK_MAIN_THREAD;
Dianne Hackborn33d738a2014-09-12 14:23:58 -07001813 } else if (e instanceof UnsupportedOperationException) {
1814 code = EX_UNSUPPORTED_OPERATION;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001815 } else if (e instanceof ServiceSpecificException) {
1816 code = EX_SERVICE_SPECIFIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 }
1818 writeInt(code);
Brad Fitzpatrick703e5d32010-07-15 13:16:41 -07001819 StrictMode.clearGatheredViolations();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 if (code == 0) {
1821 if (e instanceof RuntimeException) {
1822 throw (RuntimeException) e;
1823 }
1824 throw new RuntimeException(e);
1825 }
1826 writeString(e.getMessage());
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001827 switch (code) {
1828 case EX_SERVICE_SPECIFIC:
1829 writeInt(((ServiceSpecificException) e).errorCode);
1830 break;
1831 case EX_PARCELABLE:
1832 // Write parceled exception prefixed by length
1833 final int sizePosition = dataPosition();
1834 writeInt(0);
1835 writeParcelable((Parcelable) e, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
1836 final int payloadPosition = dataPosition();
1837 setDataPosition(sizePosition);
1838 writeInt(payloadPosition - sizePosition);
1839 setDataPosition(payloadPosition);
1840 break;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 }
1843
1844 /**
1845 * Special function for writing information at the front of the Parcel
1846 * indicating that no exception occurred.
1847 *
1848 * @see #writeException
1849 * @see #readException
1850 */
1851 public final void writeNoException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001852 // Despite the name of this function ("write no exception"),
1853 // it should instead be thought of as "write the RPC response
1854 // header", but because this function name is written out by
1855 // the AIDL compiler, we're not going to rename it.
1856 //
1857 // The response header, in the non-exception case (see also
1858 // writeException above, also called by the AIDL compiler), is
1859 // either a 0 (the default case), or EX_HAS_REPLY_HEADER if
1860 // StrictMode has gathered up violations that have occurred
1861 // during a Binder call, in which case we write out the number
1862 // of violations and their details, serialized, before the
1863 // actual RPC respons data. The receiving end of this is
1864 // readException(), below.
1865 if (StrictMode.hasGatheredViolations()) {
1866 writeInt(EX_HAS_REPLY_HEADER);
1867 final int sizePosition = dataPosition();
1868 writeInt(0); // total size of fat header, to be filled in later
1869 StrictMode.writeGatheredViolationsToParcel(this);
1870 final int payloadPosition = dataPosition();
1871 setDataPosition(sizePosition);
1872 writeInt(payloadPosition - sizePosition); // header size
1873 setDataPosition(payloadPosition);
1874 } else {
1875 writeInt(0);
1876 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 }
1878
1879 /**
1880 * Special function for reading an exception result from the header of
1881 * a parcel, to be used after receiving the result of a transaction. This
1882 * will throw the exception for you if it had been written to the Parcel,
1883 * otherwise return and let you read the normal result data from the Parcel.
1884 *
1885 * @see #writeException
1886 * @see #writeNoException
1887 */
1888 public final void readException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001889 int code = readExceptionCode();
1890 if (code != 0) {
1891 String msg = readString();
1892 readException(code, msg);
1893 }
1894 }
1895
1896 /**
1897 * Parses the header of a Binder call's response Parcel and
1898 * returns the exception code. Deals with lite or fat headers.
1899 * In the common successful case, this header is generally zero.
1900 * In less common cases, it's a small negative number and will be
1901 * followed by an error string.
1902 *
1903 * This exists purely for android.database.DatabaseUtils and
1904 * insulating it from having to handle fat headers as returned by
1905 * e.g. StrictMode-induced RPC responses.
1906 *
1907 * @hide
1908 */
1909 public final int readExceptionCode() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 int code = readInt();
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001911 if (code == EX_HAS_REPLY_HEADER) {
1912 int headerSize = readInt();
1913 if (headerSize == 0) {
1914 Log.e(TAG, "Unexpected zero-sized Parcel reply header.");
1915 } else {
1916 // Currently the only thing in the header is StrictMode stacks,
1917 // but discussions around event/RPC tracing suggest we might
1918 // put that here too. If so, switch on sub-header tags here.
1919 // But for now, just parse out the StrictMode stuff.
1920 StrictMode.readAndHandleBinderCallViolations(this);
1921 }
1922 // And fat response headers are currently only used when
1923 // there are no exceptions, so return no error:
1924 return 0;
1925 }
1926 return code;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 }
1928
1929 /**
Mark Doliner879ea452014-01-02 12:38:07 -08001930 * Throw an exception with the given message. Not intended for use
1931 * outside the Parcel class.
1932 *
1933 * @param code Used to determine which exception class to throw.
1934 * @param msg The exception message.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 */
1936 public final void readException(int code, String msg) {
1937 switch (code) {
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001938 case EX_PARCELABLE:
1939 if (readInt() > 0) {
1940 SneakyThrow.sneakyThrow(
1941 (Exception) readParcelable(Parcelable.class.getClassLoader()));
1942 } else {
1943 throw new RuntimeException(msg + " [missing Parcelable]");
1944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 case EX_SECURITY:
1946 throw new SecurityException(msg);
1947 case EX_BAD_PARCELABLE:
1948 throw new BadParcelableException(msg);
1949 case EX_ILLEGAL_ARGUMENT:
1950 throw new IllegalArgumentException(msg);
1951 case EX_NULL_POINTER:
1952 throw new NullPointerException(msg);
1953 case EX_ILLEGAL_STATE:
1954 throw new IllegalStateException(msg);
Dianne Hackborn7e714422013-09-13 17:32:57 -07001955 case EX_NETWORK_MAIN_THREAD:
1956 throw new NetworkOnMainThreadException();
Dianne Hackborn33d738a2014-09-12 14:23:58 -07001957 case EX_UNSUPPORTED_OPERATION:
1958 throw new UnsupportedOperationException(msg);
Christopher Wiley80fd1202015-11-22 17:12:37 -08001959 case EX_SERVICE_SPECIFIC:
1960 throw new ServiceSpecificException(readInt(), msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 }
1962 throw new RuntimeException("Unknown exception code: " + code
1963 + " msg " + msg);
1964 }
1965
1966 /**
1967 * Read an integer value from the parcel at the current dataPosition().
1968 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001969 public final int readInt() {
1970 return nativeReadInt(mNativePtr);
1971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972
1973 /**
1974 * Read a long integer value from the parcel at the current dataPosition().
1975 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001976 public final long readLong() {
1977 return nativeReadLong(mNativePtr);
1978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979
1980 /**
1981 * Read a floating point value from the parcel at the current
1982 * dataPosition().
1983 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001984 public final float readFloat() {
1985 return nativeReadFloat(mNativePtr);
1986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987
1988 /**
1989 * Read a double precision floating point value from the parcel at the
1990 * current dataPosition().
1991 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001992 public final double readDouble() {
1993 return nativeReadDouble(mNativePtr);
1994 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995
1996 /**
1997 * Read a string value from the parcel at the current dataPosition().
1998 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001999 public final String readString() {
2000 return nativeReadString(mNativePtr);
2001 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002
Eugene Susla36e866b2017-02-23 18:24:39 -08002003 /** @hide */
2004 public final boolean readBoolean() {
2005 return readInt() != 0;
2006 }
2007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002009 * Read a CharSequence value from the parcel at the current dataPosition().
2010 * @hide
2011 */
2012 public final CharSequence readCharSequence() {
2013 return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(this);
2014 }
2015
2016 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 * Read an object from the parcel at the current dataPosition().
2018 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002019 public final IBinder readStrongBinder() {
2020 return nativeReadStrongBinder(mNativePtr);
2021 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022
2023 /**
2024 * Read a FileDescriptor from the parcel at the current dataPosition().
2025 */
2026 public final ParcelFileDescriptor readFileDescriptor() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002027 FileDescriptor fd = nativeReadFileDescriptor(mNativePtr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 return fd != null ? new ParcelFileDescriptor(fd) : null;
2029 }
2030
Jeff Sharkeyda5a3e12013-08-11 12:54:42 -07002031 /** {@hide} */
2032 public final FileDescriptor readRawFileDescriptor() {
2033 return nativeReadFileDescriptor(mNativePtr);
2034 }
2035
Casey Dahlin2f974b22015-11-05 12:19:13 -08002036 /**
2037 * {@hide}
2038 * Read and return a new array of FileDescriptors from the parcel.
2039 * @return the FileDescriptor array, or null if the array is null.
2040 **/
2041 public final FileDescriptor[] createRawFileDescriptorArray() {
2042 int N = readInt();
2043 if (N < 0) {
2044 return null;
2045 }
2046 FileDescriptor[] f = new FileDescriptor[N];
2047 for (int i = 0; i < N; i++) {
2048 f[i] = readRawFileDescriptor();
2049 }
2050 return f;
2051 }
2052
2053 /**
2054 * {@hide}
2055 * Read an array of FileDescriptors from a parcel.
2056 * The passed array must be exactly the length of the array in the parcel.
2057 * @return the FileDescriptor array, or null if the array is null.
2058 **/
2059 public final void readRawFileDescriptorArray(FileDescriptor[] val) {
2060 int N = readInt();
2061 if (N == val.length) {
2062 for (int i=0; i<N; i++) {
2063 val[i] = readRawFileDescriptor();
2064 }
2065 } else {
2066 throw new RuntimeException("bad array lengths");
2067 }
2068 }
2069
Jeff Sharkeye53e2d92017-03-25 23:14:06 -06002070 /** @deprecated use {@link android.system.Os#open(String, int, int)} */
2071 @Deprecated
2072 static native FileDescriptor openFileDescriptor(String file, int mode)
2073 throws FileNotFoundException;
Casey Dahlin2f974b22015-11-05 12:19:13 -08002074
Jeff Sharkeye53e2d92017-03-25 23:14:06 -06002075 /** @deprecated use {@link android.system.Os#dup(FileDescriptor)} */
2076 @Deprecated
2077 static native FileDescriptor dupFileDescriptor(FileDescriptor orig) throws IOException;
2078
2079 /** @deprecated use {@link android.system.Os#close(FileDescriptor)} */
2080 @Deprecated
2081 static native void closeFileDescriptor(FileDescriptor desc) throws IOException;
2082
2083 static native void clearFileDescriptor(FileDescriptor desc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084
2085 /**
2086 * Read a byte value from the parcel at the current dataPosition().
2087 */
2088 public final byte readByte() {
2089 return (byte)(readInt() & 0xff);
2090 }
2091
2092 /**
2093 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2094 * been written with {@link #writeBundle}. Read into an existing Map object
2095 * from the parcel at the current dataPosition().
2096 */
2097 public final void readMap(Map outVal, ClassLoader loader) {
2098 int N = readInt();
2099 readMapInternal(outVal, N, loader);
2100 }
2101
2102 /**
2103 * Read into an existing List object from the parcel at the current
2104 * dataPosition(), using the given class loader to load any enclosed
2105 * Parcelables. If it is null, the default class loader is used.
2106 */
2107 public final void readList(List outVal, ClassLoader loader) {
2108 int N = readInt();
2109 readListInternal(outVal, N, loader);
2110 }
2111
2112 /**
2113 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2114 * been written with {@link #writeBundle}. Read and return a new HashMap
2115 * object from the parcel at the current dataPosition(), using the given
2116 * class loader to load any enclosed Parcelables. Returns null if
2117 * the previously written map object was null.
2118 */
2119 public final HashMap readHashMap(ClassLoader loader)
2120 {
2121 int N = readInt();
2122 if (N < 0) {
2123 return null;
2124 }
2125 HashMap m = new HashMap(N);
2126 readMapInternal(m, N, loader);
2127 return m;
2128 }
2129
2130 /**
2131 * Read and return a new Bundle object from the parcel at the current
2132 * dataPosition(). Returns null if the previously written Bundle object was
2133 * null.
2134 */
2135 public final Bundle readBundle() {
2136 return readBundle(null);
2137 }
2138
2139 /**
2140 * Read and return a new Bundle object from the parcel at the current
2141 * dataPosition(), using the given class loader to initialize the class
2142 * loader of the Bundle for later retrieval of Parcelable objects.
2143 * Returns null if the previously written Bundle object was null.
2144 */
2145 public final Bundle readBundle(ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 int length = readInt();
2147 if (length < 0) {
Dianne Hackborn4a7d8242013-10-03 10:19:20 -07002148 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 return null;
2150 }
Samuel Tana8036662015-11-23 14:36:00 -08002151
Dianne Hackborn6aff9052009-05-22 13:20:23 -07002152 final Bundle bundle = new Bundle(this, length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 if (loader != null) {
2154 bundle.setClassLoader(loader);
2155 }
2156 return bundle;
2157 }
2158
2159 /**
Craig Mautner719e6b12014-04-04 20:29:41 -07002160 * Read and return a new Bundle object from the parcel at the current
2161 * dataPosition(). Returns null if the previously written Bundle object was
2162 * null.
2163 */
2164 public final PersistableBundle readPersistableBundle() {
2165 return readPersistableBundle(null);
2166 }
2167
2168 /**
2169 * Read and return a new Bundle object from the parcel at the current
2170 * dataPosition(), using the given class loader to initialize the class
2171 * loader of the Bundle for later retrieval of Parcelable objects.
2172 * Returns null if the previously written Bundle object was null.
2173 */
2174 public final PersistableBundle readPersistableBundle(ClassLoader loader) {
2175 int length = readInt();
2176 if (length < 0) {
2177 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
2178 return null;
2179 }
2180
2181 final PersistableBundle bundle = new PersistableBundle(this, length);
2182 if (loader != null) {
2183 bundle.setClassLoader(loader);
2184 }
2185 return bundle;
2186 }
2187
2188 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002189 * Read a Size from the parcel at the current dataPosition().
2190 */
2191 public final Size readSize() {
2192 final int width = readInt();
2193 final int height = readInt();
2194 return new Size(width, height);
2195 }
2196
2197 /**
2198 * Read a SizeF from the parcel at the current dataPosition().
2199 */
2200 public final SizeF readSizeF() {
2201 final float width = readFloat();
2202 final float height = readFloat();
2203 return new SizeF(width, height);
2204 }
2205
2206 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 * Read and return a byte[] object from the parcel.
2208 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002209 public final byte[] createByteArray() {
2210 return nativeCreateByteArray(mNativePtr);
2211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212
2213 /**
2214 * Read a byte[] object from the parcel and copy it into the
2215 * given byte array.
2216 */
2217 public final void readByteArray(byte[] val) {
Jocelyn Dang46100442017-05-05 15:40:49 -07002218 boolean valid = nativeReadByteArray(mNativePtr, val, (val != null) ? val.length : 0);
2219 if (!valid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 throw new RuntimeException("bad array lengths");
2221 }
2222 }
2223
2224 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002225 * Read a blob of data from the parcel and return it as a byte array.
2226 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -07002227 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002228 */
2229 public final byte[] readBlob() {
2230 return nativeReadBlob(mNativePtr);
2231 }
2232
2233 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 * Read and return a String[] object from the parcel.
2235 * {@hide}
2236 */
2237 public final String[] readStringArray() {
2238 String[] array = null;
2239
2240 int length = readInt();
2241 if (length >= 0)
2242 {
2243 array = new String[length];
2244
2245 for (int i = 0 ; i < length ; i++)
2246 {
2247 array[i] = readString();
2248 }
2249 }
2250
2251 return array;
2252 }
2253
2254 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002255 * Read and return a CharSequence[] object from the parcel.
2256 * {@hide}
2257 */
2258 public final CharSequence[] readCharSequenceArray() {
2259 CharSequence[] array = null;
2260
2261 int length = readInt();
2262 if (length >= 0)
2263 {
2264 array = new CharSequence[length];
2265
2266 for (int i = 0 ; i < length ; i++)
2267 {
2268 array[i] = readCharSequence();
2269 }
2270 }
2271
2272 return array;
2273 }
2274
2275 /**
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002276 * Read and return an ArrayList&lt;CharSequence&gt; object from the parcel.
2277 * {@hide}
2278 */
2279 public final ArrayList<CharSequence> readCharSequenceList() {
2280 ArrayList<CharSequence> array = null;
2281
2282 int length = readInt();
2283 if (length >= 0) {
2284 array = new ArrayList<CharSequence>(length);
2285
2286 for (int i = 0 ; i < length ; i++) {
2287 array.add(readCharSequence());
2288 }
2289 }
2290
2291 return array;
2292 }
2293
2294 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 * Read and return a new ArrayList object from the parcel at the current
2296 * dataPosition(). Returns null if the previously written list object was
2297 * null. The given class loader will be used to load any enclosed
2298 * Parcelables.
2299 */
2300 public final ArrayList readArrayList(ClassLoader loader) {
2301 int N = readInt();
2302 if (N < 0) {
2303 return null;
2304 }
2305 ArrayList l = new ArrayList(N);
2306 readListInternal(l, N, loader);
2307 return l;
2308 }
2309
2310 /**
2311 * Read and return a new Object array from the parcel at the current
2312 * dataPosition(). Returns null if the previously written array was
2313 * null. The given class loader will be used to load any enclosed
2314 * Parcelables.
2315 */
2316 public final Object[] readArray(ClassLoader loader) {
2317 int N = readInt();
2318 if (N < 0) {
2319 return null;
2320 }
2321 Object[] l = new Object[N];
2322 readArrayInternal(l, N, loader);
2323 return l;
2324 }
2325
2326 /**
2327 * Read and return a new SparseArray object from the parcel at the current
2328 * dataPosition(). Returns null if the previously written list object was
2329 * null. The given class loader will be used to load any enclosed
2330 * Parcelables.
2331 */
2332 public final SparseArray readSparseArray(ClassLoader loader) {
2333 int N = readInt();
2334 if (N < 0) {
2335 return null;
2336 }
2337 SparseArray sa = new SparseArray(N);
2338 readSparseArrayInternal(sa, N, loader);
2339 return sa;
2340 }
2341
2342 /**
2343 * Read and return a new SparseBooleanArray object from the parcel at the current
2344 * dataPosition(). Returns null if the previously written list object was
2345 * null.
2346 */
2347 public final SparseBooleanArray readSparseBooleanArray() {
2348 int N = readInt();
2349 if (N < 0) {
2350 return null;
2351 }
2352 SparseBooleanArray sa = new SparseBooleanArray(N);
2353 readSparseBooleanArrayInternal(sa, N);
2354 return sa;
2355 }
2356
2357 /**
Adam Lesinski4e862812016-11-21 16:02:24 -08002358 * Read and return a new SparseIntArray object from the parcel at the current
2359 * dataPosition(). Returns null if the previously written array object was null.
Adam Lesinski205656d2017-03-23 13:38:26 -07002360 * @hide
Adam Lesinski4e862812016-11-21 16:02:24 -08002361 */
2362 public final SparseIntArray readSparseIntArray() {
2363 int N = readInt();
2364 if (N < 0) {
2365 return null;
2366 }
2367 SparseIntArray sa = new SparseIntArray(N);
2368 readSparseIntArrayInternal(sa, N);
2369 return sa;
2370 }
2371
2372 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 * Read and return a new ArrayList containing a particular object type from
2374 * the parcel that was written with {@link #writeTypedList} at the
2375 * current dataPosition(). Returns null if the
2376 * previously written list object was null. The list <em>must</em> have
2377 * previously been written via {@link #writeTypedList} with the same object
2378 * type.
2379 *
2380 * @return A newly created ArrayList containing objects with the same data
2381 * as those that were previously written.
2382 *
2383 * @see #writeTypedList
2384 */
2385 public final <T> ArrayList<T> createTypedArrayList(Parcelable.Creator<T> c) {
2386 int N = readInt();
2387 if (N < 0) {
2388 return null;
2389 }
2390 ArrayList<T> l = new ArrayList<T>(N);
2391 while (N > 0) {
2392 if (readInt() != 0) {
2393 l.add(c.createFromParcel(this));
2394 } else {
2395 l.add(null);
2396 }
2397 N--;
2398 }
2399 return l;
2400 }
2401
2402 /**
2403 * Read into the given List items containing a particular object type
2404 * that were written with {@link #writeTypedList} at the
2405 * current dataPosition(). The list <em>must</em> have
2406 * previously been written via {@link #writeTypedList} with the same object
2407 * type.
2408 *
2409 * @return A newly created ArrayList containing objects with the same data
2410 * as those that were previously written.
2411 *
2412 * @see #writeTypedList
2413 */
2414 public final <T> void readTypedList(List<T> list, Parcelable.Creator<T> c) {
2415 int M = list.size();
2416 int N = readInt();
2417 int i = 0;
2418 for (; i < M && i < N; i++) {
2419 if (readInt() != 0) {
2420 list.set(i, c.createFromParcel(this));
2421 } else {
2422 list.set(i, null);
2423 }
2424 }
2425 for (; i<N; i++) {
2426 if (readInt() != 0) {
2427 list.add(c.createFromParcel(this));
2428 } else {
2429 list.add(null);
2430 }
2431 }
2432 for (; i<M; i++) {
2433 list.remove(N);
2434 }
2435 }
2436
2437 /**
2438 * Read and return a new ArrayList containing String objects from
2439 * the parcel that was written with {@link #writeStringList} at the
2440 * current dataPosition(). Returns null if the
2441 * previously written list object was null.
2442 *
2443 * @return A newly created ArrayList containing strings with the same data
2444 * as those that were previously written.
2445 *
2446 * @see #writeStringList
2447 */
2448 public final ArrayList<String> createStringArrayList() {
2449 int N = readInt();
2450 if (N < 0) {
2451 return null;
2452 }
2453 ArrayList<String> l = new ArrayList<String>(N);
2454 while (N > 0) {
2455 l.add(readString());
2456 N--;
2457 }
2458 return l;
2459 }
2460
2461 /**
2462 * Read and return a new ArrayList containing IBinder objects from
2463 * the parcel that was written with {@link #writeBinderList} at the
2464 * current dataPosition(). Returns null if the
2465 * previously written list object was null.
2466 *
2467 * @return A newly created ArrayList containing strings with the same data
2468 * as those that were previously written.
2469 *
2470 * @see #writeBinderList
2471 */
2472 public final ArrayList<IBinder> createBinderArrayList() {
2473 int N = readInt();
2474 if (N < 0) {
2475 return null;
2476 }
2477 ArrayList<IBinder> l = new ArrayList<IBinder>(N);
2478 while (N > 0) {
2479 l.add(readStrongBinder());
2480 N--;
2481 }
2482 return l;
2483 }
2484
2485 /**
2486 * Read into the given List items String objects that were written with
2487 * {@link #writeStringList} at the current dataPosition().
2488 *
2489 * @return A newly created ArrayList containing strings with the same data
2490 * as those that were previously written.
2491 *
2492 * @see #writeStringList
2493 */
2494 public final void readStringList(List<String> list) {
2495 int M = list.size();
2496 int N = readInt();
2497 int i = 0;
2498 for (; i < M && i < N; i++) {
2499 list.set(i, readString());
2500 }
2501 for (; i<N; i++) {
2502 list.add(readString());
2503 }
2504 for (; i<M; i++) {
2505 list.remove(N);
2506 }
2507 }
2508
2509 /**
2510 * Read into the given List items IBinder objects that were written with
2511 * {@link #writeBinderList} at the current dataPosition().
2512 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 * @see #writeBinderList
2514 */
2515 public final void readBinderList(List<IBinder> list) {
2516 int M = list.size();
2517 int N = readInt();
2518 int i = 0;
2519 for (; i < M && i < N; i++) {
2520 list.set(i, readStrongBinder());
2521 }
2522 for (; i<N; i++) {
2523 list.add(readStrongBinder());
2524 }
2525 for (; i<M; i++) {
2526 list.remove(N);
2527 }
2528 }
2529
2530 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00002531 * Read the list of {@code Parcelable} objects at the current data position into the
2532 * given {@code list}. The contents of the {@code list} are replaced. If the serialized
2533 * list was {@code null}, {@code list} is cleared.
2534 *
2535 * @see #writeParcelableList(List, int)
2536 * @hide
2537 */
Eugene Susla36e866b2017-02-23 18:24:39 -08002538 public final <T extends Parcelable> List<T> readParcelableList(List<T> list, ClassLoader cl) {
Narayan Kamathbea48712016-12-01 15:38:28 +00002539 final int N = readInt();
2540 if (N == -1) {
2541 list.clear();
Eugene Susla36e866b2017-02-23 18:24:39 -08002542 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002543 }
2544
2545 final int M = list.size();
2546 int i = 0;
2547 for (; i < M && i < N; i++) {
2548 list.set(i, (T) readParcelable(cl));
2549 }
2550 for (; i<N; i++) {
2551 list.add((T) readParcelable(cl));
2552 }
2553 for (; i<M; i++) {
2554 list.remove(N);
2555 }
Eugene Susla36e866b2017-02-23 18:24:39 -08002556 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002557 }
2558
2559 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 * Read and return a new array containing a particular object type from
2561 * the parcel at the current dataPosition(). Returns null if the
2562 * previously written array was null. The array <em>must</em> have
2563 * previously been written via {@link #writeTypedArray} with the same
2564 * object type.
2565 *
2566 * @return A newly created array containing objects with the same data
2567 * as those that were previously written.
2568 *
2569 * @see #writeTypedArray
2570 */
2571 public final <T> T[] createTypedArray(Parcelable.Creator<T> c) {
2572 int N = readInt();
2573 if (N < 0) {
2574 return null;
2575 }
2576 T[] l = c.newArray(N);
2577 for (int i=0; i<N; i++) {
2578 if (readInt() != 0) {
2579 l[i] = c.createFromParcel(this);
2580 }
2581 }
2582 return l;
2583 }
2584
2585 public final <T> void readTypedArray(T[] val, Parcelable.Creator<T> c) {
2586 int N = readInt();
2587 if (N == val.length) {
2588 for (int i=0; i<N; i++) {
2589 if (readInt() != 0) {
2590 val[i] = c.createFromParcel(this);
2591 } else {
2592 val[i] = null;
2593 }
2594 }
2595 } else {
2596 throw new RuntimeException("bad array lengths");
2597 }
2598 }
2599
2600 /**
2601 * @deprecated
2602 * @hide
2603 */
2604 @Deprecated
2605 public final <T> T[] readTypedArray(Parcelable.Creator<T> c) {
2606 return createTypedArray(c);
2607 }
2608
2609 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002610 * Read and return a typed Parcelable object from a parcel.
2611 * Returns null if the previous written object was null.
2612 * The object <em>must</em> have previous been written via
2613 * {@link #writeTypedObject} with the same object type.
2614 *
2615 * @return A newly created object of the type that was previously
2616 * written.
2617 *
2618 * @see #writeTypedObject
2619 */
2620 public final <T> T readTypedObject(Parcelable.Creator<T> c) {
2621 if (readInt() != 0) {
2622 return c.createFromParcel(this);
2623 } else {
2624 return null;
2625 }
2626 }
2627
2628 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 * Write a heterogeneous array of Parcelable objects into the Parcel.
2630 * Each object in the array is written along with its class name, so
2631 * that the correct class can later be instantiated. As a result, this
2632 * has significantly more overhead than {@link #writeTypedArray}, but will
2633 * correctly handle an array containing more than one type of object.
2634 *
2635 * @param value The array of objects to be written.
2636 * @param parcelableFlags Contextual flags as per
2637 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
2638 *
2639 * @see #writeTypedArray
2640 */
2641 public final <T extends Parcelable> void writeParcelableArray(T[] value,
2642 int parcelableFlags) {
2643 if (value != null) {
2644 int N = value.length;
2645 writeInt(N);
2646 for (int i=0; i<N; i++) {
2647 writeParcelable(value[i], parcelableFlags);
2648 }
2649 } else {
2650 writeInt(-1);
2651 }
2652 }
2653
2654 /**
2655 * Read a typed object from a parcel. The given class loader will be
2656 * used to load any enclosed Parcelables. If it is null, the default class
2657 * loader will be used.
2658 */
2659 public final Object readValue(ClassLoader loader) {
2660 int type = readInt();
2661
2662 switch (type) {
2663 case VAL_NULL:
2664 return null;
2665
2666 case VAL_STRING:
2667 return readString();
2668
2669 case VAL_INTEGER:
2670 return readInt();
2671
2672 case VAL_MAP:
2673 return readHashMap(loader);
2674
2675 case VAL_PARCELABLE:
2676 return readParcelable(loader);
2677
2678 case VAL_SHORT:
2679 return (short) readInt();
2680
2681 case VAL_LONG:
2682 return readLong();
2683
2684 case VAL_FLOAT:
2685 return readFloat();
2686
2687 case VAL_DOUBLE:
2688 return readDouble();
2689
2690 case VAL_BOOLEAN:
2691 return readInt() == 1;
2692
2693 case VAL_CHARSEQUENCE:
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002694 return readCharSequence();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695
2696 case VAL_LIST:
2697 return readArrayList(loader);
2698
2699 case VAL_BOOLEANARRAY:
Samuel Tana8036662015-11-23 14:36:00 -08002700 return createBooleanArray();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701
2702 case VAL_BYTEARRAY:
2703 return createByteArray();
2704
2705 case VAL_STRINGARRAY:
2706 return readStringArray();
2707
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002708 case VAL_CHARSEQUENCEARRAY:
2709 return readCharSequenceArray();
2710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 case VAL_IBINDER:
2712 return readStrongBinder();
2713
2714 case VAL_OBJECTARRAY:
2715 return readArray(loader);
2716
2717 case VAL_INTARRAY:
2718 return createIntArray();
2719
2720 case VAL_LONGARRAY:
2721 return createLongArray();
2722
2723 case VAL_BYTE:
2724 return readByte();
2725
2726 case VAL_SERIALIZABLE:
John Spurlock5002b8c2014-01-10 13:32:12 -05002727 return readSerializable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728
2729 case VAL_PARCELABLEARRAY:
2730 return readParcelableArray(loader);
2731
2732 case VAL_SPARSEARRAY:
2733 return readSparseArray(loader);
2734
2735 case VAL_SPARSEBOOLEANARRAY:
2736 return readSparseBooleanArray();
2737
2738 case VAL_BUNDLE:
2739 return readBundle(loader); // loading will be deferred
2740
Craig Mautner719e6b12014-04-04 20:29:41 -07002741 case VAL_PERSISTABLEBUNDLE:
2742 return readPersistableBundle(loader);
2743
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002744 case VAL_SIZE:
2745 return readSize();
2746
2747 case VAL_SIZEF:
2748 return readSizeF();
2749
Samuel Tana8036662015-11-23 14:36:00 -08002750 case VAL_DOUBLEARRAY:
2751 return createDoubleArray();
2752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 default:
2754 int off = dataPosition() - 4;
2755 throw new RuntimeException(
2756 "Parcel " + this + ": Unmarshalling unknown type code " + type + " at offset " + off);
2757 }
2758 }
2759
2760 /**
2761 * Read and return a new Parcelable from the parcel. The given class loader
2762 * will be used to load any enclosed Parcelables. If it is null, the default
2763 * class loader will be used.
2764 * @param loader A ClassLoader from which to instantiate the Parcelable
2765 * object, or null for the default class loader.
2766 * @return Returns the newly created Parcelable, or null if a null
2767 * object has been written.
2768 * @throws BadParcelableException Throws BadParcelableException if there
2769 * was an error trying to instantiate the Parcelable.
2770 */
Neil Fuller44e440c2015-04-20 14:39:00 +01002771 @SuppressWarnings("unchecked")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 public final <T extends Parcelable> T readParcelable(ClassLoader loader) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002773 Parcelable.Creator<?> creator = readParcelableCreator(loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002774 if (creator == null) {
2775 return null;
2776 }
2777 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002778 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2779 (Parcelable.ClassLoaderCreator<?>) creator;
2780 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002781 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002782 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002783 }
2784
2785 /** @hide */
Neil Fuller44e440c2015-04-20 14:39:00 +01002786 @SuppressWarnings("unchecked")
2787 public final <T extends Parcelable> T readCreator(Parcelable.Creator<?> creator,
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002788 ClassLoader loader) {
2789 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002790 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2791 (Parcelable.ClassLoaderCreator<?>) creator;
2792 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002793 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002794 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002795 }
2796
2797 /** @hide */
Neil Fuller44e440c2015-04-20 14:39:00 +01002798 public final Parcelable.Creator<?> readParcelableCreator(ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 String name = readString();
2800 if (name == null) {
2801 return null;
2802 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002803 Parcelable.Creator<?> creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 synchronized (mCreators) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002805 HashMap<String,Parcelable.Creator<?>> map = mCreators.get(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 if (map == null) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002807 map = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 mCreators.put(loader, map);
2809 }
2810 creator = map.get(name);
2811 if (creator == null) {
2812 try {
Neil Fuller44e440c2015-04-20 14:39:00 +01002813 // If loader == null, explicitly emulate Class.forName(String) "caller
2814 // classloader" behavior.
2815 ClassLoader parcelableClassLoader =
2816 (loader == null ? getClass().getClassLoader() : loader);
2817 // Avoid initializing the Parcelable class until we know it implements
2818 // Parcelable and has the necessary CREATOR field. http://b/1171613.
2819 Class<?> parcelableClass = Class.forName(name, false /* initialize */,
2820 parcelableClassLoader);
2821 if (!Parcelable.class.isAssignableFrom(parcelableClass)) {
2822 throw new BadParcelableException("Parcelable protocol requires that the "
2823 + "class implements Parcelable");
2824 }
2825 Field f = parcelableClass.getField("CREATOR");
2826 if ((f.getModifiers() & Modifier.STATIC) == 0) {
2827 throw new BadParcelableException("Parcelable protocol requires "
2828 + "the CREATOR object to be static on class " + name);
2829 }
2830 Class<?> creatorType = f.getType();
2831 if (!Parcelable.Creator.class.isAssignableFrom(creatorType)) {
2832 // Fail before calling Field.get(), not after, to avoid initializing
2833 // parcelableClass unnecessarily.
2834 throw new BadParcelableException("Parcelable protocol requires a "
2835 + "Parcelable.Creator object called "
2836 + "CREATOR on class " + name);
2837 }
2838 creator = (Parcelable.Creator<?>) f.get(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 }
2840 catch (IllegalAccessException e) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002841 Log.e(TAG, "Illegal access when unmarshalling: " + name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 throw new BadParcelableException(
2843 "IllegalAccessException when unmarshalling: " + name);
2844 }
2845 catch (ClassNotFoundException e) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002846 Log.e(TAG, "Class not found when unmarshalling: " + name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 throw new BadParcelableException(
2848 "ClassNotFoundException when unmarshalling: " + name);
2849 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 catch (NoSuchFieldException e) {
2851 throw new BadParcelableException("Parcelable protocol requires a "
Neil Fuller44e440c2015-04-20 14:39:00 +01002852 + "Parcelable.Creator object called "
2853 + "CREATOR on class " + name);
Irfan Sheriff97a72f62013-01-11 10:45:51 -08002854 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 if (creator == null) {
2856 throw new BadParcelableException("Parcelable protocol requires a "
Neil Fuller44e440c2015-04-20 14:39:00 +01002857 + "non-null Parcelable.Creator object called "
2858 + "CREATOR on class " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 }
2860
2861 map.put(name, creator);
2862 }
2863 }
2864
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002865 return creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 }
2867
2868 /**
2869 * Read and return a new Parcelable array from the parcel.
2870 * The given class loader will be used to load any enclosed
2871 * Parcelables.
2872 * @return the Parcelable array, or null if the array is null
2873 */
2874 public final Parcelable[] readParcelableArray(ClassLoader loader) {
2875 int N = readInt();
2876 if (N < 0) {
2877 return null;
2878 }
2879 Parcelable[] p = new Parcelable[N];
2880 for (int i = 0; i < N; i++) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002881 p[i] = readParcelable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 }
2883 return p;
2884 }
2885
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002886 /** @hide */
2887 public final <T extends Parcelable> T[] readParcelableArray(ClassLoader loader,
2888 Class<T> clazz) {
2889 int N = readInt();
2890 if (N < 0) {
2891 return null;
2892 }
2893 T[] p = (T[]) Array.newInstance(clazz, N);
2894 for (int i = 0; i < N; i++) {
2895 p[i] = readParcelable(loader);
2896 }
2897 return p;
2898 }
2899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 /**
2901 * Read and return a new Serializable object from the parcel.
2902 * @return the Serializable object, or null if the Serializable name
2903 * wasn't found in the parcel.
2904 */
2905 public final Serializable readSerializable() {
John Spurlock5002b8c2014-01-10 13:32:12 -05002906 return readSerializable(null);
2907 }
2908
2909 private final Serializable readSerializable(final ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 String name = readString();
2911 if (name == null) {
2912 // For some reason we were unable to read the name of the Serializable (either there
2913 // is nothing left in the Parcel to read, or the next value wasn't a String), so
2914 // return null, which indicates that the name wasn't found in the parcel.
2915 return null;
2916 }
2917
2918 byte[] serializedData = createByteArray();
2919 ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
2920 try {
John Spurlock5002b8c2014-01-10 13:32:12 -05002921 ObjectInputStream ois = new ObjectInputStream(bais) {
2922 @Override
2923 protected Class<?> resolveClass(ObjectStreamClass osClass)
2924 throws IOException, ClassNotFoundException {
2925 // try the custom classloader if provided
2926 if (loader != null) {
2927 Class<?> c = Class.forName(osClass.getName(), false, loader);
2928 if (c != null) {
2929 return c;
2930 }
2931 }
2932 return super.resolveClass(osClass);
2933 }
2934 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 return (Serializable) ois.readObject();
2936 } catch (IOException ioe) {
2937 throw new RuntimeException("Parcelable encountered " +
2938 "IOException reading a Serializable object (name = " + name +
2939 ")", ioe);
2940 } catch (ClassNotFoundException cnfe) {
John Spurlock5002b8c2014-01-10 13:32:12 -05002941 throw new RuntimeException("Parcelable encountered " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 "ClassNotFoundException reading a Serializable object (name = "
2943 + name + ")", cnfe);
2944 }
2945 }
2946
2947 // Cache of previously looked up CREATOR.createFromParcel() methods for
2948 // particular classes. Keys are the names of the classes, values are
2949 // Method objects.
Neil Fuller44e440c2015-04-20 14:39:00 +01002950 private static final HashMap<ClassLoader,HashMap<String,Parcelable.Creator<?>>>
2951 mCreators = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952
Narayan Kamathb34a4612014-01-23 14:17:11 +00002953 /** @hide for internal use only. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 static protected final Parcel obtain(int obj) {
Ashok Bhat8ab665d2014-01-22 16:00:20 +00002955 throw new UnsupportedOperationException();
2956 }
2957
2958 /** @hide */
2959 static protected final Parcel obtain(long obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 final Parcel[] pool = sHolderPool;
2961 synchronized (pool) {
2962 Parcel p;
2963 for (int i=0; i<POOL_SIZE; i++) {
2964 p = pool[i];
2965 if (p != null) {
2966 pool[i] = null;
2967 if (DEBUG_RECYCLE) {
2968 p.mStack = new RuntimeException();
2969 }
2970 p.init(obj);
2971 return p;
2972 }
2973 }
2974 }
2975 return new Parcel(obj);
2976 }
2977
Ashok Bhat8ab665d2014-01-22 16:00:20 +00002978 private Parcel(long nativePtr) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 if (DEBUG_RECYCLE) {
2980 mStack = new RuntimeException();
2981 }
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002982 //Log.i(TAG, "Initializing obj=0x" + Integer.toHexString(obj), mStack);
Jeff Sharkey047238c2012-03-07 16:51:38 -08002983 init(nativePtr);
2984 }
2985
Ashok Bhat8ab665d2014-01-22 16:00:20 +00002986 private void init(long nativePtr) {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002987 if (nativePtr != 0) {
2988 mNativePtr = nativePtr;
2989 mOwnsNativeParcelObject = false;
2990 } else {
2991 mNativePtr = nativeCreate();
2992 mOwnsNativeParcelObject = true;
2993 }
2994 }
2995
2996 private void freeBuffer() {
2997 if (mOwnsNativeParcelObject) {
Adrian Roos04505652015-10-22 16:12:01 -07002998 updateNativeSize(nativeFreeBuffer(mNativePtr));
Jeff Sharkey047238c2012-03-07 16:51:38 -08002999 }
3000 }
3001
3002 private void destroy() {
3003 if (mNativePtr != 0) {
3004 if (mOwnsNativeParcelObject) {
3005 nativeDestroy(mNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -07003006 updateNativeSize(0);
Jeff Sharkey047238c2012-03-07 16:51:38 -08003007 }
3008 mNativePtr = 0;
3009 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003010 }
3011
3012 @Override
3013 protected void finalize() throws Throwable {
3014 if (DEBUG_RECYCLE) {
3015 if (mStack != null) {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003016 Log.w(TAG, "Client did not call Parcel.recycle()", mStack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017 }
3018 }
3019 destroy();
3020 }
3021
Dianne Hackborn6aff9052009-05-22 13:20:23 -07003022 /* package */ void readMapInternal(Map outVal, int N,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 ClassLoader loader) {
3024 while (N > 0) {
3025 Object key = readValue(loader);
3026 Object value = readValue(loader);
3027 outVal.put(key, value);
3028 N--;
3029 }
3030 }
3031
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003032 /* package */ void readArrayMapInternal(ArrayMap outVal, int N,
3033 ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003034 if (DEBUG_ARRAY_MAP) {
3035 RuntimeException here = new RuntimeException("here");
3036 here.fillInStackTrace();
3037 Log.d(TAG, "Reading " + N + " ArrayMap entries", here);
3038 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003039 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003040 while (N > 0) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003041 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003042 String key = readString();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003043 Object value = readValue(loader);
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003044 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " "
3045 + (dataPosition()-startPos) + " bytes: key=0x"
3046 + Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key);
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003047 outVal.append(key, value);
3048 N--;
3049 }
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003050 outVal.validate();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003051 }
3052
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003053 /* package */ void readArrayMapSafelyInternal(ArrayMap outVal, int N,
3054 ClassLoader loader) {
3055 if (DEBUG_ARRAY_MAP) {
3056 RuntimeException here = new RuntimeException("here");
3057 here.fillInStackTrace();
3058 Log.d(TAG, "Reading safely " + N + " ArrayMap entries", here);
3059 }
3060 while (N > 0) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003061 String key = readString();
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003062 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read safe #" + (N-1) + ": key=0x"
3063 + (key != null ? key.hashCode() : 0) + " " + key);
3064 Object value = readValue(loader);
3065 outVal.put(key, value);
3066 N--;
3067 }
3068 }
3069
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003070 /**
3071 * @hide For testing only.
3072 */
3073 public void readArrayMap(ArrayMap outVal, ClassLoader loader) {
3074 final int N = readInt();
3075 if (N < 0) {
3076 return;
3077 }
3078 readArrayMapInternal(outVal, N, loader);
3079 }
3080
Svet Ganovddb94882016-06-23 19:55:24 -07003081 /**
3082 * Reads an array set.
3083 *
3084 * @param loader The class loader to use.
3085 *
3086 * @hide
3087 */
3088 public @Nullable ArraySet<? extends Object> readArraySet(ClassLoader loader) {
3089 final int size = readInt();
3090 if (size < 0) {
3091 return null;
3092 }
3093 ArraySet<Object> result = new ArraySet<>(size);
3094 for (int i = 0; i < size; i++) {
3095 Object value = readValue(loader);
3096 result.append(value);
3097 }
3098 return result;
3099 }
3100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 private void readListInternal(List outVal, int N,
3102 ClassLoader loader) {
3103 while (N > 0) {
3104 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003105 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106 outVal.add(value);
3107 N--;
3108 }
3109 }
3110
3111 private void readArrayInternal(Object[] outVal, int N,
3112 ClassLoader loader) {
3113 for (int i = 0; i < N; i++) {
3114 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003115 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116 outVal[i] = value;
3117 }
3118 }
3119
3120 private void readSparseArrayInternal(SparseArray outVal, int N,
3121 ClassLoader loader) {
3122 while (N > 0) {
3123 int key = readInt();
3124 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003125 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 outVal.append(key, value);
3127 N--;
3128 }
3129 }
3130
3131
3132 private void readSparseBooleanArrayInternal(SparseBooleanArray outVal, int N) {
3133 while (N > 0) {
3134 int key = readInt();
3135 boolean value = this.readByte() == 1;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003136 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 outVal.append(key, value);
3138 N--;
3139 }
3140 }
Dan Sandler5ce04302015-04-09 23:50:15 -04003141
Adam Lesinski4e862812016-11-21 16:02:24 -08003142 private void readSparseIntArrayInternal(SparseIntArray outVal, int N) {
3143 while (N > 0) {
3144 int key = readInt();
3145 int value = readInt();
3146 outVal.append(key, value);
3147 N--;
3148 }
3149 }
3150
Dan Sandler5ce04302015-04-09 23:50:15 -04003151 /**
3152 * @hide For testing
3153 */
3154 public long getBlobAshmemSize() {
3155 return nativeGetBlobAshmemSize(mNativePtr);
3156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157}