blob: 8847414d531d758b93040637915c1ae4f0d6f428 [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
Pete Gillin60f55a252018-05-10 15:40:32 +010034import libcore.util.ArrayUtils;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -070035import libcore.util.SneakyThrow;
36
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import java.io.ByteArrayInputStream;
38import java.io.ByteArrayOutputStream;
39import java.io.FileDescriptor;
40import java.io.FileNotFoundException;
41import java.io.IOException;
42import java.io.ObjectInputStream;
43import java.io.ObjectOutputStream;
John Spurlock5002b8c2014-01-10 13:32:12 -050044import java.io.ObjectStreamClass;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import java.io.Serializable;
Makoto Onuki440a1ea2016-07-20 14:21:18 -070046import java.lang.reflect.Array;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import java.lang.reflect.Field;
Neil Fuller44e440c2015-04-20 14:39:00 +010048import java.lang.reflect.Modifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import java.util.ArrayList;
50import 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 }
Pete Gillin60f55a252018-05-10 15:40:32 +0100605 ArrayUtils.throwsIfOutOfBounds(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 }
Pete Gillin60f55a252018-05-10 15:40:32 +0100634 ArrayUtils.throwsIfOutOfBounds(b.length, offset, len);
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500635 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) {
1343 if (val == null) {
1344 writeInt(-1);
1345 return;
1346 }
1347 int N = val.size();
1348 int i=0;
1349 writeInt(N);
1350 while (i < N) {
1351 T item = val.get(i);
1352 if (item != null) {
1353 writeInt(1);
1354 item.writeToParcel(this, 0);
1355 } else {
1356 writeInt(0);
1357 }
1358 i++;
1359 }
1360 }
1361
1362 /**
1363 * Flatten a List containing String objects into the parcel, at
1364 * the current dataPosition() and growing dataCapacity() if needed. They
1365 * can later be retrieved with {@link #createStringArrayList} or
1366 * {@link #readStringList}.
1367 *
1368 * @param val The list of strings to be written.
1369 *
1370 * @see #createStringArrayList
1371 * @see #readStringList
1372 */
1373 public final void writeStringList(List<String> val) {
1374 if (val == null) {
1375 writeInt(-1);
1376 return;
1377 }
1378 int N = val.size();
1379 int i=0;
1380 writeInt(N);
1381 while (i < N) {
1382 writeString(val.get(i));
1383 i++;
1384 }
1385 }
1386
1387 /**
1388 * Flatten a List containing IBinder objects into the parcel, at
1389 * the current dataPosition() and growing dataCapacity() if needed. They
1390 * can later be retrieved with {@link #createBinderArrayList} or
1391 * {@link #readBinderList}.
1392 *
1393 * @param val The list of strings to be written.
1394 *
1395 * @see #createBinderArrayList
1396 * @see #readBinderList
1397 */
1398 public final void writeBinderList(List<IBinder> val) {
1399 if (val == null) {
1400 writeInt(-1);
1401 return;
1402 }
1403 int N = val.size();
1404 int i=0;
1405 writeInt(N);
1406 while (i < N) {
1407 writeStrongBinder(val.get(i));
1408 i++;
1409 }
1410 }
1411
1412 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00001413 * Flatten a {@code List} containing arbitrary {@code Parcelable} objects into this parcel
1414 * at the current position. They can later be retrieved using
1415 * {@link #readParcelableList(List, ClassLoader)} if required.
1416 *
1417 * @see #readParcelableList(List, ClassLoader)
1418 * @hide
1419 */
1420 public final <T extends Parcelable> void writeParcelableList(List<T> val, int flags) {
1421 if (val == null) {
1422 writeInt(-1);
1423 return;
1424 }
1425
1426 int N = val.size();
1427 int i=0;
1428 writeInt(N);
1429 while (i < N) {
1430 writeParcelable(val.get(i), flags);
1431 i++;
1432 }
1433 }
1434
1435 /**
Svet Ganov0f4928f2017-02-02 20:02:51 -08001436 * Flatten a homogeneous array containing a particular object type into
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 * the parcel, at
1438 * the current dataPosition() and growing dataCapacity() if needed. The
1439 * type of the objects in the array must be one that implements Parcelable.
1440 * Unlike the {@link #writeParcelableArray} method, however, only the
1441 * raw data of the objects is written and not their type, so you must use
1442 * {@link #readTypedArray} with the correct corresponding
1443 * {@link Parcelable.Creator} implementation to unmarshall them.
1444 *
1445 * @param val The array of objects to be written.
1446 * @param parcelableFlags Contextual flags as per
1447 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1448 *
1449 * @see #readTypedArray
1450 * @see #writeParcelableArray
1451 * @see Parcelable.Creator
1452 */
1453 public final <T extends Parcelable> void writeTypedArray(T[] val,
1454 int parcelableFlags) {
1455 if (val != null) {
1456 int N = val.length;
1457 writeInt(N);
Svet Ganov0f4928f2017-02-02 20:02:51 -08001458 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 T item = val[i];
1460 if (item != null) {
1461 writeInt(1);
1462 item.writeToParcel(this, parcelableFlags);
1463 } else {
1464 writeInt(0);
1465 }
1466 }
1467 } else {
1468 writeInt(-1);
1469 }
1470 }
1471
1472 /**
Svet Ganov0f4928f2017-02-02 20:02:51 -08001473 * Write a uniform (all items are null or the same class) array list of
1474 * parcelables.
1475 *
1476 * @param list The list to write.
1477 *
1478 * @hide
1479 */
1480 public final <T extends Parcelable> void writeTypedArrayList(@Nullable ArrayList<T> list,
1481 int parcelableFlags) {
1482 if (list != null) {
1483 int N = list.size();
1484 writeInt(N);
1485 boolean wroteCreator = false;
1486 for (int i = 0; i < N; i++) {
1487 T item = list.get(i);
1488 if (item != null) {
1489 writeInt(1);
1490 if (!wroteCreator) {
1491 writeParcelableCreator(item);
1492 wroteCreator = true;
1493 }
1494 item.writeToParcel(this, parcelableFlags);
1495 } else {
1496 writeInt(0);
1497 }
1498 }
1499 } else {
1500 writeInt(-1);
1501 }
1502 }
1503
1504 /**
1505 * Reads a uniform (all items are null or the same class) array list of
1506 * parcelables.
1507 *
1508 * @return The list or null.
1509 *
1510 * @hide
1511 */
1512 public final @Nullable <T> ArrayList<T> readTypedArrayList(@Nullable ClassLoader loader) {
1513 int N = readInt();
1514 if (N <= 0) {
1515 return null;
1516 }
1517 Parcelable.Creator<?> creator = null;
1518 ArrayList<T> result = new ArrayList<T>(N);
1519 for (int i = 0; i < N; i++) {
1520 if (readInt() != 0) {
1521 if (creator == null) {
1522 creator = readParcelableCreator(loader);
1523 if (creator == null) {
1524 return null;
1525 }
1526 }
1527 final T parcelable;
1528 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
1529 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
1530 (Parcelable.ClassLoaderCreator<?>) creator;
1531 parcelable = (T) classLoaderCreator.createFromParcel(this, loader);
1532 } else {
1533 parcelable = (T) creator.createFromParcel(this);
1534 }
1535 result.add(parcelable);
1536 } else {
1537 result.add(null);
1538 }
1539 }
1540 return result;
1541 }
1542
1543 /**
1544 * Write a uniform (all items are null or the same class) array set of
1545 * parcelables.
1546 *
1547 * @param set The set to write.
1548 *
1549 * @hide
1550 */
1551 public final <T extends Parcelable> void writeTypedArraySet(@Nullable ArraySet<T> set,
1552 int parcelableFlags) {
1553 if (set != null) {
1554 int N = set.size();
1555 writeInt(N);
1556 boolean wroteCreator = false;
1557 for (int i = 0; i < N; i++) {
1558 T item = set.valueAt(i);
1559 if (item != null) {
1560 writeInt(1);
1561 if (!wroteCreator) {
1562 writeParcelableCreator(item);
1563 wroteCreator = true;
1564 }
1565 item.writeToParcel(this, parcelableFlags);
1566 } else {
1567 writeInt(0);
1568 }
1569 }
1570 } else {
1571 writeInt(-1);
1572 }
1573 }
1574
1575 /**
1576 * Reads a uniform (all items are null or the same class) array set of
1577 * parcelables.
1578 *
1579 * @return The set or null.
1580 *
1581 * @hide
1582 */
1583 public final @Nullable <T> ArraySet<T> readTypedArraySet(@Nullable ClassLoader loader) {
1584 int N = readInt();
1585 if (N <= 0) {
1586 return null;
1587 }
1588 Parcelable.Creator<?> creator = null;
1589 ArraySet<T> result = new ArraySet<T>(N);
1590 for (int i = 0; i < N; i++) {
1591 T parcelable = null;
1592 if (readInt() != 0) {
1593 if (creator == null) {
1594 creator = readParcelableCreator(loader);
1595 if (creator == null) {
1596 return null;
1597 }
1598 }
1599 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
1600 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
1601 (Parcelable.ClassLoaderCreator<?>) creator;
1602 parcelable = (T) classLoaderCreator.createFromParcel(this, loader);
1603 } else {
1604 parcelable = (T) creator.createFromParcel(this);
1605 }
1606 }
1607 result.append(parcelable);
1608 }
1609 return result;
1610 }
1611
1612 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001613 * Flatten the Parcelable object into the parcel.
1614 *
1615 * @param val The Parcelable object to be written.
1616 * @param parcelableFlags Contextual flags as per
1617 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1618 *
1619 * @see #readTypedObject
1620 */
1621 public final <T extends Parcelable> void writeTypedObject(T val, int parcelableFlags) {
1622 if (val != null) {
1623 writeInt(1);
1624 val.writeToParcel(this, parcelableFlags);
1625 } else {
1626 writeInt(0);
1627 }
1628 }
1629
1630 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 * Flatten a generic object in to a parcel. The given Object value may
1632 * currently be one of the following types:
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001633 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 * <ul>
1635 * <li> null
1636 * <li> String
1637 * <li> Byte
1638 * <li> Short
1639 * <li> Integer
1640 * <li> Long
1641 * <li> Float
1642 * <li> Double
1643 * <li> Boolean
1644 * <li> String[]
1645 * <li> boolean[]
1646 * <li> byte[]
1647 * <li> int[]
1648 * <li> long[]
1649 * <li> Object[] (supporting objects of the same type defined here).
1650 * <li> {@link Bundle}
1651 * <li> Map (as supported by {@link #writeMap}).
1652 * <li> Any object that implements the {@link Parcelable} protocol.
1653 * <li> Parcelable[]
1654 * <li> CharSequence (as supported by {@link TextUtils#writeToParcel}).
1655 * <li> List (as supported by {@link #writeList}).
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001656 * <li> {@link SparseArray} (as supported by {@link #writeSparseArray(SparseArray)}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 * <li> {@link IBinder}
1658 * <li> Any object that implements Serializable (but see
1659 * {@link #writeSerializable} for caveats). Note that all of the
1660 * previous types have relatively efficient implementations for
1661 * writing to a Parcel; having to rely on the generic serialization
1662 * approach is much less efficient and should be avoided whenever
1663 * possible.
1664 * </ul>
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001665 *
1666 * <p class="caution">{@link Parcelable} objects are written with
1667 * {@link Parcelable#writeToParcel} using contextual flags of 0. When
1668 * serializing objects containing {@link ParcelFileDescriptor}s,
1669 * this may result in file descriptor leaks when they are returned from
1670 * Binder calls (where {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE}
1671 * should be used).</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 */
1673 public final void writeValue(Object v) {
1674 if (v == null) {
1675 writeInt(VAL_NULL);
1676 } else if (v instanceof String) {
1677 writeInt(VAL_STRING);
1678 writeString((String) v);
1679 } else if (v instanceof Integer) {
1680 writeInt(VAL_INTEGER);
1681 writeInt((Integer) v);
1682 } else if (v instanceof Map) {
1683 writeInt(VAL_MAP);
1684 writeMap((Map) v);
1685 } else if (v instanceof Bundle) {
1686 // Must be before Parcelable
1687 writeInt(VAL_BUNDLE);
1688 writeBundle((Bundle) v);
Samuel Tanceafe5e2015-12-11 16:50:58 -08001689 } else if (v instanceof PersistableBundle) {
1690 writeInt(VAL_PERSISTABLEBUNDLE);
1691 writePersistableBundle((PersistableBundle) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 } else if (v instanceof Parcelable) {
Samuel Tanceafe5e2015-12-11 16:50:58 -08001693 // IMPOTANT: cases for classes that implement Parcelable must
1694 // come before the Parcelable case, so that their specific VAL_*
1695 // types will be written.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 writeInt(VAL_PARCELABLE);
1697 writeParcelable((Parcelable) v, 0);
1698 } else if (v instanceof Short) {
1699 writeInt(VAL_SHORT);
1700 writeInt(((Short) v).intValue());
1701 } else if (v instanceof Long) {
1702 writeInt(VAL_LONG);
1703 writeLong((Long) v);
1704 } else if (v instanceof Float) {
1705 writeInt(VAL_FLOAT);
1706 writeFloat((Float) v);
1707 } else if (v instanceof Double) {
1708 writeInt(VAL_DOUBLE);
1709 writeDouble((Double) v);
1710 } else if (v instanceof Boolean) {
1711 writeInt(VAL_BOOLEAN);
1712 writeInt((Boolean) v ? 1 : 0);
1713 } else if (v instanceof CharSequence) {
1714 // Must be after String
1715 writeInt(VAL_CHARSEQUENCE);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001716 writeCharSequence((CharSequence) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 } else if (v instanceof List) {
1718 writeInt(VAL_LIST);
1719 writeList((List) v);
1720 } else if (v instanceof SparseArray) {
1721 writeInt(VAL_SPARSEARRAY);
1722 writeSparseArray((SparseArray) v);
1723 } else if (v instanceof boolean[]) {
1724 writeInt(VAL_BOOLEANARRAY);
1725 writeBooleanArray((boolean[]) v);
1726 } else if (v instanceof byte[]) {
1727 writeInt(VAL_BYTEARRAY);
1728 writeByteArray((byte[]) v);
1729 } else if (v instanceof String[]) {
1730 writeInt(VAL_STRINGARRAY);
1731 writeStringArray((String[]) v);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001732 } else if (v instanceof CharSequence[]) {
1733 // Must be after String[] and before Object[]
1734 writeInt(VAL_CHARSEQUENCEARRAY);
1735 writeCharSequenceArray((CharSequence[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 } else if (v instanceof IBinder) {
1737 writeInt(VAL_IBINDER);
1738 writeStrongBinder((IBinder) v);
1739 } else if (v instanceof Parcelable[]) {
1740 writeInt(VAL_PARCELABLEARRAY);
1741 writeParcelableArray((Parcelable[]) v, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 } else if (v instanceof int[]) {
1743 writeInt(VAL_INTARRAY);
1744 writeIntArray((int[]) v);
1745 } else if (v instanceof long[]) {
1746 writeInt(VAL_LONGARRAY);
1747 writeLongArray((long[]) v);
1748 } else if (v instanceof Byte) {
1749 writeInt(VAL_BYTE);
1750 writeInt((Byte) v);
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001751 } else if (v instanceof Size) {
1752 writeInt(VAL_SIZE);
1753 writeSize((Size) v);
1754 } else if (v instanceof SizeF) {
1755 writeInt(VAL_SIZEF);
1756 writeSizeF((SizeF) v);
Samuel Tana8036662015-11-23 14:36:00 -08001757 } else if (v instanceof double[]) {
1758 writeInt(VAL_DOUBLEARRAY);
1759 writeDoubleArray((double[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 } else {
Paul Duffinac5a0822014-02-03 15:02:17 +00001761 Class<?> clazz = v.getClass();
1762 if (clazz.isArray() && clazz.getComponentType() == Object.class) {
1763 // Only pure Object[] are written here, Other arrays of non-primitive types are
1764 // handled by serialization as this does not record the component type.
1765 writeInt(VAL_OBJECTARRAY);
1766 writeArray((Object[]) v);
1767 } else if (v instanceof Serializable) {
1768 // Must be last
1769 writeInt(VAL_SERIALIZABLE);
1770 writeSerializable((Serializable) v);
1771 } else {
1772 throw new RuntimeException("Parcel: unable to marshal value " + v);
1773 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 }
1775 }
1776
1777 /**
1778 * Flatten the name of the class of the Parcelable and its contents
1779 * into the parcel.
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001780 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 * @param p The Parcelable object to be written.
1782 * @param parcelableFlags Contextual flags as per
1783 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1784 */
1785 public final void writeParcelable(Parcelable p, int parcelableFlags) {
1786 if (p == null) {
1787 writeString(null);
1788 return;
1789 }
Neil Fuller44e440c2015-04-20 14:39:00 +01001790 writeParcelableCreator(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 p.writeToParcel(this, parcelableFlags);
1792 }
1793
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001794 /** @hide */
1795 public final void writeParcelableCreator(Parcelable p) {
1796 String name = p.getClass().getName();
1797 writeString(name);
1798 }
1799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 /**
1801 * Write a generic serializable object in to a Parcel. It is strongly
1802 * recommended that this method be avoided, since the serialization
1803 * overhead is extremely large, and this approach will be much slower than
1804 * using the other approaches to writing data in to a Parcel.
1805 */
1806 public final void writeSerializable(Serializable s) {
1807 if (s == null) {
1808 writeString(null);
1809 return;
1810 }
1811 String name = s.getClass().getName();
1812 writeString(name);
1813
1814 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1815 try {
1816 ObjectOutputStream oos = new ObjectOutputStream(baos);
1817 oos.writeObject(s);
1818 oos.close();
1819
1820 writeByteArray(baos.toByteArray());
1821 } catch (IOException ioe) {
1822 throw new RuntimeException("Parcelable encountered " +
1823 "IOException writing serializable object (name = " + name +
1824 ")", ioe);
1825 }
1826 }
1827
1828 /**
1829 * Special function for writing an exception result at the header of
1830 * a parcel, to be used when returning an exception from a transaction.
1831 * Note that this currently only supports a few exception types; any other
1832 * exception will be re-thrown by this function as a RuntimeException
1833 * (to be caught by the system's last-resort exception handling when
1834 * dispatching a transaction).
Samuel Tana8036662015-11-23 14:36:00 -08001835 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 * <p>The supported exception types are:
1837 * <ul>
1838 * <li>{@link BadParcelableException}
1839 * <li>{@link IllegalArgumentException}
1840 * <li>{@link IllegalStateException}
1841 * <li>{@link NullPointerException}
1842 * <li>{@link SecurityException}
Dianne Hackborn18482ae2017-08-01 17:41:00 -07001843 * <li>{@link UnsupportedOperationException}
Dianne Hackborn7e714422013-09-13 17:32:57 -07001844 * <li>{@link NetworkOnMainThreadException}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 * </ul>
Samuel Tana8036662015-11-23 14:36:00 -08001846 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 * @param e The Exception to be written.
1848 *
1849 * @see #writeNoException
1850 * @see #readException
1851 */
1852 public final void writeException(Exception e) {
1853 int code = 0;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001854 if (e instanceof Parcelable
1855 && (e.getClass().getClassLoader() == Parcelable.class.getClassLoader())) {
1856 // We only send Parcelable exceptions that are in the
1857 // BootClassLoader to ensure that the receiver can unpack them
1858 code = EX_PARCELABLE;
1859 } else if (e instanceof SecurityException) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 code = EX_SECURITY;
1861 } else if (e instanceof BadParcelableException) {
1862 code = EX_BAD_PARCELABLE;
1863 } else if (e instanceof IllegalArgumentException) {
1864 code = EX_ILLEGAL_ARGUMENT;
1865 } else if (e instanceof NullPointerException) {
1866 code = EX_NULL_POINTER;
1867 } else if (e instanceof IllegalStateException) {
1868 code = EX_ILLEGAL_STATE;
Dianne Hackborn7e714422013-09-13 17:32:57 -07001869 } else if (e instanceof NetworkOnMainThreadException) {
1870 code = EX_NETWORK_MAIN_THREAD;
Dianne Hackborn33d738a2014-09-12 14:23:58 -07001871 } else if (e instanceof UnsupportedOperationException) {
1872 code = EX_UNSUPPORTED_OPERATION;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001873 } else if (e instanceof ServiceSpecificException) {
1874 code = EX_SERVICE_SPECIFIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 }
1876 writeInt(code);
Brad Fitzpatrick703e5d32010-07-15 13:16:41 -07001877 StrictMode.clearGatheredViolations();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 if (code == 0) {
1879 if (e instanceof RuntimeException) {
1880 throw (RuntimeException) e;
1881 }
1882 throw new RuntimeException(e);
1883 }
1884 writeString(e.getMessage());
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001885 switch (code) {
1886 case EX_SERVICE_SPECIFIC:
1887 writeInt(((ServiceSpecificException) e).errorCode);
1888 break;
1889 case EX_PARCELABLE:
1890 // Write parceled exception prefixed by length
1891 final int sizePosition = dataPosition();
1892 writeInt(0);
1893 writeParcelable((Parcelable) e, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
1894 final int payloadPosition = dataPosition();
1895 setDataPosition(sizePosition);
1896 writeInt(payloadPosition - sizePosition);
1897 setDataPosition(payloadPosition);
1898 break;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 }
1901
1902 /**
1903 * Special function for writing information at the front of the Parcel
1904 * indicating that no exception occurred.
1905 *
1906 * @see #writeException
1907 * @see #readException
1908 */
1909 public final void writeNoException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001910 // Despite the name of this function ("write no exception"),
1911 // it should instead be thought of as "write the RPC response
1912 // header", but because this function name is written out by
1913 // the AIDL compiler, we're not going to rename it.
1914 //
1915 // The response header, in the non-exception case (see also
1916 // writeException above, also called by the AIDL compiler), is
1917 // either a 0 (the default case), or EX_HAS_REPLY_HEADER if
1918 // StrictMode has gathered up violations that have occurred
1919 // during a Binder call, in which case we write out the number
1920 // of violations and their details, serialized, before the
1921 // actual RPC respons data. The receiving end of this is
1922 // readException(), below.
1923 if (StrictMode.hasGatheredViolations()) {
1924 writeInt(EX_HAS_REPLY_HEADER);
1925 final int sizePosition = dataPosition();
1926 writeInt(0); // total size of fat header, to be filled in later
1927 StrictMode.writeGatheredViolationsToParcel(this);
1928 final int payloadPosition = dataPosition();
1929 setDataPosition(sizePosition);
1930 writeInt(payloadPosition - sizePosition); // header size
1931 setDataPosition(payloadPosition);
1932 } else {
1933 writeInt(0);
1934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 }
1936
1937 /**
1938 * Special function for reading an exception result from the header of
1939 * a parcel, to be used after receiving the result of a transaction. This
1940 * will throw the exception for you if it had been written to the Parcel,
1941 * otherwise return and let you read the normal result data from the Parcel.
1942 *
1943 * @see #writeException
1944 * @see #writeNoException
1945 */
1946 public final void readException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001947 int code = readExceptionCode();
1948 if (code != 0) {
1949 String msg = readString();
1950 readException(code, msg);
1951 }
1952 }
1953
1954 /**
1955 * Parses the header of a Binder call's response Parcel and
1956 * returns the exception code. Deals with lite or fat headers.
1957 * In the common successful case, this header is generally zero.
1958 * In less common cases, it's a small negative number and will be
1959 * followed by an error string.
1960 *
1961 * This exists purely for android.database.DatabaseUtils and
1962 * insulating it from having to handle fat headers as returned by
1963 * e.g. StrictMode-induced RPC responses.
1964 *
1965 * @hide
1966 */
1967 public final int readExceptionCode() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 int code = readInt();
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001969 if (code == EX_HAS_REPLY_HEADER) {
1970 int headerSize = readInt();
1971 if (headerSize == 0) {
1972 Log.e(TAG, "Unexpected zero-sized Parcel reply header.");
1973 } else {
1974 // Currently the only thing in the header is StrictMode stacks,
1975 // but discussions around event/RPC tracing suggest we might
1976 // put that here too. If so, switch on sub-header tags here.
1977 // But for now, just parse out the StrictMode stuff.
1978 StrictMode.readAndHandleBinderCallViolations(this);
1979 }
1980 // And fat response headers are currently only used when
1981 // there are no exceptions, so return no error:
1982 return 0;
1983 }
1984 return code;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 }
1986
1987 /**
Mark Doliner879ea452014-01-02 12:38:07 -08001988 * Throw an exception with the given message. Not intended for use
1989 * outside the Parcel class.
1990 *
1991 * @param code Used to determine which exception class to throw.
1992 * @param msg The exception message.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 */
1994 public final void readException(int code, String msg) {
1995 switch (code) {
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001996 case EX_PARCELABLE:
1997 if (readInt() > 0) {
1998 SneakyThrow.sneakyThrow(
1999 (Exception) readParcelable(Parcelable.class.getClassLoader()));
2000 } else {
2001 throw new RuntimeException(msg + " [missing Parcelable]");
2002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 case EX_SECURITY:
2004 throw new SecurityException(msg);
2005 case EX_BAD_PARCELABLE:
2006 throw new BadParcelableException(msg);
2007 case EX_ILLEGAL_ARGUMENT:
2008 throw new IllegalArgumentException(msg);
2009 case EX_NULL_POINTER:
2010 throw new NullPointerException(msg);
2011 case EX_ILLEGAL_STATE:
2012 throw new IllegalStateException(msg);
Dianne Hackborn7e714422013-09-13 17:32:57 -07002013 case EX_NETWORK_MAIN_THREAD:
2014 throw new NetworkOnMainThreadException();
Dianne Hackborn33d738a2014-09-12 14:23:58 -07002015 case EX_UNSUPPORTED_OPERATION:
2016 throw new UnsupportedOperationException(msg);
Christopher Wiley80fd1202015-11-22 17:12:37 -08002017 case EX_SERVICE_SPECIFIC:
2018 throw new ServiceSpecificException(readInt(), msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 }
2020 throw new RuntimeException("Unknown exception code: " + code
2021 + " msg " + msg);
2022 }
2023
2024 /**
2025 * Read an integer value from the parcel at the current dataPosition().
2026 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002027 public final int readInt() {
2028 return nativeReadInt(mNativePtr);
2029 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030
2031 /**
2032 * Read a long integer value from the parcel at the current dataPosition().
2033 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002034 public final long readLong() {
2035 return nativeReadLong(mNativePtr);
2036 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037
2038 /**
2039 * Read a floating point value from the parcel at the current
2040 * dataPosition().
2041 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002042 public final float readFloat() {
2043 return nativeReadFloat(mNativePtr);
2044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045
2046 /**
2047 * Read a double precision floating point value from the parcel at the
2048 * current dataPosition().
2049 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002050 public final double readDouble() {
2051 return nativeReadDouble(mNativePtr);
2052 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053
2054 /**
2055 * Read a string value from the parcel at the current dataPosition().
2056 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002057 public final String readString() {
Makoto Onuki4501c61d2017-07-27 15:56:40 -07002058 return mReadWriteHelper.readString(this);
2059 }
2060
2061 /**
2062 * Read a string without going though a {@link ReadWriteHelper}. Subclasses of
2063 * {@link ReadWriteHelper} must use this method instead of {@link #readString} to avoid
2064 * infinity recursive calls.
2065 *
2066 * @hide
2067 */
2068 public String readStringNoHelper() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002069 return nativeReadString(mNativePtr);
2070 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071
Eugene Susla36e866b2017-02-23 18:24:39 -08002072 /** @hide */
2073 public final boolean readBoolean() {
2074 return readInt() != 0;
2075 }
2076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002078 * Read a CharSequence value from the parcel at the current dataPosition().
2079 * @hide
2080 */
2081 public final CharSequence readCharSequence() {
2082 return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(this);
2083 }
2084
2085 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 * Read an object from the parcel at the current dataPosition().
2087 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002088 public final IBinder readStrongBinder() {
2089 return nativeReadStrongBinder(mNativePtr);
2090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091
2092 /**
2093 * Read a FileDescriptor from the parcel at the current dataPosition().
2094 */
2095 public final ParcelFileDescriptor readFileDescriptor() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002096 FileDescriptor fd = nativeReadFileDescriptor(mNativePtr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 return fd != null ? new ParcelFileDescriptor(fd) : null;
2098 }
2099
Jeff Sharkeyda5a3e12013-08-11 12:54:42 -07002100 /** {@hide} */
2101 public final FileDescriptor readRawFileDescriptor() {
2102 return nativeReadFileDescriptor(mNativePtr);
2103 }
2104
Casey Dahlin2f974b22015-11-05 12:19:13 -08002105 /**
2106 * {@hide}
2107 * Read and return a new array of FileDescriptors from the parcel.
2108 * @return the FileDescriptor array, or null if the array is null.
2109 **/
2110 public final FileDescriptor[] createRawFileDescriptorArray() {
2111 int N = readInt();
2112 if (N < 0) {
2113 return null;
2114 }
2115 FileDescriptor[] f = new FileDescriptor[N];
2116 for (int i = 0; i < N; i++) {
2117 f[i] = readRawFileDescriptor();
2118 }
2119 return f;
2120 }
2121
2122 /**
2123 * {@hide}
2124 * Read an array of FileDescriptors from a parcel.
2125 * The passed array must be exactly the length of the array in the parcel.
2126 * @return the FileDescriptor array, or null if the array is null.
2127 **/
2128 public final void readRawFileDescriptorArray(FileDescriptor[] val) {
2129 int N = readInt();
2130 if (N == val.length) {
2131 for (int i=0; i<N; i++) {
2132 val[i] = readRawFileDescriptor();
2133 }
2134 } else {
2135 throw new RuntimeException("bad array lengths");
2136 }
2137 }
2138
Jeff Sharkeye53e2d92017-03-25 23:14:06 -06002139 /** @deprecated use {@link android.system.Os#open(String, int, int)} */
2140 @Deprecated
2141 static native FileDescriptor openFileDescriptor(String file, int mode)
2142 throws FileNotFoundException;
Casey Dahlin2f974b22015-11-05 12:19:13 -08002143
Jeff Sharkeye53e2d92017-03-25 23:14:06 -06002144 /** @deprecated use {@link android.system.Os#dup(FileDescriptor)} */
2145 @Deprecated
2146 static native FileDescriptor dupFileDescriptor(FileDescriptor orig) throws IOException;
2147
2148 /** @deprecated use {@link android.system.Os#close(FileDescriptor)} */
2149 @Deprecated
2150 static native void closeFileDescriptor(FileDescriptor desc) throws IOException;
2151
2152 static native void clearFileDescriptor(FileDescriptor desc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153
2154 /**
2155 * Read a byte value from the parcel at the current dataPosition().
2156 */
2157 public final byte readByte() {
2158 return (byte)(readInt() & 0xff);
2159 }
2160
2161 /**
2162 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2163 * been written with {@link #writeBundle}. Read into an existing Map object
2164 * from the parcel at the current dataPosition().
2165 */
2166 public final void readMap(Map outVal, ClassLoader loader) {
2167 int N = readInt();
2168 readMapInternal(outVal, N, loader);
2169 }
2170
2171 /**
2172 * Read into an existing List object from the parcel at the current
2173 * dataPosition(), using the given class loader to load any enclosed
2174 * Parcelables. If it is null, the default class loader is used.
2175 */
2176 public final void readList(List outVal, ClassLoader loader) {
2177 int N = readInt();
2178 readListInternal(outVal, N, loader);
2179 }
2180
2181 /**
2182 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2183 * been written with {@link #writeBundle}. Read and return a new HashMap
2184 * object from the parcel at the current dataPosition(), using the given
2185 * class loader to load any enclosed Parcelables. Returns null if
2186 * the previously written map object was null.
2187 */
2188 public final HashMap readHashMap(ClassLoader loader)
2189 {
2190 int N = readInt();
2191 if (N < 0) {
2192 return null;
2193 }
2194 HashMap m = new HashMap(N);
2195 readMapInternal(m, N, loader);
2196 return m;
2197 }
2198
2199 /**
2200 * Read and return a new Bundle object from the parcel at the current
2201 * dataPosition(). Returns null if the previously written Bundle object was
2202 * null.
2203 */
2204 public final Bundle readBundle() {
2205 return readBundle(null);
2206 }
2207
2208 /**
2209 * Read and return a new Bundle object from the parcel at the current
2210 * dataPosition(), using the given class loader to initialize the class
2211 * loader of the Bundle for later retrieval of Parcelable objects.
2212 * Returns null if the previously written Bundle object was null.
2213 */
2214 public final Bundle readBundle(ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 int length = readInt();
2216 if (length < 0) {
Dianne Hackborn4a7d8242013-10-03 10:19:20 -07002217 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 return null;
2219 }
Samuel Tana8036662015-11-23 14:36:00 -08002220
Dianne Hackborn6aff9052009-05-22 13:20:23 -07002221 final Bundle bundle = new Bundle(this, length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 if (loader != null) {
2223 bundle.setClassLoader(loader);
2224 }
2225 return bundle;
2226 }
2227
2228 /**
Craig Mautner719e6b12014-04-04 20:29:41 -07002229 * Read and return a new Bundle object from the parcel at the current
2230 * dataPosition(). Returns null if the previously written Bundle object was
2231 * null.
2232 */
2233 public final PersistableBundle readPersistableBundle() {
2234 return readPersistableBundle(null);
2235 }
2236
2237 /**
2238 * Read and return a new Bundle object from the parcel at the current
2239 * dataPosition(), using the given class loader to initialize the class
2240 * loader of the Bundle for later retrieval of Parcelable objects.
2241 * Returns null if the previously written Bundle object was null.
2242 */
2243 public final PersistableBundle readPersistableBundle(ClassLoader loader) {
2244 int length = readInt();
2245 if (length < 0) {
2246 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
2247 return null;
2248 }
2249
2250 final PersistableBundle bundle = new PersistableBundle(this, length);
2251 if (loader != null) {
2252 bundle.setClassLoader(loader);
2253 }
2254 return bundle;
2255 }
2256
2257 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002258 * Read a Size from the parcel at the current dataPosition().
2259 */
2260 public final Size readSize() {
2261 final int width = readInt();
2262 final int height = readInt();
2263 return new Size(width, height);
2264 }
2265
2266 /**
2267 * Read a SizeF from the parcel at the current dataPosition().
2268 */
2269 public final SizeF readSizeF() {
2270 final float width = readFloat();
2271 final float height = readFloat();
2272 return new SizeF(width, height);
2273 }
2274
2275 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 * Read and return a byte[] object from the parcel.
2277 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002278 public final byte[] createByteArray() {
2279 return nativeCreateByteArray(mNativePtr);
2280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281
2282 /**
2283 * Read a byte[] object from the parcel and copy it into the
2284 * given byte array.
2285 */
2286 public final void readByteArray(byte[] val) {
Jocelyn Dang46100442017-05-05 15:40:49 -07002287 boolean valid = nativeReadByteArray(mNativePtr, val, (val != null) ? val.length : 0);
2288 if (!valid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 throw new RuntimeException("bad array lengths");
2290 }
2291 }
2292
2293 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002294 * Read a blob of data from the parcel and return it as a byte array.
2295 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -07002296 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002297 */
2298 public final byte[] readBlob() {
2299 return nativeReadBlob(mNativePtr);
2300 }
2301
2302 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 * Read and return a String[] object from the parcel.
2304 * {@hide}
2305 */
2306 public final String[] readStringArray() {
2307 String[] array = null;
2308
2309 int length = readInt();
2310 if (length >= 0)
2311 {
2312 array = new String[length];
2313
2314 for (int i = 0 ; i < length ; i++)
2315 {
2316 array[i] = readString();
2317 }
2318 }
2319
2320 return array;
2321 }
2322
2323 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002324 * Read and return a CharSequence[] object from the parcel.
2325 * {@hide}
2326 */
2327 public final CharSequence[] readCharSequenceArray() {
2328 CharSequence[] array = null;
2329
2330 int length = readInt();
2331 if (length >= 0)
2332 {
2333 array = new CharSequence[length];
2334
2335 for (int i = 0 ; i < length ; i++)
2336 {
2337 array[i] = readCharSequence();
2338 }
2339 }
2340
2341 return array;
2342 }
2343
2344 /**
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002345 * Read and return an ArrayList&lt;CharSequence&gt; object from the parcel.
2346 * {@hide}
2347 */
2348 public final ArrayList<CharSequence> readCharSequenceList() {
2349 ArrayList<CharSequence> array = null;
2350
2351 int length = readInt();
2352 if (length >= 0) {
2353 array = new ArrayList<CharSequence>(length);
2354
2355 for (int i = 0 ; i < length ; i++) {
2356 array.add(readCharSequence());
2357 }
2358 }
2359
2360 return array;
2361 }
2362
2363 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 * Read and return a new ArrayList object from the parcel at the current
2365 * dataPosition(). Returns null if the previously written list object was
2366 * null. The given class loader will be used to load any enclosed
2367 * Parcelables.
2368 */
2369 public final ArrayList readArrayList(ClassLoader loader) {
2370 int N = readInt();
2371 if (N < 0) {
2372 return null;
2373 }
2374 ArrayList l = new ArrayList(N);
2375 readListInternal(l, N, loader);
2376 return l;
2377 }
2378
2379 /**
2380 * Read and return a new Object array from the parcel at the current
2381 * dataPosition(). Returns null if the previously written array was
2382 * null. The given class loader will be used to load any enclosed
2383 * Parcelables.
2384 */
2385 public final Object[] readArray(ClassLoader loader) {
2386 int N = readInt();
2387 if (N < 0) {
2388 return null;
2389 }
2390 Object[] l = new Object[N];
2391 readArrayInternal(l, N, loader);
2392 return l;
2393 }
2394
2395 /**
2396 * Read and return a new SparseArray object from the parcel at the current
2397 * dataPosition(). Returns null if the previously written list object was
2398 * null. The given class loader will be used to load any enclosed
2399 * Parcelables.
2400 */
2401 public final SparseArray readSparseArray(ClassLoader loader) {
2402 int N = readInt();
2403 if (N < 0) {
2404 return null;
2405 }
2406 SparseArray sa = new SparseArray(N);
2407 readSparseArrayInternal(sa, N, loader);
2408 return sa;
2409 }
2410
2411 /**
2412 * Read and return a new SparseBooleanArray object from the parcel at the current
2413 * dataPosition(). Returns null if the previously written list object was
2414 * null.
2415 */
2416 public final SparseBooleanArray readSparseBooleanArray() {
2417 int N = readInt();
2418 if (N < 0) {
2419 return null;
2420 }
2421 SparseBooleanArray sa = new SparseBooleanArray(N);
2422 readSparseBooleanArrayInternal(sa, N);
2423 return sa;
2424 }
2425
2426 /**
Adam Lesinski4e862812016-11-21 16:02:24 -08002427 * Read and return a new SparseIntArray object from the parcel at the current
2428 * dataPosition(). Returns null if the previously written array object was null.
Adam Lesinski205656d2017-03-23 13:38:26 -07002429 * @hide
Adam Lesinski4e862812016-11-21 16:02:24 -08002430 */
2431 public final SparseIntArray readSparseIntArray() {
2432 int N = readInt();
2433 if (N < 0) {
2434 return null;
2435 }
2436 SparseIntArray sa = new SparseIntArray(N);
2437 readSparseIntArrayInternal(sa, N);
2438 return sa;
2439 }
2440
2441 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 * Read and return a new ArrayList containing a particular object type from
2443 * the parcel that was written with {@link #writeTypedList} at the
2444 * current dataPosition(). Returns null if the
2445 * previously written list object was null. The list <em>must</em> have
2446 * previously been written via {@link #writeTypedList} with the same object
2447 * type.
2448 *
2449 * @return A newly created ArrayList containing objects with the same data
2450 * as those that were previously written.
2451 *
2452 * @see #writeTypedList
2453 */
2454 public final <T> ArrayList<T> createTypedArrayList(Parcelable.Creator<T> c) {
2455 int N = readInt();
2456 if (N < 0) {
2457 return null;
2458 }
2459 ArrayList<T> l = new ArrayList<T>(N);
2460 while (N > 0) {
2461 if (readInt() != 0) {
2462 l.add(c.createFromParcel(this));
2463 } else {
2464 l.add(null);
2465 }
2466 N--;
2467 }
2468 return l;
2469 }
2470
2471 /**
2472 * Read into the given List items containing a particular object type
2473 * that were written with {@link #writeTypedList} at the
2474 * current dataPosition(). The list <em>must</em> have
2475 * previously been written via {@link #writeTypedList} with the same object
2476 * type.
2477 *
2478 * @return A newly created ArrayList containing objects with the same data
2479 * as those that were previously written.
2480 *
2481 * @see #writeTypedList
2482 */
2483 public final <T> void readTypedList(List<T> list, Parcelable.Creator<T> c) {
2484 int M = list.size();
2485 int N = readInt();
2486 int i = 0;
2487 for (; i < M && i < N; i++) {
2488 if (readInt() != 0) {
2489 list.set(i, c.createFromParcel(this));
2490 } else {
2491 list.set(i, null);
2492 }
2493 }
2494 for (; i<N; i++) {
2495 if (readInt() != 0) {
2496 list.add(c.createFromParcel(this));
2497 } else {
2498 list.add(null);
2499 }
2500 }
2501 for (; i<M; i++) {
2502 list.remove(N);
2503 }
2504 }
2505
2506 /**
2507 * Read and return a new ArrayList containing String objects from
2508 * the parcel that was written with {@link #writeStringList} at the
2509 * current dataPosition(). Returns null if the
2510 * previously written list object was null.
2511 *
2512 * @return A newly created ArrayList containing strings with the same data
2513 * as those that were previously written.
2514 *
2515 * @see #writeStringList
2516 */
2517 public final ArrayList<String> createStringArrayList() {
2518 int N = readInt();
2519 if (N < 0) {
2520 return null;
2521 }
2522 ArrayList<String> l = new ArrayList<String>(N);
2523 while (N > 0) {
2524 l.add(readString());
2525 N--;
2526 }
2527 return l;
2528 }
2529
2530 /**
2531 * Read and return a new ArrayList containing IBinder objects from
2532 * the parcel that was written with {@link #writeBinderList} at the
2533 * current dataPosition(). Returns null if the
2534 * previously written list object was null.
2535 *
2536 * @return A newly created ArrayList containing strings with the same data
2537 * as those that were previously written.
2538 *
2539 * @see #writeBinderList
2540 */
2541 public final ArrayList<IBinder> createBinderArrayList() {
2542 int N = readInt();
2543 if (N < 0) {
2544 return null;
2545 }
2546 ArrayList<IBinder> l = new ArrayList<IBinder>(N);
2547 while (N > 0) {
2548 l.add(readStrongBinder());
2549 N--;
2550 }
2551 return l;
2552 }
2553
2554 /**
2555 * Read into the given List items String objects that were written with
2556 * {@link #writeStringList} at the current dataPosition().
2557 *
2558 * @return A newly created ArrayList containing strings with the same data
2559 * as those that were previously written.
2560 *
2561 * @see #writeStringList
2562 */
2563 public final void readStringList(List<String> list) {
2564 int M = list.size();
2565 int N = readInt();
2566 int i = 0;
2567 for (; i < M && i < N; i++) {
2568 list.set(i, readString());
2569 }
2570 for (; i<N; i++) {
2571 list.add(readString());
2572 }
2573 for (; i<M; i++) {
2574 list.remove(N);
2575 }
2576 }
2577
2578 /**
2579 * Read into the given List items IBinder objects that were written with
2580 * {@link #writeBinderList} at the current dataPosition().
2581 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 * @see #writeBinderList
2583 */
2584 public final void readBinderList(List<IBinder> list) {
2585 int M = list.size();
2586 int N = readInt();
2587 int i = 0;
2588 for (; i < M && i < N; i++) {
2589 list.set(i, readStrongBinder());
2590 }
2591 for (; i<N; i++) {
2592 list.add(readStrongBinder());
2593 }
2594 for (; i<M; i++) {
2595 list.remove(N);
2596 }
2597 }
2598
2599 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00002600 * Read the list of {@code Parcelable} objects at the current data position into the
2601 * given {@code list}. The contents of the {@code list} are replaced. If the serialized
2602 * list was {@code null}, {@code list} is cleared.
2603 *
2604 * @see #writeParcelableList(List, int)
2605 * @hide
2606 */
Eugene Susla36e866b2017-02-23 18:24:39 -08002607 public final <T extends Parcelable> List<T> readParcelableList(List<T> list, ClassLoader cl) {
Narayan Kamathbea48712016-12-01 15:38:28 +00002608 final int N = readInt();
2609 if (N == -1) {
2610 list.clear();
Eugene Susla36e866b2017-02-23 18:24:39 -08002611 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002612 }
2613
2614 final int M = list.size();
2615 int i = 0;
2616 for (; i < M && i < N; i++) {
2617 list.set(i, (T) readParcelable(cl));
2618 }
2619 for (; i<N; i++) {
2620 list.add((T) readParcelable(cl));
2621 }
2622 for (; i<M; i++) {
2623 list.remove(N);
2624 }
Eugene Susla36e866b2017-02-23 18:24:39 -08002625 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002626 }
2627
2628 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 * Read and return a new array containing a particular object type from
2630 * the parcel at the current dataPosition(). Returns null if the
2631 * previously written array was null. The array <em>must</em> have
2632 * previously been written via {@link #writeTypedArray} with the same
2633 * object type.
2634 *
2635 * @return A newly created array containing objects with the same data
2636 * as those that were previously written.
2637 *
2638 * @see #writeTypedArray
2639 */
2640 public final <T> T[] createTypedArray(Parcelable.Creator<T> c) {
2641 int N = readInt();
2642 if (N < 0) {
2643 return null;
2644 }
2645 T[] l = c.newArray(N);
2646 for (int i=0; i<N; i++) {
2647 if (readInt() != 0) {
2648 l[i] = c.createFromParcel(this);
2649 }
2650 }
2651 return l;
2652 }
2653
2654 public final <T> void readTypedArray(T[] val, Parcelable.Creator<T> c) {
2655 int N = readInt();
2656 if (N == val.length) {
2657 for (int i=0; i<N; i++) {
2658 if (readInt() != 0) {
2659 val[i] = c.createFromParcel(this);
2660 } else {
2661 val[i] = null;
2662 }
2663 }
2664 } else {
2665 throw new RuntimeException("bad array lengths");
2666 }
2667 }
2668
2669 /**
2670 * @deprecated
2671 * @hide
2672 */
2673 @Deprecated
2674 public final <T> T[] readTypedArray(Parcelable.Creator<T> c) {
2675 return createTypedArray(c);
2676 }
2677
2678 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002679 * Read and return a typed Parcelable object from a parcel.
2680 * Returns null if the previous written object was null.
2681 * The object <em>must</em> have previous been written via
2682 * {@link #writeTypedObject} with the same object type.
2683 *
2684 * @return A newly created object of the type that was previously
2685 * written.
2686 *
2687 * @see #writeTypedObject
2688 */
2689 public final <T> T readTypedObject(Parcelable.Creator<T> c) {
2690 if (readInt() != 0) {
2691 return c.createFromParcel(this);
2692 } else {
2693 return null;
2694 }
2695 }
2696
2697 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 * Write a heterogeneous array of Parcelable objects into the Parcel.
2699 * Each object in the array is written along with its class name, so
2700 * that the correct class can later be instantiated. As a result, this
2701 * has significantly more overhead than {@link #writeTypedArray}, but will
2702 * correctly handle an array containing more than one type of object.
2703 *
2704 * @param value The array of objects to be written.
2705 * @param parcelableFlags Contextual flags as per
2706 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
2707 *
2708 * @see #writeTypedArray
2709 */
2710 public final <T extends Parcelable> void writeParcelableArray(T[] value,
2711 int parcelableFlags) {
2712 if (value != null) {
2713 int N = value.length;
2714 writeInt(N);
2715 for (int i=0; i<N; i++) {
2716 writeParcelable(value[i], parcelableFlags);
2717 }
2718 } else {
2719 writeInt(-1);
2720 }
2721 }
2722
2723 /**
2724 * Read a typed object from a parcel. The given class loader will be
2725 * used to load any enclosed Parcelables. If it is null, the default class
2726 * loader will be used.
2727 */
2728 public final Object readValue(ClassLoader loader) {
2729 int type = readInt();
2730
2731 switch (type) {
2732 case VAL_NULL:
2733 return null;
2734
2735 case VAL_STRING:
2736 return readString();
2737
2738 case VAL_INTEGER:
2739 return readInt();
2740
2741 case VAL_MAP:
2742 return readHashMap(loader);
2743
2744 case VAL_PARCELABLE:
2745 return readParcelable(loader);
2746
2747 case VAL_SHORT:
2748 return (short) readInt();
2749
2750 case VAL_LONG:
2751 return readLong();
2752
2753 case VAL_FLOAT:
2754 return readFloat();
2755
2756 case VAL_DOUBLE:
2757 return readDouble();
2758
2759 case VAL_BOOLEAN:
2760 return readInt() == 1;
2761
2762 case VAL_CHARSEQUENCE:
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002763 return readCharSequence();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764
2765 case VAL_LIST:
2766 return readArrayList(loader);
2767
2768 case VAL_BOOLEANARRAY:
Samuel Tana8036662015-11-23 14:36:00 -08002769 return createBooleanArray();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770
2771 case VAL_BYTEARRAY:
2772 return createByteArray();
2773
2774 case VAL_STRINGARRAY:
2775 return readStringArray();
2776
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002777 case VAL_CHARSEQUENCEARRAY:
2778 return readCharSequenceArray();
2779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 case VAL_IBINDER:
2781 return readStrongBinder();
2782
2783 case VAL_OBJECTARRAY:
2784 return readArray(loader);
2785
2786 case VAL_INTARRAY:
2787 return createIntArray();
2788
2789 case VAL_LONGARRAY:
2790 return createLongArray();
2791
2792 case VAL_BYTE:
2793 return readByte();
2794
2795 case VAL_SERIALIZABLE:
John Spurlock5002b8c2014-01-10 13:32:12 -05002796 return readSerializable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797
2798 case VAL_PARCELABLEARRAY:
2799 return readParcelableArray(loader);
2800
2801 case VAL_SPARSEARRAY:
2802 return readSparseArray(loader);
2803
2804 case VAL_SPARSEBOOLEANARRAY:
2805 return readSparseBooleanArray();
2806
2807 case VAL_BUNDLE:
2808 return readBundle(loader); // loading will be deferred
2809
Craig Mautner719e6b12014-04-04 20:29:41 -07002810 case VAL_PERSISTABLEBUNDLE:
2811 return readPersistableBundle(loader);
2812
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002813 case VAL_SIZE:
2814 return readSize();
2815
2816 case VAL_SIZEF:
2817 return readSizeF();
2818
Samuel Tana8036662015-11-23 14:36:00 -08002819 case VAL_DOUBLEARRAY:
2820 return createDoubleArray();
2821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 default:
2823 int off = dataPosition() - 4;
2824 throw new RuntimeException(
2825 "Parcel " + this + ": Unmarshalling unknown type code " + type + " at offset " + off);
2826 }
2827 }
2828
2829 /**
2830 * Read and return a new Parcelable from the parcel. The given class loader
2831 * will be used to load any enclosed Parcelables. If it is null, the default
2832 * class loader will be used.
2833 * @param loader A ClassLoader from which to instantiate the Parcelable
2834 * object, or null for the default class loader.
2835 * @return Returns the newly created Parcelable, or null if a null
2836 * object has been written.
2837 * @throws BadParcelableException Throws BadParcelableException if there
2838 * was an error trying to instantiate the Parcelable.
2839 */
Neil Fuller44e440c2015-04-20 14:39:00 +01002840 @SuppressWarnings("unchecked")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 public final <T extends Parcelable> T readParcelable(ClassLoader loader) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002842 Parcelable.Creator<?> creator = readParcelableCreator(loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002843 if (creator == null) {
2844 return null;
2845 }
2846 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002847 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2848 (Parcelable.ClassLoaderCreator<?>) creator;
2849 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002850 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002851 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002852 }
2853
2854 /** @hide */
Neil Fuller44e440c2015-04-20 14:39:00 +01002855 @SuppressWarnings("unchecked")
2856 public final <T extends Parcelable> T readCreator(Parcelable.Creator<?> creator,
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002857 ClassLoader loader) {
2858 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002859 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2860 (Parcelable.ClassLoaderCreator<?>) creator;
2861 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002862 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002863 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002864 }
2865
2866 /** @hide */
Neil Fuller44e440c2015-04-20 14:39:00 +01002867 public final Parcelable.Creator<?> readParcelableCreator(ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 String name = readString();
2869 if (name == null) {
2870 return null;
2871 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002872 Parcelable.Creator<?> creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 synchronized (mCreators) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002874 HashMap<String,Parcelable.Creator<?>> map = mCreators.get(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 if (map == null) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002876 map = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 mCreators.put(loader, map);
2878 }
2879 creator = map.get(name);
2880 if (creator == null) {
2881 try {
Neil Fuller44e440c2015-04-20 14:39:00 +01002882 // If loader == null, explicitly emulate Class.forName(String) "caller
2883 // classloader" behavior.
2884 ClassLoader parcelableClassLoader =
2885 (loader == null ? getClass().getClassLoader() : loader);
2886 // Avoid initializing the Parcelable class until we know it implements
2887 // Parcelable and has the necessary CREATOR field. http://b/1171613.
2888 Class<?> parcelableClass = Class.forName(name, false /* initialize */,
2889 parcelableClassLoader);
2890 if (!Parcelable.class.isAssignableFrom(parcelableClass)) {
2891 throw new BadParcelableException("Parcelable protocol requires that the "
2892 + "class implements Parcelable");
2893 }
2894 Field f = parcelableClass.getField("CREATOR");
2895 if ((f.getModifiers() & Modifier.STATIC) == 0) {
2896 throw new BadParcelableException("Parcelable protocol requires "
2897 + "the CREATOR object to be static on class " + name);
2898 }
2899 Class<?> creatorType = f.getType();
2900 if (!Parcelable.Creator.class.isAssignableFrom(creatorType)) {
2901 // Fail before calling Field.get(), not after, to avoid initializing
2902 // parcelableClass unnecessarily.
2903 throw new BadParcelableException("Parcelable protocol requires a "
2904 + "Parcelable.Creator object called "
2905 + "CREATOR on class " + name);
2906 }
2907 creator = (Parcelable.Creator<?>) f.get(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908 }
2909 catch (IllegalAccessException e) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002910 Log.e(TAG, "Illegal access when unmarshalling: " + name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 throw new BadParcelableException(
2912 "IllegalAccessException when unmarshalling: " + name);
2913 }
2914 catch (ClassNotFoundException e) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002915 Log.e(TAG, "Class not found when unmarshalling: " + name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 throw new BadParcelableException(
2917 "ClassNotFoundException when unmarshalling: " + name);
2918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 catch (NoSuchFieldException e) {
2920 throw new BadParcelableException("Parcelable protocol requires a "
Neil Fuller44e440c2015-04-20 14:39:00 +01002921 + "Parcelable.Creator object called "
2922 + "CREATOR on class " + name);
Irfan Sheriff97a72f62013-01-11 10:45:51 -08002923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 if (creator == null) {
2925 throw new BadParcelableException("Parcelable protocol requires a "
Neil Fuller44e440c2015-04-20 14:39:00 +01002926 + "non-null Parcelable.Creator object called "
2927 + "CREATOR on class " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 }
2929
2930 map.put(name, creator);
2931 }
2932 }
2933
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002934 return creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 }
2936
2937 /**
2938 * Read and return a new Parcelable array from the parcel.
2939 * The given class loader will be used to load any enclosed
2940 * Parcelables.
2941 * @return the Parcelable array, or null if the array is null
2942 */
2943 public final Parcelable[] readParcelableArray(ClassLoader loader) {
2944 int N = readInt();
2945 if (N < 0) {
2946 return null;
2947 }
2948 Parcelable[] p = new Parcelable[N];
2949 for (int i = 0; i < N; i++) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002950 p[i] = readParcelable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 }
2952 return p;
2953 }
2954
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002955 /** @hide */
2956 public final <T extends Parcelable> T[] readParcelableArray(ClassLoader loader,
2957 Class<T> clazz) {
2958 int N = readInt();
2959 if (N < 0) {
2960 return null;
2961 }
2962 T[] p = (T[]) Array.newInstance(clazz, N);
2963 for (int i = 0; i < N; i++) {
2964 p[i] = readParcelable(loader);
2965 }
2966 return p;
2967 }
2968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 /**
2970 * Read and return a new Serializable object from the parcel.
2971 * @return the Serializable object, or null if the Serializable name
2972 * wasn't found in the parcel.
2973 */
2974 public final Serializable readSerializable() {
John Spurlock5002b8c2014-01-10 13:32:12 -05002975 return readSerializable(null);
2976 }
2977
2978 private final Serializable readSerializable(final ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 String name = readString();
2980 if (name == null) {
2981 // For some reason we were unable to read the name of the Serializable (either there
2982 // is nothing left in the Parcel to read, or the next value wasn't a String), so
2983 // return null, which indicates that the name wasn't found in the parcel.
2984 return null;
2985 }
2986
2987 byte[] serializedData = createByteArray();
2988 ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
2989 try {
John Spurlock5002b8c2014-01-10 13:32:12 -05002990 ObjectInputStream ois = new ObjectInputStream(bais) {
2991 @Override
2992 protected Class<?> resolveClass(ObjectStreamClass osClass)
2993 throws IOException, ClassNotFoundException {
2994 // try the custom classloader if provided
2995 if (loader != null) {
2996 Class<?> c = Class.forName(osClass.getName(), false, loader);
2997 if (c != null) {
2998 return c;
2999 }
3000 }
3001 return super.resolveClass(osClass);
3002 }
3003 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 return (Serializable) ois.readObject();
3005 } catch (IOException ioe) {
3006 throw new RuntimeException("Parcelable encountered " +
3007 "IOException reading a Serializable object (name = " + name +
3008 ")", ioe);
3009 } catch (ClassNotFoundException cnfe) {
John Spurlock5002b8c2014-01-10 13:32:12 -05003010 throw new RuntimeException("Parcelable encountered " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 "ClassNotFoundException reading a Serializable object (name = "
3012 + name + ")", cnfe);
3013 }
3014 }
3015
3016 // Cache of previously looked up CREATOR.createFromParcel() methods for
3017 // particular classes. Keys are the names of the classes, values are
3018 // Method objects.
Neil Fuller44e440c2015-04-20 14:39:00 +01003019 private static final HashMap<ClassLoader,HashMap<String,Parcelable.Creator<?>>>
3020 mCreators = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021
Narayan Kamathb34a4612014-01-23 14:17:11 +00003022 /** @hide for internal use only. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 static protected final Parcel obtain(int obj) {
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003024 throw new UnsupportedOperationException();
3025 }
3026
3027 /** @hide */
3028 static protected final Parcel obtain(long obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 final Parcel[] pool = sHolderPool;
3030 synchronized (pool) {
3031 Parcel p;
3032 for (int i=0; i<POOL_SIZE; i++) {
3033 p = pool[i];
3034 if (p != null) {
3035 pool[i] = null;
3036 if (DEBUG_RECYCLE) {
3037 p.mStack = new RuntimeException();
3038 }
3039 p.init(obj);
3040 return p;
3041 }
3042 }
3043 }
3044 return new Parcel(obj);
3045 }
3046
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003047 private Parcel(long nativePtr) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 if (DEBUG_RECYCLE) {
3049 mStack = new RuntimeException();
3050 }
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003051 //Log.i(TAG, "Initializing obj=0x" + Integer.toHexString(obj), mStack);
Jeff Sharkey047238c2012-03-07 16:51:38 -08003052 init(nativePtr);
3053 }
3054
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003055 private void init(long nativePtr) {
Jeff Sharkey047238c2012-03-07 16:51:38 -08003056 if (nativePtr != 0) {
3057 mNativePtr = nativePtr;
3058 mOwnsNativeParcelObject = false;
3059 } else {
3060 mNativePtr = nativeCreate();
3061 mOwnsNativeParcelObject = true;
3062 }
3063 }
3064
3065 private void freeBuffer() {
3066 if (mOwnsNativeParcelObject) {
Adrian Roos04505652015-10-22 16:12:01 -07003067 updateNativeSize(nativeFreeBuffer(mNativePtr));
Jeff Sharkey047238c2012-03-07 16:51:38 -08003068 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -07003069 mReadWriteHelper = ReadWriteHelper.DEFAULT;
Jeff Sharkey047238c2012-03-07 16:51:38 -08003070 }
3071
3072 private void destroy() {
3073 if (mNativePtr != 0) {
3074 if (mOwnsNativeParcelObject) {
3075 nativeDestroy(mNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -07003076 updateNativeSize(0);
Jeff Sharkey047238c2012-03-07 16:51:38 -08003077 }
3078 mNativePtr = 0;
3079 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -07003080 mReadWriteHelper = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081 }
3082
3083 @Override
3084 protected void finalize() throws Throwable {
3085 if (DEBUG_RECYCLE) {
3086 if (mStack != null) {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003087 Log.w(TAG, "Client did not call Parcel.recycle()", mStack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 }
3089 }
3090 destroy();
3091 }
3092
Dianne Hackborn6aff9052009-05-22 13:20:23 -07003093 /* package */ void readMapInternal(Map outVal, int N,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094 ClassLoader loader) {
3095 while (N > 0) {
3096 Object key = readValue(loader);
3097 Object value = readValue(loader);
3098 outVal.put(key, value);
3099 N--;
3100 }
3101 }
3102
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003103 /* package */ void readArrayMapInternal(ArrayMap outVal, int N,
3104 ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003105 if (DEBUG_ARRAY_MAP) {
3106 RuntimeException here = new RuntimeException("here");
3107 here.fillInStackTrace();
3108 Log.d(TAG, "Reading " + N + " ArrayMap entries", here);
3109 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003110 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003111 while (N > 0) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003112 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003113 String key = readString();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003114 Object value = readValue(loader);
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003115 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " "
3116 + (dataPosition()-startPos) + " bytes: key=0x"
3117 + Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key);
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003118 outVal.append(key, value);
3119 N--;
3120 }
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003121 outVal.validate();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003122 }
3123
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003124 /* package */ void readArrayMapSafelyInternal(ArrayMap outVal, int N,
3125 ClassLoader loader) {
3126 if (DEBUG_ARRAY_MAP) {
3127 RuntimeException here = new RuntimeException("here");
3128 here.fillInStackTrace();
3129 Log.d(TAG, "Reading safely " + N + " ArrayMap entries", here);
3130 }
3131 while (N > 0) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003132 String key = readString();
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003133 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read safe #" + (N-1) + ": key=0x"
3134 + (key != null ? key.hashCode() : 0) + " " + key);
3135 Object value = readValue(loader);
3136 outVal.put(key, value);
3137 N--;
3138 }
3139 }
3140
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003141 /**
3142 * @hide For testing only.
3143 */
3144 public void readArrayMap(ArrayMap outVal, ClassLoader loader) {
3145 final int N = readInt();
3146 if (N < 0) {
3147 return;
3148 }
3149 readArrayMapInternal(outVal, N, loader);
3150 }
3151
Svet Ganovddb94882016-06-23 19:55:24 -07003152 /**
3153 * Reads an array set.
3154 *
3155 * @param loader The class loader to use.
3156 *
3157 * @hide
3158 */
3159 public @Nullable ArraySet<? extends Object> readArraySet(ClassLoader loader) {
3160 final int size = readInt();
3161 if (size < 0) {
3162 return null;
3163 }
3164 ArraySet<Object> result = new ArraySet<>(size);
3165 for (int i = 0; i < size; i++) {
3166 Object value = readValue(loader);
3167 result.append(value);
3168 }
3169 return result;
3170 }
3171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 private void readListInternal(List outVal, int N,
3173 ClassLoader loader) {
3174 while (N > 0) {
3175 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003176 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 outVal.add(value);
3178 N--;
3179 }
3180 }
3181
3182 private void readArrayInternal(Object[] outVal, int N,
3183 ClassLoader loader) {
3184 for (int i = 0; i < N; i++) {
3185 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003186 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003187 outVal[i] = value;
3188 }
3189 }
3190
3191 private void readSparseArrayInternal(SparseArray outVal, int N,
3192 ClassLoader loader) {
3193 while (N > 0) {
3194 int key = readInt();
3195 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003196 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 outVal.append(key, value);
3198 N--;
3199 }
3200 }
3201
3202
3203 private void readSparseBooleanArrayInternal(SparseBooleanArray outVal, int N) {
3204 while (N > 0) {
3205 int key = readInt();
3206 boolean value = this.readByte() == 1;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003207 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 outVal.append(key, value);
3209 N--;
3210 }
3211 }
Dan Sandler5ce04302015-04-09 23:50:15 -04003212
Adam Lesinski4e862812016-11-21 16:02:24 -08003213 private void readSparseIntArrayInternal(SparseIntArray outVal, int N) {
3214 while (N > 0) {
3215 int key = readInt();
3216 int value = readInt();
3217 outVal.append(key, value);
3218 N--;
3219 }
3220 }
3221
Dan Sandler5ce04302015-04-09 23:50:15 -04003222 /**
3223 * @hide For testing
3224 */
3225 public long getBlobAshmemSize() {
3226 return nativeGetBlobAshmemSize(mNativePtr);
3227 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228}