blob: e99d303642ba0d91e0ad21b1953bfb38ff8a1eac [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
Adrian Roos04505652015-10-22 16:12:01 -070019import android.annotation.IntegerRes;
Svet Ganovddb94882016-06-23 19:55:24 -070020import android.annotation.Nullable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.text.TextUtils;
Dianne Hackbornb87655b2013-07-17 19:06:22 -070022import android.util.ArrayMap;
Svet Ganovddb94882016-06-23 19:55:24 -070023import android.util.ArraySet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.util.Log;
Jeff Sharkey5ef33982014-09-04 18:13:39 -070025import android.util.Size;
26import android.util.SizeF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.util.SparseArray;
28import android.util.SparseBooleanArray;
Adam Lesinski4e862812016-11-21 16:02:24 -080029import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
Jeff Sharkeye628b7d2017-01-17 13:50:20 -070031import libcore.util.SneakyThrow;
32
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import java.io.ByteArrayInputStream;
34import java.io.ByteArrayOutputStream;
35import java.io.FileDescriptor;
36import java.io.FileNotFoundException;
37import java.io.IOException;
38import java.io.ObjectInputStream;
39import java.io.ObjectOutputStream;
John Spurlock5002b8c2014-01-10 13:32:12 -050040import java.io.ObjectStreamClass;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import java.io.Serializable;
Makoto Onuki440a1ea2016-07-20 14:21:18 -070042import java.lang.reflect.Array;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import java.lang.reflect.Field;
Neil Fuller44e440c2015-04-20 14:39:00 +010044import java.lang.reflect.Modifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import java.util.ArrayList;
Elliott Hughesa28b83e2011-02-28 14:26:13 -080046import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import java.util.HashMap;
48import java.util.List;
49import java.util.Map;
50import java.util.Set;
51
John Reck71207b52016-09-28 13:28:09 -070052import dalvik.annotation.optimization.FastNative;
Adrian Roos04505652015-10-22 16:12:01 -070053import dalvik.system.VMRuntime;
54
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
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -0700135 * write and read a single Parceable object that is not null, you can directly
136 * 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 private RuntimeException mStack;
209
210 private static final int POOL_SIZE = 6;
211 private static final Parcel[] sOwnedPool = new Parcel[POOL_SIZE];
212 private static final Parcel[] sHolderPool = new Parcel[POOL_SIZE];
213
Robert Quattlebaum9cd7af32017-01-04 13:28:01 -0800214 // Keep in sync with frameworks/native/include/private/binder/ParcelValTypes.h.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 private static final int VAL_NULL = -1;
216 private static final int VAL_STRING = 0;
217 private static final int VAL_INTEGER = 1;
218 private static final int VAL_MAP = 2;
219 private static final int VAL_BUNDLE = 3;
220 private static final int VAL_PARCELABLE = 4;
221 private static final int VAL_SHORT = 5;
222 private static final int VAL_LONG = 6;
223 private static final int VAL_FLOAT = 7;
224 private static final int VAL_DOUBLE = 8;
225 private static final int VAL_BOOLEAN = 9;
226 private static final int VAL_CHARSEQUENCE = 10;
227 private static final int VAL_LIST = 11;
228 private static final int VAL_SPARSEARRAY = 12;
229 private static final int VAL_BYTEARRAY = 13;
230 private static final int VAL_STRINGARRAY = 14;
231 private static final int VAL_IBINDER = 15;
232 private static final int VAL_PARCELABLEARRAY = 16;
233 private static final int VAL_OBJECTARRAY = 17;
234 private static final int VAL_INTARRAY = 18;
235 private static final int VAL_LONGARRAY = 19;
236 private static final int VAL_BYTE = 20;
237 private static final int VAL_SERIALIZABLE = 21;
238 private static final int VAL_SPARSEBOOLEANARRAY = 22;
239 private static final int VAL_BOOLEANARRAY = 23;
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000240 private static final int VAL_CHARSEQUENCEARRAY = 24;
Craig Mautner719e6b12014-04-04 20:29:41 -0700241 private static final int VAL_PERSISTABLEBUNDLE = 25;
Jeff Sharkey5ef33982014-09-04 18:13:39 -0700242 private static final int VAL_SIZE = 26;
243 private static final int VAL_SIZEF = 27;
Samuel Tana8036662015-11-23 14:36:00 -0800244 private static final int VAL_DOUBLEARRAY = 28;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700246 // The initial int32 in a Binder call's reply Parcel header:
Christopher Wiley80fd1202015-11-22 17:12:37 -0800247 // Keep these in sync with libbinder's binder/Status.h.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 private static final int EX_SECURITY = -1;
249 private static final int EX_BAD_PARCELABLE = -2;
250 private static final int EX_ILLEGAL_ARGUMENT = -3;
251 private static final int EX_NULL_POINTER = -4;
252 private static final int EX_ILLEGAL_STATE = -5;
Dianne Hackborn7e714422013-09-13 17:32:57 -0700253 private static final int EX_NETWORK_MAIN_THREAD = -6;
Dianne Hackborn33d738a2014-09-12 14:23:58 -0700254 private static final int EX_UNSUPPORTED_OPERATION = -7;
Christopher Wiley80fd1202015-11-22 17:12:37 -0800255 private static final int EX_SERVICE_SPECIFIC = -8;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -0700256 private static final int EX_PARCELABLE = -9;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700257 private static final int EX_HAS_REPLY_HEADER = -128; // special; see below
Christopher Wiley80fd1202015-11-22 17:12:37 -0800258 // EX_TRANSACTION_FAILED is used exclusively in native code.
259 // see libbinder's binder/Status.h
260 private static final int EX_TRANSACTION_FAILED = -129;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700261
John Reck71207b52016-09-28 13:28:09 -0700262 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000263 private static native int nativeDataSize(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700264 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000265 private static native int nativeDataAvail(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700266 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000267 private static native int nativeDataPosition(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700268 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000269 private static native int nativeDataCapacity(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700270 @FastNative
Adrian Roos04505652015-10-22 16:12:01 -0700271 private static native long nativeSetDataSize(long nativePtr, int size);
John Reck71207b52016-09-28 13:28:09 -0700272 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000273 private static native void nativeSetDataPosition(long nativePtr, int pos);
John Reck71207b52016-09-28 13:28:09 -0700274 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000275 private static native void nativeSetDataCapacity(long nativePtr, int size);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800276
John Reck71207b52016-09-28 13:28:09 -0700277 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000278 private static native boolean nativePushAllowFds(long nativePtr, boolean allowFds);
John Reck71207b52016-09-28 13:28:09 -0700279 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000280 private static native void nativeRestoreAllowFds(long nativePtr, boolean lastValue);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800281
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000282 private static native void nativeWriteByteArray(long nativePtr, byte[] b, int offset, int len);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700283 private static native void nativeWriteBlob(long nativePtr, byte[] b, int offset, int len);
John Reck71207b52016-09-28 13:28:09 -0700284 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000285 private static native void nativeWriteInt(long nativePtr, int val);
John Reck71207b52016-09-28 13:28:09 -0700286 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000287 private static native void nativeWriteLong(long nativePtr, long val);
John Reck71207b52016-09-28 13:28:09 -0700288 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000289 private static native void nativeWriteFloat(long nativePtr, float val);
John Reck71207b52016-09-28 13:28:09 -0700290 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000291 private static native void nativeWriteDouble(long nativePtr, double val);
292 private static native void nativeWriteString(long nativePtr, String val);
293 private static native void nativeWriteStrongBinder(long nativePtr, IBinder val);
Adrian Roos04505652015-10-22 16:12:01 -0700294 private static native long nativeWriteFileDescriptor(long nativePtr, FileDescriptor val);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800295
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000296 private static native byte[] nativeCreateByteArray(long nativePtr);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700297 private static native byte[] nativeReadBlob(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700298 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000299 private static native int nativeReadInt(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700300 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000301 private static native long nativeReadLong(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700302 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000303 private static native float nativeReadFloat(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700304 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000305 private static native double nativeReadDouble(long nativePtr);
306 private static native String nativeReadString(long nativePtr);
307 private static native IBinder nativeReadStrongBinder(long nativePtr);
308 private static native FileDescriptor nativeReadFileDescriptor(long nativePtr);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800309
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000310 private static native long nativeCreate();
Adrian Roos04505652015-10-22 16:12:01 -0700311 private static native long nativeFreeBuffer(long nativePtr);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000312 private static native void nativeDestroy(long nativePtr);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800313
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000314 private static native byte[] nativeMarshall(long nativePtr);
Adrian Roos04505652015-10-22 16:12:01 -0700315 private static native long nativeUnmarshall(
John Spurlocke0852362015-02-04 15:47:40 -0500316 long nativePtr, byte[] data, int offset, int length);
Adrian Roos04505652015-10-22 16:12:01 -0700317 private static native long nativeAppendFrom(
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000318 long thisNativePtr, long otherNativePtr, int offset, int length);
John Reck71207b52016-09-28 13:28:09 -0700319 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000320 private static native boolean nativeHasFileDescriptors(long nativePtr);
321 private static native void nativeWriteInterfaceToken(long nativePtr, String interfaceName);
322 private static native void nativeEnforceInterface(long nativePtr, String interfaceName);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800323
Dan Sandleraa861662015-04-21 10:24:32 -0400324 private static native long nativeGetBlobAshmemSize(long nativePtr);
Dan Sandler5ce04302015-04-09 23:50:15 -0400325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 public final static Parcelable.Creator<String> STRING_CREATOR
327 = new Parcelable.Creator<String>() {
328 public String createFromParcel(Parcel source) {
329 return source.readString();
330 }
331 public String[] newArray(int size) {
332 return new String[size];
333 }
334 };
335
336 /**
337 * Retrieve a new Parcel object from the pool.
338 */
339 public static Parcel obtain() {
340 final Parcel[] pool = sOwnedPool;
341 synchronized (pool) {
342 Parcel p;
343 for (int i=0; i<POOL_SIZE; i++) {
344 p = pool[i];
345 if (p != null) {
346 pool[i] = null;
347 if (DEBUG_RECYCLE) {
348 p.mStack = new RuntimeException();
349 }
350 return p;
351 }
352 }
353 }
354 return new Parcel(0);
355 }
356
357 /**
358 * Put a Parcel object back into the pool. You must not touch
359 * the object after this call.
360 */
361 public final void recycle() {
362 if (DEBUG_RECYCLE) mStack = null;
363 freeBuffer();
Jeff Sharkey047238c2012-03-07 16:51:38 -0800364
365 final Parcel[] pool;
366 if (mOwnsNativeParcelObject) {
367 pool = sOwnedPool;
368 } else {
369 mNativePtr = 0;
370 pool = sHolderPool;
371 }
372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 synchronized (pool) {
374 for (int i=0; i<POOL_SIZE; i++) {
375 if (pool[i] == null) {
376 pool[i] = this;
377 return;
378 }
379 }
380 }
381 }
382
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800383 /** @hide */
384 public static native long getGlobalAllocSize();
385
386 /** @hide */
387 public static native long getGlobalAllocCount();
388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 /**
390 * Returns the total amount of data contained in the parcel.
391 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800392 public final int dataSize() {
393 return nativeDataSize(mNativePtr);
394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395
396 /**
397 * Returns the amount of data remaining to be read from the
398 * parcel. That is, {@link #dataSize}-{@link #dataPosition}.
399 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800400 public final int dataAvail() {
401 return nativeDataAvail(mNativePtr);
402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403
404 /**
405 * Returns the current position in the parcel data. Never
406 * more than {@link #dataSize}.
407 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800408 public final int dataPosition() {
409 return nativeDataPosition(mNativePtr);
410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411
412 /**
413 * Returns the total amount of space in the parcel. This is always
414 * >= {@link #dataSize}. The difference between it and dataSize() is the
415 * amount of room left until the parcel needs to re-allocate its
416 * data buffer.
417 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800418 public final int dataCapacity() {
419 return nativeDataCapacity(mNativePtr);
420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421
422 /**
423 * Change the amount of data in the parcel. Can be either smaller or
424 * larger than the current size. If larger than the current capacity,
425 * more memory will be allocated.
426 *
427 * @param size The new number of bytes in the Parcel.
428 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800429 public final void setDataSize(int size) {
Adrian Roos04505652015-10-22 16:12:01 -0700430 updateNativeSize(nativeSetDataSize(mNativePtr, size));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432
433 /**
434 * Move the current read/write position in the parcel.
435 * @param pos New offset in the parcel; must be between 0 and
436 * {@link #dataSize}.
437 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800438 public final void setDataPosition(int pos) {
439 nativeSetDataPosition(mNativePtr, pos);
440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441
442 /**
443 * Change the capacity (current available space) of the parcel.
444 *
445 * @param size The new capacity of the parcel, in bytes. Can not be
446 * less than {@link #dataSize} -- that is, you can not drop existing data
447 * with this method.
448 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800449 public final void setDataCapacity(int size) {
450 nativeSetDataCapacity(mNativePtr, size);
451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400453 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800454 public final boolean pushAllowFds(boolean allowFds) {
455 return nativePushAllowFds(mNativePtr, allowFds);
456 }
Dianne Hackbornc04db7e2011-10-03 21:09:35 -0700457
458 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800459 public final void restoreAllowFds(boolean lastValue) {
460 nativeRestoreAllowFds(mNativePtr, lastValue);
461 }
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 /**
464 * Returns the raw bytes of the parcel.
465 *
466 * <p class="note">The data you retrieve here <strong>must not</strong>
467 * be placed in any kind of persistent storage (on local disk, across
468 * a network, etc). For that, you should use standard serialization
469 * or another kind of general serialization mechanism. The Parcel
470 * marshalled representation is highly optimized for local IPC, and as
471 * such does not attempt to maintain compatibility with data created
472 * in different versions of the platform.
473 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800474 public final byte[] marshall() {
475 return nativeMarshall(mNativePtr);
476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477
478 /**
479 * Set the bytes in data to be the raw bytes of this Parcel.
480 */
John Spurlocke0852362015-02-04 15:47:40 -0500481 public final void unmarshall(byte[] data, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700482 updateNativeSize(nativeUnmarshall(mNativePtr, data, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484
Jeff Sharkey047238c2012-03-07 16:51:38 -0800485 public final void appendFrom(Parcel parcel, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700486 updateNativeSize(nativeAppendFrom(mNativePtr, parcel.mNativePtr, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488
489 /**
490 * Report whether the parcel contains any marshalled file descriptors.
491 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800492 public final boolean hasFileDescriptors() {
493 return nativeHasFileDescriptors(mNativePtr);
494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495
496 /**
497 * Store or read an IBinder interface token in the parcel at the current
498 * {@link #dataPosition}. This is used to validate that the marshalled
499 * transaction is intended for the target interface.
500 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800501 public final void writeInterfaceToken(String interfaceName) {
502 nativeWriteInterfaceToken(mNativePtr, interfaceName);
503 }
504
505 public final void enforceInterface(String interfaceName) {
506 nativeEnforceInterface(mNativePtr, interfaceName);
507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508
509 /**
Elliott Hughesa28b83e2011-02-28 14:26:13 -0800510 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 * growing {@link #dataCapacity} if needed.
512 * @param b Bytes to place into the parcel.
513 */
514 public final void writeByteArray(byte[] b) {
515 writeByteArray(b, 0, (b != null) ? b.length : 0);
516 }
517
518 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900519 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 * growing {@link #dataCapacity} if needed.
521 * @param b Bytes to place into the parcel.
522 * @param offset Index of first byte to be written.
523 * @param len Number of bytes to write.
524 */
525 public final void writeByteArray(byte[] b, int offset, int len) {
526 if (b == null) {
527 writeInt(-1);
528 return;
529 }
Elliott Hughesa28b83e2011-02-28 14:26:13 -0800530 Arrays.checkOffsetAndCount(b.length, offset, len);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800531 nativeWriteByteArray(mNativePtr, b, offset, len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 }
533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700535 * Write a blob of data into the parcel at the current {@link #dataPosition},
536 * growing {@link #dataCapacity} if needed.
537 * @param b Bytes to place into the parcel.
538 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -0700539 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700540 */
541 public final void writeBlob(byte[] b) {
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500542 writeBlob(b, 0, (b != null) ? b.length : 0);
543 }
544
545 /**
546 * Write a blob of data into the parcel at the current {@link #dataPosition},
547 * growing {@link #dataCapacity} if needed.
548 * @param b Bytes to place into the parcel.
549 * @param offset Index of first byte to be written.
550 * @param len Number of bytes to write.
551 * {@hide}
552 * {@SystemApi}
553 */
554 public final void writeBlob(byte[] b, int offset, int len) {
555 if (b == null) {
556 writeInt(-1);
557 return;
558 }
559 Arrays.checkOffsetAndCount(b.length, offset, len);
560 nativeWriteBlob(mNativePtr, b, offset, len);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700561 }
562
563 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 * Write an integer value into the parcel at the current dataPosition(),
565 * growing dataCapacity() if needed.
566 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800567 public final void writeInt(int val) {
568 nativeWriteInt(mNativePtr, val);
569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570
571 /**
572 * Write a long integer value into the parcel at the current dataPosition(),
573 * growing dataCapacity() if needed.
574 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800575 public final void writeLong(long val) {
576 nativeWriteLong(mNativePtr, val);
577 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578
579 /**
580 * Write a floating point value into the parcel at the current
581 * dataPosition(), growing dataCapacity() if needed.
582 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800583 public final void writeFloat(float val) {
584 nativeWriteFloat(mNativePtr, val);
585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586
587 /**
588 * Write a double precision floating point value into the parcel at the
589 * current dataPosition(), growing dataCapacity() if needed.
590 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800591 public final void writeDouble(double val) {
592 nativeWriteDouble(mNativePtr, val);
593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594
595 /**
596 * Write a string value into the parcel at the current dataPosition(),
597 * growing dataCapacity() if needed.
598 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800599 public final void writeString(String val) {
600 nativeWriteString(mNativePtr, val);
601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602
603 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000604 * Write a CharSequence value into the parcel at the current dataPosition(),
605 * growing dataCapacity() if needed.
606 * @hide
607 */
608 public final void writeCharSequence(CharSequence val) {
609 TextUtils.writeToParcel(val, this, 0);
610 }
611
612 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 * Write an object into the parcel at the current dataPosition(),
614 * growing dataCapacity() if needed.
615 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800616 public final void writeStrongBinder(IBinder val) {
617 nativeWriteStrongBinder(mNativePtr, val);
618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619
620 /**
621 * Write an object into the parcel at the current dataPosition(),
622 * growing dataCapacity() if needed.
623 */
624 public final void writeStrongInterface(IInterface val) {
625 writeStrongBinder(val == null ? null : val.asBinder());
626 }
627
628 /**
629 * Write a FileDescriptor into the parcel at the current dataPosition(),
630 * growing dataCapacity() if needed.
Dan Egnorb3e4ef32010-07-20 09:03:35 -0700631 *
632 * <p class="caution">The file descriptor will not be closed, which may
633 * result in file descriptor leaks when objects are returned from Binder
634 * calls. Use {@link ParcelFileDescriptor#writeToParcel} instead, which
635 * accepts contextual flags and will close the original file descriptor
636 * if {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} is set.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800638 public final void writeFileDescriptor(FileDescriptor val) {
Adrian Roos04505652015-10-22 16:12:01 -0700639 updateNativeSize(nativeWriteFileDescriptor(mNativePtr, val));
640 }
641
642 private void updateNativeSize(long newNativeSize) {
643 if (mOwnsNativeParcelObject) {
644 if (newNativeSize > Integer.MAX_VALUE) {
645 newNativeSize = Integer.MAX_VALUE;
646 }
647 if (newNativeSize != mNativeSize) {
648 int delta = (int) (newNativeSize - mNativeSize);
649 if (delta > 0) {
650 VMRuntime.getRuntime().registerNativeAllocation(delta);
651 } else {
652 VMRuntime.getRuntime().registerNativeFree(-delta);
653 }
654 mNativeSize = newNativeSize;
655 }
656 }
Jeff Sharkey047238c2012-03-07 16:51:38 -0800657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658
659 /**
Casey Dahlin2f974b22015-11-05 12:19:13 -0800660 * {@hide}
661 * This will be the new name for writeFileDescriptor, for consistency.
662 **/
663 public final void writeRawFileDescriptor(FileDescriptor val) {
664 nativeWriteFileDescriptor(mNativePtr, val);
665 }
666
667 /**
668 * {@hide}
669 * Write an array of FileDescriptor objects into the Parcel.
670 *
671 * @param value The array of objects to be written.
672 */
673 public final void writeRawFileDescriptorArray(FileDescriptor[] value) {
674 if (value != null) {
675 int N = value.length;
676 writeInt(N);
677 for (int i=0; i<N; i++) {
678 writeRawFileDescriptor(value[i]);
679 }
680 } else {
681 writeInt(-1);
682 }
683 }
684
685 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900686 * Write a byte value into the parcel at the current dataPosition(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 * growing dataCapacity() if needed.
688 */
689 public final void writeByte(byte val) {
690 writeInt(val);
691 }
692
693 /**
694 * Please use {@link #writeBundle} instead. Flattens a Map into the parcel
695 * at the current dataPosition(),
696 * growing dataCapacity() if needed. The Map keys must be String objects.
697 * The Map values are written using {@link #writeValue} and must follow
698 * the specification there.
Samuel Tana8036662015-11-23 14:36:00 -0800699 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 * <p>It is strongly recommended to use {@link #writeBundle} instead of
701 * this method, since the Bundle class provides a type-safe API that
702 * allows you to avoid mysterious type errors at the point of marshalling.
703 */
704 public final void writeMap(Map val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700705 writeMapInternal((Map<String, Object>) val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 }
707
708 /**
709 * Flatten a Map into the parcel at the current dataPosition(),
710 * growing dataCapacity() if needed. The Map keys must be String objects.
711 */
Dianne Hackborn6aff9052009-05-22 13:20:23 -0700712 /* package */ void writeMapInternal(Map<String,Object> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 if (val == null) {
714 writeInt(-1);
715 return;
716 }
717 Set<Map.Entry<String,Object>> entries = val.entrySet();
718 writeInt(entries.size());
719 for (Map.Entry<String,Object> e : entries) {
720 writeValue(e.getKey());
721 writeValue(e.getValue());
722 }
723 }
724
725 /**
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700726 * Flatten an ArrayMap into the parcel at the current dataPosition(),
727 * growing dataCapacity() if needed. The Map keys must be String objects.
728 */
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700729 /* package */ void writeArrayMapInternal(ArrayMap<String, Object> val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700730 if (val == null) {
731 writeInt(-1);
732 return;
733 }
Samuel Tan3cefe6a2015-12-14 13:29:17 -0800734 // Keep the format of this Parcel in sync with writeToParcelInner() in
735 // frameworks/native/libs/binder/PersistableBundle.cpp.
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700736 final int N = val.size();
737 writeInt(N);
Dianne Hackborne784d1e2013-09-20 18:13:52 -0700738 if (DEBUG_ARRAY_MAP) {
739 RuntimeException here = new RuntimeException("here");
740 here.fillInStackTrace();
741 Log.d(TAG, "Writing " + N + " ArrayMap entries", here);
742 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700743 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700744 for (int i=0; i<N; i++) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700745 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700746 writeString(val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700747 writeValue(val.valueAt(i));
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700748 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Write #" + i + " "
749 + (dataPosition()-startPos) + " bytes: key=0x"
750 + Integer.toHexString(val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0)
751 + " " + val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700752 }
753 }
754
755 /**
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700756 * @hide For testing only.
757 */
758 public void writeArrayMap(ArrayMap<String, Object> val) {
759 writeArrayMapInternal(val);
760 }
761
762 /**
Svet Ganovddb94882016-06-23 19:55:24 -0700763 * Write an array set to the parcel.
764 *
765 * @param val The array set to write.
766 *
767 * @hide
768 */
769 public void writeArraySet(@Nullable ArraySet<? extends Object> val) {
770 final int size = (val != null) ? val.size() : -1;
771 writeInt(size);
772 for (int i = 0; i < size; i++) {
773 writeValue(val.valueAt(i));
774 }
775 }
776
777 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 * Flatten a Bundle into the parcel at the current dataPosition(),
779 * growing dataCapacity() if needed.
780 */
781 public final void writeBundle(Bundle val) {
782 if (val == null) {
783 writeInt(-1);
784 return;
785 }
786
Dianne Hackborn6aff9052009-05-22 13:20:23 -0700787 val.writeToParcel(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 }
789
790 /**
Craig Mautner719e6b12014-04-04 20:29:41 -0700791 * Flatten a PersistableBundle into the parcel at the current dataPosition(),
792 * growing dataCapacity() if needed.
793 */
794 public final void writePersistableBundle(PersistableBundle val) {
795 if (val == null) {
796 writeInt(-1);
797 return;
798 }
799
800 val.writeToParcel(this, 0);
801 }
802
803 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -0700804 * Flatten a Size into the parcel at the current dataPosition(),
805 * growing dataCapacity() if needed.
806 */
807 public final void writeSize(Size val) {
808 writeInt(val.getWidth());
809 writeInt(val.getHeight());
810 }
811
812 /**
813 * Flatten a SizeF into the parcel at the current dataPosition(),
814 * growing dataCapacity() if needed.
815 */
816 public final void writeSizeF(SizeF val) {
817 writeFloat(val.getWidth());
818 writeFloat(val.getHeight());
819 }
820
821 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 * Flatten a List into the parcel at the current dataPosition(), growing
823 * dataCapacity() if needed. The List values are written using
824 * {@link #writeValue} and must follow the specification there.
825 */
826 public final void writeList(List val) {
827 if (val == null) {
828 writeInt(-1);
829 return;
830 }
831 int N = val.size();
832 int i=0;
833 writeInt(N);
834 while (i < N) {
835 writeValue(val.get(i));
836 i++;
837 }
838 }
839
840 /**
841 * Flatten an Object array into the parcel at the current dataPosition(),
842 * growing dataCapacity() if needed. The array values are written using
843 * {@link #writeValue} and must follow the specification there.
844 */
845 public final void writeArray(Object[] val) {
846 if (val == null) {
847 writeInt(-1);
848 return;
849 }
850 int N = val.length;
851 int i=0;
852 writeInt(N);
853 while (i < N) {
854 writeValue(val[i]);
855 i++;
856 }
857 }
858
859 /**
860 * Flatten a generic SparseArray into the parcel at the current
861 * dataPosition(), growing dataCapacity() if needed. The SparseArray
862 * values are written using {@link #writeValue} and must follow the
863 * specification there.
864 */
865 public final void writeSparseArray(SparseArray<Object> val) {
866 if (val == null) {
867 writeInt(-1);
868 return;
869 }
870 int N = val.size();
871 writeInt(N);
872 int i=0;
873 while (i < N) {
874 writeInt(val.keyAt(i));
875 writeValue(val.valueAt(i));
876 i++;
877 }
878 }
879
880 public final void writeSparseBooleanArray(SparseBooleanArray val) {
881 if (val == null) {
882 writeInt(-1);
883 return;
884 }
885 int N = val.size();
886 writeInt(N);
887 int i=0;
888 while (i < N) {
889 writeInt(val.keyAt(i));
890 writeByte((byte)(val.valueAt(i) ? 1 : 0));
891 i++;
892 }
893 }
894
Adam Lesinski4e862812016-11-21 16:02:24 -0800895 public final void writeSparseIntArray(SparseIntArray val) {
896 if (val == null) {
897 writeInt(-1);
898 return;
899 }
900 int N = val.size();
901 writeInt(N);
902 int i=0;
903 while (i < N) {
904 writeInt(val.keyAt(i));
905 writeInt(val.valueAt(i));
906 i++;
907 }
908 }
909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 public final void writeBooleanArray(boolean[] val) {
911 if (val != null) {
912 int N = val.length;
913 writeInt(N);
914 for (int i=0; i<N; i++) {
915 writeInt(val[i] ? 1 : 0);
916 }
917 } else {
918 writeInt(-1);
919 }
920 }
921
922 public final boolean[] createBooleanArray() {
923 int N = readInt();
924 // >>2 as a fast divide-by-4 works in the create*Array() functions
925 // because dataAvail() will never return a negative number. 4 is
926 // the size of a stored boolean in the stream.
927 if (N >= 0 && N <= (dataAvail() >> 2)) {
928 boolean[] val = new boolean[N];
929 for (int i=0; i<N; i++) {
930 val[i] = readInt() != 0;
931 }
932 return val;
933 } else {
934 return null;
935 }
936 }
937
938 public final void readBooleanArray(boolean[] val) {
939 int N = readInt();
940 if (N == val.length) {
941 for (int i=0; i<N; i++) {
942 val[i] = readInt() != 0;
943 }
944 } else {
945 throw new RuntimeException("bad array lengths");
946 }
947 }
948
949 public final void writeCharArray(char[] val) {
950 if (val != null) {
951 int N = val.length;
952 writeInt(N);
953 for (int i=0; i<N; i++) {
954 writeInt((int)val[i]);
955 }
956 } else {
957 writeInt(-1);
958 }
959 }
960
961 public final char[] createCharArray() {
962 int N = readInt();
963 if (N >= 0 && N <= (dataAvail() >> 2)) {
964 char[] val = new char[N];
965 for (int i=0; i<N; i++) {
966 val[i] = (char)readInt();
967 }
968 return val;
969 } else {
970 return null;
971 }
972 }
973
974 public final void readCharArray(char[] val) {
975 int N = readInt();
976 if (N == val.length) {
977 for (int i=0; i<N; i++) {
978 val[i] = (char)readInt();
979 }
980 } else {
981 throw new RuntimeException("bad array lengths");
982 }
983 }
984
985 public final void writeIntArray(int[] val) {
986 if (val != null) {
987 int N = val.length;
988 writeInt(N);
989 for (int i=0; i<N; i++) {
990 writeInt(val[i]);
991 }
992 } else {
993 writeInt(-1);
994 }
995 }
996
997 public final int[] createIntArray() {
998 int N = readInt();
999 if (N >= 0 && N <= (dataAvail() >> 2)) {
1000 int[] val = new int[N];
1001 for (int i=0; i<N; i++) {
1002 val[i] = readInt();
1003 }
1004 return val;
1005 } else {
1006 return null;
1007 }
1008 }
1009
1010 public final void readIntArray(int[] val) {
1011 int N = readInt();
1012 if (N == val.length) {
1013 for (int i=0; i<N; i++) {
1014 val[i] = readInt();
1015 }
1016 } else {
1017 throw new RuntimeException("bad array lengths");
1018 }
1019 }
1020
1021 public final void writeLongArray(long[] val) {
1022 if (val != null) {
1023 int N = val.length;
1024 writeInt(N);
1025 for (int i=0; i<N; i++) {
1026 writeLong(val[i]);
1027 }
1028 } else {
1029 writeInt(-1);
1030 }
1031 }
1032
1033 public final long[] createLongArray() {
1034 int N = readInt();
1035 // >>3 because stored longs are 64 bits
1036 if (N >= 0 && N <= (dataAvail() >> 3)) {
1037 long[] val = new long[N];
1038 for (int i=0; i<N; i++) {
1039 val[i] = readLong();
1040 }
1041 return val;
1042 } else {
1043 return null;
1044 }
1045 }
1046
1047 public final void readLongArray(long[] val) {
1048 int N = readInt();
1049 if (N == val.length) {
1050 for (int i=0; i<N; i++) {
1051 val[i] = readLong();
1052 }
1053 } else {
1054 throw new RuntimeException("bad array lengths");
1055 }
1056 }
1057
1058 public final void writeFloatArray(float[] val) {
1059 if (val != null) {
1060 int N = val.length;
1061 writeInt(N);
1062 for (int i=0; i<N; i++) {
1063 writeFloat(val[i]);
1064 }
1065 } else {
1066 writeInt(-1);
1067 }
1068 }
1069
1070 public final float[] createFloatArray() {
1071 int N = readInt();
1072 // >>2 because stored floats are 4 bytes
1073 if (N >= 0 && N <= (dataAvail() >> 2)) {
1074 float[] val = new float[N];
1075 for (int i=0; i<N; i++) {
1076 val[i] = readFloat();
1077 }
1078 return val;
1079 } else {
1080 return null;
1081 }
1082 }
1083
1084 public final void readFloatArray(float[] val) {
1085 int N = readInt();
1086 if (N == val.length) {
1087 for (int i=0; i<N; i++) {
1088 val[i] = readFloat();
1089 }
1090 } else {
1091 throw new RuntimeException("bad array lengths");
1092 }
1093 }
1094
1095 public final void writeDoubleArray(double[] val) {
1096 if (val != null) {
1097 int N = val.length;
1098 writeInt(N);
1099 for (int i=0; i<N; i++) {
1100 writeDouble(val[i]);
1101 }
1102 } else {
1103 writeInt(-1);
1104 }
1105 }
1106
1107 public final double[] createDoubleArray() {
1108 int N = readInt();
1109 // >>3 because stored doubles are 8 bytes
1110 if (N >= 0 && N <= (dataAvail() >> 3)) {
1111 double[] val = new double[N];
1112 for (int i=0; i<N; i++) {
1113 val[i] = readDouble();
1114 }
1115 return val;
1116 } else {
1117 return null;
1118 }
1119 }
1120
1121 public final void readDoubleArray(double[] val) {
1122 int N = readInt();
1123 if (N == val.length) {
1124 for (int i=0; i<N; i++) {
1125 val[i] = readDouble();
1126 }
1127 } else {
1128 throw new RuntimeException("bad array lengths");
1129 }
1130 }
1131
1132 public final void writeStringArray(String[] val) {
1133 if (val != null) {
1134 int N = val.length;
1135 writeInt(N);
1136 for (int i=0; i<N; i++) {
1137 writeString(val[i]);
1138 }
1139 } else {
1140 writeInt(-1);
1141 }
1142 }
1143
1144 public final String[] createStringArray() {
1145 int N = readInt();
1146 if (N >= 0) {
1147 String[] val = new String[N];
1148 for (int i=0; i<N; i++) {
1149 val[i] = readString();
1150 }
1151 return val;
1152 } else {
1153 return null;
1154 }
1155 }
1156
1157 public final void readStringArray(String[] val) {
1158 int N = readInt();
1159 if (N == val.length) {
1160 for (int i=0; i<N; i++) {
1161 val[i] = readString();
1162 }
1163 } else {
1164 throw new RuntimeException("bad array lengths");
1165 }
1166 }
1167
1168 public final void writeBinderArray(IBinder[] val) {
1169 if (val != null) {
1170 int N = val.length;
1171 writeInt(N);
1172 for (int i=0; i<N; i++) {
1173 writeStrongBinder(val[i]);
1174 }
1175 } else {
1176 writeInt(-1);
1177 }
1178 }
1179
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001180 /**
1181 * @hide
1182 */
1183 public final void writeCharSequenceArray(CharSequence[] val) {
1184 if (val != null) {
1185 int N = val.length;
1186 writeInt(N);
1187 for (int i=0; i<N; i++) {
1188 writeCharSequence(val[i]);
1189 }
1190 } else {
1191 writeInt(-1);
1192 }
1193 }
1194
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001195 /**
1196 * @hide
1197 */
1198 public final void writeCharSequenceList(ArrayList<CharSequence> val) {
1199 if (val != null) {
1200 int N = val.size();
1201 writeInt(N);
1202 for (int i=0; i<N; i++) {
1203 writeCharSequence(val.get(i));
1204 }
1205 } else {
1206 writeInt(-1);
1207 }
1208 }
1209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 public final IBinder[] createBinderArray() {
1211 int N = readInt();
1212 if (N >= 0) {
1213 IBinder[] val = new IBinder[N];
1214 for (int i=0; i<N; i++) {
1215 val[i] = readStrongBinder();
1216 }
1217 return val;
1218 } else {
1219 return null;
1220 }
1221 }
1222
1223 public final void readBinderArray(IBinder[] val) {
1224 int N = readInt();
1225 if (N == val.length) {
1226 for (int i=0; i<N; i++) {
1227 val[i] = readStrongBinder();
1228 }
1229 } else {
1230 throw new RuntimeException("bad array lengths");
1231 }
1232 }
1233
1234 /**
1235 * Flatten a List containing a particular object type into the parcel, at
1236 * the current dataPosition() and growing dataCapacity() if needed. The
1237 * type of the objects in the list must be one that implements Parcelable.
1238 * Unlike the generic writeList() method, however, only the raw data of the
1239 * objects is written and not their type, so you must use the corresponding
1240 * readTypedList() to unmarshall them.
1241 *
1242 * @param val The list of objects to be written.
1243 *
1244 * @see #createTypedArrayList
1245 * @see #readTypedList
1246 * @see Parcelable
1247 */
1248 public final <T extends Parcelable> void writeTypedList(List<T> val) {
1249 if (val == null) {
1250 writeInt(-1);
1251 return;
1252 }
1253 int N = val.size();
1254 int i=0;
1255 writeInt(N);
1256 while (i < N) {
1257 T item = val.get(i);
1258 if (item != null) {
1259 writeInt(1);
1260 item.writeToParcel(this, 0);
1261 } else {
1262 writeInt(0);
1263 }
1264 i++;
1265 }
1266 }
1267
1268 /**
1269 * Flatten a List containing String objects into the parcel, at
1270 * the current dataPosition() and growing dataCapacity() if needed. They
1271 * can later be retrieved with {@link #createStringArrayList} or
1272 * {@link #readStringList}.
1273 *
1274 * @param val The list of strings to be written.
1275 *
1276 * @see #createStringArrayList
1277 * @see #readStringList
1278 */
1279 public final void writeStringList(List<String> val) {
1280 if (val == null) {
1281 writeInt(-1);
1282 return;
1283 }
1284 int N = val.size();
1285 int i=0;
1286 writeInt(N);
1287 while (i < N) {
1288 writeString(val.get(i));
1289 i++;
1290 }
1291 }
1292
1293 /**
1294 * Flatten a List containing IBinder objects into the parcel, at
1295 * the current dataPosition() and growing dataCapacity() if needed. They
1296 * can later be retrieved with {@link #createBinderArrayList} or
1297 * {@link #readBinderList}.
1298 *
1299 * @param val The list of strings to be written.
1300 *
1301 * @see #createBinderArrayList
1302 * @see #readBinderList
1303 */
1304 public final void writeBinderList(List<IBinder> val) {
1305 if (val == null) {
1306 writeInt(-1);
1307 return;
1308 }
1309 int N = val.size();
1310 int i=0;
1311 writeInt(N);
1312 while (i < N) {
1313 writeStrongBinder(val.get(i));
1314 i++;
1315 }
1316 }
1317
1318 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00001319 * Flatten a {@code List} containing arbitrary {@code Parcelable} objects into this parcel
1320 * at the current position. They can later be retrieved using
1321 * {@link #readParcelableList(List, ClassLoader)} if required.
1322 *
1323 * @see #readParcelableList(List, ClassLoader)
1324 * @hide
1325 */
1326 public final <T extends Parcelable> void writeParcelableList(List<T> val, int flags) {
1327 if (val == null) {
1328 writeInt(-1);
1329 return;
1330 }
1331
1332 int N = val.size();
1333 int i=0;
1334 writeInt(N);
1335 while (i < N) {
1336 writeParcelable(val.get(i), flags);
1337 i++;
1338 }
1339 }
1340
1341 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 * Flatten a heterogeneous array containing a particular object type into
1343 * the parcel, at
1344 * the current dataPosition() and growing dataCapacity() if needed. The
1345 * type of the objects in the array must be one that implements Parcelable.
1346 * Unlike the {@link #writeParcelableArray} method, however, only the
1347 * raw data of the objects is written and not their type, so you must use
1348 * {@link #readTypedArray} with the correct corresponding
1349 * {@link Parcelable.Creator} implementation to unmarshall them.
1350 *
1351 * @param val The array of objects to be written.
1352 * @param parcelableFlags Contextual flags as per
1353 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1354 *
1355 * @see #readTypedArray
1356 * @see #writeParcelableArray
1357 * @see Parcelable.Creator
1358 */
1359 public final <T extends Parcelable> void writeTypedArray(T[] val,
1360 int parcelableFlags) {
1361 if (val != null) {
1362 int N = val.length;
1363 writeInt(N);
1364 for (int i=0; i<N; i++) {
1365 T item = val[i];
1366 if (item != null) {
1367 writeInt(1);
1368 item.writeToParcel(this, parcelableFlags);
1369 } else {
1370 writeInt(0);
1371 }
1372 }
1373 } else {
1374 writeInt(-1);
1375 }
1376 }
1377
1378 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001379 * Flatten the Parcelable object into the parcel.
1380 *
1381 * @param val The Parcelable object to be written.
1382 * @param parcelableFlags Contextual flags as per
1383 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1384 *
1385 * @see #readTypedObject
1386 */
1387 public final <T extends Parcelable> void writeTypedObject(T val, int parcelableFlags) {
1388 if (val != null) {
1389 writeInt(1);
1390 val.writeToParcel(this, parcelableFlags);
1391 } else {
1392 writeInt(0);
1393 }
1394 }
1395
1396 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 * Flatten a generic object in to a parcel. The given Object value may
1398 * currently be one of the following types:
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001399 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 * <ul>
1401 * <li> null
1402 * <li> String
1403 * <li> Byte
1404 * <li> Short
1405 * <li> Integer
1406 * <li> Long
1407 * <li> Float
1408 * <li> Double
1409 * <li> Boolean
1410 * <li> String[]
1411 * <li> boolean[]
1412 * <li> byte[]
1413 * <li> int[]
1414 * <li> long[]
1415 * <li> Object[] (supporting objects of the same type defined here).
1416 * <li> {@link Bundle}
1417 * <li> Map (as supported by {@link #writeMap}).
1418 * <li> Any object that implements the {@link Parcelable} protocol.
1419 * <li> Parcelable[]
1420 * <li> CharSequence (as supported by {@link TextUtils#writeToParcel}).
1421 * <li> List (as supported by {@link #writeList}).
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001422 * <li> {@link SparseArray} (as supported by {@link #writeSparseArray(SparseArray)}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 * <li> {@link IBinder}
1424 * <li> Any object that implements Serializable (but see
1425 * {@link #writeSerializable} for caveats). Note that all of the
1426 * previous types have relatively efficient implementations for
1427 * writing to a Parcel; having to rely on the generic serialization
1428 * approach is much less efficient and should be avoided whenever
1429 * possible.
1430 * </ul>
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001431 *
1432 * <p class="caution">{@link Parcelable} objects are written with
1433 * {@link Parcelable#writeToParcel} using contextual flags of 0. When
1434 * serializing objects containing {@link ParcelFileDescriptor}s,
1435 * this may result in file descriptor leaks when they are returned from
1436 * Binder calls (where {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE}
1437 * should be used).</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 */
1439 public final void writeValue(Object v) {
1440 if (v == null) {
1441 writeInt(VAL_NULL);
1442 } else if (v instanceof String) {
1443 writeInt(VAL_STRING);
1444 writeString((String) v);
1445 } else if (v instanceof Integer) {
1446 writeInt(VAL_INTEGER);
1447 writeInt((Integer) v);
1448 } else if (v instanceof Map) {
1449 writeInt(VAL_MAP);
1450 writeMap((Map) v);
1451 } else if (v instanceof Bundle) {
1452 // Must be before Parcelable
1453 writeInt(VAL_BUNDLE);
1454 writeBundle((Bundle) v);
Samuel Tanceafe5e2015-12-11 16:50:58 -08001455 } else if (v instanceof PersistableBundle) {
1456 writeInt(VAL_PERSISTABLEBUNDLE);
1457 writePersistableBundle((PersistableBundle) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 } else if (v instanceof Parcelable) {
Samuel Tanceafe5e2015-12-11 16:50:58 -08001459 // IMPOTANT: cases for classes that implement Parcelable must
1460 // come before the Parcelable case, so that their specific VAL_*
1461 // types will be written.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 writeInt(VAL_PARCELABLE);
1463 writeParcelable((Parcelable) v, 0);
1464 } else if (v instanceof Short) {
1465 writeInt(VAL_SHORT);
1466 writeInt(((Short) v).intValue());
1467 } else if (v instanceof Long) {
1468 writeInt(VAL_LONG);
1469 writeLong((Long) v);
1470 } else if (v instanceof Float) {
1471 writeInt(VAL_FLOAT);
1472 writeFloat((Float) v);
1473 } else if (v instanceof Double) {
1474 writeInt(VAL_DOUBLE);
1475 writeDouble((Double) v);
1476 } else if (v instanceof Boolean) {
1477 writeInt(VAL_BOOLEAN);
1478 writeInt((Boolean) v ? 1 : 0);
1479 } else if (v instanceof CharSequence) {
1480 // Must be after String
1481 writeInt(VAL_CHARSEQUENCE);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001482 writeCharSequence((CharSequence) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 } else if (v instanceof List) {
1484 writeInt(VAL_LIST);
1485 writeList((List) v);
1486 } else if (v instanceof SparseArray) {
1487 writeInt(VAL_SPARSEARRAY);
1488 writeSparseArray((SparseArray) v);
1489 } else if (v instanceof boolean[]) {
1490 writeInt(VAL_BOOLEANARRAY);
1491 writeBooleanArray((boolean[]) v);
1492 } else if (v instanceof byte[]) {
1493 writeInt(VAL_BYTEARRAY);
1494 writeByteArray((byte[]) v);
1495 } else if (v instanceof String[]) {
1496 writeInt(VAL_STRINGARRAY);
1497 writeStringArray((String[]) v);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001498 } else if (v instanceof CharSequence[]) {
1499 // Must be after String[] and before Object[]
1500 writeInt(VAL_CHARSEQUENCEARRAY);
1501 writeCharSequenceArray((CharSequence[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 } else if (v instanceof IBinder) {
1503 writeInt(VAL_IBINDER);
1504 writeStrongBinder((IBinder) v);
1505 } else if (v instanceof Parcelable[]) {
1506 writeInt(VAL_PARCELABLEARRAY);
1507 writeParcelableArray((Parcelable[]) v, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 } else if (v instanceof int[]) {
1509 writeInt(VAL_INTARRAY);
1510 writeIntArray((int[]) v);
1511 } else if (v instanceof long[]) {
1512 writeInt(VAL_LONGARRAY);
1513 writeLongArray((long[]) v);
1514 } else if (v instanceof Byte) {
1515 writeInt(VAL_BYTE);
1516 writeInt((Byte) v);
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001517 } else if (v instanceof Size) {
1518 writeInt(VAL_SIZE);
1519 writeSize((Size) v);
1520 } else if (v instanceof SizeF) {
1521 writeInt(VAL_SIZEF);
1522 writeSizeF((SizeF) v);
Samuel Tana8036662015-11-23 14:36:00 -08001523 } else if (v instanceof double[]) {
1524 writeInt(VAL_DOUBLEARRAY);
1525 writeDoubleArray((double[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 } else {
Paul Duffinac5a0822014-02-03 15:02:17 +00001527 Class<?> clazz = v.getClass();
1528 if (clazz.isArray() && clazz.getComponentType() == Object.class) {
1529 // Only pure Object[] are written here, Other arrays of non-primitive types are
1530 // handled by serialization as this does not record the component type.
1531 writeInt(VAL_OBJECTARRAY);
1532 writeArray((Object[]) v);
1533 } else if (v instanceof Serializable) {
1534 // Must be last
1535 writeInt(VAL_SERIALIZABLE);
1536 writeSerializable((Serializable) v);
1537 } else {
1538 throw new RuntimeException("Parcel: unable to marshal value " + v);
1539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 }
1541 }
1542
1543 /**
1544 * Flatten the name of the class of the Parcelable and its contents
1545 * into the parcel.
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001546 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 * @param p The Parcelable object to be written.
1548 * @param parcelableFlags Contextual flags as per
1549 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1550 */
1551 public final void writeParcelable(Parcelable p, int parcelableFlags) {
1552 if (p == null) {
1553 writeString(null);
1554 return;
1555 }
Neil Fuller44e440c2015-04-20 14:39:00 +01001556 writeParcelableCreator(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 p.writeToParcel(this, parcelableFlags);
1558 }
1559
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001560 /** @hide */
1561 public final void writeParcelableCreator(Parcelable p) {
1562 String name = p.getClass().getName();
1563 writeString(name);
1564 }
1565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 /**
1567 * Write a generic serializable object in to a Parcel. It is strongly
1568 * recommended that this method be avoided, since the serialization
1569 * overhead is extremely large, and this approach will be much slower than
1570 * using the other approaches to writing data in to a Parcel.
1571 */
1572 public final void writeSerializable(Serializable s) {
1573 if (s == null) {
1574 writeString(null);
1575 return;
1576 }
1577 String name = s.getClass().getName();
1578 writeString(name);
1579
1580 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1581 try {
1582 ObjectOutputStream oos = new ObjectOutputStream(baos);
1583 oos.writeObject(s);
1584 oos.close();
1585
1586 writeByteArray(baos.toByteArray());
1587 } catch (IOException ioe) {
1588 throw new RuntimeException("Parcelable encountered " +
1589 "IOException writing serializable object (name = " + name +
1590 ")", ioe);
1591 }
1592 }
1593
1594 /**
1595 * Special function for writing an exception result at the header of
1596 * a parcel, to be used when returning an exception from a transaction.
1597 * Note that this currently only supports a few exception types; any other
1598 * exception will be re-thrown by this function as a RuntimeException
1599 * (to be caught by the system's last-resort exception handling when
1600 * dispatching a transaction).
Samuel Tana8036662015-11-23 14:36:00 -08001601 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 * <p>The supported exception types are:
1603 * <ul>
1604 * <li>{@link BadParcelableException}
1605 * <li>{@link IllegalArgumentException}
1606 * <li>{@link IllegalStateException}
1607 * <li>{@link NullPointerException}
1608 * <li>{@link SecurityException}
Dianne Hackborn7e714422013-09-13 17:32:57 -07001609 * <li>{@link NetworkOnMainThreadException}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 * </ul>
Samuel Tana8036662015-11-23 14:36:00 -08001611 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 * @param e The Exception to be written.
1613 *
1614 * @see #writeNoException
1615 * @see #readException
1616 */
1617 public final void writeException(Exception e) {
1618 int code = 0;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001619 if (e instanceof Parcelable
1620 && (e.getClass().getClassLoader() == Parcelable.class.getClassLoader())) {
1621 // We only send Parcelable exceptions that are in the
1622 // BootClassLoader to ensure that the receiver can unpack them
1623 code = EX_PARCELABLE;
1624 } else if (e instanceof SecurityException) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 code = EX_SECURITY;
1626 } else if (e instanceof BadParcelableException) {
1627 code = EX_BAD_PARCELABLE;
1628 } else if (e instanceof IllegalArgumentException) {
1629 code = EX_ILLEGAL_ARGUMENT;
1630 } else if (e instanceof NullPointerException) {
1631 code = EX_NULL_POINTER;
1632 } else if (e instanceof IllegalStateException) {
1633 code = EX_ILLEGAL_STATE;
Dianne Hackborn7e714422013-09-13 17:32:57 -07001634 } else if (e instanceof NetworkOnMainThreadException) {
1635 code = EX_NETWORK_MAIN_THREAD;
Dianne Hackborn33d738a2014-09-12 14:23:58 -07001636 } else if (e instanceof UnsupportedOperationException) {
1637 code = EX_UNSUPPORTED_OPERATION;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001638 } else if (e instanceof ServiceSpecificException) {
1639 code = EX_SERVICE_SPECIFIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 }
1641 writeInt(code);
Brad Fitzpatrick703e5d32010-07-15 13:16:41 -07001642 StrictMode.clearGatheredViolations();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 if (code == 0) {
1644 if (e instanceof RuntimeException) {
1645 throw (RuntimeException) e;
1646 }
1647 throw new RuntimeException(e);
1648 }
1649 writeString(e.getMessage());
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001650 switch (code) {
1651 case EX_SERVICE_SPECIFIC:
1652 writeInt(((ServiceSpecificException) e).errorCode);
1653 break;
1654 case EX_PARCELABLE:
1655 // Write parceled exception prefixed by length
1656 final int sizePosition = dataPosition();
1657 writeInt(0);
1658 writeParcelable((Parcelable) e, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
1659 final int payloadPosition = dataPosition();
1660 setDataPosition(sizePosition);
1661 writeInt(payloadPosition - sizePosition);
1662 setDataPosition(payloadPosition);
1663 break;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 }
1666
1667 /**
1668 * Special function for writing information at the front of the Parcel
1669 * indicating that no exception occurred.
1670 *
1671 * @see #writeException
1672 * @see #readException
1673 */
1674 public final void writeNoException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001675 // Despite the name of this function ("write no exception"),
1676 // it should instead be thought of as "write the RPC response
1677 // header", but because this function name is written out by
1678 // the AIDL compiler, we're not going to rename it.
1679 //
1680 // The response header, in the non-exception case (see also
1681 // writeException above, also called by the AIDL compiler), is
1682 // either a 0 (the default case), or EX_HAS_REPLY_HEADER if
1683 // StrictMode has gathered up violations that have occurred
1684 // during a Binder call, in which case we write out the number
1685 // of violations and their details, serialized, before the
1686 // actual RPC respons data. The receiving end of this is
1687 // readException(), below.
1688 if (StrictMode.hasGatheredViolations()) {
1689 writeInt(EX_HAS_REPLY_HEADER);
1690 final int sizePosition = dataPosition();
1691 writeInt(0); // total size of fat header, to be filled in later
1692 StrictMode.writeGatheredViolationsToParcel(this);
1693 final int payloadPosition = dataPosition();
1694 setDataPosition(sizePosition);
1695 writeInt(payloadPosition - sizePosition); // header size
1696 setDataPosition(payloadPosition);
1697 } else {
1698 writeInt(0);
1699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 }
1701
1702 /**
1703 * Special function for reading an exception result from the header of
1704 * a parcel, to be used after receiving the result of a transaction. This
1705 * will throw the exception for you if it had been written to the Parcel,
1706 * otherwise return and let you read the normal result data from the Parcel.
1707 *
1708 * @see #writeException
1709 * @see #writeNoException
1710 */
1711 public final void readException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001712 int code = readExceptionCode();
1713 if (code != 0) {
1714 String msg = readString();
1715 readException(code, msg);
1716 }
1717 }
1718
1719 /**
1720 * Parses the header of a Binder call's response Parcel and
1721 * returns the exception code. Deals with lite or fat headers.
1722 * In the common successful case, this header is generally zero.
1723 * In less common cases, it's a small negative number and will be
1724 * followed by an error string.
1725 *
1726 * This exists purely for android.database.DatabaseUtils and
1727 * insulating it from having to handle fat headers as returned by
1728 * e.g. StrictMode-induced RPC responses.
1729 *
1730 * @hide
1731 */
1732 public final int readExceptionCode() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 int code = readInt();
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001734 if (code == EX_HAS_REPLY_HEADER) {
1735 int headerSize = readInt();
1736 if (headerSize == 0) {
1737 Log.e(TAG, "Unexpected zero-sized Parcel reply header.");
1738 } else {
1739 // Currently the only thing in the header is StrictMode stacks,
1740 // but discussions around event/RPC tracing suggest we might
1741 // put that here too. If so, switch on sub-header tags here.
1742 // But for now, just parse out the StrictMode stuff.
1743 StrictMode.readAndHandleBinderCallViolations(this);
1744 }
1745 // And fat response headers are currently only used when
1746 // there are no exceptions, so return no error:
1747 return 0;
1748 }
1749 return code;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 }
1751
1752 /**
Mark Doliner879ea452014-01-02 12:38:07 -08001753 * Throw an exception with the given message. Not intended for use
1754 * outside the Parcel class.
1755 *
1756 * @param code Used to determine which exception class to throw.
1757 * @param msg The exception message.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 */
1759 public final void readException(int code, String msg) {
1760 switch (code) {
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001761 case EX_PARCELABLE:
1762 if (readInt() > 0) {
1763 SneakyThrow.sneakyThrow(
1764 (Exception) readParcelable(Parcelable.class.getClassLoader()));
1765 } else {
1766 throw new RuntimeException(msg + " [missing Parcelable]");
1767 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 case EX_SECURITY:
1769 throw new SecurityException(msg);
1770 case EX_BAD_PARCELABLE:
1771 throw new BadParcelableException(msg);
1772 case EX_ILLEGAL_ARGUMENT:
1773 throw new IllegalArgumentException(msg);
1774 case EX_NULL_POINTER:
1775 throw new NullPointerException(msg);
1776 case EX_ILLEGAL_STATE:
1777 throw new IllegalStateException(msg);
Dianne Hackborn7e714422013-09-13 17:32:57 -07001778 case EX_NETWORK_MAIN_THREAD:
1779 throw new NetworkOnMainThreadException();
Dianne Hackborn33d738a2014-09-12 14:23:58 -07001780 case EX_UNSUPPORTED_OPERATION:
1781 throw new UnsupportedOperationException(msg);
Christopher Wiley80fd1202015-11-22 17:12:37 -08001782 case EX_SERVICE_SPECIFIC:
1783 throw new ServiceSpecificException(readInt(), msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 }
1785 throw new RuntimeException("Unknown exception code: " + code
1786 + " msg " + msg);
1787 }
1788
1789 /**
1790 * Read an integer value from the parcel at the current dataPosition().
1791 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001792 public final int readInt() {
1793 return nativeReadInt(mNativePtr);
1794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795
1796 /**
1797 * Read a long integer value from the parcel at the current dataPosition().
1798 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001799 public final long readLong() {
1800 return nativeReadLong(mNativePtr);
1801 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802
1803 /**
1804 * Read a floating point value from the parcel at the current
1805 * dataPosition().
1806 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001807 public final float readFloat() {
1808 return nativeReadFloat(mNativePtr);
1809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810
1811 /**
1812 * Read a double precision floating point value from the parcel at the
1813 * current dataPosition().
1814 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001815 public final double readDouble() {
1816 return nativeReadDouble(mNativePtr);
1817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818
1819 /**
1820 * Read a string value from the parcel at the current dataPosition().
1821 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001822 public final String readString() {
1823 return nativeReadString(mNativePtr);
1824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825
1826 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001827 * Read a CharSequence value from the parcel at the current dataPosition().
1828 * @hide
1829 */
1830 public final CharSequence readCharSequence() {
1831 return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(this);
1832 }
1833
1834 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 * Read an object from the parcel at the current dataPosition().
1836 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08001837 public final IBinder readStrongBinder() {
1838 return nativeReadStrongBinder(mNativePtr);
1839 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840
1841 /**
1842 * Read a FileDescriptor from the parcel at the current dataPosition().
1843 */
1844 public final ParcelFileDescriptor readFileDescriptor() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08001845 FileDescriptor fd = nativeReadFileDescriptor(mNativePtr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 return fd != null ? new ParcelFileDescriptor(fd) : null;
1847 }
1848
Jeff Sharkeyda5a3e12013-08-11 12:54:42 -07001849 /** {@hide} */
1850 public final FileDescriptor readRawFileDescriptor() {
1851 return nativeReadFileDescriptor(mNativePtr);
1852 }
1853
Casey Dahlin2f974b22015-11-05 12:19:13 -08001854 /**
1855 * {@hide}
1856 * Read and return a new array of FileDescriptors from the parcel.
1857 * @return the FileDescriptor array, or null if the array is null.
1858 **/
1859 public final FileDescriptor[] createRawFileDescriptorArray() {
1860 int N = readInt();
1861 if (N < 0) {
1862 return null;
1863 }
1864 FileDescriptor[] f = new FileDescriptor[N];
1865 for (int i = 0; i < N; i++) {
1866 f[i] = readRawFileDescriptor();
1867 }
1868 return f;
1869 }
1870
1871 /**
1872 * {@hide}
1873 * Read an array of FileDescriptors from a parcel.
1874 * The passed array must be exactly the length of the array in the parcel.
1875 * @return the FileDescriptor array, or null if the array is null.
1876 **/
1877 public final void readRawFileDescriptorArray(FileDescriptor[] val) {
1878 int N = readInt();
1879 if (N == val.length) {
1880 for (int i=0; i<N; i++) {
1881 val[i] = readRawFileDescriptor();
1882 }
1883 } else {
1884 throw new RuntimeException("bad array lengths");
1885 }
1886 }
1887
1888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 /*package*/ static native FileDescriptor openFileDescriptor(String file,
1890 int mode) throws FileNotFoundException;
Dianne Hackborn9a849832011-04-07 15:11:57 -07001891 /*package*/ static native FileDescriptor dupFileDescriptor(FileDescriptor orig)
1892 throws IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 /*package*/ static native void closeFileDescriptor(FileDescriptor desc)
1894 throws IOException;
Dianne Hackbornc9119f52011-02-28 18:03:26 -08001895 /*package*/ static native void clearFileDescriptor(FileDescriptor desc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896
1897 /**
1898 * Read a byte value from the parcel at the current dataPosition().
1899 */
1900 public final byte readByte() {
1901 return (byte)(readInt() & 0xff);
1902 }
1903
1904 /**
1905 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
1906 * been written with {@link #writeBundle}. Read into an existing Map object
1907 * from the parcel at the current dataPosition().
1908 */
1909 public final void readMap(Map outVal, ClassLoader loader) {
1910 int N = readInt();
1911 readMapInternal(outVal, N, loader);
1912 }
1913
1914 /**
1915 * Read into an existing List object from the parcel at the current
1916 * dataPosition(), using the given class loader to load any enclosed
1917 * Parcelables. If it is null, the default class loader is used.
1918 */
1919 public final void readList(List outVal, ClassLoader loader) {
1920 int N = readInt();
1921 readListInternal(outVal, N, loader);
1922 }
1923
1924 /**
1925 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
1926 * been written with {@link #writeBundle}. Read and return a new HashMap
1927 * object from the parcel at the current dataPosition(), using the given
1928 * class loader to load any enclosed Parcelables. Returns null if
1929 * the previously written map object was null.
1930 */
1931 public final HashMap readHashMap(ClassLoader loader)
1932 {
1933 int N = readInt();
1934 if (N < 0) {
1935 return null;
1936 }
1937 HashMap m = new HashMap(N);
1938 readMapInternal(m, N, loader);
1939 return m;
1940 }
1941
1942 /**
1943 * Read and return a new Bundle object from the parcel at the current
1944 * dataPosition(). Returns null if the previously written Bundle object was
1945 * null.
1946 */
1947 public final Bundle readBundle() {
1948 return readBundle(null);
1949 }
1950
1951 /**
1952 * Read and return a new Bundle object from the parcel at the current
1953 * dataPosition(), using the given class loader to initialize the class
1954 * loader of the Bundle for later retrieval of Parcelable objects.
1955 * Returns null if the previously written Bundle object was null.
1956 */
1957 public final Bundle readBundle(ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 int length = readInt();
1959 if (length < 0) {
Dianne Hackborn4a7d8242013-10-03 10:19:20 -07001960 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 return null;
1962 }
Samuel Tana8036662015-11-23 14:36:00 -08001963
Dianne Hackborn6aff9052009-05-22 13:20:23 -07001964 final Bundle bundle = new Bundle(this, length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 if (loader != null) {
1966 bundle.setClassLoader(loader);
1967 }
1968 return bundle;
1969 }
1970
1971 /**
Craig Mautner719e6b12014-04-04 20:29:41 -07001972 * Read and return a new Bundle object from the parcel at the current
1973 * dataPosition(). Returns null if the previously written Bundle object was
1974 * null.
1975 */
1976 public final PersistableBundle readPersistableBundle() {
1977 return readPersistableBundle(null);
1978 }
1979
1980 /**
1981 * Read and return a new Bundle object from the parcel at the current
1982 * dataPosition(), using the given class loader to initialize the class
1983 * loader of the Bundle for later retrieval of Parcelable objects.
1984 * Returns null if the previously written Bundle object was null.
1985 */
1986 public final PersistableBundle readPersistableBundle(ClassLoader loader) {
1987 int length = readInt();
1988 if (length < 0) {
1989 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
1990 return null;
1991 }
1992
1993 final PersistableBundle bundle = new PersistableBundle(this, length);
1994 if (loader != null) {
1995 bundle.setClassLoader(loader);
1996 }
1997 return bundle;
1998 }
1999
2000 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002001 * Read a Size from the parcel at the current dataPosition().
2002 */
2003 public final Size readSize() {
2004 final int width = readInt();
2005 final int height = readInt();
2006 return new Size(width, height);
2007 }
2008
2009 /**
2010 * Read a SizeF from the parcel at the current dataPosition().
2011 */
2012 public final SizeF readSizeF() {
2013 final float width = readFloat();
2014 final float height = readFloat();
2015 return new SizeF(width, height);
2016 }
2017
2018 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 * Read and return a byte[] object from the parcel.
2020 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002021 public final byte[] createByteArray() {
2022 return nativeCreateByteArray(mNativePtr);
2023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024
2025 /**
2026 * Read a byte[] object from the parcel and copy it into the
2027 * given byte array.
2028 */
2029 public final void readByteArray(byte[] val) {
2030 // TODO: make this a native method to avoid the extra copy.
2031 byte[] ba = createByteArray();
2032 if (ba.length == val.length) {
2033 System.arraycopy(ba, 0, val, 0, ba.length);
2034 } else {
2035 throw new RuntimeException("bad array lengths");
2036 }
2037 }
2038
2039 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002040 * Read a blob of data from the parcel and return it as a byte array.
2041 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -07002042 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002043 */
2044 public final byte[] readBlob() {
2045 return nativeReadBlob(mNativePtr);
2046 }
2047
2048 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 * Read and return a String[] object from the parcel.
2050 * {@hide}
2051 */
2052 public final String[] readStringArray() {
2053 String[] array = null;
2054
2055 int length = readInt();
2056 if (length >= 0)
2057 {
2058 array = new String[length];
2059
2060 for (int i = 0 ; i < length ; i++)
2061 {
2062 array[i] = readString();
2063 }
2064 }
2065
2066 return array;
2067 }
2068
2069 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002070 * Read and return a CharSequence[] object from the parcel.
2071 * {@hide}
2072 */
2073 public final CharSequence[] readCharSequenceArray() {
2074 CharSequence[] array = null;
2075
2076 int length = readInt();
2077 if (length >= 0)
2078 {
2079 array = new CharSequence[length];
2080
2081 for (int i = 0 ; i < length ; i++)
2082 {
2083 array[i] = readCharSequence();
2084 }
2085 }
2086
2087 return array;
2088 }
2089
2090 /**
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002091 * Read and return an ArrayList&lt;CharSequence&gt; object from the parcel.
2092 * {@hide}
2093 */
2094 public final ArrayList<CharSequence> readCharSequenceList() {
2095 ArrayList<CharSequence> array = null;
2096
2097 int length = readInt();
2098 if (length >= 0) {
2099 array = new ArrayList<CharSequence>(length);
2100
2101 for (int i = 0 ; i < length ; i++) {
2102 array.add(readCharSequence());
2103 }
2104 }
2105
2106 return array;
2107 }
2108
2109 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 * Read and return a new ArrayList object from the parcel at the current
2111 * dataPosition(). Returns null if the previously written list object was
2112 * null. The given class loader will be used to load any enclosed
2113 * Parcelables.
2114 */
2115 public final ArrayList readArrayList(ClassLoader loader) {
2116 int N = readInt();
2117 if (N < 0) {
2118 return null;
2119 }
2120 ArrayList l = new ArrayList(N);
2121 readListInternal(l, N, loader);
2122 return l;
2123 }
2124
2125 /**
2126 * Read and return a new Object array from the parcel at the current
2127 * dataPosition(). Returns null if the previously written array was
2128 * null. The given class loader will be used to load any enclosed
2129 * Parcelables.
2130 */
2131 public final Object[] readArray(ClassLoader loader) {
2132 int N = readInt();
2133 if (N < 0) {
2134 return null;
2135 }
2136 Object[] l = new Object[N];
2137 readArrayInternal(l, N, loader);
2138 return l;
2139 }
2140
2141 /**
2142 * Read and return a new SparseArray object from the parcel at the current
2143 * dataPosition(). Returns null if the previously written list object was
2144 * null. The given class loader will be used to load any enclosed
2145 * Parcelables.
2146 */
2147 public final SparseArray readSparseArray(ClassLoader loader) {
2148 int N = readInt();
2149 if (N < 0) {
2150 return null;
2151 }
2152 SparseArray sa = new SparseArray(N);
2153 readSparseArrayInternal(sa, N, loader);
2154 return sa;
2155 }
2156
2157 /**
2158 * Read and return a new SparseBooleanArray object from the parcel at the current
2159 * dataPosition(). Returns null if the previously written list object was
2160 * null.
2161 */
2162 public final SparseBooleanArray readSparseBooleanArray() {
2163 int N = readInt();
2164 if (N < 0) {
2165 return null;
2166 }
2167 SparseBooleanArray sa = new SparseBooleanArray(N);
2168 readSparseBooleanArrayInternal(sa, N);
2169 return sa;
2170 }
2171
2172 /**
Adam Lesinski4e862812016-11-21 16:02:24 -08002173 * Read and return a new SparseIntArray object from the parcel at the current
2174 * dataPosition(). Returns null if the previously written array object was null.
2175 */
2176 public final SparseIntArray readSparseIntArray() {
2177 int N = readInt();
2178 if (N < 0) {
2179 return null;
2180 }
2181 SparseIntArray sa = new SparseIntArray(N);
2182 readSparseIntArrayInternal(sa, N);
2183 return sa;
2184 }
2185
2186 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 * Read and return a new ArrayList containing a particular object type from
2188 * the parcel that was written with {@link #writeTypedList} at the
2189 * current dataPosition(). Returns null if the
2190 * previously written list object was null. The list <em>must</em> have
2191 * previously been written via {@link #writeTypedList} with the same object
2192 * type.
2193 *
2194 * @return A newly created ArrayList containing objects with the same data
2195 * as those that were previously written.
2196 *
2197 * @see #writeTypedList
2198 */
2199 public final <T> ArrayList<T> createTypedArrayList(Parcelable.Creator<T> c) {
2200 int N = readInt();
2201 if (N < 0) {
2202 return null;
2203 }
2204 ArrayList<T> l = new ArrayList<T>(N);
2205 while (N > 0) {
2206 if (readInt() != 0) {
2207 l.add(c.createFromParcel(this));
2208 } else {
2209 l.add(null);
2210 }
2211 N--;
2212 }
2213 return l;
2214 }
2215
2216 /**
2217 * Read into the given List items containing a particular object type
2218 * that were written with {@link #writeTypedList} at the
2219 * current dataPosition(). The list <em>must</em> have
2220 * previously been written via {@link #writeTypedList} with the same object
2221 * type.
2222 *
2223 * @return A newly created ArrayList containing objects with the same data
2224 * as those that were previously written.
2225 *
2226 * @see #writeTypedList
2227 */
2228 public final <T> void readTypedList(List<T> list, Parcelable.Creator<T> c) {
2229 int M = list.size();
2230 int N = readInt();
2231 int i = 0;
2232 for (; i < M && i < N; i++) {
2233 if (readInt() != 0) {
2234 list.set(i, c.createFromParcel(this));
2235 } else {
2236 list.set(i, null);
2237 }
2238 }
2239 for (; i<N; i++) {
2240 if (readInt() != 0) {
2241 list.add(c.createFromParcel(this));
2242 } else {
2243 list.add(null);
2244 }
2245 }
2246 for (; i<M; i++) {
2247 list.remove(N);
2248 }
2249 }
2250
2251 /**
2252 * Read and return a new ArrayList containing String objects from
2253 * the parcel that was written with {@link #writeStringList} at the
2254 * current dataPosition(). Returns null if the
2255 * previously written list object was null.
2256 *
2257 * @return A newly created ArrayList containing strings with the same data
2258 * as those that were previously written.
2259 *
2260 * @see #writeStringList
2261 */
2262 public final ArrayList<String> createStringArrayList() {
2263 int N = readInt();
2264 if (N < 0) {
2265 return null;
2266 }
2267 ArrayList<String> l = new ArrayList<String>(N);
2268 while (N > 0) {
2269 l.add(readString());
2270 N--;
2271 }
2272 return l;
2273 }
2274
2275 /**
2276 * Read and return a new ArrayList containing IBinder objects from
2277 * the parcel that was written with {@link #writeBinderList} at the
2278 * current dataPosition(). Returns null if the
2279 * previously written list object was null.
2280 *
2281 * @return A newly created ArrayList containing strings with the same data
2282 * as those that were previously written.
2283 *
2284 * @see #writeBinderList
2285 */
2286 public final ArrayList<IBinder> createBinderArrayList() {
2287 int N = readInt();
2288 if (N < 0) {
2289 return null;
2290 }
2291 ArrayList<IBinder> l = new ArrayList<IBinder>(N);
2292 while (N > 0) {
2293 l.add(readStrongBinder());
2294 N--;
2295 }
2296 return l;
2297 }
2298
2299 /**
2300 * Read into the given List items String objects that were written with
2301 * {@link #writeStringList} at the current dataPosition().
2302 *
2303 * @return A newly created ArrayList containing strings with the same data
2304 * as those that were previously written.
2305 *
2306 * @see #writeStringList
2307 */
2308 public final void readStringList(List<String> list) {
2309 int M = list.size();
2310 int N = readInt();
2311 int i = 0;
2312 for (; i < M && i < N; i++) {
2313 list.set(i, readString());
2314 }
2315 for (; i<N; i++) {
2316 list.add(readString());
2317 }
2318 for (; i<M; i++) {
2319 list.remove(N);
2320 }
2321 }
2322
2323 /**
2324 * Read into the given List items IBinder objects that were written with
2325 * {@link #writeBinderList} at the current dataPosition().
2326 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 * @see #writeBinderList
2328 */
2329 public final void readBinderList(List<IBinder> list) {
2330 int M = list.size();
2331 int N = readInt();
2332 int i = 0;
2333 for (; i < M && i < N; i++) {
2334 list.set(i, readStrongBinder());
2335 }
2336 for (; i<N; i++) {
2337 list.add(readStrongBinder());
2338 }
2339 for (; i<M; i++) {
2340 list.remove(N);
2341 }
2342 }
2343
2344 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00002345 * Read the list of {@code Parcelable} objects at the current data position into the
2346 * given {@code list}. The contents of the {@code list} are replaced. If the serialized
2347 * list was {@code null}, {@code list} is cleared.
2348 *
2349 * @see #writeParcelableList(List, int)
2350 * @hide
2351 */
2352 public final <T extends Parcelable> void readParcelableList(List<T> list, ClassLoader cl) {
2353 final int N = readInt();
2354 if (N == -1) {
2355 list.clear();
2356 return;
2357 }
2358
2359 final int M = list.size();
2360 int i = 0;
2361 for (; i < M && i < N; i++) {
2362 list.set(i, (T) readParcelable(cl));
2363 }
2364 for (; i<N; i++) {
2365 list.add((T) readParcelable(cl));
2366 }
2367 for (; i<M; i++) {
2368 list.remove(N);
2369 }
2370 }
2371
2372 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 * Read and return a new array containing a particular object type from
2374 * the parcel at the current dataPosition(). Returns null if the
2375 * previously written array was null. The array <em>must</em> have
2376 * previously been written via {@link #writeTypedArray} with the same
2377 * object type.
2378 *
2379 * @return A newly created array containing objects with the same data
2380 * as those that were previously written.
2381 *
2382 * @see #writeTypedArray
2383 */
2384 public final <T> T[] createTypedArray(Parcelable.Creator<T> c) {
2385 int N = readInt();
2386 if (N < 0) {
2387 return null;
2388 }
2389 T[] l = c.newArray(N);
2390 for (int i=0; i<N; i++) {
2391 if (readInt() != 0) {
2392 l[i] = c.createFromParcel(this);
2393 }
2394 }
2395 return l;
2396 }
2397
2398 public final <T> void readTypedArray(T[] val, Parcelable.Creator<T> c) {
2399 int N = readInt();
2400 if (N == val.length) {
2401 for (int i=0; i<N; i++) {
2402 if (readInt() != 0) {
2403 val[i] = c.createFromParcel(this);
2404 } else {
2405 val[i] = null;
2406 }
2407 }
2408 } else {
2409 throw new RuntimeException("bad array lengths");
2410 }
2411 }
2412
2413 /**
2414 * @deprecated
2415 * @hide
2416 */
2417 @Deprecated
2418 public final <T> T[] readTypedArray(Parcelable.Creator<T> c) {
2419 return createTypedArray(c);
2420 }
2421
2422 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002423 * Read and return a typed Parcelable object from a parcel.
2424 * Returns null if the previous written object was null.
2425 * The object <em>must</em> have previous been written via
2426 * {@link #writeTypedObject} with the same object type.
2427 *
2428 * @return A newly created object of the type that was previously
2429 * written.
2430 *
2431 * @see #writeTypedObject
2432 */
2433 public final <T> T readTypedObject(Parcelable.Creator<T> c) {
2434 if (readInt() != 0) {
2435 return c.createFromParcel(this);
2436 } else {
2437 return null;
2438 }
2439 }
2440
2441 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 * Write a heterogeneous array of Parcelable objects into the Parcel.
2443 * Each object in the array is written along with its class name, so
2444 * that the correct class can later be instantiated. As a result, this
2445 * has significantly more overhead than {@link #writeTypedArray}, but will
2446 * correctly handle an array containing more than one type of object.
2447 *
2448 * @param value The array of objects to be written.
2449 * @param parcelableFlags Contextual flags as per
2450 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
2451 *
2452 * @see #writeTypedArray
2453 */
2454 public final <T extends Parcelable> void writeParcelableArray(T[] value,
2455 int parcelableFlags) {
2456 if (value != null) {
2457 int N = value.length;
2458 writeInt(N);
2459 for (int i=0; i<N; i++) {
2460 writeParcelable(value[i], parcelableFlags);
2461 }
2462 } else {
2463 writeInt(-1);
2464 }
2465 }
2466
2467 /**
2468 * Read a typed object from a parcel. The given class loader will be
2469 * used to load any enclosed Parcelables. If it is null, the default class
2470 * loader will be used.
2471 */
2472 public final Object readValue(ClassLoader loader) {
2473 int type = readInt();
2474
2475 switch (type) {
2476 case VAL_NULL:
2477 return null;
2478
2479 case VAL_STRING:
2480 return readString();
2481
2482 case VAL_INTEGER:
2483 return readInt();
2484
2485 case VAL_MAP:
2486 return readHashMap(loader);
2487
2488 case VAL_PARCELABLE:
2489 return readParcelable(loader);
2490
2491 case VAL_SHORT:
2492 return (short) readInt();
2493
2494 case VAL_LONG:
2495 return readLong();
2496
2497 case VAL_FLOAT:
2498 return readFloat();
2499
2500 case VAL_DOUBLE:
2501 return readDouble();
2502
2503 case VAL_BOOLEAN:
2504 return readInt() == 1;
2505
2506 case VAL_CHARSEQUENCE:
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002507 return readCharSequence();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508
2509 case VAL_LIST:
2510 return readArrayList(loader);
2511
2512 case VAL_BOOLEANARRAY:
Samuel Tana8036662015-11-23 14:36:00 -08002513 return createBooleanArray();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514
2515 case VAL_BYTEARRAY:
2516 return createByteArray();
2517
2518 case VAL_STRINGARRAY:
2519 return readStringArray();
2520
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002521 case VAL_CHARSEQUENCEARRAY:
2522 return readCharSequenceArray();
2523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524 case VAL_IBINDER:
2525 return readStrongBinder();
2526
2527 case VAL_OBJECTARRAY:
2528 return readArray(loader);
2529
2530 case VAL_INTARRAY:
2531 return createIntArray();
2532
2533 case VAL_LONGARRAY:
2534 return createLongArray();
2535
2536 case VAL_BYTE:
2537 return readByte();
2538
2539 case VAL_SERIALIZABLE:
John Spurlock5002b8c2014-01-10 13:32:12 -05002540 return readSerializable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541
2542 case VAL_PARCELABLEARRAY:
2543 return readParcelableArray(loader);
2544
2545 case VAL_SPARSEARRAY:
2546 return readSparseArray(loader);
2547
2548 case VAL_SPARSEBOOLEANARRAY:
2549 return readSparseBooleanArray();
2550
2551 case VAL_BUNDLE:
2552 return readBundle(loader); // loading will be deferred
2553
Craig Mautner719e6b12014-04-04 20:29:41 -07002554 case VAL_PERSISTABLEBUNDLE:
2555 return readPersistableBundle(loader);
2556
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002557 case VAL_SIZE:
2558 return readSize();
2559
2560 case VAL_SIZEF:
2561 return readSizeF();
2562
Samuel Tana8036662015-11-23 14:36:00 -08002563 case VAL_DOUBLEARRAY:
2564 return createDoubleArray();
2565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 default:
2567 int off = dataPosition() - 4;
2568 throw new RuntimeException(
2569 "Parcel " + this + ": Unmarshalling unknown type code " + type + " at offset " + off);
2570 }
2571 }
2572
2573 /**
2574 * Read and return a new Parcelable from the parcel. The given class loader
2575 * will be used to load any enclosed Parcelables. If it is null, the default
2576 * class loader will be used.
2577 * @param loader A ClassLoader from which to instantiate the Parcelable
2578 * object, or null for the default class loader.
2579 * @return Returns the newly created Parcelable, or null if a null
2580 * object has been written.
2581 * @throws BadParcelableException Throws BadParcelableException if there
2582 * was an error trying to instantiate the Parcelable.
2583 */
Neil Fuller44e440c2015-04-20 14:39:00 +01002584 @SuppressWarnings("unchecked")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 public final <T extends Parcelable> T readParcelable(ClassLoader loader) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002586 Parcelable.Creator<?> creator = readParcelableCreator(loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002587 if (creator == null) {
2588 return null;
2589 }
2590 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002591 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2592 (Parcelable.ClassLoaderCreator<?>) creator;
2593 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002594 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002595 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002596 }
2597
2598 /** @hide */
Neil Fuller44e440c2015-04-20 14:39:00 +01002599 @SuppressWarnings("unchecked")
2600 public final <T extends Parcelable> T readCreator(Parcelable.Creator<?> creator,
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002601 ClassLoader loader) {
2602 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002603 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2604 (Parcelable.ClassLoaderCreator<?>) creator;
2605 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002606 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002607 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002608 }
2609
2610 /** @hide */
Neil Fuller44e440c2015-04-20 14:39:00 +01002611 public final Parcelable.Creator<?> readParcelableCreator(ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612 String name = readString();
2613 if (name == null) {
2614 return null;
2615 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002616 Parcelable.Creator<?> creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 synchronized (mCreators) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002618 HashMap<String,Parcelable.Creator<?>> map = mCreators.get(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 if (map == null) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002620 map = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 mCreators.put(loader, map);
2622 }
2623 creator = map.get(name);
2624 if (creator == null) {
2625 try {
Neil Fuller44e440c2015-04-20 14:39:00 +01002626 // If loader == null, explicitly emulate Class.forName(String) "caller
2627 // classloader" behavior.
2628 ClassLoader parcelableClassLoader =
2629 (loader == null ? getClass().getClassLoader() : loader);
2630 // Avoid initializing the Parcelable class until we know it implements
2631 // Parcelable and has the necessary CREATOR field. http://b/1171613.
2632 Class<?> parcelableClass = Class.forName(name, false /* initialize */,
2633 parcelableClassLoader);
2634 if (!Parcelable.class.isAssignableFrom(parcelableClass)) {
2635 throw new BadParcelableException("Parcelable protocol requires that the "
2636 + "class implements Parcelable");
2637 }
2638 Field f = parcelableClass.getField("CREATOR");
2639 if ((f.getModifiers() & Modifier.STATIC) == 0) {
2640 throw new BadParcelableException("Parcelable protocol requires "
2641 + "the CREATOR object to be static on class " + name);
2642 }
2643 Class<?> creatorType = f.getType();
2644 if (!Parcelable.Creator.class.isAssignableFrom(creatorType)) {
2645 // Fail before calling Field.get(), not after, to avoid initializing
2646 // parcelableClass unnecessarily.
2647 throw new BadParcelableException("Parcelable protocol requires a "
2648 + "Parcelable.Creator object called "
2649 + "CREATOR on class " + name);
2650 }
2651 creator = (Parcelable.Creator<?>) f.get(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 }
2653 catch (IllegalAccessException e) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002654 Log.e(TAG, "Illegal access when unmarshalling: " + name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 throw new BadParcelableException(
2656 "IllegalAccessException when unmarshalling: " + name);
2657 }
2658 catch (ClassNotFoundException e) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002659 Log.e(TAG, "Class not found when unmarshalling: " + name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 throw new BadParcelableException(
2661 "ClassNotFoundException when unmarshalling: " + name);
2662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663 catch (NoSuchFieldException e) {
2664 throw new BadParcelableException("Parcelable protocol requires a "
Neil Fuller44e440c2015-04-20 14:39:00 +01002665 + "Parcelable.Creator object called "
2666 + "CREATOR on class " + name);
Irfan Sheriff97a72f62013-01-11 10:45:51 -08002667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002668 if (creator == null) {
2669 throw new BadParcelableException("Parcelable protocol requires a "
Neil Fuller44e440c2015-04-20 14:39:00 +01002670 + "non-null Parcelable.Creator object called "
2671 + "CREATOR on class " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 }
2673
2674 map.put(name, creator);
2675 }
2676 }
2677
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002678 return creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 }
2680
2681 /**
2682 * Read and return a new Parcelable array from the parcel.
2683 * The given class loader will be used to load any enclosed
2684 * Parcelables.
2685 * @return the Parcelable array, or null if the array is null
2686 */
2687 public final Parcelable[] readParcelableArray(ClassLoader loader) {
2688 int N = readInt();
2689 if (N < 0) {
2690 return null;
2691 }
2692 Parcelable[] p = new Parcelable[N];
2693 for (int i = 0; i < N; i++) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002694 p[i] = readParcelable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 }
2696 return p;
2697 }
2698
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002699 /** @hide */
2700 public final <T extends Parcelable> T[] readParcelableArray(ClassLoader loader,
2701 Class<T> clazz) {
2702 int N = readInt();
2703 if (N < 0) {
2704 return null;
2705 }
2706 T[] p = (T[]) Array.newInstance(clazz, N);
2707 for (int i = 0; i < N; i++) {
2708 p[i] = readParcelable(loader);
2709 }
2710 return p;
2711 }
2712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 /**
2714 * Read and return a new Serializable object from the parcel.
2715 * @return the Serializable object, or null if the Serializable name
2716 * wasn't found in the parcel.
2717 */
2718 public final Serializable readSerializable() {
John Spurlock5002b8c2014-01-10 13:32:12 -05002719 return readSerializable(null);
2720 }
2721
2722 private final Serializable readSerializable(final ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 String name = readString();
2724 if (name == null) {
2725 // For some reason we were unable to read the name of the Serializable (either there
2726 // is nothing left in the Parcel to read, or the next value wasn't a String), so
2727 // return null, which indicates that the name wasn't found in the parcel.
2728 return null;
2729 }
2730
2731 byte[] serializedData = createByteArray();
2732 ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
2733 try {
John Spurlock5002b8c2014-01-10 13:32:12 -05002734 ObjectInputStream ois = new ObjectInputStream(bais) {
2735 @Override
2736 protected Class<?> resolveClass(ObjectStreamClass osClass)
2737 throws IOException, ClassNotFoundException {
2738 // try the custom classloader if provided
2739 if (loader != null) {
2740 Class<?> c = Class.forName(osClass.getName(), false, loader);
2741 if (c != null) {
2742 return c;
2743 }
2744 }
2745 return super.resolveClass(osClass);
2746 }
2747 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 return (Serializable) ois.readObject();
2749 } catch (IOException ioe) {
2750 throw new RuntimeException("Parcelable encountered " +
2751 "IOException reading a Serializable object (name = " + name +
2752 ")", ioe);
2753 } catch (ClassNotFoundException cnfe) {
John Spurlock5002b8c2014-01-10 13:32:12 -05002754 throw new RuntimeException("Parcelable encountered " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 "ClassNotFoundException reading a Serializable object (name = "
2756 + name + ")", cnfe);
2757 }
2758 }
2759
2760 // Cache of previously looked up CREATOR.createFromParcel() methods for
2761 // particular classes. Keys are the names of the classes, values are
2762 // Method objects.
Neil Fuller44e440c2015-04-20 14:39:00 +01002763 private static final HashMap<ClassLoader,HashMap<String,Parcelable.Creator<?>>>
2764 mCreators = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765
Narayan Kamathb34a4612014-01-23 14:17:11 +00002766 /** @hide for internal use only. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 static protected final Parcel obtain(int obj) {
Ashok Bhat8ab665d2014-01-22 16:00:20 +00002768 throw new UnsupportedOperationException();
2769 }
2770
2771 /** @hide */
2772 static protected final Parcel obtain(long obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 final Parcel[] pool = sHolderPool;
2774 synchronized (pool) {
2775 Parcel p;
2776 for (int i=0; i<POOL_SIZE; i++) {
2777 p = pool[i];
2778 if (p != null) {
2779 pool[i] = null;
2780 if (DEBUG_RECYCLE) {
2781 p.mStack = new RuntimeException();
2782 }
2783 p.init(obj);
2784 return p;
2785 }
2786 }
2787 }
2788 return new Parcel(obj);
2789 }
2790
Ashok Bhat8ab665d2014-01-22 16:00:20 +00002791 private Parcel(long nativePtr) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 if (DEBUG_RECYCLE) {
2793 mStack = new RuntimeException();
2794 }
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002795 //Log.i(TAG, "Initializing obj=0x" + Integer.toHexString(obj), mStack);
Jeff Sharkey047238c2012-03-07 16:51:38 -08002796 init(nativePtr);
2797 }
2798
Ashok Bhat8ab665d2014-01-22 16:00:20 +00002799 private void init(long nativePtr) {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002800 if (nativePtr != 0) {
2801 mNativePtr = nativePtr;
2802 mOwnsNativeParcelObject = false;
2803 } else {
2804 mNativePtr = nativeCreate();
2805 mOwnsNativeParcelObject = true;
2806 }
2807 }
2808
2809 private void freeBuffer() {
2810 if (mOwnsNativeParcelObject) {
Adrian Roos04505652015-10-22 16:12:01 -07002811 updateNativeSize(nativeFreeBuffer(mNativePtr));
Jeff Sharkey047238c2012-03-07 16:51:38 -08002812 }
2813 }
2814
2815 private void destroy() {
2816 if (mNativePtr != 0) {
2817 if (mOwnsNativeParcelObject) {
2818 nativeDestroy(mNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -07002819 updateNativeSize(0);
Jeff Sharkey047238c2012-03-07 16:51:38 -08002820 }
2821 mNativePtr = 0;
2822 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 }
2824
2825 @Override
2826 protected void finalize() throws Throwable {
2827 if (DEBUG_RECYCLE) {
2828 if (mStack != null) {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002829 Log.w(TAG, "Client did not call Parcel.recycle()", mStack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 }
2831 }
2832 destroy();
2833 }
2834
Dianne Hackborn6aff9052009-05-22 13:20:23 -07002835 /* package */ void readMapInternal(Map outVal, int N,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 ClassLoader loader) {
2837 while (N > 0) {
2838 Object key = readValue(loader);
2839 Object value = readValue(loader);
2840 outVal.put(key, value);
2841 N--;
2842 }
2843 }
2844
Dianne Hackbornb87655b2013-07-17 19:06:22 -07002845 /* package */ void readArrayMapInternal(ArrayMap outVal, int N,
2846 ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07002847 if (DEBUG_ARRAY_MAP) {
2848 RuntimeException here = new RuntimeException("here");
2849 here.fillInStackTrace();
2850 Log.d(TAG, "Reading " + N + " ArrayMap entries", here);
2851 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07002852 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -07002853 while (N > 0) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07002854 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07002855 String key = readString();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07002856 Object value = readValue(loader);
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07002857 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " "
2858 + (dataPosition()-startPos) + " bytes: key=0x"
2859 + Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key);
Dianne Hackbornb87655b2013-07-17 19:06:22 -07002860 outVal.append(key, value);
2861 N--;
2862 }
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07002863 outVal.validate();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07002864 }
2865
Dianne Hackborne784d1e2013-09-20 18:13:52 -07002866 /* package */ void readArrayMapSafelyInternal(ArrayMap outVal, int N,
2867 ClassLoader loader) {
2868 if (DEBUG_ARRAY_MAP) {
2869 RuntimeException here = new RuntimeException("here");
2870 here.fillInStackTrace();
2871 Log.d(TAG, "Reading safely " + N + " ArrayMap entries", here);
2872 }
2873 while (N > 0) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07002874 String key = readString();
Dianne Hackborne784d1e2013-09-20 18:13:52 -07002875 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read safe #" + (N-1) + ": key=0x"
2876 + (key != null ? key.hashCode() : 0) + " " + key);
2877 Object value = readValue(loader);
2878 outVal.put(key, value);
2879 N--;
2880 }
2881 }
2882
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07002883 /**
2884 * @hide For testing only.
2885 */
2886 public void readArrayMap(ArrayMap outVal, ClassLoader loader) {
2887 final int N = readInt();
2888 if (N < 0) {
2889 return;
2890 }
2891 readArrayMapInternal(outVal, N, loader);
2892 }
2893
Svet Ganovddb94882016-06-23 19:55:24 -07002894 /**
2895 * Reads an array set.
2896 *
2897 * @param loader The class loader to use.
2898 *
2899 * @hide
2900 */
2901 public @Nullable ArraySet<? extends Object> readArraySet(ClassLoader loader) {
2902 final int size = readInt();
2903 if (size < 0) {
2904 return null;
2905 }
2906 ArraySet<Object> result = new ArraySet<>(size);
2907 for (int i = 0; i < size; i++) {
2908 Object value = readValue(loader);
2909 result.append(value);
2910 }
2911 return result;
2912 }
2913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 private void readListInternal(List outVal, int N,
2915 ClassLoader loader) {
2916 while (N > 0) {
2917 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002918 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 outVal.add(value);
2920 N--;
2921 }
2922 }
2923
2924 private void readArrayInternal(Object[] outVal, int N,
2925 ClassLoader loader) {
2926 for (int i = 0; i < N; i++) {
2927 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002928 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 outVal[i] = value;
2930 }
2931 }
2932
2933 private void readSparseArrayInternal(SparseArray outVal, int N,
2934 ClassLoader loader) {
2935 while (N > 0) {
2936 int key = readInt();
2937 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002938 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 outVal.append(key, value);
2940 N--;
2941 }
2942 }
2943
2944
2945 private void readSparseBooleanArrayInternal(SparseBooleanArray outVal, int N) {
2946 while (N > 0) {
2947 int key = readInt();
2948 boolean value = this.readByte() == 1;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002949 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 outVal.append(key, value);
2951 N--;
2952 }
2953 }
Dan Sandler5ce04302015-04-09 23:50:15 -04002954
Adam Lesinski4e862812016-11-21 16:02:24 -08002955 private void readSparseIntArrayInternal(SparseIntArray outVal, int N) {
2956 while (N > 0) {
2957 int key = readInt();
2958 int value = readInt();
2959 outVal.append(key, value);
2960 N--;
2961 }
2962 }
2963
Dan Sandler5ce04302015-04-09 23:50:15 -04002964 /**
2965 * @hide For testing
2966 */
2967 public long getBlobAshmemSize() {
2968 return nativeGetBlobAshmemSize(mNativePtr);
2969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970}