blob: 857e8a60098d2a20d95059091bbade2186fda95b [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 Onuki4501c61d2017-07-27 15:56:40 -0700294 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 Onuki4501c61d2017-07-27 15:56:40 -0700309 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 /**
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700342 * @hide
343 */
344 public static class ReadWriteHelper {
345 public static final ReadWriteHelper DEFAULT = new ReadWriteHelper();
346
347 /**
348 * Called when writing a string to a parcel. Subclasses wanting to write a string
349 * must use {@link #writeStringNoHelper(String)} to avoid
350 * infinity recursive calls.
351 */
352 public void writeString(Parcel p, String s) {
353 nativeWriteString(p.mNativePtr, s);
354 }
355
356 /**
357 * Called when reading a string to a parcel. Subclasses wanting to read a string
358 * must use {@link #readStringNoHelper()} to avoid
359 * infinity recursive calls.
360 */
361 public String readString(Parcel p) {
362 return nativeReadString(p.mNativePtr);
363 }
364 }
365
366 private ReadWriteHelper mReadWriteHelper = ReadWriteHelper.DEFAULT;
367
368 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 * Retrieve a new Parcel object from the pool.
370 */
371 public static Parcel obtain() {
372 final Parcel[] pool = sOwnedPool;
373 synchronized (pool) {
374 Parcel p;
375 for (int i=0; i<POOL_SIZE; i++) {
376 p = pool[i];
377 if (p != null) {
378 pool[i] = null;
379 if (DEBUG_RECYCLE) {
380 p.mStack = new RuntimeException();
381 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700382 p.mReadWriteHelper = ReadWriteHelper.DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 return p;
384 }
385 }
386 }
387 return new Parcel(0);
388 }
389
390 /**
391 * Put a Parcel object back into the pool. You must not touch
392 * the object after this call.
393 */
394 public final void recycle() {
395 if (DEBUG_RECYCLE) mStack = null;
396 freeBuffer();
Jeff Sharkey047238c2012-03-07 16:51:38 -0800397
398 final Parcel[] pool;
399 if (mOwnsNativeParcelObject) {
400 pool = sOwnedPool;
401 } else {
402 mNativePtr = 0;
403 pool = sHolderPool;
404 }
405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 synchronized (pool) {
407 for (int i=0; i<POOL_SIZE; i++) {
408 if (pool[i] == null) {
409 pool[i] = this;
410 return;
411 }
412 }
413 }
414 }
415
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700416 /**
417 * Set a {@link ReadWriteHelper}, which can be used to avoid having duplicate strings, for
418 * example.
419 *
420 * @hide
421 */
422 public void setReadWriteHelper(ReadWriteHelper helper) {
423 mReadWriteHelper = helper != null ? helper : ReadWriteHelper.DEFAULT;
424 }
425
426 /**
427 * @return whether this parcel has a {@link ReadWriteHelper}.
428 *
429 * @hide
430 */
431 public boolean hasReadWriteHelper() {
432 return (mReadWriteHelper != null) && (mReadWriteHelper != ReadWriteHelper.DEFAULT);
433 }
434
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800435 /** @hide */
436 public static native long getGlobalAllocSize();
437
438 /** @hide */
439 public static native long getGlobalAllocCount();
440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 /**
442 * Returns the total amount of data contained in the parcel.
443 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800444 public final int dataSize() {
445 return nativeDataSize(mNativePtr);
446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447
448 /**
449 * Returns the amount of data remaining to be read from the
450 * parcel. That is, {@link #dataSize}-{@link #dataPosition}.
451 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800452 public final int dataAvail() {
453 return nativeDataAvail(mNativePtr);
454 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455
456 /**
457 * Returns the current position in the parcel data. Never
458 * more than {@link #dataSize}.
459 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800460 public final int dataPosition() {
461 return nativeDataPosition(mNativePtr);
462 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463
464 /**
465 * Returns the total amount of space in the parcel. This is always
466 * >= {@link #dataSize}. The difference between it and dataSize() is the
467 * amount of room left until the parcel needs to re-allocate its
468 * data buffer.
469 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800470 public final int dataCapacity() {
471 return nativeDataCapacity(mNativePtr);
472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473
474 /**
475 * Change the amount of data in the parcel. Can be either smaller or
476 * larger than the current size. If larger than the current capacity,
477 * more memory will be allocated.
478 *
479 * @param size The new number of bytes in the Parcel.
480 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800481 public final void setDataSize(int size) {
Michael Wachenschwanz138bebf2017-05-18 22:09:18 +0000482 updateNativeSize(nativeSetDataSize(mNativePtr, size));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484
485 /**
486 * Move the current read/write position in the parcel.
487 * @param pos New offset in the parcel; must be between 0 and
488 * {@link #dataSize}.
489 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800490 public final void setDataPosition(int pos) {
491 nativeSetDataPosition(mNativePtr, pos);
492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493
494 /**
495 * Change the capacity (current available space) of the parcel.
496 *
497 * @param size The new capacity of the parcel, in bytes. Can not be
498 * less than {@link #dataSize} -- that is, you can not drop existing data
499 * with this method.
500 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800501 public final void setDataCapacity(int size) {
502 nativeSetDataCapacity(mNativePtr, size);
503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400505 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800506 public final boolean pushAllowFds(boolean allowFds) {
507 return nativePushAllowFds(mNativePtr, allowFds);
508 }
Dianne Hackbornc04db7e2011-10-03 21:09:35 -0700509
510 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800511 public final void restoreAllowFds(boolean lastValue) {
512 nativeRestoreAllowFds(mNativePtr, lastValue);
513 }
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 /**
516 * Returns the raw bytes of the parcel.
517 *
518 * <p class="note">The data you retrieve here <strong>must not</strong>
519 * be placed in any kind of persistent storage (on local disk, across
520 * a network, etc). For that, you should use standard serialization
521 * or another kind of general serialization mechanism. The Parcel
522 * marshalled representation is highly optimized for local IPC, and as
523 * such does not attempt to maintain compatibility with data created
524 * in different versions of the platform.
525 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800526 public final byte[] marshall() {
527 return nativeMarshall(mNativePtr);
528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529
530 /**
531 * Set the bytes in data to be the raw bytes of this Parcel.
532 */
John Spurlocke0852362015-02-04 15:47:40 -0500533 public final void unmarshall(byte[] data, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700534 updateNativeSize(nativeUnmarshall(mNativePtr, data, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536
Jeff Sharkey047238c2012-03-07 16:51:38 -0800537 public final void appendFrom(Parcel parcel, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700538 updateNativeSize(nativeAppendFrom(mNativePtr, parcel.mNativePtr, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700541 /** @hide */
542 public final int compareData(Parcel other) {
543 return nativeCompareData(mNativePtr, other.mNativePtr);
544 }
545
Dianne Hackborn98305522017-05-05 17:53:53 -0700546 /** @hide */
547 public final void setClassCookie(Class clz, Object cookie) {
548 if (mClassCookies == null) {
549 mClassCookies = new ArrayMap<>();
550 }
551 mClassCookies.put(clz, cookie);
552 }
553
554 /** @hide */
555 public final Object getClassCookie(Class clz) {
556 return mClassCookies != null ? mClassCookies.get(clz) : null;
557 }
558
559 /** @hide */
560 public final void adoptClassCookies(Parcel from) {
561 mClassCookies = from.mClassCookies;
562 }
563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 /**
565 * Report whether the parcel contains any marshalled file descriptors.
566 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800567 public final boolean hasFileDescriptors() {
568 return nativeHasFileDescriptors(mNativePtr);
569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570
571 /**
572 * Store or read an IBinder interface token in the parcel at the current
573 * {@link #dataPosition}. This is used to validate that the marshalled
574 * transaction is intended for the target interface.
575 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800576 public final void writeInterfaceToken(String interfaceName) {
577 nativeWriteInterfaceToken(mNativePtr, interfaceName);
578 }
579
580 public final void enforceInterface(String interfaceName) {
581 nativeEnforceInterface(mNativePtr, interfaceName);
582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583
584 /**
Elliott Hughesa28b83e2011-02-28 14:26:13 -0800585 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 * growing {@link #dataCapacity} if needed.
587 * @param b Bytes to place into the parcel.
588 */
589 public final void writeByteArray(byte[] b) {
590 writeByteArray(b, 0, (b != null) ? b.length : 0);
591 }
592
593 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900594 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 * growing {@link #dataCapacity} if needed.
596 * @param b Bytes to place into the parcel.
597 * @param offset Index of first byte to be written.
598 * @param len Number of bytes to write.
599 */
600 public final void writeByteArray(byte[] b, int offset, int len) {
601 if (b == null) {
602 writeInt(-1);
603 return;
604 }
Elliott Hughesa28b83e2011-02-28 14:26:13 -0800605 Arrays.checkOffsetAndCount(b.length, offset, len);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800606 nativeWriteByteArray(mNativePtr, b, offset, len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 }
608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700610 * Write a blob of data into the parcel at the current {@link #dataPosition},
611 * growing {@link #dataCapacity} if needed.
612 * @param b Bytes to place into the parcel.
613 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -0700614 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700615 */
616 public final void writeBlob(byte[] b) {
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500617 writeBlob(b, 0, (b != null) ? b.length : 0);
618 }
619
620 /**
621 * Write a blob of data into the parcel at the current {@link #dataPosition},
622 * growing {@link #dataCapacity} if needed.
623 * @param b Bytes to place into the parcel.
624 * @param offset Index of first byte to be written.
625 * @param len Number of bytes to write.
626 * {@hide}
627 * {@SystemApi}
628 */
629 public final void writeBlob(byte[] b, int offset, int len) {
630 if (b == null) {
631 writeInt(-1);
632 return;
633 }
634 Arrays.checkOffsetAndCount(b.length, offset, len);
635 nativeWriteBlob(mNativePtr, b, offset, len);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700636 }
637
638 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 * Write an integer value into the parcel at the current dataPosition(),
640 * growing dataCapacity() if needed.
641 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800642 public final void writeInt(int val) {
643 nativeWriteInt(mNativePtr, val);
644 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645
646 /**
647 * Write a long integer value into the parcel at the current dataPosition(),
648 * growing dataCapacity() if needed.
649 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800650 public final void writeLong(long val) {
651 nativeWriteLong(mNativePtr, val);
652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653
654 /**
655 * Write a floating point value into the parcel at the current
656 * dataPosition(), growing dataCapacity() if needed.
657 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800658 public final void writeFloat(float val) {
659 nativeWriteFloat(mNativePtr, val);
660 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661
662 /**
663 * Write a double precision floating point value into the parcel at the
664 * current dataPosition(), growing dataCapacity() if needed.
665 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800666 public final void writeDouble(double val) {
667 nativeWriteDouble(mNativePtr, val);
668 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669
670 /**
671 * Write a string value into the parcel at the current dataPosition(),
672 * growing dataCapacity() if needed.
673 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800674 public final void writeString(String val) {
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700675 mReadWriteHelper.writeString(this, val);
676 }
677
678 /**
679 * Write a string without going though a {@link ReadWriteHelper}. Subclasses of
680 * {@link ReadWriteHelper} must use this method instead of {@link #writeString} to avoid
681 * infinity recursive calls.
682 *
683 * @hide
684 */
685 public void writeStringNoHelper(String val) {
Jeff Sharkey047238c2012-03-07 16:51:38 -0800686 nativeWriteString(mNativePtr, val);
687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688
Eugene Susla36e866b2017-02-23 18:24:39 -0800689 /** @hide */
690 public final void writeBoolean(boolean val) {
691 writeInt(val ? 1 : 0);
692 }
693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000695 * Write a CharSequence value into the parcel at the current dataPosition(),
696 * growing dataCapacity() if needed.
697 * @hide
698 */
699 public final void writeCharSequence(CharSequence val) {
700 TextUtils.writeToParcel(val, this, 0);
701 }
702
703 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 * Write an object into the parcel at the current dataPosition(),
705 * growing dataCapacity() if needed.
706 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800707 public final void writeStrongBinder(IBinder val) {
708 nativeWriteStrongBinder(mNativePtr, val);
709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710
711 /**
712 * Write an object into the parcel at the current dataPosition(),
713 * growing dataCapacity() if needed.
714 */
715 public final void writeStrongInterface(IInterface val) {
716 writeStrongBinder(val == null ? null : val.asBinder());
717 }
718
719 /**
720 * Write a FileDescriptor into the parcel at the current dataPosition(),
721 * growing dataCapacity() if needed.
Dan Egnorb3e4ef32010-07-20 09:03:35 -0700722 *
723 * <p class="caution">The file descriptor will not be closed, which may
724 * result in file descriptor leaks when objects are returned from Binder
725 * calls. Use {@link ParcelFileDescriptor#writeToParcel} instead, which
726 * accepts contextual flags and will close the original file descriptor
727 * if {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} is set.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800729 public final void writeFileDescriptor(FileDescriptor val) {
Adrian Roos04505652015-10-22 16:12:01 -0700730 updateNativeSize(nativeWriteFileDescriptor(mNativePtr, val));
731 }
732
733 private void updateNativeSize(long newNativeSize) {
734 if (mOwnsNativeParcelObject) {
735 if (newNativeSize > Integer.MAX_VALUE) {
736 newNativeSize = Integer.MAX_VALUE;
737 }
738 if (newNativeSize != mNativeSize) {
739 int delta = (int) (newNativeSize - mNativeSize);
740 if (delta > 0) {
741 VMRuntime.getRuntime().registerNativeAllocation(delta);
742 } else {
743 VMRuntime.getRuntime().registerNativeFree(-delta);
744 }
745 mNativeSize = newNativeSize;
746 }
747 }
Jeff Sharkey047238c2012-03-07 16:51:38 -0800748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749
750 /**
Casey Dahlin2f974b22015-11-05 12:19:13 -0800751 * {@hide}
752 * This will be the new name for writeFileDescriptor, for consistency.
753 **/
754 public final void writeRawFileDescriptor(FileDescriptor val) {
755 nativeWriteFileDescriptor(mNativePtr, val);
756 }
757
758 /**
759 * {@hide}
760 * Write an array of FileDescriptor objects into the Parcel.
761 *
762 * @param value The array of objects to be written.
763 */
764 public final void writeRawFileDescriptorArray(FileDescriptor[] value) {
765 if (value != null) {
766 int N = value.length;
767 writeInt(N);
768 for (int i=0; i<N; i++) {
769 writeRawFileDescriptor(value[i]);
770 }
771 } else {
772 writeInt(-1);
773 }
774 }
775
776 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900777 * Write a byte value into the parcel at the current dataPosition(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 * growing dataCapacity() if needed.
779 */
780 public final void writeByte(byte val) {
781 writeInt(val);
782 }
783
784 /**
785 * Please use {@link #writeBundle} instead. Flattens a Map into the parcel
786 * at the current dataPosition(),
787 * growing dataCapacity() if needed. The Map keys must be String objects.
788 * The Map values are written using {@link #writeValue} and must follow
789 * the specification there.
Samuel Tana8036662015-11-23 14:36:00 -0800790 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 * <p>It is strongly recommended to use {@link #writeBundle} instead of
792 * this method, since the Bundle class provides a type-safe API that
793 * allows you to avoid mysterious type errors at the point of marshalling.
794 */
795 public final void writeMap(Map val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700796 writeMapInternal((Map<String, Object>) val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 }
798
799 /**
800 * Flatten a Map into the parcel at the current dataPosition(),
801 * growing dataCapacity() if needed. The Map keys must be String objects.
802 */
Dianne Hackborn6aff9052009-05-22 13:20:23 -0700803 /* package */ void writeMapInternal(Map<String,Object> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 if (val == null) {
805 writeInt(-1);
806 return;
807 }
808 Set<Map.Entry<String,Object>> entries = val.entrySet();
809 writeInt(entries.size());
810 for (Map.Entry<String,Object> e : entries) {
811 writeValue(e.getKey());
812 writeValue(e.getValue());
813 }
814 }
815
816 /**
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700817 * Flatten an ArrayMap into the parcel at the current dataPosition(),
818 * growing dataCapacity() if needed. The Map keys must be String objects.
819 */
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700820 /* package */ void writeArrayMapInternal(ArrayMap<String, Object> val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700821 if (val == null) {
822 writeInt(-1);
823 return;
824 }
Samuel Tan3cefe6a2015-12-14 13:29:17 -0800825 // Keep the format of this Parcel in sync with writeToParcelInner() in
826 // frameworks/native/libs/binder/PersistableBundle.cpp.
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700827 final int N = val.size();
828 writeInt(N);
Dianne Hackborne784d1e2013-09-20 18:13:52 -0700829 if (DEBUG_ARRAY_MAP) {
830 RuntimeException here = new RuntimeException("here");
831 here.fillInStackTrace();
832 Log.d(TAG, "Writing " + N + " ArrayMap entries", here);
833 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700834 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700835 for (int i=0; i<N; i++) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700836 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700837 writeString(val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700838 writeValue(val.valueAt(i));
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700839 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Write #" + i + " "
840 + (dataPosition()-startPos) + " bytes: key=0x"
841 + Integer.toHexString(val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0)
842 + " " + val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700843 }
844 }
845
846 /**
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700847 * @hide For testing only.
848 */
849 public void writeArrayMap(ArrayMap<String, Object> val) {
850 writeArrayMapInternal(val);
851 }
852
853 /**
Svet Ganovddb94882016-06-23 19:55:24 -0700854 * Write an array set to the parcel.
855 *
856 * @param val The array set to write.
857 *
858 * @hide
859 */
860 public void writeArraySet(@Nullable ArraySet<? extends Object> val) {
861 final int size = (val != null) ? val.size() : -1;
862 writeInt(size);
863 for (int i = 0; i < size; i++) {
864 writeValue(val.valueAt(i));
865 }
866 }
867
868 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 * Flatten a Bundle into the parcel at the current dataPosition(),
870 * growing dataCapacity() if needed.
871 */
872 public final void writeBundle(Bundle val) {
873 if (val == null) {
874 writeInt(-1);
875 return;
876 }
877
Dianne Hackborn6aff9052009-05-22 13:20:23 -0700878 val.writeToParcel(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 }
880
881 /**
Craig Mautner719e6b12014-04-04 20:29:41 -0700882 * Flatten a PersistableBundle into the parcel at the current dataPosition(),
883 * growing dataCapacity() if needed.
884 */
885 public final void writePersistableBundle(PersistableBundle val) {
886 if (val == null) {
887 writeInt(-1);
888 return;
889 }
890
891 val.writeToParcel(this, 0);
892 }
893
894 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -0700895 * Flatten a Size into the parcel at the current dataPosition(),
896 * growing dataCapacity() if needed.
897 */
898 public final void writeSize(Size val) {
899 writeInt(val.getWidth());
900 writeInt(val.getHeight());
901 }
902
903 /**
904 * Flatten a SizeF into the parcel at the current dataPosition(),
905 * growing dataCapacity() if needed.
906 */
907 public final void writeSizeF(SizeF val) {
908 writeFloat(val.getWidth());
909 writeFloat(val.getHeight());
910 }
911
912 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 * Flatten a List into the parcel at the current dataPosition(), growing
914 * dataCapacity() if needed. The List values are written using
915 * {@link #writeValue} and must follow the specification there.
916 */
917 public final void writeList(List val) {
918 if (val == null) {
919 writeInt(-1);
920 return;
921 }
922 int N = val.size();
923 int i=0;
924 writeInt(N);
925 while (i < N) {
926 writeValue(val.get(i));
927 i++;
928 }
929 }
930
931 /**
932 * Flatten an Object array into the parcel at the current dataPosition(),
933 * growing dataCapacity() if needed. The array values are written using
934 * {@link #writeValue} and must follow the specification there.
935 */
936 public final void writeArray(Object[] val) {
937 if (val == null) {
938 writeInt(-1);
939 return;
940 }
941 int N = val.length;
942 int i=0;
943 writeInt(N);
944 while (i < N) {
945 writeValue(val[i]);
946 i++;
947 }
948 }
949
950 /**
951 * Flatten a generic SparseArray into the parcel at the current
952 * dataPosition(), growing dataCapacity() if needed. The SparseArray
953 * values are written using {@link #writeValue} and must follow the
954 * specification there.
955 */
956 public final void writeSparseArray(SparseArray<Object> val) {
957 if (val == null) {
958 writeInt(-1);
959 return;
960 }
961 int N = val.size();
962 writeInt(N);
963 int i=0;
964 while (i < N) {
965 writeInt(val.keyAt(i));
966 writeValue(val.valueAt(i));
967 i++;
968 }
969 }
970
971 public final void writeSparseBooleanArray(SparseBooleanArray val) {
972 if (val == null) {
973 writeInt(-1);
974 return;
975 }
976 int N = val.size();
977 writeInt(N);
978 int i=0;
979 while (i < N) {
980 writeInt(val.keyAt(i));
981 writeByte((byte)(val.valueAt(i) ? 1 : 0));
982 i++;
983 }
984 }
985
Adam Lesinski205656d2017-03-23 13:38:26 -0700986 /**
987 * @hide
988 */
Adam Lesinski4e862812016-11-21 16:02:24 -0800989 public final void writeSparseIntArray(SparseIntArray val) {
990 if (val == null) {
991 writeInt(-1);
992 return;
993 }
994 int N = val.size();
995 writeInt(N);
996 int i=0;
997 while (i < N) {
998 writeInt(val.keyAt(i));
999 writeInt(val.valueAt(i));
1000 i++;
1001 }
1002 }
1003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 public final void writeBooleanArray(boolean[] val) {
1005 if (val != null) {
1006 int N = val.length;
1007 writeInt(N);
1008 for (int i=0; i<N; i++) {
1009 writeInt(val[i] ? 1 : 0);
1010 }
1011 } else {
1012 writeInt(-1);
1013 }
1014 }
1015
1016 public final boolean[] createBooleanArray() {
1017 int N = readInt();
1018 // >>2 as a fast divide-by-4 works in the create*Array() functions
1019 // because dataAvail() will never return a negative number. 4 is
1020 // the size of a stored boolean in the stream.
1021 if (N >= 0 && N <= (dataAvail() >> 2)) {
1022 boolean[] val = new boolean[N];
1023 for (int i=0; i<N; i++) {
1024 val[i] = readInt() != 0;
1025 }
1026 return val;
1027 } else {
1028 return null;
1029 }
1030 }
1031
1032 public final void readBooleanArray(boolean[] val) {
1033 int N = readInt();
1034 if (N == val.length) {
1035 for (int i=0; i<N; i++) {
1036 val[i] = readInt() != 0;
1037 }
1038 } else {
1039 throw new RuntimeException("bad array lengths");
1040 }
1041 }
1042
1043 public final void writeCharArray(char[] val) {
1044 if (val != null) {
1045 int N = val.length;
1046 writeInt(N);
1047 for (int i=0; i<N; i++) {
1048 writeInt((int)val[i]);
1049 }
1050 } else {
1051 writeInt(-1);
1052 }
1053 }
1054
1055 public final char[] createCharArray() {
1056 int N = readInt();
1057 if (N >= 0 && N <= (dataAvail() >> 2)) {
1058 char[] val = new char[N];
1059 for (int i=0; i<N; i++) {
1060 val[i] = (char)readInt();
1061 }
1062 return val;
1063 } else {
1064 return null;
1065 }
1066 }
1067
1068 public final void readCharArray(char[] val) {
1069 int N = readInt();
1070 if (N == val.length) {
1071 for (int i=0; i<N; i++) {
1072 val[i] = (char)readInt();
1073 }
1074 } else {
1075 throw new RuntimeException("bad array lengths");
1076 }
1077 }
1078
1079 public final void writeIntArray(int[] val) {
1080 if (val != null) {
1081 int N = val.length;
1082 writeInt(N);
1083 for (int i=0; i<N; i++) {
1084 writeInt(val[i]);
1085 }
1086 } else {
1087 writeInt(-1);
1088 }
1089 }
1090
1091 public final int[] createIntArray() {
1092 int N = readInt();
1093 if (N >= 0 && N <= (dataAvail() >> 2)) {
1094 int[] val = new int[N];
1095 for (int i=0; i<N; i++) {
1096 val[i] = readInt();
1097 }
1098 return val;
1099 } else {
1100 return null;
1101 }
1102 }
1103
1104 public final void readIntArray(int[] val) {
1105 int N = readInt();
1106 if (N == val.length) {
1107 for (int i=0; i<N; i++) {
1108 val[i] = readInt();
1109 }
1110 } else {
1111 throw new RuntimeException("bad array lengths");
1112 }
1113 }
1114
1115 public final void writeLongArray(long[] val) {
1116 if (val != null) {
1117 int N = val.length;
1118 writeInt(N);
1119 for (int i=0; i<N; i++) {
1120 writeLong(val[i]);
1121 }
1122 } else {
1123 writeInt(-1);
1124 }
1125 }
1126
1127 public final long[] createLongArray() {
1128 int N = readInt();
1129 // >>3 because stored longs are 64 bits
1130 if (N >= 0 && N <= (dataAvail() >> 3)) {
1131 long[] val = new long[N];
1132 for (int i=0; i<N; i++) {
1133 val[i] = readLong();
1134 }
1135 return val;
1136 } else {
1137 return null;
1138 }
1139 }
1140
1141 public final void readLongArray(long[] val) {
1142 int N = readInt();
1143 if (N == val.length) {
1144 for (int i=0; i<N; i++) {
1145 val[i] = readLong();
1146 }
1147 } else {
1148 throw new RuntimeException("bad array lengths");
1149 }
1150 }
1151
1152 public final void writeFloatArray(float[] val) {
1153 if (val != null) {
1154 int N = val.length;
1155 writeInt(N);
1156 for (int i=0; i<N; i++) {
1157 writeFloat(val[i]);
1158 }
1159 } else {
1160 writeInt(-1);
1161 }
1162 }
1163
1164 public final float[] createFloatArray() {
1165 int N = readInt();
1166 // >>2 because stored floats are 4 bytes
1167 if (N >= 0 && N <= (dataAvail() >> 2)) {
1168 float[] val = new float[N];
1169 for (int i=0; i<N; i++) {
1170 val[i] = readFloat();
1171 }
1172 return val;
1173 } else {
1174 return null;
1175 }
1176 }
1177
1178 public final void readFloatArray(float[] val) {
1179 int N = readInt();
1180 if (N == val.length) {
1181 for (int i=0; i<N; i++) {
1182 val[i] = readFloat();
1183 }
1184 } else {
1185 throw new RuntimeException("bad array lengths");
1186 }
1187 }
1188
1189 public final void writeDoubleArray(double[] val) {
1190 if (val != null) {
1191 int N = val.length;
1192 writeInt(N);
1193 for (int i=0; i<N; i++) {
1194 writeDouble(val[i]);
1195 }
1196 } else {
1197 writeInt(-1);
1198 }
1199 }
1200
1201 public final double[] createDoubleArray() {
1202 int N = readInt();
1203 // >>3 because stored doubles are 8 bytes
1204 if (N >= 0 && N <= (dataAvail() >> 3)) {
1205 double[] val = new double[N];
1206 for (int i=0; i<N; i++) {
1207 val[i] = readDouble();
1208 }
1209 return val;
1210 } else {
1211 return null;
1212 }
1213 }
1214
1215 public final void readDoubleArray(double[] val) {
1216 int N = readInt();
1217 if (N == val.length) {
1218 for (int i=0; i<N; i++) {
1219 val[i] = readDouble();
1220 }
1221 } else {
1222 throw new RuntimeException("bad array lengths");
1223 }
1224 }
1225
1226 public final void writeStringArray(String[] val) {
1227 if (val != null) {
1228 int N = val.length;
1229 writeInt(N);
1230 for (int i=0; i<N; i++) {
1231 writeString(val[i]);
1232 }
1233 } else {
1234 writeInt(-1);
1235 }
1236 }
1237
1238 public final String[] createStringArray() {
1239 int N = readInt();
1240 if (N >= 0) {
1241 String[] val = new String[N];
1242 for (int i=0; i<N; i++) {
1243 val[i] = readString();
1244 }
1245 return val;
1246 } else {
1247 return null;
1248 }
1249 }
1250
1251 public final void readStringArray(String[] val) {
1252 int N = readInt();
1253 if (N == val.length) {
1254 for (int i=0; i<N; i++) {
1255 val[i] = readString();
1256 }
1257 } else {
1258 throw new RuntimeException("bad array lengths");
1259 }
1260 }
1261
1262 public final void writeBinderArray(IBinder[] val) {
1263 if (val != null) {
1264 int N = val.length;
1265 writeInt(N);
1266 for (int i=0; i<N; i++) {
1267 writeStrongBinder(val[i]);
1268 }
1269 } else {
1270 writeInt(-1);
1271 }
1272 }
1273
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001274 /**
1275 * @hide
1276 */
1277 public final void writeCharSequenceArray(CharSequence[] val) {
1278 if (val != null) {
1279 int N = val.length;
1280 writeInt(N);
1281 for (int i=0; i<N; i++) {
1282 writeCharSequence(val[i]);
1283 }
1284 } else {
1285 writeInt(-1);
1286 }
1287 }
1288
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001289 /**
1290 * @hide
1291 */
1292 public final void writeCharSequenceList(ArrayList<CharSequence> val) {
1293 if (val != null) {
1294 int N = val.size();
1295 writeInt(N);
1296 for (int i=0; i<N; i++) {
1297 writeCharSequence(val.get(i));
1298 }
1299 } else {
1300 writeInt(-1);
1301 }
1302 }
1303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 public final IBinder[] createBinderArray() {
1305 int N = readInt();
1306 if (N >= 0) {
1307 IBinder[] val = new IBinder[N];
1308 for (int i=0; i<N; i++) {
1309 val[i] = readStrongBinder();
1310 }
1311 return val;
1312 } else {
1313 return null;
1314 }
1315 }
1316
1317 public final void readBinderArray(IBinder[] val) {
1318 int N = readInt();
1319 if (N == val.length) {
1320 for (int i=0; i<N; i++) {
1321 val[i] = readStrongBinder();
1322 }
1323 } else {
1324 throw new RuntimeException("bad array lengths");
1325 }
1326 }
1327
1328 /**
1329 * Flatten a List containing a particular object type into the parcel, at
1330 * the current dataPosition() and growing dataCapacity() if needed. The
1331 * type of the objects in the list must be one that implements Parcelable.
1332 * Unlike the generic writeList() method, however, only the raw data of the
1333 * objects is written and not their type, so you must use the corresponding
1334 * readTypedList() to unmarshall them.
1335 *
1336 * @param val The list of objects to be written.
1337 *
1338 * @see #createTypedArrayList
1339 * @see #readTypedList
1340 * @see Parcelable
1341 */
1342 public final <T extends Parcelable> void writeTypedList(List<T> val) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001343 writeTypedList(val, 0);
1344 }
1345
1346 /**
1347 * @hide
1348 */
1349 public <T extends Parcelable> void writeTypedList(List<T> val, int parcelableFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 if (val == null) {
1351 writeInt(-1);
1352 return;
1353 }
1354 int N = val.size();
1355 int i=0;
1356 writeInt(N);
1357 while (i < N) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001358 writeTypedObject(val.get(i), parcelableFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 i++;
1360 }
1361 }
1362
1363 /**
1364 * Flatten a List containing String objects into the parcel, at
1365 * the current dataPosition() and growing dataCapacity() if needed. They
1366 * can later be retrieved with {@link #createStringArrayList} or
1367 * {@link #readStringList}.
1368 *
1369 * @param val The list of strings to be written.
1370 *
1371 * @see #createStringArrayList
1372 * @see #readStringList
1373 */
1374 public final void writeStringList(List<String> val) {
1375 if (val == null) {
1376 writeInt(-1);
1377 return;
1378 }
1379 int N = val.size();
1380 int i=0;
1381 writeInt(N);
1382 while (i < N) {
1383 writeString(val.get(i));
1384 i++;
1385 }
1386 }
1387
1388 /**
1389 * Flatten a List containing IBinder objects into the parcel, at
1390 * the current dataPosition() and growing dataCapacity() if needed. They
1391 * can later be retrieved with {@link #createBinderArrayList} or
1392 * {@link #readBinderList}.
1393 *
1394 * @param val The list of strings to be written.
1395 *
1396 * @see #createBinderArrayList
1397 * @see #readBinderList
1398 */
1399 public final void writeBinderList(List<IBinder> val) {
1400 if (val == null) {
1401 writeInt(-1);
1402 return;
1403 }
1404 int N = val.size();
1405 int i=0;
1406 writeInt(N);
1407 while (i < N) {
1408 writeStrongBinder(val.get(i));
1409 i++;
1410 }
1411 }
1412
1413 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00001414 * Flatten a {@code List} containing arbitrary {@code Parcelable} objects into this parcel
1415 * at the current position. They can later be retrieved using
1416 * {@link #readParcelableList(List, ClassLoader)} if required.
1417 *
1418 * @see #readParcelableList(List, ClassLoader)
1419 * @hide
1420 */
1421 public final <T extends Parcelable> void writeParcelableList(List<T> val, int flags) {
1422 if (val == null) {
1423 writeInt(-1);
1424 return;
1425 }
1426
1427 int N = val.size();
1428 int i=0;
1429 writeInt(N);
1430 while (i < N) {
1431 writeParcelable(val.get(i), flags);
1432 i++;
1433 }
1434 }
1435
1436 /**
Svet Ganov0f4928f2017-02-02 20:02:51 -08001437 * Flatten a homogeneous array containing a particular object type into
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 * the parcel, at
1439 * the current dataPosition() and growing dataCapacity() if needed. The
1440 * type of the objects in the array must be one that implements Parcelable.
1441 * Unlike the {@link #writeParcelableArray} method, however, only the
1442 * raw data of the objects is written and not their type, so you must use
1443 * {@link #readTypedArray} with the correct corresponding
1444 * {@link Parcelable.Creator} implementation to unmarshall them.
1445 *
1446 * @param val The array of objects to be written.
1447 * @param parcelableFlags Contextual flags as per
1448 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1449 *
1450 * @see #readTypedArray
1451 * @see #writeParcelableArray
1452 * @see Parcelable.Creator
1453 */
1454 public final <T extends Parcelable> void writeTypedArray(T[] val,
1455 int parcelableFlags) {
1456 if (val != null) {
1457 int N = val.length;
1458 writeInt(N);
Svet Ganov0f4928f2017-02-02 20:02:51 -08001459 for (int i = 0; i < N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001460 writeTypedObject(val[i], parcelableFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 }
1462 } else {
1463 writeInt(-1);
1464 }
1465 }
1466
1467 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001468 * Flatten the Parcelable object into the parcel.
1469 *
1470 * @param val The Parcelable object to be written.
1471 * @param parcelableFlags Contextual flags as per
1472 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1473 *
1474 * @see #readTypedObject
1475 */
1476 public final <T extends Parcelable> void writeTypedObject(T val, int parcelableFlags) {
1477 if (val != null) {
1478 writeInt(1);
1479 val.writeToParcel(this, parcelableFlags);
1480 } else {
1481 writeInt(0);
1482 }
1483 }
1484
1485 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 * Flatten a generic object in to a parcel. The given Object value may
1487 * currently be one of the following types:
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001488 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 * <ul>
1490 * <li> null
1491 * <li> String
1492 * <li> Byte
1493 * <li> Short
1494 * <li> Integer
1495 * <li> Long
1496 * <li> Float
1497 * <li> Double
1498 * <li> Boolean
1499 * <li> String[]
1500 * <li> boolean[]
1501 * <li> byte[]
1502 * <li> int[]
1503 * <li> long[]
1504 * <li> Object[] (supporting objects of the same type defined here).
1505 * <li> {@link Bundle}
1506 * <li> Map (as supported by {@link #writeMap}).
1507 * <li> Any object that implements the {@link Parcelable} protocol.
1508 * <li> Parcelable[]
1509 * <li> CharSequence (as supported by {@link TextUtils#writeToParcel}).
1510 * <li> List (as supported by {@link #writeList}).
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001511 * <li> {@link SparseArray} (as supported by {@link #writeSparseArray(SparseArray)}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 * <li> {@link IBinder}
1513 * <li> Any object that implements Serializable (but see
1514 * {@link #writeSerializable} for caveats). Note that all of the
1515 * previous types have relatively efficient implementations for
1516 * writing to a Parcel; having to rely on the generic serialization
1517 * approach is much less efficient and should be avoided whenever
1518 * possible.
1519 * </ul>
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001520 *
1521 * <p class="caution">{@link Parcelable} objects are written with
1522 * {@link Parcelable#writeToParcel} using contextual flags of 0. When
1523 * serializing objects containing {@link ParcelFileDescriptor}s,
1524 * this may result in file descriptor leaks when they are returned from
1525 * Binder calls (where {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE}
1526 * should be used).</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 */
1528 public final void writeValue(Object v) {
1529 if (v == null) {
1530 writeInt(VAL_NULL);
1531 } else if (v instanceof String) {
1532 writeInt(VAL_STRING);
1533 writeString((String) v);
1534 } else if (v instanceof Integer) {
1535 writeInt(VAL_INTEGER);
1536 writeInt((Integer) v);
1537 } else if (v instanceof Map) {
1538 writeInt(VAL_MAP);
1539 writeMap((Map) v);
1540 } else if (v instanceof Bundle) {
1541 // Must be before Parcelable
1542 writeInt(VAL_BUNDLE);
1543 writeBundle((Bundle) v);
Samuel Tanceafe5e2015-12-11 16:50:58 -08001544 } else if (v instanceof PersistableBundle) {
1545 writeInt(VAL_PERSISTABLEBUNDLE);
1546 writePersistableBundle((PersistableBundle) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 } else if (v instanceof Parcelable) {
Samuel Tanceafe5e2015-12-11 16:50:58 -08001548 // IMPOTANT: cases for classes that implement Parcelable must
1549 // come before the Parcelable case, so that their specific VAL_*
1550 // types will be written.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 writeInt(VAL_PARCELABLE);
1552 writeParcelable((Parcelable) v, 0);
1553 } else if (v instanceof Short) {
1554 writeInt(VAL_SHORT);
1555 writeInt(((Short) v).intValue());
1556 } else if (v instanceof Long) {
1557 writeInt(VAL_LONG);
1558 writeLong((Long) v);
1559 } else if (v instanceof Float) {
1560 writeInt(VAL_FLOAT);
1561 writeFloat((Float) v);
1562 } else if (v instanceof Double) {
1563 writeInt(VAL_DOUBLE);
1564 writeDouble((Double) v);
1565 } else if (v instanceof Boolean) {
1566 writeInt(VAL_BOOLEAN);
1567 writeInt((Boolean) v ? 1 : 0);
1568 } else if (v instanceof CharSequence) {
1569 // Must be after String
1570 writeInt(VAL_CHARSEQUENCE);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001571 writeCharSequence((CharSequence) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 } else if (v instanceof List) {
1573 writeInt(VAL_LIST);
1574 writeList((List) v);
1575 } else if (v instanceof SparseArray) {
1576 writeInt(VAL_SPARSEARRAY);
1577 writeSparseArray((SparseArray) v);
1578 } else if (v instanceof boolean[]) {
1579 writeInt(VAL_BOOLEANARRAY);
1580 writeBooleanArray((boolean[]) v);
1581 } else if (v instanceof byte[]) {
1582 writeInt(VAL_BYTEARRAY);
1583 writeByteArray((byte[]) v);
1584 } else if (v instanceof String[]) {
1585 writeInt(VAL_STRINGARRAY);
1586 writeStringArray((String[]) v);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001587 } else if (v instanceof CharSequence[]) {
1588 // Must be after String[] and before Object[]
1589 writeInt(VAL_CHARSEQUENCEARRAY);
1590 writeCharSequenceArray((CharSequence[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 } else if (v instanceof IBinder) {
1592 writeInt(VAL_IBINDER);
1593 writeStrongBinder((IBinder) v);
1594 } else if (v instanceof Parcelable[]) {
1595 writeInt(VAL_PARCELABLEARRAY);
1596 writeParcelableArray((Parcelable[]) v, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 } else if (v instanceof int[]) {
1598 writeInt(VAL_INTARRAY);
1599 writeIntArray((int[]) v);
1600 } else if (v instanceof long[]) {
1601 writeInt(VAL_LONGARRAY);
1602 writeLongArray((long[]) v);
1603 } else if (v instanceof Byte) {
1604 writeInt(VAL_BYTE);
1605 writeInt((Byte) v);
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001606 } else if (v instanceof Size) {
1607 writeInt(VAL_SIZE);
1608 writeSize((Size) v);
1609 } else if (v instanceof SizeF) {
1610 writeInt(VAL_SIZEF);
1611 writeSizeF((SizeF) v);
Samuel Tana8036662015-11-23 14:36:00 -08001612 } else if (v instanceof double[]) {
1613 writeInt(VAL_DOUBLEARRAY);
1614 writeDoubleArray((double[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 } else {
Paul Duffinac5a0822014-02-03 15:02:17 +00001616 Class<?> clazz = v.getClass();
1617 if (clazz.isArray() && clazz.getComponentType() == Object.class) {
1618 // Only pure Object[] are written here, Other arrays of non-primitive types are
1619 // handled by serialization as this does not record the component type.
1620 writeInt(VAL_OBJECTARRAY);
1621 writeArray((Object[]) v);
1622 } else if (v instanceof Serializable) {
1623 // Must be last
1624 writeInt(VAL_SERIALIZABLE);
1625 writeSerializable((Serializable) v);
1626 } else {
1627 throw new RuntimeException("Parcel: unable to marshal value " + v);
1628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 }
1630 }
1631
1632 /**
1633 * Flatten the name of the class of the Parcelable and its contents
1634 * into the parcel.
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001635 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 * @param p The Parcelable object to be written.
1637 * @param parcelableFlags Contextual flags as per
1638 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1639 */
1640 public final void writeParcelable(Parcelable p, int parcelableFlags) {
1641 if (p == null) {
1642 writeString(null);
1643 return;
1644 }
Neil Fuller44e440c2015-04-20 14:39:00 +01001645 writeParcelableCreator(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 p.writeToParcel(this, parcelableFlags);
1647 }
1648
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001649 /** @hide */
1650 public final void writeParcelableCreator(Parcelable p) {
1651 String name = p.getClass().getName();
1652 writeString(name);
1653 }
1654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 /**
1656 * Write a generic serializable object in to a Parcel. It is strongly
1657 * recommended that this method be avoided, since the serialization
1658 * overhead is extremely large, and this approach will be much slower than
1659 * using the other approaches to writing data in to a Parcel.
1660 */
1661 public final void writeSerializable(Serializable s) {
1662 if (s == null) {
1663 writeString(null);
1664 return;
1665 }
1666 String name = s.getClass().getName();
1667 writeString(name);
1668
1669 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1670 try {
1671 ObjectOutputStream oos = new ObjectOutputStream(baos);
1672 oos.writeObject(s);
1673 oos.close();
1674
1675 writeByteArray(baos.toByteArray());
1676 } catch (IOException ioe) {
1677 throw new RuntimeException("Parcelable encountered " +
1678 "IOException writing serializable object (name = " + name +
1679 ")", ioe);
1680 }
1681 }
1682
1683 /**
1684 * Special function for writing an exception result at the header of
1685 * a parcel, to be used when returning an exception from a transaction.
1686 * Note that this currently only supports a few exception types; any other
1687 * exception will be re-thrown by this function as a RuntimeException
1688 * (to be caught by the system's last-resort exception handling when
1689 * dispatching a transaction).
Samuel Tana8036662015-11-23 14:36:00 -08001690 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 * <p>The supported exception types are:
1692 * <ul>
1693 * <li>{@link BadParcelableException}
1694 * <li>{@link IllegalArgumentException}
1695 * <li>{@link IllegalStateException}
1696 * <li>{@link NullPointerException}
1697 * <li>{@link SecurityException}
Dianne Hackborn18482ae2017-08-01 17:41:00 -07001698 * <li>{@link UnsupportedOperationException}
Dianne Hackborn7e714422013-09-13 17:32:57 -07001699 * <li>{@link NetworkOnMainThreadException}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 * </ul>
Samuel Tana8036662015-11-23 14:36:00 -08001701 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 * @param e The Exception to be written.
1703 *
1704 * @see #writeNoException
1705 * @see #readException
1706 */
1707 public final void writeException(Exception e) {
1708 int code = 0;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001709 if (e instanceof Parcelable
1710 && (e.getClass().getClassLoader() == Parcelable.class.getClassLoader())) {
1711 // We only send Parcelable exceptions that are in the
1712 // BootClassLoader to ensure that the receiver can unpack them
1713 code = EX_PARCELABLE;
1714 } else if (e instanceof SecurityException) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 code = EX_SECURITY;
1716 } else if (e instanceof BadParcelableException) {
1717 code = EX_BAD_PARCELABLE;
1718 } else if (e instanceof IllegalArgumentException) {
1719 code = EX_ILLEGAL_ARGUMENT;
1720 } else if (e instanceof NullPointerException) {
1721 code = EX_NULL_POINTER;
1722 } else if (e instanceof IllegalStateException) {
1723 code = EX_ILLEGAL_STATE;
Dianne Hackborn7e714422013-09-13 17:32:57 -07001724 } else if (e instanceof NetworkOnMainThreadException) {
1725 code = EX_NETWORK_MAIN_THREAD;
Dianne Hackborn33d738a2014-09-12 14:23:58 -07001726 } else if (e instanceof UnsupportedOperationException) {
1727 code = EX_UNSUPPORTED_OPERATION;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001728 } else if (e instanceof ServiceSpecificException) {
1729 code = EX_SERVICE_SPECIFIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 }
1731 writeInt(code);
Brad Fitzpatrick703e5d32010-07-15 13:16:41 -07001732 StrictMode.clearGatheredViolations();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 if (code == 0) {
1734 if (e instanceof RuntimeException) {
1735 throw (RuntimeException) e;
1736 }
1737 throw new RuntimeException(e);
1738 }
1739 writeString(e.getMessage());
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001740 switch (code) {
1741 case EX_SERVICE_SPECIFIC:
1742 writeInt(((ServiceSpecificException) e).errorCode);
1743 break;
1744 case EX_PARCELABLE:
1745 // Write parceled exception prefixed by length
1746 final int sizePosition = dataPosition();
1747 writeInt(0);
1748 writeParcelable((Parcelable) e, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
1749 final int payloadPosition = dataPosition();
1750 setDataPosition(sizePosition);
1751 writeInt(payloadPosition - sizePosition);
1752 setDataPosition(payloadPosition);
1753 break;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001754 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 }
1756
1757 /**
1758 * Special function for writing information at the front of the Parcel
1759 * indicating that no exception occurred.
1760 *
1761 * @see #writeException
1762 * @see #readException
1763 */
1764 public final void writeNoException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001765 // Despite the name of this function ("write no exception"),
1766 // it should instead be thought of as "write the RPC response
1767 // header", but because this function name is written out by
1768 // the AIDL compiler, we're not going to rename it.
1769 //
1770 // The response header, in the non-exception case (see also
1771 // writeException above, also called by the AIDL compiler), is
1772 // either a 0 (the default case), or EX_HAS_REPLY_HEADER if
1773 // StrictMode has gathered up violations that have occurred
1774 // during a Binder call, in which case we write out the number
1775 // of violations and their details, serialized, before the
1776 // actual RPC respons data. The receiving end of this is
1777 // readException(), below.
1778 if (StrictMode.hasGatheredViolations()) {
1779 writeInt(EX_HAS_REPLY_HEADER);
1780 final int sizePosition = dataPosition();
1781 writeInt(0); // total size of fat header, to be filled in later
1782 StrictMode.writeGatheredViolationsToParcel(this);
1783 final int payloadPosition = dataPosition();
1784 setDataPosition(sizePosition);
1785 writeInt(payloadPosition - sizePosition); // header size
1786 setDataPosition(payloadPosition);
1787 } else {
1788 writeInt(0);
1789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 }
1791
1792 /**
1793 * Special function for reading an exception result from the header of
1794 * a parcel, to be used after receiving the result of a transaction. This
1795 * will throw the exception for you if it had been written to the Parcel,
1796 * otherwise return and let you read the normal result data from the Parcel.
1797 *
1798 * @see #writeException
1799 * @see #writeNoException
1800 */
1801 public final void readException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001802 int code = readExceptionCode();
1803 if (code != 0) {
1804 String msg = readString();
1805 readException(code, msg);
1806 }
1807 }
1808
1809 /**
1810 * Parses the header of a Binder call's response Parcel and
1811 * returns the exception code. Deals with lite or fat headers.
1812 * In the common successful case, this header is generally zero.
1813 * In less common cases, it's a small negative number and will be
1814 * followed by an error string.
1815 *
1816 * This exists purely for android.database.DatabaseUtils and
1817 * insulating it from having to handle fat headers as returned by
1818 * e.g. StrictMode-induced RPC responses.
1819 *
1820 * @hide
1821 */
1822 public final int readExceptionCode() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 int code = readInt();
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001824 if (code == EX_HAS_REPLY_HEADER) {
1825 int headerSize = readInt();
1826 if (headerSize == 0) {
1827 Log.e(TAG, "Unexpected zero-sized Parcel reply header.");
1828 } else {
1829 // Currently the only thing in the header is StrictMode stacks,
1830 // but discussions around event/RPC tracing suggest we might
1831 // put that here too. If so, switch on sub-header tags here.
1832 // But for now, just parse out the StrictMode stuff.
1833 StrictMode.readAndHandleBinderCallViolations(this);
1834 }
1835 // And fat response headers are currently only used when
1836 // there are no exceptions, so return no error:
1837 return 0;
1838 }
1839 return code;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 }
1841
1842 /**
Mark Doliner879ea452014-01-02 12:38:07 -08001843 * Throw an exception with the given message. Not intended for use
1844 * outside the Parcel class.
1845 *
1846 * @param code Used to determine which exception class to throw.
1847 * @param msg The exception message.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 */
1849 public final void readException(int code, String msg) {
1850 switch (code) {
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001851 case EX_PARCELABLE:
1852 if (readInt() > 0) {
1853 SneakyThrow.sneakyThrow(
1854 (Exception) readParcelable(Parcelable.class.getClassLoader()));
1855 } else {
1856 throw new RuntimeException(msg + " [missing Parcelable]");
1857 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 case EX_SECURITY:
1859 throw new SecurityException(msg);
1860 case EX_BAD_PARCELABLE:
1861 throw new BadParcelableException(msg);
1862 case EX_ILLEGAL_ARGUMENT:
1863 throw new IllegalArgumentException(msg);
1864 case EX_NULL_POINTER:
1865 throw new NullPointerException(msg);
1866 case EX_ILLEGAL_STATE:
1867 throw new IllegalStateException(msg);
Dianne Hackborn7e714422013-09-13 17:32:57 -07001868 case EX_NETWORK_MAIN_THREAD:
1869 throw new NetworkOnMainThreadException();
Dianne Hackborn33d738a2014-09-12 14:23:58 -07001870 case EX_UNSUPPORTED_OPERATION:
1871 throw new UnsupportedOperationException(msg);
Christopher Wiley80fd1202015-11-22 17:12:37 -08001872 case EX_SERVICE_SPECIFIC:
1873 throw new ServiceSpecificException(readInt(), msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 }
1875 throw new RuntimeException("Unknown exception code: " + code
1876 + " msg " + msg);
1877 }
1878
1879 /**
1880 * Read an integer value from the parcel at the current dataPosition().
1881 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001882 public final int readInt() {
1883 return nativeReadInt(mNativePtr);
1884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885
1886 /**
1887 * Read a long integer value from the parcel at the current dataPosition().
1888 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001889 public final long readLong() {
1890 return nativeReadLong(mNativePtr);
1891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892
1893 /**
1894 * Read a floating point value from the parcel at the current
1895 * dataPosition().
1896 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001897 public final float readFloat() {
1898 return nativeReadFloat(mNativePtr);
1899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900
1901 /**
1902 * Read a double precision floating point value from the parcel at the
1903 * current dataPosition().
1904 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001905 public final double readDouble() {
1906 return nativeReadDouble(mNativePtr);
1907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908
1909 /**
1910 * Read a string value from the parcel at the current dataPosition().
1911 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001912 public final String readString() {
Makoto Onuki4501c61d2017-07-27 15:56:40 -07001913 return mReadWriteHelper.readString(this);
1914 }
1915
1916 /**
1917 * Read a string without going though a {@link ReadWriteHelper}. Subclasses of
1918 * {@link ReadWriteHelper} must use this method instead of {@link #readString} to avoid
1919 * infinity recursive calls.
1920 *
1921 * @hide
1922 */
1923 public String readStringNoHelper() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08001924 return nativeReadString(mNativePtr);
1925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926
Eugene Susla36e866b2017-02-23 18:24:39 -08001927 /** @hide */
1928 public final boolean readBoolean() {
1929 return readInt() != 0;
1930 }
1931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001933 * Read a CharSequence value from the parcel at the current dataPosition().
1934 * @hide
1935 */
1936 public final CharSequence readCharSequence() {
1937 return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(this);
1938 }
1939
1940 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 * Read an object from the parcel at the current dataPosition().
1942 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001943 public final IBinder readStrongBinder() {
1944 return nativeReadStrongBinder(mNativePtr);
1945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946
1947 /**
1948 * Read a FileDescriptor from the parcel at the current dataPosition().
1949 */
1950 public final ParcelFileDescriptor readFileDescriptor() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08001951 FileDescriptor fd = nativeReadFileDescriptor(mNativePtr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 return fd != null ? new ParcelFileDescriptor(fd) : null;
1953 }
1954
Jeff Sharkeyda5a3e12013-08-11 12:54:42 -07001955 /** {@hide} */
1956 public final FileDescriptor readRawFileDescriptor() {
1957 return nativeReadFileDescriptor(mNativePtr);
1958 }
1959
Casey Dahlin2f974b22015-11-05 12:19:13 -08001960 /**
1961 * {@hide}
1962 * Read and return a new array of FileDescriptors from the parcel.
1963 * @return the FileDescriptor array, or null if the array is null.
1964 **/
1965 public final FileDescriptor[] createRawFileDescriptorArray() {
1966 int N = readInt();
1967 if (N < 0) {
1968 return null;
1969 }
1970 FileDescriptor[] f = new FileDescriptor[N];
1971 for (int i = 0; i < N; i++) {
1972 f[i] = readRawFileDescriptor();
1973 }
1974 return f;
1975 }
1976
1977 /**
1978 * {@hide}
1979 * Read an array of FileDescriptors from a parcel.
1980 * The passed array must be exactly the length of the array in the parcel.
1981 * @return the FileDescriptor array, or null if the array is null.
1982 **/
1983 public final void readRawFileDescriptorArray(FileDescriptor[] val) {
1984 int N = readInt();
1985 if (N == val.length) {
1986 for (int i=0; i<N; i++) {
1987 val[i] = readRawFileDescriptor();
1988 }
1989 } else {
1990 throw new RuntimeException("bad array lengths");
1991 }
1992 }
1993
Jeff Sharkeye53e2d92017-03-25 23:14:06 -06001994 /** @deprecated use {@link android.system.Os#open(String, int, int)} */
1995 @Deprecated
1996 static native FileDescriptor openFileDescriptor(String file, int mode)
1997 throws FileNotFoundException;
Casey Dahlin2f974b22015-11-05 12:19:13 -08001998
Jeff Sharkeye53e2d92017-03-25 23:14:06 -06001999 /** @deprecated use {@link android.system.Os#dup(FileDescriptor)} */
2000 @Deprecated
2001 static native FileDescriptor dupFileDescriptor(FileDescriptor orig) throws IOException;
2002
2003 /** @deprecated use {@link android.system.Os#close(FileDescriptor)} */
2004 @Deprecated
2005 static native void closeFileDescriptor(FileDescriptor desc) throws IOException;
2006
2007 static native void clearFileDescriptor(FileDescriptor desc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008
2009 /**
2010 * Read a byte value from the parcel at the current dataPosition().
2011 */
2012 public final byte readByte() {
2013 return (byte)(readInt() & 0xff);
2014 }
2015
2016 /**
2017 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2018 * been written with {@link #writeBundle}. Read into an existing Map object
2019 * from the parcel at the current dataPosition().
2020 */
2021 public final void readMap(Map outVal, ClassLoader loader) {
2022 int N = readInt();
2023 readMapInternal(outVal, N, loader);
2024 }
2025
2026 /**
2027 * Read into an existing List object from the parcel at the current
2028 * dataPosition(), using the given class loader to load any enclosed
2029 * Parcelables. If it is null, the default class loader is used.
2030 */
2031 public final void readList(List outVal, ClassLoader loader) {
2032 int N = readInt();
2033 readListInternal(outVal, N, loader);
2034 }
2035
2036 /**
2037 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2038 * been written with {@link #writeBundle}. Read and return a new HashMap
2039 * object from the parcel at the current dataPosition(), using the given
2040 * class loader to load any enclosed Parcelables. Returns null if
2041 * the previously written map object was null.
2042 */
2043 public final HashMap readHashMap(ClassLoader loader)
2044 {
2045 int N = readInt();
2046 if (N < 0) {
2047 return null;
2048 }
2049 HashMap m = new HashMap(N);
2050 readMapInternal(m, N, loader);
2051 return m;
2052 }
2053
2054 /**
2055 * Read and return a new Bundle object from the parcel at the current
2056 * dataPosition(). Returns null if the previously written Bundle object was
2057 * null.
2058 */
2059 public final Bundle readBundle() {
2060 return readBundle(null);
2061 }
2062
2063 /**
2064 * Read and return a new Bundle object from the parcel at the current
2065 * dataPosition(), using the given class loader to initialize the class
2066 * loader of the Bundle for later retrieval of Parcelable objects.
2067 * Returns null if the previously written Bundle object was null.
2068 */
2069 public final Bundle readBundle(ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 int length = readInt();
2071 if (length < 0) {
Dianne Hackborn4a7d8242013-10-03 10:19:20 -07002072 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 return null;
2074 }
Samuel Tana8036662015-11-23 14:36:00 -08002075
Dianne Hackborn6aff9052009-05-22 13:20:23 -07002076 final Bundle bundle = new Bundle(this, length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 if (loader != null) {
2078 bundle.setClassLoader(loader);
2079 }
2080 return bundle;
2081 }
2082
2083 /**
Craig Mautner719e6b12014-04-04 20:29:41 -07002084 * Read and return a new Bundle object from the parcel at the current
2085 * dataPosition(). Returns null if the previously written Bundle object was
2086 * null.
2087 */
2088 public final PersistableBundle readPersistableBundle() {
2089 return readPersistableBundle(null);
2090 }
2091
2092 /**
2093 * Read and return a new Bundle object from the parcel at the current
2094 * dataPosition(), using the given class loader to initialize the class
2095 * loader of the Bundle for later retrieval of Parcelable objects.
2096 * Returns null if the previously written Bundle object was null.
2097 */
2098 public final PersistableBundle readPersistableBundle(ClassLoader loader) {
2099 int length = readInt();
2100 if (length < 0) {
2101 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
2102 return null;
2103 }
2104
2105 final PersistableBundle bundle = new PersistableBundle(this, length);
2106 if (loader != null) {
2107 bundle.setClassLoader(loader);
2108 }
2109 return bundle;
2110 }
2111
2112 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002113 * Read a Size from the parcel at the current dataPosition().
2114 */
2115 public final Size readSize() {
2116 final int width = readInt();
2117 final int height = readInt();
2118 return new Size(width, height);
2119 }
2120
2121 /**
2122 * Read a SizeF from the parcel at the current dataPosition().
2123 */
2124 public final SizeF readSizeF() {
2125 final float width = readFloat();
2126 final float height = readFloat();
2127 return new SizeF(width, height);
2128 }
2129
2130 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 * Read and return a byte[] object from the parcel.
2132 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002133 public final byte[] createByteArray() {
2134 return nativeCreateByteArray(mNativePtr);
2135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136
2137 /**
2138 * Read a byte[] object from the parcel and copy it into the
2139 * given byte array.
2140 */
2141 public final void readByteArray(byte[] val) {
Jocelyn Dang46100442017-05-05 15:40:49 -07002142 boolean valid = nativeReadByteArray(mNativePtr, val, (val != null) ? val.length : 0);
2143 if (!valid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 throw new RuntimeException("bad array lengths");
2145 }
2146 }
2147
2148 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002149 * Read a blob of data from the parcel and return it as a byte array.
2150 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -07002151 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002152 */
2153 public final byte[] readBlob() {
2154 return nativeReadBlob(mNativePtr);
2155 }
2156
2157 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 * Read and return a String[] object from the parcel.
2159 * {@hide}
2160 */
2161 public final String[] readStringArray() {
2162 String[] array = null;
2163
2164 int length = readInt();
2165 if (length >= 0)
2166 {
2167 array = new String[length];
2168
2169 for (int i = 0 ; i < length ; i++)
2170 {
2171 array[i] = readString();
2172 }
2173 }
2174
2175 return array;
2176 }
2177
2178 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002179 * Read and return a CharSequence[] object from the parcel.
2180 * {@hide}
2181 */
2182 public final CharSequence[] readCharSequenceArray() {
2183 CharSequence[] array = null;
2184
2185 int length = readInt();
2186 if (length >= 0)
2187 {
2188 array = new CharSequence[length];
2189
2190 for (int i = 0 ; i < length ; i++)
2191 {
2192 array[i] = readCharSequence();
2193 }
2194 }
2195
2196 return array;
2197 }
2198
2199 /**
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002200 * Read and return an ArrayList&lt;CharSequence&gt; object from the parcel.
2201 * {@hide}
2202 */
2203 public final ArrayList<CharSequence> readCharSequenceList() {
2204 ArrayList<CharSequence> array = null;
2205
2206 int length = readInt();
2207 if (length >= 0) {
2208 array = new ArrayList<CharSequence>(length);
2209
2210 for (int i = 0 ; i < length ; i++) {
2211 array.add(readCharSequence());
2212 }
2213 }
2214
2215 return array;
2216 }
2217
2218 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 * Read and return a new ArrayList object from the parcel at the current
2220 * dataPosition(). Returns null if the previously written list object was
2221 * null. The given class loader will be used to load any enclosed
2222 * Parcelables.
2223 */
2224 public final ArrayList readArrayList(ClassLoader loader) {
2225 int N = readInt();
2226 if (N < 0) {
2227 return null;
2228 }
2229 ArrayList l = new ArrayList(N);
2230 readListInternal(l, N, loader);
2231 return l;
2232 }
2233
2234 /**
2235 * Read and return a new Object array from the parcel at the current
2236 * dataPosition(). Returns null if the previously written array was
2237 * null. The given class loader will be used to load any enclosed
2238 * Parcelables.
2239 */
2240 public final Object[] readArray(ClassLoader loader) {
2241 int N = readInt();
2242 if (N < 0) {
2243 return null;
2244 }
2245 Object[] l = new Object[N];
2246 readArrayInternal(l, N, loader);
2247 return l;
2248 }
2249
2250 /**
2251 * Read and return a new SparseArray object from the parcel at the current
2252 * dataPosition(). Returns null if the previously written list object was
2253 * null. The given class loader will be used to load any enclosed
2254 * Parcelables.
2255 */
2256 public final SparseArray readSparseArray(ClassLoader loader) {
2257 int N = readInt();
2258 if (N < 0) {
2259 return null;
2260 }
2261 SparseArray sa = new SparseArray(N);
2262 readSparseArrayInternal(sa, N, loader);
2263 return sa;
2264 }
2265
2266 /**
2267 * Read and return a new SparseBooleanArray object from the parcel at the current
2268 * dataPosition(). Returns null if the previously written list object was
2269 * null.
2270 */
2271 public final SparseBooleanArray readSparseBooleanArray() {
2272 int N = readInt();
2273 if (N < 0) {
2274 return null;
2275 }
2276 SparseBooleanArray sa = new SparseBooleanArray(N);
2277 readSparseBooleanArrayInternal(sa, N);
2278 return sa;
2279 }
2280
2281 /**
Adam Lesinski4e862812016-11-21 16:02:24 -08002282 * Read and return a new SparseIntArray object from the parcel at the current
2283 * dataPosition(). Returns null if the previously written array object was null.
Adam Lesinski205656d2017-03-23 13:38:26 -07002284 * @hide
Adam Lesinski4e862812016-11-21 16:02:24 -08002285 */
2286 public final SparseIntArray readSparseIntArray() {
2287 int N = readInt();
2288 if (N < 0) {
2289 return null;
2290 }
2291 SparseIntArray sa = new SparseIntArray(N);
2292 readSparseIntArrayInternal(sa, N);
2293 return sa;
2294 }
2295
2296 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 * Read and return a new ArrayList containing a particular object type from
2298 * the parcel that was written with {@link #writeTypedList} at the
2299 * current dataPosition(). Returns null if the
2300 * previously written list object was null. The list <em>must</em> have
2301 * previously been written via {@link #writeTypedList} with the same object
2302 * type.
2303 *
2304 * @return A newly created ArrayList containing objects with the same data
2305 * as those that were previously written.
2306 *
2307 * @see #writeTypedList
2308 */
2309 public final <T> ArrayList<T> createTypedArrayList(Parcelable.Creator<T> c) {
2310 int N = readInt();
2311 if (N < 0) {
2312 return null;
2313 }
2314 ArrayList<T> l = new ArrayList<T>(N);
2315 while (N > 0) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002316 l.add(readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 N--;
2318 }
2319 return l;
2320 }
2321
2322 /**
2323 * Read into the given List items containing a particular object type
2324 * that were written with {@link #writeTypedList} at the
2325 * current dataPosition(). The list <em>must</em> have
2326 * previously been written via {@link #writeTypedList} with the same object
2327 * type.
2328 *
2329 * @return A newly created ArrayList containing objects with the same data
2330 * as those that were previously written.
2331 *
2332 * @see #writeTypedList
2333 */
2334 public final <T> void readTypedList(List<T> list, Parcelable.Creator<T> c) {
2335 int M = list.size();
2336 int N = readInt();
2337 int i = 0;
2338 for (; i < M && i < N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002339 list.set(i, readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 }
2341 for (; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002342 list.add(readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 }
2344 for (; i<M; i++) {
2345 list.remove(N);
2346 }
2347 }
2348
2349 /**
2350 * Read and return a new ArrayList containing String objects from
2351 * the parcel that was written with {@link #writeStringList} at the
2352 * current dataPosition(). Returns null if the
2353 * previously written list object was null.
2354 *
2355 * @return A newly created ArrayList containing strings with the same data
2356 * as those that were previously written.
2357 *
2358 * @see #writeStringList
2359 */
2360 public final ArrayList<String> createStringArrayList() {
2361 int N = readInt();
2362 if (N < 0) {
2363 return null;
2364 }
2365 ArrayList<String> l = new ArrayList<String>(N);
2366 while (N > 0) {
2367 l.add(readString());
2368 N--;
2369 }
2370 return l;
2371 }
2372
2373 /**
2374 * Read and return a new ArrayList containing IBinder objects from
2375 * the parcel that was written with {@link #writeBinderList} at the
2376 * current dataPosition(). Returns null if the
2377 * previously written list object was null.
2378 *
2379 * @return A newly created ArrayList containing strings with the same data
2380 * as those that were previously written.
2381 *
2382 * @see #writeBinderList
2383 */
2384 public final ArrayList<IBinder> createBinderArrayList() {
2385 int N = readInt();
2386 if (N < 0) {
2387 return null;
2388 }
2389 ArrayList<IBinder> l = new ArrayList<IBinder>(N);
2390 while (N > 0) {
2391 l.add(readStrongBinder());
2392 N--;
2393 }
2394 return l;
2395 }
2396
2397 /**
2398 * Read into the given List items String objects that were written with
2399 * {@link #writeStringList} at the current dataPosition().
2400 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 * @see #writeStringList
2402 */
2403 public final void readStringList(List<String> list) {
2404 int M = list.size();
2405 int N = readInt();
2406 int i = 0;
2407 for (; i < M && i < N; i++) {
2408 list.set(i, readString());
2409 }
2410 for (; i<N; i++) {
2411 list.add(readString());
2412 }
2413 for (; i<M; i++) {
2414 list.remove(N);
2415 }
2416 }
2417
2418 /**
2419 * Read into the given List items IBinder objects that were written with
2420 * {@link #writeBinderList} at the current dataPosition().
2421 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 * @see #writeBinderList
2423 */
2424 public final void readBinderList(List<IBinder> list) {
2425 int M = list.size();
2426 int N = readInt();
2427 int i = 0;
2428 for (; i < M && i < N; i++) {
2429 list.set(i, readStrongBinder());
2430 }
2431 for (; i<N; i++) {
2432 list.add(readStrongBinder());
2433 }
2434 for (; i<M; i++) {
2435 list.remove(N);
2436 }
2437 }
2438
2439 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00002440 * Read the list of {@code Parcelable} objects at the current data position into the
2441 * given {@code list}. The contents of the {@code list} are replaced. If the serialized
2442 * list was {@code null}, {@code list} is cleared.
2443 *
2444 * @see #writeParcelableList(List, int)
2445 * @hide
2446 */
Eugene Susla36e866b2017-02-23 18:24:39 -08002447 public final <T extends Parcelable> List<T> readParcelableList(List<T> list, ClassLoader cl) {
Narayan Kamathbea48712016-12-01 15:38:28 +00002448 final int N = readInt();
2449 if (N == -1) {
2450 list.clear();
Eugene Susla36e866b2017-02-23 18:24:39 -08002451 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002452 }
2453
2454 final int M = list.size();
2455 int i = 0;
2456 for (; i < M && i < N; i++) {
2457 list.set(i, (T) readParcelable(cl));
2458 }
2459 for (; i<N; i++) {
2460 list.add((T) readParcelable(cl));
2461 }
2462 for (; i<M; i++) {
2463 list.remove(N);
2464 }
Eugene Susla36e866b2017-02-23 18:24:39 -08002465 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002466 }
2467
2468 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 * Read and return a new array containing a particular object type from
2470 * the parcel at the current dataPosition(). Returns null if the
2471 * previously written array was null. The array <em>must</em> have
2472 * previously been written via {@link #writeTypedArray} with the same
2473 * object type.
2474 *
2475 * @return A newly created array containing objects with the same data
2476 * as those that were previously written.
2477 *
2478 * @see #writeTypedArray
2479 */
2480 public final <T> T[] createTypedArray(Parcelable.Creator<T> c) {
2481 int N = readInt();
2482 if (N < 0) {
2483 return null;
2484 }
2485 T[] l = c.newArray(N);
2486 for (int i=0; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002487 l[i] = readTypedObject(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 }
2489 return l;
2490 }
2491
2492 public final <T> void readTypedArray(T[] val, Parcelable.Creator<T> c) {
2493 int N = readInt();
2494 if (N == val.length) {
2495 for (int i=0; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002496 val[i] = readTypedObject(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 }
2498 } else {
2499 throw new RuntimeException("bad array lengths");
2500 }
2501 }
2502
2503 /**
2504 * @deprecated
2505 * @hide
2506 */
2507 @Deprecated
2508 public final <T> T[] readTypedArray(Parcelable.Creator<T> c) {
2509 return createTypedArray(c);
2510 }
2511
2512 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002513 * Read and return a typed Parcelable object from a parcel.
2514 * Returns null if the previous written object was null.
2515 * The object <em>must</em> have previous been written via
2516 * {@link #writeTypedObject} with the same object type.
2517 *
2518 * @return A newly created object of the type that was previously
2519 * written.
2520 *
2521 * @see #writeTypedObject
2522 */
2523 public final <T> T readTypedObject(Parcelable.Creator<T> c) {
2524 if (readInt() != 0) {
2525 return c.createFromParcel(this);
2526 } else {
2527 return null;
2528 }
2529 }
2530
2531 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 * Write a heterogeneous array of Parcelable objects into the Parcel.
2533 * Each object in the array is written along with its class name, so
2534 * that the correct class can later be instantiated. As a result, this
2535 * has significantly more overhead than {@link #writeTypedArray}, but will
2536 * correctly handle an array containing more than one type of object.
2537 *
2538 * @param value The array of objects to be written.
2539 * @param parcelableFlags Contextual flags as per
2540 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
2541 *
2542 * @see #writeTypedArray
2543 */
2544 public final <T extends Parcelable> void writeParcelableArray(T[] value,
2545 int parcelableFlags) {
2546 if (value != null) {
2547 int N = value.length;
2548 writeInt(N);
2549 for (int i=0; i<N; i++) {
2550 writeParcelable(value[i], parcelableFlags);
2551 }
2552 } else {
2553 writeInt(-1);
2554 }
2555 }
2556
2557 /**
2558 * Read a typed object from a parcel. The given class loader will be
2559 * used to load any enclosed Parcelables. If it is null, the default class
2560 * loader will be used.
2561 */
2562 public final Object readValue(ClassLoader loader) {
2563 int type = readInt();
2564
2565 switch (type) {
2566 case VAL_NULL:
2567 return null;
2568
2569 case VAL_STRING:
2570 return readString();
2571
2572 case VAL_INTEGER:
2573 return readInt();
2574
2575 case VAL_MAP:
2576 return readHashMap(loader);
2577
2578 case VAL_PARCELABLE:
2579 return readParcelable(loader);
2580
2581 case VAL_SHORT:
2582 return (short) readInt();
2583
2584 case VAL_LONG:
2585 return readLong();
2586
2587 case VAL_FLOAT:
2588 return readFloat();
2589
2590 case VAL_DOUBLE:
2591 return readDouble();
2592
2593 case VAL_BOOLEAN:
2594 return readInt() == 1;
2595
2596 case VAL_CHARSEQUENCE:
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002597 return readCharSequence();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598
2599 case VAL_LIST:
2600 return readArrayList(loader);
2601
2602 case VAL_BOOLEANARRAY:
Samuel Tana8036662015-11-23 14:36:00 -08002603 return createBooleanArray();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604
2605 case VAL_BYTEARRAY:
2606 return createByteArray();
2607
2608 case VAL_STRINGARRAY:
2609 return readStringArray();
2610
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002611 case VAL_CHARSEQUENCEARRAY:
2612 return readCharSequenceArray();
2613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 case VAL_IBINDER:
2615 return readStrongBinder();
2616
2617 case VAL_OBJECTARRAY:
2618 return readArray(loader);
2619
2620 case VAL_INTARRAY:
2621 return createIntArray();
2622
2623 case VAL_LONGARRAY:
2624 return createLongArray();
2625
2626 case VAL_BYTE:
2627 return readByte();
2628
2629 case VAL_SERIALIZABLE:
John Spurlock5002b8c2014-01-10 13:32:12 -05002630 return readSerializable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631
2632 case VAL_PARCELABLEARRAY:
2633 return readParcelableArray(loader);
2634
2635 case VAL_SPARSEARRAY:
2636 return readSparseArray(loader);
2637
2638 case VAL_SPARSEBOOLEANARRAY:
2639 return readSparseBooleanArray();
2640
2641 case VAL_BUNDLE:
2642 return readBundle(loader); // loading will be deferred
2643
Craig Mautner719e6b12014-04-04 20:29:41 -07002644 case VAL_PERSISTABLEBUNDLE:
2645 return readPersistableBundle(loader);
2646
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002647 case VAL_SIZE:
2648 return readSize();
2649
2650 case VAL_SIZEF:
2651 return readSizeF();
2652
Samuel Tana8036662015-11-23 14:36:00 -08002653 case VAL_DOUBLEARRAY:
2654 return createDoubleArray();
2655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 default:
2657 int off = dataPosition() - 4;
2658 throw new RuntimeException(
2659 "Parcel " + this + ": Unmarshalling unknown type code " + type + " at offset " + off);
2660 }
2661 }
2662
2663 /**
2664 * Read and return a new Parcelable from the parcel. The given class loader
2665 * will be used to load any enclosed Parcelables. If it is null, the default
2666 * class loader will be used.
2667 * @param loader A ClassLoader from which to instantiate the Parcelable
2668 * object, or null for the default class loader.
2669 * @return Returns the newly created Parcelable, or null if a null
2670 * object has been written.
2671 * @throws BadParcelableException Throws BadParcelableException if there
2672 * was an error trying to instantiate the Parcelable.
2673 */
Neil Fuller44e440c2015-04-20 14:39:00 +01002674 @SuppressWarnings("unchecked")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 public final <T extends Parcelable> T readParcelable(ClassLoader loader) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002676 Parcelable.Creator<?> creator = readParcelableCreator(loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002677 if (creator == null) {
2678 return null;
2679 }
2680 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002681 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2682 (Parcelable.ClassLoaderCreator<?>) creator;
2683 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002684 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002685 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002686 }
2687
2688 /** @hide */
Neil Fuller44e440c2015-04-20 14:39:00 +01002689 @SuppressWarnings("unchecked")
2690 public final <T extends Parcelable> T readCreator(Parcelable.Creator<?> creator,
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002691 ClassLoader loader) {
2692 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002693 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2694 (Parcelable.ClassLoaderCreator<?>) creator;
2695 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002696 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002697 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002698 }
2699
2700 /** @hide */
Neil Fuller44e440c2015-04-20 14:39:00 +01002701 public final Parcelable.Creator<?> readParcelableCreator(ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 String name = readString();
2703 if (name == null) {
2704 return null;
2705 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002706 Parcelable.Creator<?> creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 synchronized (mCreators) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002708 HashMap<String,Parcelable.Creator<?>> map = mCreators.get(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 if (map == null) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002710 map = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 mCreators.put(loader, map);
2712 }
2713 creator = map.get(name);
2714 if (creator == null) {
2715 try {
Neil Fuller44e440c2015-04-20 14:39:00 +01002716 // If loader == null, explicitly emulate Class.forName(String) "caller
2717 // classloader" behavior.
2718 ClassLoader parcelableClassLoader =
2719 (loader == null ? getClass().getClassLoader() : loader);
2720 // Avoid initializing the Parcelable class until we know it implements
2721 // Parcelable and has the necessary CREATOR field. http://b/1171613.
2722 Class<?> parcelableClass = Class.forName(name, false /* initialize */,
2723 parcelableClassLoader);
2724 if (!Parcelable.class.isAssignableFrom(parcelableClass)) {
2725 throw new BadParcelableException("Parcelable protocol requires that the "
2726 + "class implements Parcelable");
2727 }
2728 Field f = parcelableClass.getField("CREATOR");
2729 if ((f.getModifiers() & Modifier.STATIC) == 0) {
2730 throw new BadParcelableException("Parcelable protocol requires "
2731 + "the CREATOR object to be static on class " + name);
2732 }
2733 Class<?> creatorType = f.getType();
2734 if (!Parcelable.Creator.class.isAssignableFrom(creatorType)) {
2735 // Fail before calling Field.get(), not after, to avoid initializing
2736 // parcelableClass unnecessarily.
2737 throw new BadParcelableException("Parcelable protocol requires a "
2738 + "Parcelable.Creator object called "
2739 + "CREATOR on class " + name);
2740 }
2741 creator = (Parcelable.Creator<?>) f.get(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 }
2743 catch (IllegalAccessException e) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002744 Log.e(TAG, "Illegal access when unmarshalling: " + name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 throw new BadParcelableException(
2746 "IllegalAccessException when unmarshalling: " + name);
2747 }
2748 catch (ClassNotFoundException e) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002749 Log.e(TAG, "Class not found when unmarshalling: " + name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 throw new BadParcelableException(
2751 "ClassNotFoundException when unmarshalling: " + name);
2752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 catch (NoSuchFieldException e) {
2754 throw new BadParcelableException("Parcelable protocol requires a "
Neil Fuller44e440c2015-04-20 14:39:00 +01002755 + "Parcelable.Creator object called "
2756 + "CREATOR on class " + name);
Irfan Sheriff97a72f62013-01-11 10:45:51 -08002757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 if (creator == null) {
2759 throw new BadParcelableException("Parcelable protocol requires a "
Neil Fuller44e440c2015-04-20 14:39:00 +01002760 + "non-null Parcelable.Creator object called "
2761 + "CREATOR on class " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 }
2763
2764 map.put(name, creator);
2765 }
2766 }
2767
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002768 return creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 }
2770
2771 /**
2772 * Read and return a new Parcelable array from the parcel.
2773 * The given class loader will be used to load any enclosed
2774 * Parcelables.
2775 * @return the Parcelable array, or null if the array is null
2776 */
2777 public final Parcelable[] readParcelableArray(ClassLoader loader) {
2778 int N = readInt();
2779 if (N < 0) {
2780 return null;
2781 }
2782 Parcelable[] p = new Parcelable[N];
2783 for (int i = 0; i < N; i++) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002784 p[i] = readParcelable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 }
2786 return p;
2787 }
2788
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002789 /** @hide */
2790 public final <T extends Parcelable> T[] readParcelableArray(ClassLoader loader,
2791 Class<T> clazz) {
2792 int N = readInt();
2793 if (N < 0) {
2794 return null;
2795 }
2796 T[] p = (T[]) Array.newInstance(clazz, N);
2797 for (int i = 0; i < N; i++) {
2798 p[i] = readParcelable(loader);
2799 }
2800 return p;
2801 }
2802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 /**
2804 * Read and return a new Serializable object from the parcel.
2805 * @return the Serializable object, or null if the Serializable name
2806 * wasn't found in the parcel.
2807 */
2808 public final Serializable readSerializable() {
John Spurlock5002b8c2014-01-10 13:32:12 -05002809 return readSerializable(null);
2810 }
2811
2812 private final Serializable readSerializable(final ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 String name = readString();
2814 if (name == null) {
2815 // For some reason we were unable to read the name of the Serializable (either there
2816 // is nothing left in the Parcel to read, or the next value wasn't a String), so
2817 // return null, which indicates that the name wasn't found in the parcel.
2818 return null;
2819 }
2820
2821 byte[] serializedData = createByteArray();
2822 ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
2823 try {
John Spurlock5002b8c2014-01-10 13:32:12 -05002824 ObjectInputStream ois = new ObjectInputStream(bais) {
2825 @Override
2826 protected Class<?> resolveClass(ObjectStreamClass osClass)
2827 throws IOException, ClassNotFoundException {
2828 // try the custom classloader if provided
2829 if (loader != null) {
2830 Class<?> c = Class.forName(osClass.getName(), false, loader);
2831 if (c != null) {
2832 return c;
2833 }
2834 }
2835 return super.resolveClass(osClass);
2836 }
2837 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 return (Serializable) ois.readObject();
2839 } catch (IOException ioe) {
2840 throw new RuntimeException("Parcelable encountered " +
2841 "IOException reading a Serializable object (name = " + name +
2842 ")", ioe);
2843 } catch (ClassNotFoundException cnfe) {
John Spurlock5002b8c2014-01-10 13:32:12 -05002844 throw new RuntimeException("Parcelable encountered " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 "ClassNotFoundException reading a Serializable object (name = "
2846 + name + ")", cnfe);
2847 }
2848 }
2849
2850 // Cache of previously looked up CREATOR.createFromParcel() methods for
2851 // particular classes. Keys are the names of the classes, values are
2852 // Method objects.
Neil Fuller44e440c2015-04-20 14:39:00 +01002853 private static final HashMap<ClassLoader,HashMap<String,Parcelable.Creator<?>>>
2854 mCreators = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855
Narayan Kamathb34a4612014-01-23 14:17:11 +00002856 /** @hide for internal use only. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 static protected final Parcel obtain(int obj) {
Ashok Bhat8ab665d2014-01-22 16:00:20 +00002858 throw new UnsupportedOperationException();
2859 }
2860
2861 /** @hide */
2862 static protected final Parcel obtain(long obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 final Parcel[] pool = sHolderPool;
2864 synchronized (pool) {
2865 Parcel p;
2866 for (int i=0; i<POOL_SIZE; i++) {
2867 p = pool[i];
2868 if (p != null) {
2869 pool[i] = null;
2870 if (DEBUG_RECYCLE) {
2871 p.mStack = new RuntimeException();
2872 }
2873 p.init(obj);
2874 return p;
2875 }
2876 }
2877 }
2878 return new Parcel(obj);
2879 }
2880
Ashok Bhat8ab665d2014-01-22 16:00:20 +00002881 private Parcel(long nativePtr) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 if (DEBUG_RECYCLE) {
2883 mStack = new RuntimeException();
2884 }
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002885 //Log.i(TAG, "Initializing obj=0x" + Integer.toHexString(obj), mStack);
Jeff Sharkey047238c2012-03-07 16:51:38 -08002886 init(nativePtr);
2887 }
2888
Ashok Bhat8ab665d2014-01-22 16:00:20 +00002889 private void init(long nativePtr) {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002890 if (nativePtr != 0) {
2891 mNativePtr = nativePtr;
2892 mOwnsNativeParcelObject = false;
2893 } else {
2894 mNativePtr = nativeCreate();
2895 mOwnsNativeParcelObject = true;
2896 }
2897 }
2898
2899 private void freeBuffer() {
2900 if (mOwnsNativeParcelObject) {
Adrian Roos04505652015-10-22 16:12:01 -07002901 updateNativeSize(nativeFreeBuffer(mNativePtr));
Jeff Sharkey047238c2012-03-07 16:51:38 -08002902 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -07002903 mReadWriteHelper = ReadWriteHelper.DEFAULT;
Jeff Sharkey047238c2012-03-07 16:51:38 -08002904 }
2905
2906 private void destroy() {
2907 if (mNativePtr != 0) {
2908 if (mOwnsNativeParcelObject) {
2909 nativeDestroy(mNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -07002910 updateNativeSize(0);
Jeff Sharkey047238c2012-03-07 16:51:38 -08002911 }
2912 mNativePtr = 0;
2913 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -07002914 mReadWriteHelper = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 }
2916
2917 @Override
2918 protected void finalize() throws Throwable {
2919 if (DEBUG_RECYCLE) {
2920 if (mStack != null) {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002921 Log.w(TAG, "Client did not call Parcel.recycle()", mStack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 }
2923 }
2924 destroy();
2925 }
2926
Dianne Hackborn6aff9052009-05-22 13:20:23 -07002927 /* package */ void readMapInternal(Map outVal, int N,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 ClassLoader loader) {
2929 while (N > 0) {
2930 Object key = readValue(loader);
2931 Object value = readValue(loader);
2932 outVal.put(key, value);
2933 N--;
2934 }
2935 }
2936
Dianne Hackbornb87655b2013-07-17 19:06:22 -07002937 /* package */ void readArrayMapInternal(ArrayMap outVal, int N,
2938 ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07002939 if (DEBUG_ARRAY_MAP) {
2940 RuntimeException here = new RuntimeException("here");
2941 here.fillInStackTrace();
2942 Log.d(TAG, "Reading " + N + " ArrayMap entries", here);
2943 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07002944 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -07002945 while (N > 0) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07002946 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07002947 String key = readString();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07002948 Object value = readValue(loader);
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07002949 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " "
2950 + (dataPosition()-startPos) + " bytes: key=0x"
2951 + Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key);
Dianne Hackbornb87655b2013-07-17 19:06:22 -07002952 outVal.append(key, value);
2953 N--;
2954 }
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07002955 outVal.validate();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07002956 }
2957
Dianne Hackborne784d1e2013-09-20 18:13:52 -07002958 /* package */ void readArrayMapSafelyInternal(ArrayMap outVal, int N,
2959 ClassLoader loader) {
2960 if (DEBUG_ARRAY_MAP) {
2961 RuntimeException here = new RuntimeException("here");
2962 here.fillInStackTrace();
2963 Log.d(TAG, "Reading safely " + N + " ArrayMap entries", here);
2964 }
2965 while (N > 0) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07002966 String key = readString();
Dianne Hackborne784d1e2013-09-20 18:13:52 -07002967 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read safe #" + (N-1) + ": key=0x"
2968 + (key != null ? key.hashCode() : 0) + " " + key);
2969 Object value = readValue(loader);
2970 outVal.put(key, value);
2971 N--;
2972 }
2973 }
2974
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07002975 /**
2976 * @hide For testing only.
2977 */
2978 public void readArrayMap(ArrayMap outVal, ClassLoader loader) {
2979 final int N = readInt();
2980 if (N < 0) {
2981 return;
2982 }
2983 readArrayMapInternal(outVal, N, loader);
2984 }
2985
Svet Ganovddb94882016-06-23 19:55:24 -07002986 /**
2987 * Reads an array set.
2988 *
2989 * @param loader The class loader to use.
2990 *
2991 * @hide
2992 */
2993 public @Nullable ArraySet<? extends Object> readArraySet(ClassLoader loader) {
2994 final int size = readInt();
2995 if (size < 0) {
2996 return null;
2997 }
2998 ArraySet<Object> result = new ArraySet<>(size);
2999 for (int i = 0; i < size; i++) {
3000 Object value = readValue(loader);
3001 result.append(value);
3002 }
3003 return result;
3004 }
3005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006 private void readListInternal(List outVal, int N,
3007 ClassLoader loader) {
3008 while (N > 0) {
3009 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003010 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 outVal.add(value);
3012 N--;
3013 }
3014 }
3015
3016 private void readArrayInternal(Object[] outVal, int N,
3017 ClassLoader loader) {
3018 for (int i = 0; i < N; i++) {
3019 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003020 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 outVal[i] = value;
3022 }
3023 }
3024
3025 private void readSparseArrayInternal(SparseArray outVal, int N,
3026 ClassLoader loader) {
3027 while (N > 0) {
3028 int key = readInt();
3029 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003030 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003031 outVal.append(key, value);
3032 N--;
3033 }
3034 }
3035
3036
3037 private void readSparseBooleanArrayInternal(SparseBooleanArray outVal, int N) {
3038 while (N > 0) {
3039 int key = readInt();
3040 boolean value = this.readByte() == 1;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003041 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 outVal.append(key, value);
3043 N--;
3044 }
3045 }
Dan Sandler5ce04302015-04-09 23:50:15 -04003046
Adam Lesinski4e862812016-11-21 16:02:24 -08003047 private void readSparseIntArrayInternal(SparseIntArray outVal, int N) {
3048 while (N > 0) {
3049 int key = readInt();
3050 int value = readInt();
3051 outVal.append(key, value);
3052 N--;
3053 }
3054 }
3055
Dan Sandler5ce04302015-04-09 23:50:15 -04003056 /**
3057 * @hide For testing
3058 */
3059 public long getBlobAshmemSize() {
3060 return nativeGetBlobAshmemSize(mNativePtr);
3061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062}