The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.os; |
| 18 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Svet Ganov | ddb9488 | 2016-06-23 19:55:24 -0700 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Howard Chen | 40abbb0 | 2019-05-24 02:04:50 +0800 | [diff] [blame] | 21 | import android.annotation.TestApi; |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 22 | import android.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.text.TextUtils; |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 24 | import android.util.ArrayMap; |
Svet Ganov | ddb9488 | 2016-06-23 19:55:24 -0700 | [diff] [blame] | 25 | import android.util.ArraySet; |
Fyodor Kupolov | 3b946f4 | 2017-11-27 10:40:46 -0800 | [diff] [blame] | 26 | import android.util.ExceptionUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.util.Log; |
Jeff Sharkey | 5ef3398 | 2014-09-04 18:13:39 -0700 | [diff] [blame] | 28 | import android.util.Size; |
| 29 | import android.util.SizeF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.util.SparseArray; |
| 31 | import android.util.SparseBooleanArray; |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 32 | import android.util.SparseIntArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 34 | import dalvik.annotation.optimization.CriticalNative; |
Jeff Sharkey | 789a8fc | 2017-04-16 13:18:35 -0600 | [diff] [blame] | 35 | import dalvik.annotation.optimization.FastNative; |
| 36 | import dalvik.system.VMRuntime; |
| 37 | |
Pete Gillin | 60f55a25 | 2018-05-10 15:40:32 +0100 | [diff] [blame] | 38 | import libcore.util.ArrayUtils; |
Jeff Sharkey | e628b7d | 2017-01-17 13:50:20 -0700 | [diff] [blame] | 39 | import libcore.util.SneakyThrow; |
| 40 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import java.io.ByteArrayInputStream; |
| 42 | import java.io.ByteArrayOutputStream; |
| 43 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import java.io.IOException; |
| 45 | import java.io.ObjectInputStream; |
| 46 | import java.io.ObjectOutputStream; |
John Spurlock | 5002b8c | 2014-01-10 13:32:12 -0500 | [diff] [blame] | 47 | import java.io.ObjectStreamClass; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import java.io.Serializable; |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 49 | import java.lang.reflect.Array; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import java.lang.reflect.Field; |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 51 | import java.lang.reflect.Modifier; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import java.util.ArrayList; |
| 53 | import java.util.HashMap; |
| 54 | import java.util.List; |
| 55 | import java.util.Map; |
| 56 | import java.util.Set; |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 57 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | /** |
| 59 | * Container for a message (data and object references) that can |
| 60 | * be sent through an IBinder. A Parcel can contain both flattened data |
| 61 | * that will be unflattened on the other side of the IPC (using the various |
| 62 | * methods here for writing specific types, or the general |
| 63 | * {@link Parcelable} interface), and references to live {@link IBinder} |
| 64 | * objects that will result in the other side receiving a proxy IBinder |
| 65 | * connected with the original IBinder in the Parcel. |
| 66 | * |
| 67 | * <p class="note">Parcel is <strong>not</strong> a general-purpose |
| 68 | * serialization mechanism. This class (and the corresponding |
| 69 | * {@link Parcelable} API for placing arbitrary objects into a Parcel) is |
| 70 | * designed as a high-performance IPC transport. As such, it is not |
| 71 | * appropriate to place any Parcel data in to persistent storage: changes |
| 72 | * in the underlying implementation of any of the data in the Parcel can |
| 73 | * render older data unreadable.</p> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 74 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | * <p>The bulk of the Parcel API revolves around reading and writing data |
| 76 | * of various types. There are six major classes of such functions available.</p> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 77 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | * <h3>Primitives</h3> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 79 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | * <p>The most basic data functions are for writing and reading primitive |
| 81 | * data types: {@link #writeByte}, {@link #readByte}, {@link #writeDouble}, |
| 82 | * {@link #readDouble}, {@link #writeFloat}, {@link #readFloat}, {@link #writeInt}, |
| 83 | * {@link #readInt}, {@link #writeLong}, {@link #readLong}, |
| 84 | * {@link #writeString}, {@link #readString}. Most other |
| 85 | * data operations are built on top of these. The given data is written and |
| 86 | * read using the endianess of the host CPU.</p> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 87 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | * <h3>Primitive Arrays</h3> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 89 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | * <p>There are a variety of methods for reading and writing raw arrays |
| 91 | * of primitive objects, which generally result in writing a 4-byte length |
| 92 | * followed by the primitive data items. The methods for reading can either |
| 93 | * read the data into an existing array, or create and return a new array. |
| 94 | * These available types are:</p> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 95 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | * <ul> |
| 97 | * <li> {@link #writeBooleanArray(boolean[])}, |
| 98 | * {@link #readBooleanArray(boolean[])}, {@link #createBooleanArray()} |
| 99 | * <li> {@link #writeByteArray(byte[])}, |
| 100 | * {@link #writeByteArray(byte[], int, int)}, {@link #readByteArray(byte[])}, |
| 101 | * {@link #createByteArray()} |
| 102 | * <li> {@link #writeCharArray(char[])}, {@link #readCharArray(char[])}, |
| 103 | * {@link #createCharArray()} |
| 104 | * <li> {@link #writeDoubleArray(double[])}, {@link #readDoubleArray(double[])}, |
| 105 | * {@link #createDoubleArray()} |
| 106 | * <li> {@link #writeFloatArray(float[])}, {@link #readFloatArray(float[])}, |
| 107 | * {@link #createFloatArray()} |
| 108 | * <li> {@link #writeIntArray(int[])}, {@link #readIntArray(int[])}, |
| 109 | * {@link #createIntArray()} |
| 110 | * <li> {@link #writeLongArray(long[])}, {@link #readLongArray(long[])}, |
| 111 | * {@link #createLongArray()} |
| 112 | * <li> {@link #writeStringArray(String[])}, {@link #readStringArray(String[])}, |
| 113 | * {@link #createStringArray()}. |
| 114 | * <li> {@link #writeSparseBooleanArray(SparseBooleanArray)}, |
| 115 | * {@link #readSparseBooleanArray()}. |
| 116 | * </ul> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 117 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | * <h3>Parcelables</h3> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 119 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | * <p>The {@link Parcelable} protocol provides an extremely efficient (but |
| 121 | * low-level) protocol for objects to write and read themselves from Parcels. |
| 122 | * You can use the direct methods {@link #writeParcelable(Parcelable, int)} |
| 123 | * and {@link #readParcelable(ClassLoader)} or |
| 124 | * {@link #writeParcelableArray} and |
| 125 | * {@link #readParcelableArray(ClassLoader)} to write or read. These |
| 126 | * methods write both the class type and its data to the Parcel, allowing |
| 127 | * that class to be reconstructed from the appropriate class loader when |
| 128 | * later reading.</p> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 129 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | * <p>There are also some methods that provide a more efficient way to work |
Jens Ole Lauridsen | 8dea874 | 2015-04-20 11:18:51 -0700 | [diff] [blame] | 131 | * with Parcelables: {@link #writeTypedObject}, {@link #writeTypedArray}, |
| 132 | * {@link #writeTypedList}, {@link #readTypedObject}, |
| 133 | * {@link #createTypedArray} and {@link #createTypedArrayList}. These methods |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | * do not write the class information of the original object: instead, the |
| 135 | * caller of the read function must know what type to expect and pass in the |
| 136 | * appropriate {@link Parcelable.Creator Parcelable.Creator} instead to |
| 137 | * properly construct the new object and read its data. (To more efficient |
Bin Chen | b6b12b5 | 2017-07-11 11:01:44 +0800 | [diff] [blame] | 138 | * write and read a single Parcelable object that is not null, you can directly |
Jens Ole Lauridsen | 8dea874 | 2015-04-20 11:18:51 -0700 | [diff] [blame] | 139 | * call {@link Parcelable#writeToParcel Parcelable.writeToParcel} and |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | * {@link Parcelable.Creator#createFromParcel Parcelable.Creator.createFromParcel} |
| 141 | * yourself.)</p> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 142 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | * <h3>Bundles</h3> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 144 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | * <p>A special type-safe container, called {@link Bundle}, is available |
| 146 | * for key/value maps of heterogeneous values. This has many optimizations |
| 147 | * for improved performance when reading and writing data, and its type-safe |
| 148 | * API avoids difficult to debug type errors when finally marshalling the |
| 149 | * data contents into a Parcel. The methods to use are |
| 150 | * {@link #writeBundle(Bundle)}, {@link #readBundle()}, and |
| 151 | * {@link #readBundle(ClassLoader)}. |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 152 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | * <h3>Active Objects</h3> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 154 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | * <p>An unusual feature of Parcel is the ability to read and write active |
| 156 | * objects. For these objects the actual contents of the object is not |
| 157 | * written, rather a special token referencing the object is written. When |
| 158 | * reading the object back from the Parcel, you do not get a new instance of |
| 159 | * the object, but rather a handle that operates on the exact same object that |
| 160 | * was originally written. There are two forms of active objects available.</p> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 161 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | * <p>{@link Binder} objects are a core facility of Android's general cross-process |
| 163 | * communication system. The {@link IBinder} interface describes an abstract |
| 164 | * protocol with a Binder object. Any such interface can be written in to |
| 165 | * a Parcel, and upon reading you will receive either the original object |
| 166 | * implementing that interface or a special proxy implementation |
| 167 | * that communicates calls back to the original object. The methods to use are |
| 168 | * {@link #writeStrongBinder(IBinder)}, |
| 169 | * {@link #writeStrongInterface(IInterface)}, {@link #readStrongBinder()}, |
| 170 | * {@link #writeBinderArray(IBinder[])}, {@link #readBinderArray(IBinder[])}, |
| 171 | * {@link #createBinderArray()}, |
| 172 | * {@link #writeBinderList(List)}, {@link #readBinderList(List)}, |
| 173 | * {@link #createBinderArrayList()}.</p> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 174 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | * <p>FileDescriptor objects, representing raw Linux file descriptor identifiers, |
| 176 | * can be written and {@link ParcelFileDescriptor} objects returned to operate |
| 177 | * on the original file descriptor. The returned file descriptor is a dup |
| 178 | * of the original file descriptor: the object and fd is different, but |
| 179 | * operating on the same underlying file stream, with the same position, etc. |
| 180 | * The methods to use are {@link #writeFileDescriptor(FileDescriptor)}, |
| 181 | * {@link #readFileDescriptor()}. |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 182 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | * <h3>Untyped Containers</h3> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 184 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | * <p>A final class of methods are for writing and reading standard Java |
| 186 | * containers of arbitrary types. These all revolve around the |
| 187 | * {@link #writeValue(Object)} and {@link #readValue(ClassLoader)} methods |
| 188 | * which define the types of objects allowed. The container methods are |
| 189 | * {@link #writeArray(Object[])}, {@link #readArray(ClassLoader)}, |
| 190 | * {@link #writeList(List)}, {@link #readList(List, ClassLoader)}, |
| 191 | * {@link #readArrayList(ClassLoader)}, |
| 192 | * {@link #writeMap(Map)}, {@link #readMap(Map, ClassLoader)}, |
| 193 | * {@link #writeSparseArray(SparseArray)}, |
| 194 | * {@link #readSparseArray(ClassLoader)}. |
| 195 | */ |
| 196 | public final class Parcel { |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 197 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | private static final boolean DEBUG_RECYCLE = false; |
Dianne Hackborn | e784d1e | 2013-09-20 18:13:52 -0700 | [diff] [blame] | 199 | private static final boolean DEBUG_ARRAY_MAP = false; |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 200 | private static final String TAG = "Parcel"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 202 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | @SuppressWarnings({"UnusedDeclaration"}) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 204 | private long mNativePtr; // used by native code |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 205 | |
| 206 | /** |
| 207 | * Flag indicating if {@link #mNativePtr} was allocated by this object, |
| 208 | * indicating that we're responsible for its lifecycle. |
| 209 | */ |
| 210 | private boolean mOwnsNativeParcelObject; |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 211 | private long mNativeSize; |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 212 | |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 213 | private ArrayMap<Class, Object> mClassCookies; |
| 214 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | private RuntimeException mStack; |
| 216 | |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 217 | /** |
| 218 | * Whether or not to parcel the stack trace of an exception. This has a performance |
| 219 | * impact, so should only be included in specific processes and only on debug builds. |
| 220 | */ |
| 221 | private static boolean sParcelExceptionStackTrace; |
| 222 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | private static final int POOL_SIZE = 6; |
| 224 | private static final Parcel[] sOwnedPool = new Parcel[POOL_SIZE]; |
| 225 | private static final Parcel[] sHolderPool = new Parcel[POOL_SIZE]; |
| 226 | |
Robert Quattlebaum | 9cd7af3 | 2017-01-04 13:28:01 -0800 | [diff] [blame] | 227 | // Keep in sync with frameworks/native/include/private/binder/ParcelValTypes.h. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | private static final int VAL_NULL = -1; |
| 229 | private static final int VAL_STRING = 0; |
| 230 | private static final int VAL_INTEGER = 1; |
| 231 | private static final int VAL_MAP = 2; |
| 232 | private static final int VAL_BUNDLE = 3; |
| 233 | private static final int VAL_PARCELABLE = 4; |
| 234 | private static final int VAL_SHORT = 5; |
| 235 | private static final int VAL_LONG = 6; |
| 236 | private static final int VAL_FLOAT = 7; |
| 237 | private static final int VAL_DOUBLE = 8; |
| 238 | private static final int VAL_BOOLEAN = 9; |
| 239 | private static final int VAL_CHARSEQUENCE = 10; |
| 240 | private static final int VAL_LIST = 11; |
| 241 | private static final int VAL_SPARSEARRAY = 12; |
| 242 | private static final int VAL_BYTEARRAY = 13; |
| 243 | private static final int VAL_STRINGARRAY = 14; |
| 244 | private static final int VAL_IBINDER = 15; |
| 245 | private static final int VAL_PARCELABLEARRAY = 16; |
| 246 | private static final int VAL_OBJECTARRAY = 17; |
| 247 | private static final int VAL_INTARRAY = 18; |
| 248 | private static final int VAL_LONGARRAY = 19; |
| 249 | private static final int VAL_BYTE = 20; |
| 250 | private static final int VAL_SERIALIZABLE = 21; |
| 251 | private static final int VAL_SPARSEBOOLEANARRAY = 22; |
| 252 | private static final int VAL_BOOLEANARRAY = 23; |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 253 | private static final int VAL_CHARSEQUENCEARRAY = 24; |
Craig Mautner | 719e6b1 | 2014-04-04 20:29:41 -0700 | [diff] [blame] | 254 | private static final int VAL_PERSISTABLEBUNDLE = 25; |
Jeff Sharkey | 5ef3398 | 2014-09-04 18:13:39 -0700 | [diff] [blame] | 255 | private static final int VAL_SIZE = 26; |
| 256 | private static final int VAL_SIZEF = 27; |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 257 | private static final int VAL_DOUBLEARRAY = 28; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 259 | // The initial int32 in a Binder call's reply Parcel header: |
Christopher Wiley | 80fd120 | 2015-11-22 17:12:37 -0800 | [diff] [blame] | 260 | // Keep these in sync with libbinder's binder/Status.h. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | private static final int EX_SECURITY = -1; |
| 262 | private static final int EX_BAD_PARCELABLE = -2; |
| 263 | private static final int EX_ILLEGAL_ARGUMENT = -3; |
| 264 | private static final int EX_NULL_POINTER = -4; |
| 265 | private static final int EX_ILLEGAL_STATE = -5; |
Dianne Hackborn | 7e71442 | 2013-09-13 17:32:57 -0700 | [diff] [blame] | 266 | private static final int EX_NETWORK_MAIN_THREAD = -6; |
Dianne Hackborn | 33d738a | 2014-09-12 14:23:58 -0700 | [diff] [blame] | 267 | private static final int EX_UNSUPPORTED_OPERATION = -7; |
Christopher Wiley | 80fd120 | 2015-11-22 17:12:37 -0800 | [diff] [blame] | 268 | private static final int EX_SERVICE_SPECIFIC = -8; |
Jeff Sharkey | e628b7d | 2017-01-17 13:50:20 -0700 | [diff] [blame] | 269 | private static final int EX_PARCELABLE = -9; |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 270 | private static final int EX_HAS_REPLY_HEADER = -128; // special; see below |
Christopher Wiley | 80fd120 | 2015-11-22 17:12:37 -0800 | [diff] [blame] | 271 | // EX_TRANSACTION_FAILED is used exclusively in native code. |
| 272 | // see libbinder's binder/Status.h |
| 273 | private static final int EX_TRANSACTION_FAILED = -129; |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 274 | |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 275 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 276 | private static native int nativeDataSize(long nativePtr); |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 277 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 278 | private static native int nativeDataAvail(long nativePtr); |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 279 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 280 | private static native int nativeDataPosition(long nativePtr); |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 281 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 282 | private static native int nativeDataCapacity(long nativePtr); |
John Reck | 71207b5 | 2016-09-28 13:28:09 -0700 | [diff] [blame] | 283 | @FastNative |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 284 | private static native long nativeSetDataSize(long nativePtr, int size); |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 285 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 286 | private static native void nativeSetDataPosition(long nativePtr, int pos); |
John Reck | 71207b5 | 2016-09-28 13:28:09 -0700 | [diff] [blame] | 287 | @FastNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 288 | private static native void nativeSetDataCapacity(long nativePtr, int size); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 289 | |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 290 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 291 | private static native boolean nativePushAllowFds(long nativePtr, boolean allowFds); |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 292 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 293 | private static native void nativeRestoreAllowFds(long nativePtr, boolean lastValue); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 294 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 295 | private static native void nativeWriteByteArray(long nativePtr, byte[] b, int offset, int len); |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 296 | private static native void nativeWriteBlob(long nativePtr, byte[] b, int offset, int len); |
John Reck | 71207b5 | 2016-09-28 13:28:09 -0700 | [diff] [blame] | 297 | @FastNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 298 | private static native void nativeWriteInt(long nativePtr, int val); |
John Reck | 71207b5 | 2016-09-28 13:28:09 -0700 | [diff] [blame] | 299 | @FastNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 300 | private static native void nativeWriteLong(long nativePtr, long val); |
John Reck | 71207b5 | 2016-09-28 13:28:09 -0700 | [diff] [blame] | 301 | @FastNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 302 | private static native void nativeWriteFloat(long nativePtr, float val); |
John Reck | 71207b5 | 2016-09-28 13:28:09 -0700 | [diff] [blame] | 303 | @FastNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 304 | private static native void nativeWriteDouble(long nativePtr, double val); |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 305 | static native void nativeWriteString(long nativePtr, String val); |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 306 | private static native void nativeWriteStrongBinder(long nativePtr, IBinder val); |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 307 | private static native long nativeWriteFileDescriptor(long nativePtr, FileDescriptor val); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 308 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 309 | private static native byte[] nativeCreateByteArray(long nativePtr); |
Jocelyn Dang | 4610044 | 2017-05-05 15:40:49 -0700 | [diff] [blame] | 310 | private static native boolean nativeReadByteArray(long nativePtr, byte[] dest, int destLen); |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 311 | private static native byte[] nativeReadBlob(long nativePtr); |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 312 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 313 | private static native int nativeReadInt(long nativePtr); |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 314 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 315 | private static native long nativeReadLong(long nativePtr); |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 316 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 317 | private static native float nativeReadFloat(long nativePtr); |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 318 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 319 | private static native double nativeReadDouble(long nativePtr); |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 320 | static native String nativeReadString(long nativePtr); |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 321 | private static native IBinder nativeReadStrongBinder(long nativePtr); |
| 322 | private static native FileDescriptor nativeReadFileDescriptor(long nativePtr); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 323 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 324 | private static native long nativeCreate(); |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 325 | private static native long nativeFreeBuffer(long nativePtr); |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 326 | private static native void nativeDestroy(long nativePtr); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 327 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 328 | private static native byte[] nativeMarshall(long nativePtr); |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 329 | private static native long nativeUnmarshall( |
John Spurlock | e085236 | 2015-02-04 15:47:40 -0500 | [diff] [blame] | 330 | long nativePtr, byte[] data, int offset, int length); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 331 | private static native int nativeCompareData(long thisNativePtr, long otherNativePtr); |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 332 | private static native long nativeAppendFrom( |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 333 | long thisNativePtr, long otherNativePtr, int offset, int length); |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 334 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 335 | private static native boolean nativeHasFileDescriptors(long nativePtr); |
| 336 | private static native void nativeWriteInterfaceToken(long nativePtr, String interfaceName); |
| 337 | private static native void nativeEnforceInterface(long nativePtr, String interfaceName); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 338 | |
Olivier Gaillard | bab444a | 2019-01-30 17:11:40 +0000 | [diff] [blame] | 339 | @CriticalNative |
| 340 | private static native boolean nativeReplaceCallingWorkSourceUid( |
| 341 | long nativePtr, int workSourceUid); |
| 342 | @CriticalNative |
| 343 | private static native int nativeReadCallingWorkSourceUid(long nativePtr); |
| 344 | |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 345 | /** Last time exception with a stack trace was written */ |
| 346 | private static volatile long sLastWriteExceptionStackTrace; |
| 347 | /** Used for throttling of writing stack trace, which is costly */ |
| 348 | private static final int WRITE_EXCEPTION_STACK_TRACE_THRESHOLD_MS = 1000; |
| 349 | |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 350 | @CriticalNative |
Dan Sandler | aa86166 | 2015-04-21 10:24:32 -0400 | [diff] [blame] | 351 | private static native long nativeGetBlobAshmemSize(long nativePtr); |
Dan Sandler | 5ce0430 | 2015-04-09 23:50:15 -0400 | [diff] [blame] | 352 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | public final static Parcelable.Creator<String> STRING_CREATOR |
| 354 | = new Parcelable.Creator<String>() { |
| 355 | public String createFromParcel(Parcel source) { |
| 356 | return source.readString(); |
| 357 | } |
| 358 | public String[] newArray(int size) { |
| 359 | return new String[size]; |
| 360 | } |
| 361 | }; |
| 362 | |
| 363 | /** |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 364 | * @hide |
| 365 | */ |
| 366 | public static class ReadWriteHelper { |
| 367 | public static final ReadWriteHelper DEFAULT = new ReadWriteHelper(); |
| 368 | |
| 369 | /** |
| 370 | * Called when writing a string to a parcel. Subclasses wanting to write a string |
| 371 | * must use {@link #writeStringNoHelper(String)} to avoid |
| 372 | * infinity recursive calls. |
| 373 | */ |
| 374 | public void writeString(Parcel p, String s) { |
| 375 | nativeWriteString(p.mNativePtr, s); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Called when reading a string to a parcel. Subclasses wanting to read a string |
| 380 | * must use {@link #readStringNoHelper()} to avoid |
| 381 | * infinity recursive calls. |
| 382 | */ |
| 383 | public String readString(Parcel p) { |
| 384 | return nativeReadString(p.mNativePtr); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | private ReadWriteHelper mReadWriteHelper = ReadWriteHelper.DEFAULT; |
| 389 | |
| 390 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | * Retrieve a new Parcel object from the pool. |
| 392 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 393 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | public static Parcel obtain() { |
| 395 | final Parcel[] pool = sOwnedPool; |
| 396 | synchronized (pool) { |
| 397 | Parcel p; |
| 398 | for (int i=0; i<POOL_SIZE; i++) { |
| 399 | p = pool[i]; |
| 400 | if (p != null) { |
| 401 | pool[i] = null; |
| 402 | if (DEBUG_RECYCLE) { |
| 403 | p.mStack = new RuntimeException(); |
| 404 | } |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 405 | p.mReadWriteHelper = ReadWriteHelper.DEFAULT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | return p; |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | return new Parcel(0); |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Put a Parcel object back into the pool. You must not touch |
| 415 | * the object after this call. |
| 416 | */ |
| 417 | public final void recycle() { |
| 418 | if (DEBUG_RECYCLE) mStack = null; |
| 419 | freeBuffer(); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 420 | |
| 421 | final Parcel[] pool; |
| 422 | if (mOwnsNativeParcelObject) { |
| 423 | pool = sOwnedPool; |
| 424 | } else { |
| 425 | mNativePtr = 0; |
| 426 | pool = sHolderPool; |
| 427 | } |
| 428 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | synchronized (pool) { |
| 430 | for (int i=0; i<POOL_SIZE; i++) { |
| 431 | if (pool[i] == null) { |
| 432 | pool[i] = this; |
| 433 | return; |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 439 | /** |
| 440 | * Set a {@link ReadWriteHelper}, which can be used to avoid having duplicate strings, for |
| 441 | * example. |
| 442 | * |
| 443 | * @hide |
| 444 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 445 | public void setReadWriteHelper(@Nullable ReadWriteHelper helper) { |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 446 | mReadWriteHelper = helper != null ? helper : ReadWriteHelper.DEFAULT; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * @return whether this parcel has a {@link ReadWriteHelper}. |
| 451 | * |
| 452 | * @hide |
| 453 | */ |
| 454 | public boolean hasReadWriteHelper() { |
| 455 | return (mReadWriteHelper != null) && (mReadWriteHelper != ReadWriteHelper.DEFAULT); |
| 456 | } |
| 457 | |
Dianne Hackborn | fabb70b | 2014-11-11 12:22:36 -0800 | [diff] [blame] | 458 | /** @hide */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 459 | @UnsupportedAppUsage |
Dianne Hackborn | fabb70b | 2014-11-11 12:22:36 -0800 | [diff] [blame] | 460 | public static native long getGlobalAllocSize(); |
| 461 | |
| 462 | /** @hide */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 463 | @UnsupportedAppUsage |
Dianne Hackborn | fabb70b | 2014-11-11 12:22:36 -0800 | [diff] [blame] | 464 | public static native long getGlobalAllocCount(); |
| 465 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | /** |
| 467 | * Returns the total amount of data contained in the parcel. |
| 468 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 469 | public final int dataSize() { |
| 470 | return nativeDataSize(mNativePtr); |
| 471 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | |
| 473 | /** |
| 474 | * Returns the amount of data remaining to be read from the |
| 475 | * parcel. That is, {@link #dataSize}-{@link #dataPosition}. |
| 476 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 477 | public final int dataAvail() { |
| 478 | return nativeDataAvail(mNativePtr); |
| 479 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | |
| 481 | /** |
| 482 | * Returns the current position in the parcel data. Never |
| 483 | * more than {@link #dataSize}. |
| 484 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 485 | public final int dataPosition() { |
| 486 | return nativeDataPosition(mNativePtr); |
| 487 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | |
| 489 | /** |
| 490 | * Returns the total amount of space in the parcel. This is always |
| 491 | * >= {@link #dataSize}. The difference between it and dataSize() is the |
| 492 | * amount of room left until the parcel needs to re-allocate its |
| 493 | * data buffer. |
| 494 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 495 | public final int dataCapacity() { |
| 496 | return nativeDataCapacity(mNativePtr); |
| 497 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | |
| 499 | /** |
| 500 | * Change the amount of data in the parcel. Can be either smaller or |
| 501 | * larger than the current size. If larger than the current capacity, |
| 502 | * more memory will be allocated. |
| 503 | * |
| 504 | * @param size The new number of bytes in the Parcel. |
| 505 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 506 | public final void setDataSize(int size) { |
Michael Wachenschwanz | 138bebf | 2017-05-18 22:09:18 +0000 | [diff] [blame] | 507 | updateNativeSize(nativeSetDataSize(mNativePtr, size)); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 508 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | |
| 510 | /** |
| 511 | * Move the current read/write position in the parcel. |
| 512 | * @param pos New offset in the parcel; must be between 0 and |
| 513 | * {@link #dataSize}. |
| 514 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 515 | public final void setDataPosition(int pos) { |
| 516 | nativeSetDataPosition(mNativePtr, pos); |
| 517 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | |
| 519 | /** |
| 520 | * Change the capacity (current available space) of the parcel. |
| 521 | * |
| 522 | * @param size The new capacity of the parcel, in bytes. Can not be |
| 523 | * less than {@link #dataSize} -- that is, you can not drop existing data |
| 524 | * with this method. |
| 525 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 526 | public final void setDataCapacity(int size) { |
| 527 | nativeSetDataCapacity(mNativePtr, size); |
| 528 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 530 | /** @hide */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 531 | public final boolean pushAllowFds(boolean allowFds) { |
| 532 | return nativePushAllowFds(mNativePtr, allowFds); |
| 533 | } |
Dianne Hackborn | c04db7e | 2011-10-03 21:09:35 -0700 | [diff] [blame] | 534 | |
| 535 | /** @hide */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 536 | public final void restoreAllowFds(boolean lastValue) { |
| 537 | nativeRestoreAllowFds(mNativePtr, lastValue); |
| 538 | } |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 539 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | /** |
| 541 | * Returns the raw bytes of the parcel. |
| 542 | * |
| 543 | * <p class="note">The data you retrieve here <strong>must not</strong> |
| 544 | * be placed in any kind of persistent storage (on local disk, across |
| 545 | * a network, etc). For that, you should use standard serialization |
| 546 | * or another kind of general serialization mechanism. The Parcel |
| 547 | * marshalled representation is highly optimized for local IPC, and as |
| 548 | * such does not attempt to maintain compatibility with data created |
| 549 | * in different versions of the platform. |
| 550 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 551 | public final byte[] marshall() { |
| 552 | return nativeMarshall(mNativePtr); |
| 553 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 554 | |
| 555 | /** |
| 556 | * Set the bytes in data to be the raw bytes of this Parcel. |
| 557 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 558 | public final void unmarshall(@NonNull byte[] data, int offset, int length) { |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 559 | updateNativeSize(nativeUnmarshall(mNativePtr, data, offset, length)); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 560 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 562 | public final void appendFrom(Parcel parcel, int offset, int length) { |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 563 | updateNativeSize(nativeAppendFrom(mNativePtr, parcel.mNativePtr, offset, length)); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 564 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 566 | /** @hide */ |
| 567 | public final int compareData(Parcel other) { |
| 568 | return nativeCompareData(mNativePtr, other.mNativePtr); |
| 569 | } |
| 570 | |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 571 | /** @hide */ |
| 572 | public final void setClassCookie(Class clz, Object cookie) { |
| 573 | if (mClassCookies == null) { |
| 574 | mClassCookies = new ArrayMap<>(); |
| 575 | } |
| 576 | mClassCookies.put(clz, cookie); |
| 577 | } |
| 578 | |
| 579 | /** @hide */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 580 | @Nullable |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 581 | public final Object getClassCookie(Class clz) { |
| 582 | return mClassCookies != null ? mClassCookies.get(clz) : null; |
| 583 | } |
| 584 | |
| 585 | /** @hide */ |
| 586 | public final void adoptClassCookies(Parcel from) { |
| 587 | mClassCookies = from.mClassCookies; |
| 588 | } |
| 589 | |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 590 | /** @hide */ |
| 591 | public Map<Class, Object> copyClassCookies() { |
| 592 | return new ArrayMap<>(mClassCookies); |
| 593 | } |
| 594 | |
| 595 | /** @hide */ |
| 596 | public void putClassCookies(Map<Class, Object> cookies) { |
| 597 | if (cookies == null) { |
| 598 | return; |
| 599 | } |
| 600 | if (mClassCookies == null) { |
| 601 | mClassCookies = new ArrayMap<>(); |
| 602 | } |
| 603 | mClassCookies.putAll(cookies); |
| 604 | } |
| 605 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | /** |
| 607 | * Report whether the parcel contains any marshalled file descriptors. |
| 608 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 609 | public final boolean hasFileDescriptors() { |
| 610 | return nativeHasFileDescriptors(mNativePtr); |
| 611 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | |
| 613 | /** |
| 614 | * Store or read an IBinder interface token in the parcel at the current |
| 615 | * {@link #dataPosition}. This is used to validate that the marshalled |
| 616 | * transaction is intended for the target interface. |
| 617 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 618 | public final void writeInterfaceToken(String interfaceName) { |
| 619 | nativeWriteInterfaceToken(mNativePtr, interfaceName); |
| 620 | } |
| 621 | |
| 622 | public final void enforceInterface(String interfaceName) { |
| 623 | nativeEnforceInterface(mNativePtr, interfaceName); |
| 624 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | |
| 626 | /** |
Olivier Gaillard | bab444a | 2019-01-30 17:11:40 +0000 | [diff] [blame] | 627 | * Writes the work source uid to the request headers. |
| 628 | * |
| 629 | * <p>It requires the headers to have been written/read already to replace the work source. |
| 630 | * |
| 631 | * @return true if the request headers have been updated. |
| 632 | * |
| 633 | * @hide |
| 634 | */ |
| 635 | public boolean replaceCallingWorkSourceUid(int workSourceUid) { |
| 636 | return nativeReplaceCallingWorkSourceUid(mNativePtr, workSourceUid); |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * Reads the work source uid from the request headers. |
| 641 | * |
| 642 | * <p>Unlike other read methods, this method does not read the parcel at the current |
| 643 | * {@link #dataPosition}. It will set the {@link #dataPosition} before the read and restore the |
| 644 | * position after reading the request header. |
| 645 | * |
| 646 | * @return the work source uid or {@link Binder#UNSET_WORKSOURCE} if headers have not been |
| 647 | * written/parsed yet. |
| 648 | * |
| 649 | * @hide |
| 650 | */ |
| 651 | public int readCallingWorkSourceUid() { |
| 652 | return nativeReadCallingWorkSourceUid(mNativePtr); |
| 653 | } |
| 654 | |
| 655 | /** |
Elliott Hughes | a28b83e | 2011-02-28 14:26:13 -0800 | [diff] [blame] | 656 | * Write a byte array into the parcel at the current {@link #dataPosition}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | * growing {@link #dataCapacity} if needed. |
| 658 | * @param b Bytes to place into the parcel. |
| 659 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 660 | public final void writeByteArray(@Nullable byte[] b) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 661 | writeByteArray(b, 0, (b != null) ? b.length : 0); |
| 662 | } |
| 663 | |
| 664 | /** |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 665 | * Write a byte array into the parcel at the current {@link #dataPosition}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 666 | * growing {@link #dataCapacity} if needed. |
| 667 | * @param b Bytes to place into the parcel. |
| 668 | * @param offset Index of first byte to be written. |
| 669 | * @param len Number of bytes to write. |
| 670 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 671 | public final void writeByteArray(@Nullable byte[] b, int offset, int len) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | if (b == null) { |
| 673 | writeInt(-1); |
| 674 | return; |
| 675 | } |
Pete Gillin | 60f55a25 | 2018-05-10 15:40:32 +0100 | [diff] [blame] | 676 | ArrayUtils.throwsIfOutOfBounds(b.length, offset, len); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 677 | nativeWriteByteArray(mNativePtr, b, offset, len); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | } |
| 679 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 | /** |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 681 | * Write a blob of data into the parcel at the current {@link #dataPosition}, |
| 682 | * growing {@link #dataCapacity} if needed. |
| 683 | * @param b Bytes to place into the parcel. |
| 684 | * {@hide} |
Sandeep Siddhartha | 39c12fa | 2014-07-25 18:37:29 -0700 | [diff] [blame] | 685 | * {@SystemApi} |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 686 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 687 | @UnsupportedAppUsage |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 688 | public final void writeBlob(@Nullable byte[] b) { |
Dan Sandler | b9f7aac3 | 2015-03-04 13:08:49 -0500 | [diff] [blame] | 689 | writeBlob(b, 0, (b != null) ? b.length : 0); |
| 690 | } |
| 691 | |
| 692 | /** |
| 693 | * Write a blob of data into the parcel at the current {@link #dataPosition}, |
| 694 | * growing {@link #dataCapacity} if needed. |
| 695 | * @param b Bytes to place into the parcel. |
| 696 | * @param offset Index of first byte to be written. |
| 697 | * @param len Number of bytes to write. |
| 698 | * {@hide} |
| 699 | * {@SystemApi} |
| 700 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 701 | public final void writeBlob(@Nullable byte[] b, int offset, int len) { |
Dan Sandler | b9f7aac3 | 2015-03-04 13:08:49 -0500 | [diff] [blame] | 702 | if (b == null) { |
| 703 | writeInt(-1); |
| 704 | return; |
| 705 | } |
Pete Gillin | 60f55a25 | 2018-05-10 15:40:32 +0100 | [diff] [blame] | 706 | ArrayUtils.throwsIfOutOfBounds(b.length, offset, len); |
Dan Sandler | b9f7aac3 | 2015-03-04 13:08:49 -0500 | [diff] [blame] | 707 | nativeWriteBlob(mNativePtr, b, offset, len); |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 711 | * Write an integer value into the parcel at the current dataPosition(), |
| 712 | * growing dataCapacity() if needed. |
| 713 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 714 | public final void writeInt(int val) { |
| 715 | nativeWriteInt(mNativePtr, val); |
| 716 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | |
| 718 | /** |
| 719 | * Write a long integer value into the parcel at the current dataPosition(), |
| 720 | * growing dataCapacity() if needed. |
| 721 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 722 | public final void writeLong(long val) { |
| 723 | nativeWriteLong(mNativePtr, val); |
| 724 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 725 | |
| 726 | /** |
| 727 | * Write a floating point value into the parcel at the current |
| 728 | * dataPosition(), growing dataCapacity() if needed. |
| 729 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 730 | public final void writeFloat(float val) { |
| 731 | nativeWriteFloat(mNativePtr, val); |
| 732 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 733 | |
| 734 | /** |
| 735 | * Write a double precision floating point value into the parcel at the |
| 736 | * current dataPosition(), growing dataCapacity() if needed. |
| 737 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 738 | public final void writeDouble(double val) { |
| 739 | nativeWriteDouble(mNativePtr, val); |
| 740 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | |
| 742 | /** |
| 743 | * Write a string value into the parcel at the current dataPosition(), |
| 744 | * growing dataCapacity() if needed. |
| 745 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 746 | public final void writeString(@Nullable String val) { |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 747 | mReadWriteHelper.writeString(this, val); |
| 748 | } |
| 749 | |
| 750 | /** |
| 751 | * Write a string without going though a {@link ReadWriteHelper}. Subclasses of |
| 752 | * {@link ReadWriteHelper} must use this method instead of {@link #writeString} to avoid |
| 753 | * infinity recursive calls. |
| 754 | * |
| 755 | * @hide |
| 756 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 757 | public void writeStringNoHelper(@Nullable String val) { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 758 | nativeWriteString(mNativePtr, val); |
| 759 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 760 | |
Jake Wharton | 2ffa431 | 2018-04-23 17:47:14 -0400 | [diff] [blame] | 761 | /** |
| 762 | * Write a boolean value into the parcel at the current dataPosition(), |
| 763 | * growing dataCapacity() if needed. |
| 764 | * |
| 765 | * <p>Note: This method currently delegates to writeInt with a value of 1 or 0 |
| 766 | * for true or false, respectively, but may change in the future. |
| 767 | */ |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 768 | public final void writeBoolean(boolean val) { |
| 769 | writeInt(val ? 1 : 0); |
| 770 | } |
| 771 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | /** |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 773 | * Write a CharSequence value into the parcel at the current dataPosition(), |
| 774 | * growing dataCapacity() if needed. |
| 775 | * @hide |
| 776 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 777 | @UnsupportedAppUsage |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 778 | public final void writeCharSequence(@Nullable CharSequence val) { |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 779 | TextUtils.writeToParcel(val, this, 0); |
| 780 | } |
| 781 | |
| 782 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 783 | * Write an object into the parcel at the current dataPosition(), |
| 784 | * growing dataCapacity() if needed. |
| 785 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 786 | public final void writeStrongBinder(IBinder val) { |
| 787 | nativeWriteStrongBinder(mNativePtr, val); |
| 788 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | |
| 790 | /** |
| 791 | * Write an object into the parcel at the current dataPosition(), |
| 792 | * growing dataCapacity() if needed. |
| 793 | */ |
| 794 | public final void writeStrongInterface(IInterface val) { |
| 795 | writeStrongBinder(val == null ? null : val.asBinder()); |
| 796 | } |
| 797 | |
| 798 | /** |
| 799 | * Write a FileDescriptor into the parcel at the current dataPosition(), |
| 800 | * growing dataCapacity() if needed. |
Dan Egnor | b3e4ef3 | 2010-07-20 09:03:35 -0700 | [diff] [blame] | 801 | * |
| 802 | * <p class="caution">The file descriptor will not be closed, which may |
| 803 | * result in file descriptor leaks when objects are returned from Binder |
| 804 | * calls. Use {@link ParcelFileDescriptor#writeToParcel} instead, which |
| 805 | * accepts contextual flags and will close the original file descriptor |
| 806 | * if {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} is set.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 808 | public final void writeFileDescriptor(@NonNull FileDescriptor val) { |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 809 | updateNativeSize(nativeWriteFileDescriptor(mNativePtr, val)); |
| 810 | } |
| 811 | |
| 812 | private void updateNativeSize(long newNativeSize) { |
| 813 | if (mOwnsNativeParcelObject) { |
| 814 | if (newNativeSize > Integer.MAX_VALUE) { |
| 815 | newNativeSize = Integer.MAX_VALUE; |
| 816 | } |
| 817 | if (newNativeSize != mNativeSize) { |
| 818 | int delta = (int) (newNativeSize - mNativeSize); |
| 819 | if (delta > 0) { |
| 820 | VMRuntime.getRuntime().registerNativeAllocation(delta); |
| 821 | } else { |
| 822 | VMRuntime.getRuntime().registerNativeFree(-delta); |
| 823 | } |
| 824 | mNativeSize = newNativeSize; |
| 825 | } |
| 826 | } |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 827 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 828 | |
| 829 | /** |
Casey Dahlin | 2f974b2 | 2015-11-05 12:19:13 -0800 | [diff] [blame] | 830 | * {@hide} |
| 831 | * This will be the new name for writeFileDescriptor, for consistency. |
| 832 | **/ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 833 | public final void writeRawFileDescriptor(@NonNull FileDescriptor val) { |
Casey Dahlin | 2f974b2 | 2015-11-05 12:19:13 -0800 | [diff] [blame] | 834 | nativeWriteFileDescriptor(mNativePtr, val); |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * {@hide} |
| 839 | * Write an array of FileDescriptor objects into the Parcel. |
| 840 | * |
| 841 | * @param value The array of objects to be written. |
| 842 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 843 | public final void writeRawFileDescriptorArray(@Nullable FileDescriptor[] value) { |
Casey Dahlin | 2f974b2 | 2015-11-05 12:19:13 -0800 | [diff] [blame] | 844 | if (value != null) { |
| 845 | int N = value.length; |
| 846 | writeInt(N); |
| 847 | for (int i=0; i<N; i++) { |
| 848 | writeRawFileDescriptor(value[i]); |
| 849 | } |
| 850 | } else { |
| 851 | writeInt(-1); |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | /** |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 856 | * Write a byte value into the parcel at the current dataPosition(), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 857 | * growing dataCapacity() if needed. |
Jake Wharton | 2ffa431 | 2018-04-23 17:47:14 -0400 | [diff] [blame] | 858 | * |
| 859 | * <p>Note: This method currently delegates to writeInt but may change in |
| 860 | * the future. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | */ |
| 862 | public final void writeByte(byte val) { |
| 863 | writeInt(val); |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * Please use {@link #writeBundle} instead. Flattens a Map into the parcel |
| 868 | * at the current dataPosition(), |
| 869 | * growing dataCapacity() if needed. The Map keys must be String objects. |
| 870 | * The Map values are written using {@link #writeValue} and must follow |
| 871 | * the specification there. |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 872 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 873 | * <p>It is strongly recommended to use {@link #writeBundle} instead of |
| 874 | * this method, since the Bundle class provides a type-safe API that |
| 875 | * allows you to avoid mysterious type errors at the point of marshalling. |
| 876 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 877 | public final void writeMap(@Nullable Map val) { |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 878 | writeMapInternal((Map<String, Object>) val); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | /** |
| 882 | * Flatten a Map into the parcel at the current dataPosition(), |
| 883 | * growing dataCapacity() if needed. The Map keys must be String objects. |
| 884 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 885 | /* package */ void writeMapInternal(@Nullable Map<String,Object> val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | if (val == null) { |
| 887 | writeInt(-1); |
| 888 | return; |
| 889 | } |
| 890 | Set<Map.Entry<String,Object>> entries = val.entrySet(); |
Michael Wachenschwanz | 80b9d69 | 2018-08-24 21:50:35 -0700 | [diff] [blame] | 891 | int size = entries.size(); |
| 892 | writeInt(size); |
| 893 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 894 | for (Map.Entry<String,Object> e : entries) { |
| 895 | writeValue(e.getKey()); |
| 896 | writeValue(e.getValue()); |
Michael Wachenschwanz | 80b9d69 | 2018-08-24 21:50:35 -0700 | [diff] [blame] | 897 | size--; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 898 | } |
Michael Wachenschwanz | 80b9d69 | 2018-08-24 21:50:35 -0700 | [diff] [blame] | 899 | |
| 900 | if (size != 0) { |
| 901 | throw new BadParcelableException("Map size does not match number of entries!"); |
| 902 | } |
| 903 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | /** |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 907 | * Flatten an ArrayMap into the parcel at the current dataPosition(), |
| 908 | * growing dataCapacity() if needed. The Map keys must be String objects. |
| 909 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 910 | /* package */ void writeArrayMapInternal(@Nullable ArrayMap<String, Object> val) { |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 911 | if (val == null) { |
| 912 | writeInt(-1); |
| 913 | return; |
| 914 | } |
Samuel Tan | 3cefe6a | 2015-12-14 13:29:17 -0800 | [diff] [blame] | 915 | // Keep the format of this Parcel in sync with writeToParcelInner() in |
| 916 | // frameworks/native/libs/binder/PersistableBundle.cpp. |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 917 | final int N = val.size(); |
| 918 | writeInt(N); |
Dianne Hackborn | e784d1e | 2013-09-20 18:13:52 -0700 | [diff] [blame] | 919 | if (DEBUG_ARRAY_MAP) { |
| 920 | RuntimeException here = new RuntimeException("here"); |
| 921 | here.fillInStackTrace(); |
| 922 | Log.d(TAG, "Writing " + N + " ArrayMap entries", here); |
| 923 | } |
Dianne Hackborn | 8aee64d | 2013-10-25 10:41:50 -0700 | [diff] [blame] | 924 | int startPos; |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 925 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 8aee64d | 2013-10-25 10:41:50 -0700 | [diff] [blame] | 926 | if (DEBUG_ARRAY_MAP) startPos = dataPosition(); |
Dianne Hackborn | 9c3e74f | 2014-08-13 15:39:50 -0700 | [diff] [blame] | 927 | writeString(val.keyAt(i)); |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 928 | writeValue(val.valueAt(i)); |
Dianne Hackborn | 8aee64d | 2013-10-25 10:41:50 -0700 | [diff] [blame] | 929 | if (DEBUG_ARRAY_MAP) Log.d(TAG, " Write #" + i + " " |
| 930 | + (dataPosition()-startPos) + " bytes: key=0x" |
| 931 | + Integer.toHexString(val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0) |
| 932 | + " " + val.keyAt(i)); |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | |
| 936 | /** |
Dianne Hackborn | 9c3e74f | 2014-08-13 15:39:50 -0700 | [diff] [blame] | 937 | * @hide For testing only. |
| 938 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 939 | @UnsupportedAppUsage |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 940 | public void writeArrayMap(@Nullable ArrayMap<String, Object> val) { |
Dianne Hackborn | 9c3e74f | 2014-08-13 15:39:50 -0700 | [diff] [blame] | 941 | writeArrayMapInternal(val); |
| 942 | } |
| 943 | |
| 944 | /** |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 945 | * Flatten an {@link ArrayMap} with string keys containing a particular object |
| 946 | * type into the parcel at the current dataPosition() and growing dataCapacity() |
| 947 | * if needed. The type of the objects in the array must be one that implements |
| 948 | * Parcelable. Only the raw data of the objects is written and not their type, |
| 949 | * so you must use the corresponding {@link #createTypedArrayMap(Parcelable.Creator)} |
| 950 | * |
| 951 | * @param val The map of objects to be written. |
| 952 | * @param parcelableFlags The parcelable flags to use. |
| 953 | * |
| 954 | * @see #createTypedArrayMap(Parcelable.Creator) |
| 955 | * @see Parcelable |
| 956 | */ |
| 957 | public <T extends Parcelable> void writeTypedArrayMap(@Nullable ArrayMap<String, T> val, |
| 958 | int parcelableFlags) { |
| 959 | if (val == null) { |
| 960 | writeInt(-1); |
| 961 | return; |
| 962 | } |
| 963 | final int count = val.size(); |
| 964 | writeInt(count); |
| 965 | for (int i = 0; i < count; i++) { |
| 966 | writeString(val.keyAt(i)); |
| 967 | writeTypedObject(val.valueAt(i), parcelableFlags); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | /** |
Svet Ganov | ddb9488 | 2016-06-23 19:55:24 -0700 | [diff] [blame] | 972 | * Write an array set to the parcel. |
| 973 | * |
| 974 | * @param val The array set to write. |
| 975 | * |
| 976 | * @hide |
| 977 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 978 | @UnsupportedAppUsage |
Svet Ganov | ddb9488 | 2016-06-23 19:55:24 -0700 | [diff] [blame] | 979 | public void writeArraySet(@Nullable ArraySet<? extends Object> val) { |
| 980 | final int size = (val != null) ? val.size() : -1; |
| 981 | writeInt(size); |
| 982 | for (int i = 0; i < size; i++) { |
| 983 | writeValue(val.valueAt(i)); |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | * Flatten a Bundle into the parcel at the current dataPosition(), |
| 989 | * growing dataCapacity() if needed. |
| 990 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 991 | public final void writeBundle(@Nullable Bundle val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 992 | if (val == null) { |
| 993 | writeInt(-1); |
| 994 | return; |
| 995 | } |
| 996 | |
Dianne Hackborn | 6aff905 | 2009-05-22 13:20:23 -0700 | [diff] [blame] | 997 | val.writeToParcel(this, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | /** |
Craig Mautner | 719e6b1 | 2014-04-04 20:29:41 -0700 | [diff] [blame] | 1001 | * Flatten a PersistableBundle into the parcel at the current dataPosition(), |
| 1002 | * growing dataCapacity() if needed. |
| 1003 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1004 | public final void writePersistableBundle(@Nullable PersistableBundle val) { |
Craig Mautner | 719e6b1 | 2014-04-04 20:29:41 -0700 | [diff] [blame] | 1005 | if (val == null) { |
| 1006 | writeInt(-1); |
| 1007 | return; |
| 1008 | } |
| 1009 | |
| 1010 | val.writeToParcel(this, 0); |
| 1011 | } |
| 1012 | |
| 1013 | /** |
Jeff Sharkey | 5ef3398 | 2014-09-04 18:13:39 -0700 | [diff] [blame] | 1014 | * Flatten a Size into the parcel at the current dataPosition(), |
| 1015 | * growing dataCapacity() if needed. |
| 1016 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1017 | public final void writeSize(@NonNull Size val) { |
Jeff Sharkey | 5ef3398 | 2014-09-04 18:13:39 -0700 | [diff] [blame] | 1018 | writeInt(val.getWidth()); |
| 1019 | writeInt(val.getHeight()); |
| 1020 | } |
| 1021 | |
| 1022 | /** |
| 1023 | * Flatten a SizeF into the parcel at the current dataPosition(), |
| 1024 | * growing dataCapacity() if needed. |
| 1025 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1026 | public final void writeSizeF(@NonNull SizeF val) { |
Jeff Sharkey | 5ef3398 | 2014-09-04 18:13:39 -0700 | [diff] [blame] | 1027 | writeFloat(val.getWidth()); |
| 1028 | writeFloat(val.getHeight()); |
| 1029 | } |
| 1030 | |
| 1031 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1032 | * Flatten a List into the parcel at the current dataPosition(), growing |
| 1033 | * dataCapacity() if needed. The List values are written using |
| 1034 | * {@link #writeValue} and must follow the specification there. |
| 1035 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1036 | public final void writeList(@Nullable List val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1037 | if (val == null) { |
| 1038 | writeInt(-1); |
| 1039 | return; |
| 1040 | } |
| 1041 | int N = val.size(); |
| 1042 | int i=0; |
| 1043 | writeInt(N); |
| 1044 | while (i < N) { |
| 1045 | writeValue(val.get(i)); |
| 1046 | i++; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | /** |
| 1051 | * Flatten an Object array into the parcel at the current dataPosition(), |
| 1052 | * growing dataCapacity() if needed. The array values are written using |
| 1053 | * {@link #writeValue} and must follow the specification there. |
| 1054 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1055 | public final void writeArray(@Nullable Object[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1056 | if (val == null) { |
| 1057 | writeInt(-1); |
| 1058 | return; |
| 1059 | } |
| 1060 | int N = val.length; |
| 1061 | int i=0; |
| 1062 | writeInt(N); |
| 1063 | while (i < N) { |
| 1064 | writeValue(val[i]); |
| 1065 | i++; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | /** |
| 1070 | * Flatten a generic SparseArray into the parcel at the current |
| 1071 | * dataPosition(), growing dataCapacity() if needed. The SparseArray |
| 1072 | * values are written using {@link #writeValue} and must follow the |
| 1073 | * specification there. |
| 1074 | */ |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 1075 | public final <T> void writeSparseArray(@Nullable SparseArray<T> val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1076 | if (val == null) { |
| 1077 | writeInt(-1); |
| 1078 | return; |
| 1079 | } |
| 1080 | int N = val.size(); |
| 1081 | writeInt(N); |
| 1082 | int i=0; |
| 1083 | while (i < N) { |
| 1084 | writeInt(val.keyAt(i)); |
| 1085 | writeValue(val.valueAt(i)); |
| 1086 | i++; |
| 1087 | } |
| 1088 | } |
| 1089 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1090 | public final void writeSparseBooleanArray(@Nullable SparseBooleanArray val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1091 | if (val == null) { |
| 1092 | writeInt(-1); |
| 1093 | return; |
| 1094 | } |
| 1095 | int N = val.size(); |
| 1096 | writeInt(N); |
| 1097 | int i=0; |
| 1098 | while (i < N) { |
| 1099 | writeInt(val.keyAt(i)); |
| 1100 | writeByte((byte)(val.valueAt(i) ? 1 : 0)); |
| 1101 | i++; |
| 1102 | } |
| 1103 | } |
| 1104 | |
Adam Lesinski | 205656d | 2017-03-23 13:38:26 -0700 | [diff] [blame] | 1105 | /** |
| 1106 | * @hide |
| 1107 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1108 | public final void writeSparseIntArray(@Nullable SparseIntArray val) { |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 1109 | if (val == null) { |
| 1110 | writeInt(-1); |
| 1111 | return; |
| 1112 | } |
| 1113 | int N = val.size(); |
| 1114 | writeInt(N); |
| 1115 | int i=0; |
| 1116 | while (i < N) { |
| 1117 | writeInt(val.keyAt(i)); |
| 1118 | writeInt(val.valueAt(i)); |
| 1119 | i++; |
| 1120 | } |
| 1121 | } |
| 1122 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1123 | public final void writeBooleanArray(@Nullable boolean[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1124 | if (val != null) { |
| 1125 | int N = val.length; |
| 1126 | writeInt(N); |
| 1127 | for (int i=0; i<N; i++) { |
| 1128 | writeInt(val[i] ? 1 : 0); |
| 1129 | } |
| 1130 | } else { |
| 1131 | writeInt(-1); |
| 1132 | } |
| 1133 | } |
| 1134 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1135 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1136 | public final boolean[] createBooleanArray() { |
| 1137 | int N = readInt(); |
| 1138 | // >>2 as a fast divide-by-4 works in the create*Array() functions |
| 1139 | // because dataAvail() will never return a negative number. 4 is |
| 1140 | // the size of a stored boolean in the stream. |
| 1141 | if (N >= 0 && N <= (dataAvail() >> 2)) { |
| 1142 | boolean[] val = new boolean[N]; |
| 1143 | for (int i=0; i<N; i++) { |
| 1144 | val[i] = readInt() != 0; |
| 1145 | } |
| 1146 | return val; |
| 1147 | } else { |
| 1148 | return null; |
| 1149 | } |
| 1150 | } |
| 1151 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1152 | public final void readBooleanArray(@NonNull boolean[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1153 | int N = readInt(); |
| 1154 | if (N == val.length) { |
| 1155 | for (int i=0; i<N; i++) { |
| 1156 | val[i] = readInt() != 0; |
| 1157 | } |
| 1158 | } else { |
| 1159 | throw new RuntimeException("bad array lengths"); |
| 1160 | } |
| 1161 | } |
| 1162 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1163 | public final void writeCharArray(@Nullable char[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1164 | if (val != null) { |
| 1165 | int N = val.length; |
| 1166 | writeInt(N); |
| 1167 | for (int i=0; i<N; i++) { |
| 1168 | writeInt((int)val[i]); |
| 1169 | } |
| 1170 | } else { |
| 1171 | writeInt(-1); |
| 1172 | } |
| 1173 | } |
| 1174 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1175 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1176 | public final char[] createCharArray() { |
| 1177 | int N = readInt(); |
| 1178 | if (N >= 0 && N <= (dataAvail() >> 2)) { |
| 1179 | char[] val = new char[N]; |
| 1180 | for (int i=0; i<N; i++) { |
| 1181 | val[i] = (char)readInt(); |
| 1182 | } |
| 1183 | return val; |
| 1184 | } else { |
| 1185 | return null; |
| 1186 | } |
| 1187 | } |
| 1188 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1189 | public final void readCharArray(@NonNull char[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1190 | int N = readInt(); |
| 1191 | if (N == val.length) { |
| 1192 | for (int i=0; i<N; i++) { |
| 1193 | val[i] = (char)readInt(); |
| 1194 | } |
| 1195 | } else { |
| 1196 | throw new RuntimeException("bad array lengths"); |
| 1197 | } |
| 1198 | } |
| 1199 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1200 | public final void writeIntArray(@Nullable int[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1201 | if (val != null) { |
| 1202 | int N = val.length; |
| 1203 | writeInt(N); |
| 1204 | for (int i=0; i<N; i++) { |
| 1205 | writeInt(val[i]); |
| 1206 | } |
| 1207 | } else { |
| 1208 | writeInt(-1); |
| 1209 | } |
| 1210 | } |
| 1211 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1212 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1213 | public final int[] createIntArray() { |
| 1214 | int N = readInt(); |
| 1215 | if (N >= 0 && N <= (dataAvail() >> 2)) { |
| 1216 | int[] val = new int[N]; |
| 1217 | for (int i=0; i<N; i++) { |
| 1218 | val[i] = readInt(); |
| 1219 | } |
| 1220 | return val; |
| 1221 | } else { |
| 1222 | return null; |
| 1223 | } |
| 1224 | } |
| 1225 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1226 | public final void readIntArray(@NonNull int[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1227 | int N = readInt(); |
| 1228 | if (N == val.length) { |
| 1229 | for (int i=0; i<N; i++) { |
| 1230 | val[i] = readInt(); |
| 1231 | } |
| 1232 | } else { |
| 1233 | throw new RuntimeException("bad array lengths"); |
| 1234 | } |
| 1235 | } |
| 1236 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1237 | public final void writeLongArray(@Nullable long[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1238 | if (val != null) { |
| 1239 | int N = val.length; |
| 1240 | writeInt(N); |
| 1241 | for (int i=0; i<N; i++) { |
| 1242 | writeLong(val[i]); |
| 1243 | } |
| 1244 | } else { |
| 1245 | writeInt(-1); |
| 1246 | } |
| 1247 | } |
| 1248 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1249 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1250 | public final long[] createLongArray() { |
| 1251 | int N = readInt(); |
| 1252 | // >>3 because stored longs are 64 bits |
| 1253 | if (N >= 0 && N <= (dataAvail() >> 3)) { |
| 1254 | long[] val = new long[N]; |
| 1255 | for (int i=0; i<N; i++) { |
| 1256 | val[i] = readLong(); |
| 1257 | } |
| 1258 | return val; |
| 1259 | } else { |
| 1260 | return null; |
| 1261 | } |
| 1262 | } |
| 1263 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1264 | public final void readLongArray(@NonNull long[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1265 | int N = readInt(); |
| 1266 | if (N == val.length) { |
| 1267 | for (int i=0; i<N; i++) { |
| 1268 | val[i] = readLong(); |
| 1269 | } |
| 1270 | } else { |
| 1271 | throw new RuntimeException("bad array lengths"); |
| 1272 | } |
| 1273 | } |
| 1274 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1275 | public final void writeFloatArray(@Nullable float[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1276 | if (val != null) { |
| 1277 | int N = val.length; |
| 1278 | writeInt(N); |
| 1279 | for (int i=0; i<N; i++) { |
| 1280 | writeFloat(val[i]); |
| 1281 | } |
| 1282 | } else { |
| 1283 | writeInt(-1); |
| 1284 | } |
| 1285 | } |
| 1286 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1287 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1288 | public final float[] createFloatArray() { |
| 1289 | int N = readInt(); |
| 1290 | // >>2 because stored floats are 4 bytes |
| 1291 | if (N >= 0 && N <= (dataAvail() >> 2)) { |
| 1292 | float[] val = new float[N]; |
| 1293 | for (int i=0; i<N; i++) { |
| 1294 | val[i] = readFloat(); |
| 1295 | } |
| 1296 | return val; |
| 1297 | } else { |
| 1298 | return null; |
| 1299 | } |
| 1300 | } |
| 1301 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1302 | public final void readFloatArray(@NonNull float[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1303 | int N = readInt(); |
| 1304 | if (N == val.length) { |
| 1305 | for (int i=0; i<N; i++) { |
| 1306 | val[i] = readFloat(); |
| 1307 | } |
| 1308 | } else { |
| 1309 | throw new RuntimeException("bad array lengths"); |
| 1310 | } |
| 1311 | } |
| 1312 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1313 | public final void writeDoubleArray(@Nullable double[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1314 | if (val != null) { |
| 1315 | int N = val.length; |
| 1316 | writeInt(N); |
| 1317 | for (int i=0; i<N; i++) { |
| 1318 | writeDouble(val[i]); |
| 1319 | } |
| 1320 | } else { |
| 1321 | writeInt(-1); |
| 1322 | } |
| 1323 | } |
| 1324 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1325 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1326 | public final double[] createDoubleArray() { |
| 1327 | int N = readInt(); |
| 1328 | // >>3 because stored doubles are 8 bytes |
| 1329 | if (N >= 0 && N <= (dataAvail() >> 3)) { |
| 1330 | double[] val = new double[N]; |
| 1331 | for (int i=0; i<N; i++) { |
| 1332 | val[i] = readDouble(); |
| 1333 | } |
| 1334 | return val; |
| 1335 | } else { |
| 1336 | return null; |
| 1337 | } |
| 1338 | } |
| 1339 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1340 | public final void readDoubleArray(@NonNull double[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1341 | int N = readInt(); |
| 1342 | if (N == val.length) { |
| 1343 | for (int i=0; i<N; i++) { |
| 1344 | val[i] = readDouble(); |
| 1345 | } |
| 1346 | } else { |
| 1347 | throw new RuntimeException("bad array lengths"); |
| 1348 | } |
| 1349 | } |
| 1350 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1351 | public final void writeStringArray(@Nullable String[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1352 | if (val != null) { |
| 1353 | int N = val.length; |
| 1354 | writeInt(N); |
| 1355 | for (int i=0; i<N; i++) { |
| 1356 | writeString(val[i]); |
| 1357 | } |
| 1358 | } else { |
| 1359 | writeInt(-1); |
| 1360 | } |
| 1361 | } |
| 1362 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1363 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1364 | public final String[] createStringArray() { |
| 1365 | int N = readInt(); |
| 1366 | if (N >= 0) { |
| 1367 | String[] val = new String[N]; |
| 1368 | for (int i=0; i<N; i++) { |
| 1369 | val[i] = readString(); |
| 1370 | } |
| 1371 | return val; |
| 1372 | } else { |
| 1373 | return null; |
| 1374 | } |
| 1375 | } |
| 1376 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1377 | public final void readStringArray(@NonNull String[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1378 | int N = readInt(); |
| 1379 | if (N == val.length) { |
| 1380 | for (int i=0; i<N; i++) { |
| 1381 | val[i] = readString(); |
| 1382 | } |
| 1383 | } else { |
| 1384 | throw new RuntimeException("bad array lengths"); |
| 1385 | } |
| 1386 | } |
| 1387 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1388 | public final void writeBinderArray(@Nullable IBinder[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1389 | if (val != null) { |
| 1390 | int N = val.length; |
| 1391 | writeInt(N); |
| 1392 | for (int i=0; i<N; i++) { |
| 1393 | writeStrongBinder(val[i]); |
| 1394 | } |
| 1395 | } else { |
| 1396 | writeInt(-1); |
| 1397 | } |
| 1398 | } |
| 1399 | |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 1400 | /** |
| 1401 | * @hide |
| 1402 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1403 | public final void writeCharSequenceArray(@Nullable CharSequence[] val) { |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 1404 | if (val != null) { |
| 1405 | int N = val.length; |
| 1406 | writeInt(N); |
| 1407 | for (int i=0; i<N; i++) { |
| 1408 | writeCharSequence(val[i]); |
| 1409 | } |
| 1410 | } else { |
| 1411 | writeInt(-1); |
| 1412 | } |
| 1413 | } |
| 1414 | |
Dianne Hackborn | 3d07c94 | 2015-03-13 18:02:54 -0700 | [diff] [blame] | 1415 | /** |
| 1416 | * @hide |
| 1417 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1418 | public final void writeCharSequenceList(@Nullable ArrayList<CharSequence> val) { |
Dianne Hackborn | 3d07c94 | 2015-03-13 18:02:54 -0700 | [diff] [blame] | 1419 | if (val != null) { |
| 1420 | int N = val.size(); |
| 1421 | writeInt(N); |
| 1422 | for (int i=0; i<N; i++) { |
| 1423 | writeCharSequence(val.get(i)); |
| 1424 | } |
| 1425 | } else { |
| 1426 | writeInt(-1); |
| 1427 | } |
| 1428 | } |
| 1429 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1430 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1431 | public final IBinder[] createBinderArray() { |
| 1432 | int N = readInt(); |
| 1433 | if (N >= 0) { |
| 1434 | IBinder[] val = new IBinder[N]; |
| 1435 | for (int i=0; i<N; i++) { |
| 1436 | val[i] = readStrongBinder(); |
| 1437 | } |
| 1438 | return val; |
| 1439 | } else { |
| 1440 | return null; |
| 1441 | } |
| 1442 | } |
| 1443 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1444 | public final void readBinderArray(@NonNull IBinder[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1445 | int N = readInt(); |
| 1446 | if (N == val.length) { |
| 1447 | for (int i=0; i<N; i++) { |
| 1448 | val[i] = readStrongBinder(); |
| 1449 | } |
| 1450 | } else { |
| 1451 | throw new RuntimeException("bad array lengths"); |
| 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | /** |
| 1456 | * Flatten a List containing a particular object type into the parcel, at |
| 1457 | * the current dataPosition() and growing dataCapacity() if needed. The |
| 1458 | * type of the objects in the list must be one that implements Parcelable. |
| 1459 | * Unlike the generic writeList() method, however, only the raw data of the |
| 1460 | * objects is written and not their type, so you must use the corresponding |
| 1461 | * readTypedList() to unmarshall them. |
| 1462 | * |
| 1463 | * @param val The list of objects to be written. |
| 1464 | * |
| 1465 | * @see #createTypedArrayList |
| 1466 | * @see #readTypedList |
| 1467 | * @see Parcelable |
| 1468 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1469 | public final <T extends Parcelable> void writeTypedList(@Nullable List<T> val) { |
Sunny Goyal | 0e60f22 | 2017-09-21 21:39:20 -0700 | [diff] [blame] | 1470 | writeTypedList(val, 0); |
| 1471 | } |
| 1472 | |
| 1473 | /** |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 1474 | * Flatten a {@link SparseArray} containing a particular object type into the parcel |
| 1475 | * at the current dataPosition() and growing dataCapacity() if needed. The |
| 1476 | * type of the objects in the array must be one that implements Parcelable. |
| 1477 | * Unlike the generic {@link #writeSparseArray(SparseArray)} method, however, only |
| 1478 | * the raw data of the objects is written and not their type, so you must use the |
| 1479 | * corresponding {@link #createTypedSparseArray(Parcelable.Creator)}. |
| 1480 | * |
| 1481 | * @param val The list of objects to be written. |
| 1482 | * @param parcelableFlags The parcelable flags to use. |
| 1483 | * |
| 1484 | * @see #createTypedSparseArray(Parcelable.Creator) |
| 1485 | * @see Parcelable |
| 1486 | */ |
| 1487 | public final <T extends Parcelable> void writeTypedSparseArray(@Nullable SparseArray<T> val, |
| 1488 | int parcelableFlags) { |
| 1489 | if (val == null) { |
| 1490 | writeInt(-1); |
| 1491 | return; |
| 1492 | } |
| 1493 | final int count = val.size(); |
| 1494 | writeInt(count); |
| 1495 | for (int i = 0; i < count; i++) { |
| 1496 | writeInt(val.keyAt(i)); |
| 1497 | writeTypedObject(val.valueAt(i), parcelableFlags); |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | /** |
Sunny Goyal | 0e60f22 | 2017-09-21 21:39:20 -0700 | [diff] [blame] | 1502 | * @hide |
| 1503 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1504 | public <T extends Parcelable> void writeTypedList(@Nullable List<T> val, int parcelableFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1505 | if (val == null) { |
| 1506 | writeInt(-1); |
| 1507 | return; |
| 1508 | } |
| 1509 | int N = val.size(); |
| 1510 | int i=0; |
| 1511 | writeInt(N); |
| 1512 | while (i < N) { |
Sunny Goyal | 0e60f22 | 2017-09-21 21:39:20 -0700 | [diff] [blame] | 1513 | writeTypedObject(val.get(i), parcelableFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1514 | i++; |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | /** |
| 1519 | * Flatten a List containing String objects into the parcel, at |
| 1520 | * the current dataPosition() and growing dataCapacity() if needed. They |
| 1521 | * can later be retrieved with {@link #createStringArrayList} or |
| 1522 | * {@link #readStringList}. |
| 1523 | * |
| 1524 | * @param val The list of strings to be written. |
| 1525 | * |
| 1526 | * @see #createStringArrayList |
| 1527 | * @see #readStringList |
| 1528 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1529 | public final void writeStringList(@Nullable List<String> val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1530 | if (val == null) { |
| 1531 | writeInt(-1); |
| 1532 | return; |
| 1533 | } |
| 1534 | int N = val.size(); |
| 1535 | int i=0; |
| 1536 | writeInt(N); |
| 1537 | while (i < N) { |
| 1538 | writeString(val.get(i)); |
| 1539 | i++; |
| 1540 | } |
| 1541 | } |
| 1542 | |
| 1543 | /** |
| 1544 | * Flatten a List containing IBinder objects into the parcel, at |
| 1545 | * the current dataPosition() and growing dataCapacity() if needed. They |
| 1546 | * can later be retrieved with {@link #createBinderArrayList} or |
| 1547 | * {@link #readBinderList}. |
| 1548 | * |
| 1549 | * @param val The list of strings to be written. |
| 1550 | * |
| 1551 | * @see #createBinderArrayList |
| 1552 | * @see #readBinderList |
| 1553 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1554 | public final void writeBinderList(@Nullable List<IBinder> val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1555 | if (val == null) { |
| 1556 | writeInt(-1); |
| 1557 | return; |
| 1558 | } |
| 1559 | int N = val.size(); |
| 1560 | int i=0; |
| 1561 | writeInt(N); |
| 1562 | while (i < N) { |
| 1563 | writeStrongBinder(val.get(i)); |
| 1564 | i++; |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | /** |
Narayan Kamath | bea4871 | 2016-12-01 15:38:28 +0000 | [diff] [blame] | 1569 | * Flatten a {@code List} containing arbitrary {@code Parcelable} objects into this parcel |
| 1570 | * at the current position. They can later be retrieved using |
| 1571 | * {@link #readParcelableList(List, ClassLoader)} if required. |
| 1572 | * |
| 1573 | * @see #readParcelableList(List, ClassLoader) |
Narayan Kamath | bea4871 | 2016-12-01 15:38:28 +0000 | [diff] [blame] | 1574 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1575 | public final <T extends Parcelable> void writeParcelableList(@Nullable List<T> val, int flags) { |
Narayan Kamath | bea4871 | 2016-12-01 15:38:28 +0000 | [diff] [blame] | 1576 | if (val == null) { |
| 1577 | writeInt(-1); |
| 1578 | return; |
| 1579 | } |
| 1580 | |
| 1581 | int N = val.size(); |
| 1582 | int i=0; |
| 1583 | writeInt(N); |
| 1584 | while (i < N) { |
| 1585 | writeParcelable(val.get(i), flags); |
| 1586 | i++; |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | /** |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 1591 | * Flatten a homogeneous array containing a particular object type into |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1592 | * the parcel, at |
| 1593 | * the current dataPosition() and growing dataCapacity() if needed. The |
| 1594 | * type of the objects in the array must be one that implements Parcelable. |
| 1595 | * Unlike the {@link #writeParcelableArray} method, however, only the |
| 1596 | * raw data of the objects is written and not their type, so you must use |
| 1597 | * {@link #readTypedArray} with the correct corresponding |
| 1598 | * {@link Parcelable.Creator} implementation to unmarshall them. |
| 1599 | * |
| 1600 | * @param val The array of objects to be written. |
| 1601 | * @param parcelableFlags Contextual flags as per |
| 1602 | * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}. |
| 1603 | * |
| 1604 | * @see #readTypedArray |
| 1605 | * @see #writeParcelableArray |
| 1606 | * @see Parcelable.Creator |
| 1607 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1608 | public final <T extends Parcelable> void writeTypedArray(@Nullable T[] val, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1609 | int parcelableFlags) { |
| 1610 | if (val != null) { |
| 1611 | int N = val.length; |
| 1612 | writeInt(N); |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 1613 | for (int i = 0; i < N; i++) { |
Sunny Goyal | 0e60f22 | 2017-09-21 21:39:20 -0700 | [diff] [blame] | 1614 | writeTypedObject(val[i], parcelableFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1615 | } |
| 1616 | } else { |
| 1617 | writeInt(-1); |
| 1618 | } |
| 1619 | } |
| 1620 | |
| 1621 | /** |
Jens Ole Lauridsen | 8dea874 | 2015-04-20 11:18:51 -0700 | [diff] [blame] | 1622 | * Flatten the Parcelable object into the parcel. |
| 1623 | * |
| 1624 | * @param val The Parcelable object to be written. |
| 1625 | * @param parcelableFlags Contextual flags as per |
| 1626 | * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}. |
| 1627 | * |
| 1628 | * @see #readTypedObject |
| 1629 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1630 | public final <T extends Parcelable> void writeTypedObject(@Nullable T val, |
| 1631 | int parcelableFlags) { |
Jens Ole Lauridsen | 8dea874 | 2015-04-20 11:18:51 -0700 | [diff] [blame] | 1632 | if (val != null) { |
| 1633 | writeInt(1); |
| 1634 | val.writeToParcel(this, parcelableFlags); |
| 1635 | } else { |
| 1636 | writeInt(0); |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1641 | * Flatten a generic object in to a parcel. The given Object value may |
| 1642 | * currently be one of the following types: |
Dan Egnor | b3e4ef3 | 2010-07-20 09:03:35 -0700 | [diff] [blame] | 1643 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1644 | * <ul> |
| 1645 | * <li> null |
| 1646 | * <li> String |
| 1647 | * <li> Byte |
| 1648 | * <li> Short |
| 1649 | * <li> Integer |
| 1650 | * <li> Long |
| 1651 | * <li> Float |
| 1652 | * <li> Double |
| 1653 | * <li> Boolean |
| 1654 | * <li> String[] |
| 1655 | * <li> boolean[] |
| 1656 | * <li> byte[] |
| 1657 | * <li> int[] |
| 1658 | * <li> long[] |
| 1659 | * <li> Object[] (supporting objects of the same type defined here). |
| 1660 | * <li> {@link Bundle} |
| 1661 | * <li> Map (as supported by {@link #writeMap}). |
| 1662 | * <li> Any object that implements the {@link Parcelable} protocol. |
| 1663 | * <li> Parcelable[] |
| 1664 | * <li> CharSequence (as supported by {@link TextUtils#writeToParcel}). |
| 1665 | * <li> List (as supported by {@link #writeList}). |
Dan Egnor | b3e4ef3 | 2010-07-20 09:03:35 -0700 | [diff] [blame] | 1666 | * <li> {@link SparseArray} (as supported by {@link #writeSparseArray(SparseArray)}). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1667 | * <li> {@link IBinder} |
| 1668 | * <li> Any object that implements Serializable (but see |
| 1669 | * {@link #writeSerializable} for caveats). Note that all of the |
| 1670 | * previous types have relatively efficient implementations for |
| 1671 | * writing to a Parcel; having to rely on the generic serialization |
| 1672 | * approach is much less efficient and should be avoided whenever |
| 1673 | * possible. |
| 1674 | * </ul> |
Dan Egnor | b3e4ef3 | 2010-07-20 09:03:35 -0700 | [diff] [blame] | 1675 | * |
| 1676 | * <p class="caution">{@link Parcelable} objects are written with |
| 1677 | * {@link Parcelable#writeToParcel} using contextual flags of 0. When |
| 1678 | * serializing objects containing {@link ParcelFileDescriptor}s, |
| 1679 | * this may result in file descriptor leaks when they are returned from |
| 1680 | * Binder calls (where {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} |
| 1681 | * should be used).</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1682 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1683 | public final void writeValue(@Nullable Object v) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1684 | if (v == null) { |
| 1685 | writeInt(VAL_NULL); |
| 1686 | } else if (v instanceof String) { |
| 1687 | writeInt(VAL_STRING); |
| 1688 | writeString((String) v); |
| 1689 | } else if (v instanceof Integer) { |
| 1690 | writeInt(VAL_INTEGER); |
| 1691 | writeInt((Integer) v); |
| 1692 | } else if (v instanceof Map) { |
| 1693 | writeInt(VAL_MAP); |
| 1694 | writeMap((Map) v); |
| 1695 | } else if (v instanceof Bundle) { |
| 1696 | // Must be before Parcelable |
| 1697 | writeInt(VAL_BUNDLE); |
| 1698 | writeBundle((Bundle) v); |
Samuel Tan | ceafe5e | 2015-12-11 16:50:58 -0800 | [diff] [blame] | 1699 | } else if (v instanceof PersistableBundle) { |
| 1700 | writeInt(VAL_PERSISTABLEBUNDLE); |
| 1701 | writePersistableBundle((PersistableBundle) v); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1702 | } else if (v instanceof Parcelable) { |
Samuel Tan | ceafe5e | 2015-12-11 16:50:58 -0800 | [diff] [blame] | 1703 | // IMPOTANT: cases for classes that implement Parcelable must |
| 1704 | // come before the Parcelable case, so that their specific VAL_* |
| 1705 | // types will be written. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1706 | writeInt(VAL_PARCELABLE); |
| 1707 | writeParcelable((Parcelable) v, 0); |
| 1708 | } else if (v instanceof Short) { |
| 1709 | writeInt(VAL_SHORT); |
| 1710 | writeInt(((Short) v).intValue()); |
| 1711 | } else if (v instanceof Long) { |
| 1712 | writeInt(VAL_LONG); |
| 1713 | writeLong((Long) v); |
| 1714 | } else if (v instanceof Float) { |
| 1715 | writeInt(VAL_FLOAT); |
| 1716 | writeFloat((Float) v); |
| 1717 | } else if (v instanceof Double) { |
| 1718 | writeInt(VAL_DOUBLE); |
| 1719 | writeDouble((Double) v); |
| 1720 | } else if (v instanceof Boolean) { |
| 1721 | writeInt(VAL_BOOLEAN); |
| 1722 | writeInt((Boolean) v ? 1 : 0); |
| 1723 | } else if (v instanceof CharSequence) { |
| 1724 | // Must be after String |
| 1725 | writeInt(VAL_CHARSEQUENCE); |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 1726 | writeCharSequence((CharSequence) v); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1727 | } else if (v instanceof List) { |
| 1728 | writeInt(VAL_LIST); |
| 1729 | writeList((List) v); |
| 1730 | } else if (v instanceof SparseArray) { |
| 1731 | writeInt(VAL_SPARSEARRAY); |
| 1732 | writeSparseArray((SparseArray) v); |
| 1733 | } else if (v instanceof boolean[]) { |
| 1734 | writeInt(VAL_BOOLEANARRAY); |
| 1735 | writeBooleanArray((boolean[]) v); |
| 1736 | } else if (v instanceof byte[]) { |
| 1737 | writeInt(VAL_BYTEARRAY); |
| 1738 | writeByteArray((byte[]) v); |
| 1739 | } else if (v instanceof String[]) { |
| 1740 | writeInt(VAL_STRINGARRAY); |
| 1741 | writeStringArray((String[]) v); |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 1742 | } else if (v instanceof CharSequence[]) { |
| 1743 | // Must be after String[] and before Object[] |
| 1744 | writeInt(VAL_CHARSEQUENCEARRAY); |
| 1745 | writeCharSequenceArray((CharSequence[]) v); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1746 | } else if (v instanceof IBinder) { |
| 1747 | writeInt(VAL_IBINDER); |
| 1748 | writeStrongBinder((IBinder) v); |
| 1749 | } else if (v instanceof Parcelable[]) { |
| 1750 | writeInt(VAL_PARCELABLEARRAY); |
| 1751 | writeParcelableArray((Parcelable[]) v, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1752 | } else if (v instanceof int[]) { |
| 1753 | writeInt(VAL_INTARRAY); |
| 1754 | writeIntArray((int[]) v); |
| 1755 | } else if (v instanceof long[]) { |
| 1756 | writeInt(VAL_LONGARRAY); |
| 1757 | writeLongArray((long[]) v); |
| 1758 | } else if (v instanceof Byte) { |
| 1759 | writeInt(VAL_BYTE); |
| 1760 | writeInt((Byte) v); |
Jeff Sharkey | 5ef3398 | 2014-09-04 18:13:39 -0700 | [diff] [blame] | 1761 | } else if (v instanceof Size) { |
| 1762 | writeInt(VAL_SIZE); |
| 1763 | writeSize((Size) v); |
| 1764 | } else if (v instanceof SizeF) { |
| 1765 | writeInt(VAL_SIZEF); |
| 1766 | writeSizeF((SizeF) v); |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 1767 | } else if (v instanceof double[]) { |
| 1768 | writeInt(VAL_DOUBLEARRAY); |
| 1769 | writeDoubleArray((double[]) v); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1770 | } else { |
Paul Duffin | ac5a082 | 2014-02-03 15:02:17 +0000 | [diff] [blame] | 1771 | Class<?> clazz = v.getClass(); |
| 1772 | if (clazz.isArray() && clazz.getComponentType() == Object.class) { |
| 1773 | // Only pure Object[] are written here, Other arrays of non-primitive types are |
| 1774 | // handled by serialization as this does not record the component type. |
| 1775 | writeInt(VAL_OBJECTARRAY); |
| 1776 | writeArray((Object[]) v); |
| 1777 | } else if (v instanceof Serializable) { |
| 1778 | // Must be last |
| 1779 | writeInt(VAL_SERIALIZABLE); |
| 1780 | writeSerializable((Serializable) v); |
| 1781 | } else { |
| 1782 | throw new RuntimeException("Parcel: unable to marshal value " + v); |
| 1783 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | /** |
| 1788 | * Flatten the name of the class of the Parcelable and its contents |
| 1789 | * into the parcel. |
Dan Egnor | b3e4ef3 | 2010-07-20 09:03:35 -0700 | [diff] [blame] | 1790 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1791 | * @param p The Parcelable object to be written. |
| 1792 | * @param parcelableFlags Contextual flags as per |
| 1793 | * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}. |
| 1794 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1795 | public final void writeParcelable(@Nullable Parcelable p, int parcelableFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1796 | if (p == null) { |
| 1797 | writeString(null); |
| 1798 | return; |
| 1799 | } |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 1800 | writeParcelableCreator(p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1801 | p.writeToParcel(this, parcelableFlags); |
| 1802 | } |
| 1803 | |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 1804 | /** @hide */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 1805 | @UnsupportedAppUsage |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1806 | public final void writeParcelableCreator(@NonNull Parcelable p) { |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 1807 | String name = p.getClass().getName(); |
| 1808 | writeString(name); |
| 1809 | } |
| 1810 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1811 | /** |
| 1812 | * Write a generic serializable object in to a Parcel. It is strongly |
| 1813 | * recommended that this method be avoided, since the serialization |
| 1814 | * overhead is extremely large, and this approach will be much slower than |
| 1815 | * using the other approaches to writing data in to a Parcel. |
| 1816 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1817 | public final void writeSerializable(@Nullable Serializable s) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1818 | if (s == null) { |
| 1819 | writeString(null); |
| 1820 | return; |
| 1821 | } |
| 1822 | String name = s.getClass().getName(); |
| 1823 | writeString(name); |
| 1824 | |
| 1825 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 1826 | try { |
| 1827 | ObjectOutputStream oos = new ObjectOutputStream(baos); |
| 1828 | oos.writeObject(s); |
| 1829 | oos.close(); |
| 1830 | |
| 1831 | writeByteArray(baos.toByteArray()); |
| 1832 | } catch (IOException ioe) { |
| 1833 | throw new RuntimeException("Parcelable encountered " + |
| 1834 | "IOException writing serializable object (name = " + name + |
| 1835 | ")", ioe); |
| 1836 | } |
| 1837 | } |
| 1838 | |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 1839 | /** @hide For debugging purposes */ |
| 1840 | public static void setStackTraceParceling(boolean enabled) { |
| 1841 | sParcelExceptionStackTrace = enabled; |
| 1842 | } |
| 1843 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1844 | /** |
| 1845 | * Special function for writing an exception result at the header of |
| 1846 | * a parcel, to be used when returning an exception from a transaction. |
| 1847 | * Note that this currently only supports a few exception types; any other |
| 1848 | * exception will be re-thrown by this function as a RuntimeException |
| 1849 | * (to be caught by the system's last-resort exception handling when |
| 1850 | * dispatching a transaction). |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 1851 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1852 | * <p>The supported exception types are: |
| 1853 | * <ul> |
| 1854 | * <li>{@link BadParcelableException} |
| 1855 | * <li>{@link IllegalArgumentException} |
| 1856 | * <li>{@link IllegalStateException} |
| 1857 | * <li>{@link NullPointerException} |
| 1858 | * <li>{@link SecurityException} |
Dianne Hackborn | 18482ae | 2017-08-01 17:41:00 -0700 | [diff] [blame] | 1859 | * <li>{@link UnsupportedOperationException} |
Dianne Hackborn | 7e71442 | 2013-09-13 17:32:57 -0700 | [diff] [blame] | 1860 | * <li>{@link NetworkOnMainThreadException} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1861 | * </ul> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 1862 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1863 | * @param e The Exception to be written. |
| 1864 | * |
| 1865 | * @see #writeNoException |
| 1866 | * @see #readException |
| 1867 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1868 | public final void writeException(@NonNull Exception e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1869 | int code = 0; |
Jeff Sharkey | e628b7d | 2017-01-17 13:50:20 -0700 | [diff] [blame] | 1870 | if (e instanceof Parcelable |
| 1871 | && (e.getClass().getClassLoader() == Parcelable.class.getClassLoader())) { |
| 1872 | // We only send Parcelable exceptions that are in the |
| 1873 | // BootClassLoader to ensure that the receiver can unpack them |
| 1874 | code = EX_PARCELABLE; |
| 1875 | } else if (e instanceof SecurityException) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1876 | code = EX_SECURITY; |
| 1877 | } else if (e instanceof BadParcelableException) { |
| 1878 | code = EX_BAD_PARCELABLE; |
| 1879 | } else if (e instanceof IllegalArgumentException) { |
| 1880 | code = EX_ILLEGAL_ARGUMENT; |
| 1881 | } else if (e instanceof NullPointerException) { |
| 1882 | code = EX_NULL_POINTER; |
| 1883 | } else if (e instanceof IllegalStateException) { |
| 1884 | code = EX_ILLEGAL_STATE; |
Dianne Hackborn | 7e71442 | 2013-09-13 17:32:57 -0700 | [diff] [blame] | 1885 | } else if (e instanceof NetworkOnMainThreadException) { |
| 1886 | code = EX_NETWORK_MAIN_THREAD; |
Dianne Hackborn | 33d738a | 2014-09-12 14:23:58 -0700 | [diff] [blame] | 1887 | } else if (e instanceof UnsupportedOperationException) { |
| 1888 | code = EX_UNSUPPORTED_OPERATION; |
Christopher Wiley | 80fd120 | 2015-11-22 17:12:37 -0800 | [diff] [blame] | 1889 | } else if (e instanceof ServiceSpecificException) { |
| 1890 | code = EX_SERVICE_SPECIFIC; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1891 | } |
| 1892 | writeInt(code); |
Brad Fitzpatrick | 703e5d3 | 2010-07-15 13:16:41 -0700 | [diff] [blame] | 1893 | StrictMode.clearGatheredViolations(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1894 | if (code == 0) { |
| 1895 | if (e instanceof RuntimeException) { |
| 1896 | throw (RuntimeException) e; |
| 1897 | } |
| 1898 | throw new RuntimeException(e); |
| 1899 | } |
| 1900 | writeString(e.getMessage()); |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 1901 | final long timeNow = sParcelExceptionStackTrace ? SystemClock.elapsedRealtime() : 0; |
| 1902 | if (sParcelExceptionStackTrace && (timeNow - sLastWriteExceptionStackTrace |
| 1903 | > WRITE_EXCEPTION_STACK_TRACE_THRESHOLD_MS)) { |
| 1904 | sLastWriteExceptionStackTrace = timeNow; |
| 1905 | final int sizePosition = dataPosition(); |
| 1906 | writeInt(0); // Header size will be filled in later |
| 1907 | StackTraceElement[] stackTrace = e.getStackTrace(); |
| 1908 | final int truncatedSize = Math.min(stackTrace.length, 5); |
| 1909 | StringBuilder sb = new StringBuilder(); |
| 1910 | for (int i = 0; i < truncatedSize; i++) { |
Fyodor Kupolov | 679d998 | 2017-11-21 10:42:23 -0800 | [diff] [blame] | 1911 | sb.append("\tat ").append(stackTrace[i]).append('\n'); |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 1912 | } |
| 1913 | writeString(sb.toString()); |
| 1914 | final int payloadPosition = dataPosition(); |
| 1915 | setDataPosition(sizePosition); |
| 1916 | // Write stack trace header size. Used in native side to skip the header |
| 1917 | writeInt(payloadPosition - sizePosition); |
| 1918 | setDataPosition(payloadPosition); |
| 1919 | } else { |
| 1920 | writeInt(0); |
| 1921 | } |
Jeff Sharkey | e628b7d | 2017-01-17 13:50:20 -0700 | [diff] [blame] | 1922 | switch (code) { |
| 1923 | case EX_SERVICE_SPECIFIC: |
| 1924 | writeInt(((ServiceSpecificException) e).errorCode); |
| 1925 | break; |
| 1926 | case EX_PARCELABLE: |
| 1927 | // Write parceled exception prefixed by length |
| 1928 | final int sizePosition = dataPosition(); |
| 1929 | writeInt(0); |
| 1930 | writeParcelable((Parcelable) e, Parcelable.PARCELABLE_WRITE_RETURN_VALUE); |
| 1931 | final int payloadPosition = dataPosition(); |
| 1932 | setDataPosition(sizePosition); |
| 1933 | writeInt(payloadPosition - sizePosition); |
| 1934 | setDataPosition(payloadPosition); |
| 1935 | break; |
Christopher Wiley | 80fd120 | 2015-11-22 17:12:37 -0800 | [diff] [blame] | 1936 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | /** |
| 1940 | * Special function for writing information at the front of the Parcel |
| 1941 | * indicating that no exception occurred. |
| 1942 | * |
| 1943 | * @see #writeException |
| 1944 | * @see #readException |
| 1945 | */ |
| 1946 | public final void writeNoException() { |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1947 | // Despite the name of this function ("write no exception"), |
| 1948 | // it should instead be thought of as "write the RPC response |
| 1949 | // header", but because this function name is written out by |
| 1950 | // the AIDL compiler, we're not going to rename it. |
| 1951 | // |
| 1952 | // The response header, in the non-exception case (see also |
| 1953 | // writeException above, also called by the AIDL compiler), is |
| 1954 | // either a 0 (the default case), or EX_HAS_REPLY_HEADER if |
| 1955 | // StrictMode has gathered up violations that have occurred |
| 1956 | // during a Binder call, in which case we write out the number |
| 1957 | // of violations and their details, serialized, before the |
| 1958 | // actual RPC respons data. The receiving end of this is |
| 1959 | // readException(), below. |
| 1960 | if (StrictMode.hasGatheredViolations()) { |
| 1961 | writeInt(EX_HAS_REPLY_HEADER); |
| 1962 | final int sizePosition = dataPosition(); |
| 1963 | writeInt(0); // total size of fat header, to be filled in later |
| 1964 | StrictMode.writeGatheredViolationsToParcel(this); |
| 1965 | final int payloadPosition = dataPosition(); |
| 1966 | setDataPosition(sizePosition); |
| 1967 | writeInt(payloadPosition - sizePosition); // header size |
| 1968 | setDataPosition(payloadPosition); |
| 1969 | } else { |
| 1970 | writeInt(0); |
| 1971 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | /** |
| 1975 | * Special function for reading an exception result from the header of |
| 1976 | * a parcel, to be used after receiving the result of a transaction. This |
| 1977 | * will throw the exception for you if it had been written to the Parcel, |
| 1978 | * otherwise return and let you read the normal result data from the Parcel. |
| 1979 | * |
| 1980 | * @see #writeException |
| 1981 | * @see #writeNoException |
| 1982 | */ |
| 1983 | public final void readException() { |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1984 | int code = readExceptionCode(); |
| 1985 | if (code != 0) { |
| 1986 | String msg = readString(); |
Takamasa Kuramitsu | 2214b82 | 2018-04-02 14:44:59 +0900 | [diff] [blame] | 1987 | readException(code, msg); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1988 | } |
| 1989 | } |
| 1990 | |
| 1991 | /** |
| 1992 | * Parses the header of a Binder call's response Parcel and |
| 1993 | * returns the exception code. Deals with lite or fat headers. |
| 1994 | * In the common successful case, this header is generally zero. |
| 1995 | * In less common cases, it's a small negative number and will be |
| 1996 | * followed by an error string. |
| 1997 | * |
| 1998 | * This exists purely for android.database.DatabaseUtils and |
| 1999 | * insulating it from having to handle fat headers as returned by |
| 2000 | * e.g. StrictMode-induced RPC responses. |
| 2001 | * |
| 2002 | * @hide |
| 2003 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 2004 | @UnsupportedAppUsage |
Howard Chen | 40abbb0 | 2019-05-24 02:04:50 +0800 | [diff] [blame] | 2005 | @TestApi |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 2006 | public final int readExceptionCode() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2007 | int code = readInt(); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 2008 | if (code == EX_HAS_REPLY_HEADER) { |
| 2009 | int headerSize = readInt(); |
| 2010 | if (headerSize == 0) { |
| 2011 | Log.e(TAG, "Unexpected zero-sized Parcel reply header."); |
| 2012 | } else { |
| 2013 | // Currently the only thing in the header is StrictMode stacks, |
| 2014 | // but discussions around event/RPC tracing suggest we might |
| 2015 | // put that here too. If so, switch on sub-header tags here. |
| 2016 | // But for now, just parse out the StrictMode stuff. |
| 2017 | StrictMode.readAndHandleBinderCallViolations(this); |
| 2018 | } |
| 2019 | // And fat response headers are currently only used when |
| 2020 | // there are no exceptions, so return no error: |
| 2021 | return 0; |
| 2022 | } |
| 2023 | return code; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | /** |
Mark Doliner | 879ea45 | 2014-01-02 12:38:07 -0800 | [diff] [blame] | 2027 | * Throw an exception with the given message. Not intended for use |
| 2028 | * outside the Parcel class. |
| 2029 | * |
| 2030 | * @param code Used to determine which exception class to throw. |
| 2031 | * @param msg The exception message. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2032 | */ |
| 2033 | public final void readException(int code, String msg) { |
Takamasa Kuramitsu | 2214b82 | 2018-04-02 14:44:59 +0900 | [diff] [blame] | 2034 | String remoteStackTrace = null; |
| 2035 | final int remoteStackPayloadSize = readInt(); |
| 2036 | if (remoteStackPayloadSize > 0) { |
| 2037 | remoteStackTrace = readString(); |
| 2038 | } |
| 2039 | Exception e = createException(code, msg); |
| 2040 | // Attach remote stack trace if availalble |
| 2041 | if (remoteStackTrace != null) { |
| 2042 | RemoteException cause = new RemoteException( |
| 2043 | "Remote stack trace:\n" + remoteStackTrace, null, false, false); |
| 2044 | try { |
| 2045 | Throwable rootCause = ExceptionUtils.getRootCause(e); |
| 2046 | if (rootCause != null) { |
| 2047 | rootCause.initCause(cause); |
| 2048 | } |
| 2049 | } catch (RuntimeException ex) { |
| 2050 | Log.e(TAG, "Cannot set cause " + cause + " for " + e, ex); |
| 2051 | } |
| 2052 | } |
| 2053 | SneakyThrow.sneakyThrow(e); |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | /** |
| 2057 | * Creates an exception with the given message. |
| 2058 | * |
| 2059 | * @param code Used to determine which exception class to throw. |
| 2060 | * @param msg The exception message. |
| 2061 | */ |
| 2062 | private Exception createException(int code, String msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2063 | switch (code) { |
Jeff Sharkey | e628b7d | 2017-01-17 13:50:20 -0700 | [diff] [blame] | 2064 | case EX_PARCELABLE: |
| 2065 | if (readInt() > 0) { |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 2066 | return (Exception) readParcelable(Parcelable.class.getClassLoader()); |
Jeff Sharkey | e628b7d | 2017-01-17 13:50:20 -0700 | [diff] [blame] | 2067 | } else { |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 2068 | return new RuntimeException(msg + " [missing Parcelable]"); |
Jeff Sharkey | e628b7d | 2017-01-17 13:50:20 -0700 | [diff] [blame] | 2069 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2070 | case EX_SECURITY: |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 2071 | return new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2072 | case EX_BAD_PARCELABLE: |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 2073 | return new BadParcelableException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2074 | case EX_ILLEGAL_ARGUMENT: |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 2075 | return new IllegalArgumentException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2076 | case EX_NULL_POINTER: |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 2077 | return new NullPointerException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2078 | case EX_ILLEGAL_STATE: |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 2079 | return new IllegalStateException(msg); |
Dianne Hackborn | 7e71442 | 2013-09-13 17:32:57 -0700 | [diff] [blame] | 2080 | case EX_NETWORK_MAIN_THREAD: |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 2081 | return new NetworkOnMainThreadException(); |
Dianne Hackborn | 33d738a | 2014-09-12 14:23:58 -0700 | [diff] [blame] | 2082 | case EX_UNSUPPORTED_OPERATION: |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 2083 | return new UnsupportedOperationException(msg); |
Christopher Wiley | 80fd120 | 2015-11-22 17:12:37 -0800 | [diff] [blame] | 2084 | case EX_SERVICE_SPECIFIC: |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 2085 | return new ServiceSpecificException(readInt(), msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2086 | } |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 2087 | return new RuntimeException("Unknown exception code: " + code |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2088 | + " msg " + msg); |
| 2089 | } |
| 2090 | |
| 2091 | /** |
| 2092 | * Read an integer value from the parcel at the current dataPosition(). |
| 2093 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 2094 | public final int readInt() { |
| 2095 | return nativeReadInt(mNativePtr); |
| 2096 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2097 | |
| 2098 | /** |
| 2099 | * Read a long integer value from the parcel at the current dataPosition(). |
| 2100 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 2101 | public final long readLong() { |
| 2102 | return nativeReadLong(mNativePtr); |
| 2103 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2104 | |
| 2105 | /** |
| 2106 | * Read a floating point value from the parcel at the current |
| 2107 | * dataPosition(). |
| 2108 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 2109 | public final float readFloat() { |
| 2110 | return nativeReadFloat(mNativePtr); |
| 2111 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2112 | |
| 2113 | /** |
| 2114 | * Read a double precision floating point value from the parcel at the |
| 2115 | * current dataPosition(). |
| 2116 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 2117 | public final double readDouble() { |
| 2118 | return nativeReadDouble(mNativePtr); |
| 2119 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2120 | |
| 2121 | /** |
| 2122 | * Read a string value from the parcel at the current dataPosition(). |
| 2123 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2124 | @Nullable |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 2125 | public final String readString() { |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 2126 | return mReadWriteHelper.readString(this); |
| 2127 | } |
| 2128 | |
| 2129 | /** |
| 2130 | * Read a string without going though a {@link ReadWriteHelper}. Subclasses of |
| 2131 | * {@link ReadWriteHelper} must use this method instead of {@link #readString} to avoid |
| 2132 | * infinity recursive calls. |
| 2133 | * |
| 2134 | * @hide |
| 2135 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2136 | @Nullable |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 2137 | public String readStringNoHelper() { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 2138 | return nativeReadString(mNativePtr); |
| 2139 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2140 | |
Jake Wharton | 2ffa431 | 2018-04-23 17:47:14 -0400 | [diff] [blame] | 2141 | /** |
| 2142 | * Read a boolean value from the parcel at the current dataPosition(). |
| 2143 | */ |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 2144 | public final boolean readBoolean() { |
| 2145 | return readInt() != 0; |
| 2146 | } |
| 2147 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2148 | /** |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 2149 | * Read a CharSequence value from the parcel at the current dataPosition(). |
| 2150 | * @hide |
| 2151 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 2152 | @UnsupportedAppUsage |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2153 | @Nullable |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 2154 | public final CharSequence readCharSequence() { |
| 2155 | return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(this); |
| 2156 | } |
| 2157 | |
| 2158 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2159 | * Read an object from the parcel at the current dataPosition(). |
| 2160 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 2161 | public final IBinder readStrongBinder() { |
| 2162 | return nativeReadStrongBinder(mNativePtr); |
| 2163 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2164 | |
| 2165 | /** |
| 2166 | * Read a FileDescriptor from the parcel at the current dataPosition(). |
| 2167 | */ |
| 2168 | public final ParcelFileDescriptor readFileDescriptor() { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 2169 | FileDescriptor fd = nativeReadFileDescriptor(mNativePtr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2170 | return fd != null ? new ParcelFileDescriptor(fd) : null; |
| 2171 | } |
| 2172 | |
Jeff Sharkey | da5a3e1 | 2013-08-11 12:54:42 -0700 | [diff] [blame] | 2173 | /** {@hide} */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 2174 | @UnsupportedAppUsage |
Jeff Sharkey | da5a3e1 | 2013-08-11 12:54:42 -0700 | [diff] [blame] | 2175 | public final FileDescriptor readRawFileDescriptor() { |
| 2176 | return nativeReadFileDescriptor(mNativePtr); |
| 2177 | } |
| 2178 | |
Casey Dahlin | 2f974b2 | 2015-11-05 12:19:13 -0800 | [diff] [blame] | 2179 | /** |
| 2180 | * {@hide} |
| 2181 | * Read and return a new array of FileDescriptors from the parcel. |
| 2182 | * @return the FileDescriptor array, or null if the array is null. |
| 2183 | **/ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2184 | @Nullable |
Casey Dahlin | 2f974b2 | 2015-11-05 12:19:13 -0800 | [diff] [blame] | 2185 | public final FileDescriptor[] createRawFileDescriptorArray() { |
| 2186 | int N = readInt(); |
| 2187 | if (N < 0) { |
| 2188 | return null; |
| 2189 | } |
| 2190 | FileDescriptor[] f = new FileDescriptor[N]; |
| 2191 | for (int i = 0; i < N; i++) { |
| 2192 | f[i] = readRawFileDescriptor(); |
| 2193 | } |
| 2194 | return f; |
| 2195 | } |
| 2196 | |
| 2197 | /** |
| 2198 | * {@hide} |
| 2199 | * Read an array of FileDescriptors from a parcel. |
| 2200 | * The passed array must be exactly the length of the array in the parcel. |
| 2201 | * @return the FileDescriptor array, or null if the array is null. |
| 2202 | **/ |
| 2203 | public final void readRawFileDescriptorArray(FileDescriptor[] val) { |
| 2204 | int N = readInt(); |
| 2205 | if (N == val.length) { |
| 2206 | for (int i=0; i<N; i++) { |
| 2207 | val[i] = readRawFileDescriptor(); |
| 2208 | } |
| 2209 | } else { |
| 2210 | throw new RuntimeException("bad array lengths"); |
| 2211 | } |
| 2212 | } |
| 2213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2214 | /** |
| 2215 | * Read a byte value from the parcel at the current dataPosition(). |
| 2216 | */ |
| 2217 | public final byte readByte() { |
| 2218 | return (byte)(readInt() & 0xff); |
| 2219 | } |
| 2220 | |
| 2221 | /** |
| 2222 | * Please use {@link #readBundle(ClassLoader)} instead (whose data must have |
| 2223 | * been written with {@link #writeBundle}. Read into an existing Map object |
| 2224 | * from the parcel at the current dataPosition(). |
| 2225 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2226 | public final void readMap(@NonNull Map outVal, @Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2227 | int N = readInt(); |
| 2228 | readMapInternal(outVal, N, loader); |
| 2229 | } |
| 2230 | |
| 2231 | /** |
| 2232 | * Read into an existing List object from the parcel at the current |
| 2233 | * dataPosition(), using the given class loader to load any enclosed |
| 2234 | * Parcelables. If it is null, the default class loader is used. |
| 2235 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2236 | public final void readList(@NonNull List outVal, @Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2237 | int N = readInt(); |
| 2238 | readListInternal(outVal, N, loader); |
| 2239 | } |
| 2240 | |
| 2241 | /** |
| 2242 | * Please use {@link #readBundle(ClassLoader)} instead (whose data must have |
| 2243 | * been written with {@link #writeBundle}. Read and return a new HashMap |
| 2244 | * object from the parcel at the current dataPosition(), using the given |
| 2245 | * class loader to load any enclosed Parcelables. Returns null if |
| 2246 | * the previously written map object was null. |
| 2247 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2248 | @Nullable |
| 2249 | public final HashMap readHashMap(@Nullable ClassLoader loader) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2250 | { |
| 2251 | int N = readInt(); |
| 2252 | if (N < 0) { |
| 2253 | return null; |
| 2254 | } |
| 2255 | HashMap m = new HashMap(N); |
| 2256 | readMapInternal(m, N, loader); |
| 2257 | return m; |
| 2258 | } |
| 2259 | |
| 2260 | /** |
| 2261 | * Read and return a new Bundle object from the parcel at the current |
| 2262 | * dataPosition(). Returns null if the previously written Bundle object was |
| 2263 | * null. |
| 2264 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2265 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2266 | public final Bundle readBundle() { |
| 2267 | return readBundle(null); |
| 2268 | } |
| 2269 | |
| 2270 | /** |
| 2271 | * Read and return a new Bundle object from the parcel at the current |
| 2272 | * dataPosition(), using the given class loader to initialize the class |
| 2273 | * loader of the Bundle for later retrieval of Parcelable objects. |
| 2274 | * Returns null if the previously written Bundle object was null. |
| 2275 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2276 | @Nullable |
| 2277 | public final Bundle readBundle(@Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2278 | int length = readInt(); |
| 2279 | if (length < 0) { |
Dianne Hackborn | 4a7d824 | 2013-10-03 10:19:20 -0700 | [diff] [blame] | 2280 | if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2281 | return null; |
| 2282 | } |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 2283 | |
Dianne Hackborn | 6aff905 | 2009-05-22 13:20:23 -0700 | [diff] [blame] | 2284 | final Bundle bundle = new Bundle(this, length); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2285 | if (loader != null) { |
| 2286 | bundle.setClassLoader(loader); |
| 2287 | } |
| 2288 | return bundle; |
| 2289 | } |
| 2290 | |
| 2291 | /** |
Craig Mautner | 719e6b1 | 2014-04-04 20:29:41 -0700 | [diff] [blame] | 2292 | * Read and return a new Bundle object from the parcel at the current |
| 2293 | * dataPosition(). Returns null if the previously written Bundle object was |
| 2294 | * null. |
| 2295 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2296 | @Nullable |
Craig Mautner | 719e6b1 | 2014-04-04 20:29:41 -0700 | [diff] [blame] | 2297 | public final PersistableBundle readPersistableBundle() { |
| 2298 | return readPersistableBundle(null); |
| 2299 | } |
| 2300 | |
| 2301 | /** |
| 2302 | * Read and return a new Bundle object from the parcel at the current |
| 2303 | * dataPosition(), using the given class loader to initialize the class |
| 2304 | * loader of the Bundle for later retrieval of Parcelable objects. |
| 2305 | * Returns null if the previously written Bundle object was null. |
| 2306 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2307 | @Nullable |
| 2308 | public final PersistableBundle readPersistableBundle(@Nullable ClassLoader loader) { |
Craig Mautner | 719e6b1 | 2014-04-04 20:29:41 -0700 | [diff] [blame] | 2309 | int length = readInt(); |
| 2310 | if (length < 0) { |
| 2311 | if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length); |
| 2312 | return null; |
| 2313 | } |
| 2314 | |
| 2315 | final PersistableBundle bundle = new PersistableBundle(this, length); |
| 2316 | if (loader != null) { |
| 2317 | bundle.setClassLoader(loader); |
| 2318 | } |
| 2319 | return bundle; |
| 2320 | } |
| 2321 | |
| 2322 | /** |
Jeff Sharkey | 5ef3398 | 2014-09-04 18:13:39 -0700 | [diff] [blame] | 2323 | * Read a Size from the parcel at the current dataPosition(). |
| 2324 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2325 | @NonNull |
Jeff Sharkey | 5ef3398 | 2014-09-04 18:13:39 -0700 | [diff] [blame] | 2326 | public final Size readSize() { |
| 2327 | final int width = readInt(); |
| 2328 | final int height = readInt(); |
| 2329 | return new Size(width, height); |
| 2330 | } |
| 2331 | |
| 2332 | /** |
| 2333 | * Read a SizeF from the parcel at the current dataPosition(). |
| 2334 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2335 | @NonNull |
Jeff Sharkey | 5ef3398 | 2014-09-04 18:13:39 -0700 | [diff] [blame] | 2336 | public final SizeF readSizeF() { |
| 2337 | final float width = readFloat(); |
| 2338 | final float height = readFloat(); |
| 2339 | return new SizeF(width, height); |
| 2340 | } |
| 2341 | |
| 2342 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2343 | * Read and return a byte[] object from the parcel. |
| 2344 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2345 | @Nullable |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 2346 | public final byte[] createByteArray() { |
| 2347 | return nativeCreateByteArray(mNativePtr); |
| 2348 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2349 | |
| 2350 | /** |
| 2351 | * Read a byte[] object from the parcel and copy it into the |
| 2352 | * given byte array. |
| 2353 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2354 | public final void readByteArray(@NonNull byte[] val) { |
Jocelyn Dang | 4610044 | 2017-05-05 15:40:49 -0700 | [diff] [blame] | 2355 | boolean valid = nativeReadByteArray(mNativePtr, val, (val != null) ? val.length : 0); |
| 2356 | if (!valid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2357 | throw new RuntimeException("bad array lengths"); |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | /** |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 2362 | * Read a blob of data from the parcel and return it as a byte array. |
| 2363 | * {@hide} |
Sandeep Siddhartha | 39c12fa | 2014-07-25 18:37:29 -0700 | [diff] [blame] | 2364 | * {@SystemApi} |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 2365 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 2366 | @UnsupportedAppUsage |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2367 | @Nullable |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 2368 | public final byte[] readBlob() { |
| 2369 | return nativeReadBlob(mNativePtr); |
| 2370 | } |
| 2371 | |
| 2372 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2373 | * Read and return a String[] object from the parcel. |
| 2374 | * {@hide} |
| 2375 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 2376 | @UnsupportedAppUsage |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2377 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2378 | public final String[] readStringArray() { |
| 2379 | String[] array = null; |
| 2380 | |
| 2381 | int length = readInt(); |
| 2382 | if (length >= 0) |
| 2383 | { |
| 2384 | array = new String[length]; |
| 2385 | |
| 2386 | for (int i = 0 ; i < length ; i++) |
| 2387 | { |
| 2388 | array[i] = readString(); |
| 2389 | } |
| 2390 | } |
| 2391 | |
| 2392 | return array; |
| 2393 | } |
| 2394 | |
| 2395 | /** |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 2396 | * Read and return a CharSequence[] object from the parcel. |
| 2397 | * {@hide} |
| 2398 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2399 | @Nullable |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 2400 | public final CharSequence[] readCharSequenceArray() { |
| 2401 | CharSequence[] array = null; |
| 2402 | |
| 2403 | int length = readInt(); |
| 2404 | if (length >= 0) |
| 2405 | { |
| 2406 | array = new CharSequence[length]; |
| 2407 | |
| 2408 | for (int i = 0 ; i < length ; i++) |
| 2409 | { |
| 2410 | array[i] = readCharSequence(); |
| 2411 | } |
| 2412 | } |
| 2413 | |
| 2414 | return array; |
| 2415 | } |
| 2416 | |
| 2417 | /** |
Dianne Hackborn | 3d07c94 | 2015-03-13 18:02:54 -0700 | [diff] [blame] | 2418 | * Read and return an ArrayList<CharSequence> object from the parcel. |
| 2419 | * {@hide} |
| 2420 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2421 | @Nullable |
Dianne Hackborn | 3d07c94 | 2015-03-13 18:02:54 -0700 | [diff] [blame] | 2422 | public final ArrayList<CharSequence> readCharSequenceList() { |
| 2423 | ArrayList<CharSequence> array = null; |
| 2424 | |
| 2425 | int length = readInt(); |
| 2426 | if (length >= 0) { |
| 2427 | array = new ArrayList<CharSequence>(length); |
| 2428 | |
| 2429 | for (int i = 0 ; i < length ; i++) { |
| 2430 | array.add(readCharSequence()); |
| 2431 | } |
| 2432 | } |
| 2433 | |
| 2434 | return array; |
| 2435 | } |
| 2436 | |
| 2437 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2438 | * Read and return a new ArrayList object from the parcel at the current |
| 2439 | * dataPosition(). Returns null if the previously written list object was |
| 2440 | * null. The given class loader will be used to load any enclosed |
| 2441 | * Parcelables. |
| 2442 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2443 | @Nullable |
| 2444 | public final ArrayList readArrayList(@Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2445 | int N = readInt(); |
| 2446 | if (N < 0) { |
| 2447 | return null; |
| 2448 | } |
| 2449 | ArrayList l = new ArrayList(N); |
| 2450 | readListInternal(l, N, loader); |
| 2451 | return l; |
| 2452 | } |
| 2453 | |
| 2454 | /** |
| 2455 | * Read and return a new Object array from the parcel at the current |
| 2456 | * dataPosition(). Returns null if the previously written array was |
| 2457 | * null. The given class loader will be used to load any enclosed |
| 2458 | * Parcelables. |
| 2459 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2460 | @Nullable |
| 2461 | public final Object[] readArray(@Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2462 | int N = readInt(); |
| 2463 | if (N < 0) { |
| 2464 | return null; |
| 2465 | } |
| 2466 | Object[] l = new Object[N]; |
| 2467 | readArrayInternal(l, N, loader); |
| 2468 | return l; |
| 2469 | } |
| 2470 | |
| 2471 | /** |
| 2472 | * Read and return a new SparseArray object from the parcel at the current |
| 2473 | * dataPosition(). Returns null if the previously written list object was |
| 2474 | * null. The given class loader will be used to load any enclosed |
| 2475 | * Parcelables. |
| 2476 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2477 | @Nullable |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 2478 | public final <T> SparseArray<T> readSparseArray(@Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2479 | int N = readInt(); |
| 2480 | if (N < 0) { |
| 2481 | return null; |
| 2482 | } |
| 2483 | SparseArray sa = new SparseArray(N); |
| 2484 | readSparseArrayInternal(sa, N, loader); |
| 2485 | return sa; |
| 2486 | } |
| 2487 | |
| 2488 | /** |
| 2489 | * Read and return a new SparseBooleanArray object from the parcel at the current |
| 2490 | * dataPosition(). Returns null if the previously written list object was |
| 2491 | * null. |
| 2492 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2493 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2494 | public final SparseBooleanArray readSparseBooleanArray() { |
| 2495 | int N = readInt(); |
| 2496 | if (N < 0) { |
| 2497 | return null; |
| 2498 | } |
| 2499 | SparseBooleanArray sa = new SparseBooleanArray(N); |
| 2500 | readSparseBooleanArrayInternal(sa, N); |
| 2501 | return sa; |
| 2502 | } |
| 2503 | |
| 2504 | /** |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 2505 | * Read and return a new SparseIntArray object from the parcel at the current |
| 2506 | * dataPosition(). Returns null if the previously written array object was null. |
Adam Lesinski | 205656d | 2017-03-23 13:38:26 -0700 | [diff] [blame] | 2507 | * @hide |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 2508 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2509 | @Nullable |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 2510 | public final SparseIntArray readSparseIntArray() { |
| 2511 | int N = readInt(); |
| 2512 | if (N < 0) { |
| 2513 | return null; |
| 2514 | } |
| 2515 | SparseIntArray sa = new SparseIntArray(N); |
| 2516 | readSparseIntArrayInternal(sa, N); |
| 2517 | return sa; |
| 2518 | } |
| 2519 | |
| 2520 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2521 | * Read and return a new ArrayList containing a particular object type from |
| 2522 | * the parcel that was written with {@link #writeTypedList} at the |
| 2523 | * current dataPosition(). Returns null if the |
| 2524 | * previously written list object was null. The list <em>must</em> have |
| 2525 | * previously been written via {@link #writeTypedList} with the same object |
| 2526 | * type. |
| 2527 | * |
| 2528 | * @return A newly created ArrayList containing objects with the same data |
| 2529 | * as those that were previously written. |
| 2530 | * |
| 2531 | * @see #writeTypedList |
| 2532 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2533 | @Nullable |
| 2534 | public final <T> ArrayList<T> createTypedArrayList(@NonNull Parcelable.Creator<T> c) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2535 | int N = readInt(); |
| 2536 | if (N < 0) { |
| 2537 | return null; |
| 2538 | } |
| 2539 | ArrayList<T> l = new ArrayList<T>(N); |
| 2540 | while (N > 0) { |
Sunny Goyal | 0e60f22 | 2017-09-21 21:39:20 -0700 | [diff] [blame] | 2541 | l.add(readTypedObject(c)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2542 | N--; |
| 2543 | } |
| 2544 | return l; |
| 2545 | } |
| 2546 | |
| 2547 | /** |
| 2548 | * Read into the given List items containing a particular object type |
| 2549 | * that were written with {@link #writeTypedList} at the |
| 2550 | * current dataPosition(). The list <em>must</em> have |
| 2551 | * previously been written via {@link #writeTypedList} with the same object |
| 2552 | * type. |
| 2553 | * |
| 2554 | * @return A newly created ArrayList containing objects with the same data |
| 2555 | * as those that were previously written. |
| 2556 | * |
| 2557 | * @see #writeTypedList |
| 2558 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2559 | public final <T> void readTypedList(@NonNull List<T> list, @NonNull Parcelable.Creator<T> c) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2560 | int M = list.size(); |
| 2561 | int N = readInt(); |
| 2562 | int i = 0; |
| 2563 | for (; i < M && i < N; i++) { |
Sunny Goyal | 0e60f22 | 2017-09-21 21:39:20 -0700 | [diff] [blame] | 2564 | list.set(i, readTypedObject(c)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2565 | } |
| 2566 | for (; i<N; i++) { |
Sunny Goyal | 0e60f22 | 2017-09-21 21:39:20 -0700 | [diff] [blame] | 2567 | list.add(readTypedObject(c)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2568 | } |
| 2569 | for (; i<M; i++) { |
| 2570 | list.remove(N); |
| 2571 | } |
| 2572 | } |
| 2573 | |
| 2574 | /** |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 2575 | * Read into a new {@link SparseArray} items containing a particular object type |
| 2576 | * that were written with {@link #writeTypedSparseArray(SparseArray, int)} at the |
| 2577 | * current dataPosition(). The list <em>must</em> have previously been written |
| 2578 | * via {@link #writeTypedSparseArray(SparseArray, int)} with the same object type. |
| 2579 | * |
| 2580 | * @param creator The creator to use when for instantiation. |
| 2581 | * |
| 2582 | * @return A newly created {@link SparseArray} containing objects with the same data |
| 2583 | * as those that were previously written. |
| 2584 | * |
| 2585 | * @see #writeTypedSparseArray(SparseArray, int) |
| 2586 | */ |
| 2587 | public final @Nullable <T extends Parcelable> SparseArray<T> createTypedSparseArray( |
| 2588 | @NonNull Parcelable.Creator<T> creator) { |
| 2589 | final int count = readInt(); |
| 2590 | if (count < 0) { |
| 2591 | return null; |
| 2592 | } |
| 2593 | final SparseArray<T> array = new SparseArray<>(count); |
| 2594 | for (int i = 0; i < count; i++) { |
| 2595 | final int index = readInt(); |
| 2596 | final T value = readTypedObject(creator); |
| 2597 | array.append(index, value); |
| 2598 | } |
| 2599 | return array; |
| 2600 | } |
| 2601 | |
| 2602 | /** |
| 2603 | * Read into a new {@link ArrayMap} with string keys items containing a particular |
| 2604 | * object type that were written with {@link #writeTypedArrayMap(ArrayMap, int)} at the |
| 2605 | * current dataPosition(). The list <em>must</em> have previously been written |
| 2606 | * via {@link #writeTypedArrayMap(ArrayMap, int)} with the same object type. |
| 2607 | * |
| 2608 | * @param creator The creator to use when for instantiation. |
| 2609 | * |
| 2610 | * @return A newly created {@link ArrayMap} containing objects with the same data |
| 2611 | * as those that were previously written. |
| 2612 | * |
| 2613 | * @see #writeTypedArrayMap(ArrayMap, int) |
| 2614 | */ |
| 2615 | public final @Nullable <T extends Parcelable> ArrayMap<String, T> createTypedArrayMap( |
| 2616 | @NonNull Parcelable.Creator<T> creator) { |
| 2617 | final int count = readInt(); |
| 2618 | if (count < 0) { |
| 2619 | return null; |
| 2620 | } |
| 2621 | final ArrayMap<String, T> map = new ArrayMap<>(count); |
| 2622 | for (int i = 0; i < count; i++) { |
| 2623 | final String key = readString(); |
| 2624 | final T value = readTypedObject(creator); |
| 2625 | map.append(key, value); |
| 2626 | } |
| 2627 | return map; |
| 2628 | } |
| 2629 | |
| 2630 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2631 | * Read and return a new ArrayList containing String objects from |
| 2632 | * the parcel that was written with {@link #writeStringList} at the |
| 2633 | * current dataPosition(). Returns null if the |
| 2634 | * previously written list object was null. |
| 2635 | * |
| 2636 | * @return A newly created ArrayList containing strings with the same data |
| 2637 | * as those that were previously written. |
| 2638 | * |
| 2639 | * @see #writeStringList |
| 2640 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2641 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2642 | public final ArrayList<String> createStringArrayList() { |
| 2643 | int N = readInt(); |
| 2644 | if (N < 0) { |
| 2645 | return null; |
| 2646 | } |
| 2647 | ArrayList<String> l = new ArrayList<String>(N); |
| 2648 | while (N > 0) { |
| 2649 | l.add(readString()); |
| 2650 | N--; |
| 2651 | } |
| 2652 | return l; |
| 2653 | } |
| 2654 | |
| 2655 | /** |
| 2656 | * Read and return a new ArrayList containing IBinder objects from |
| 2657 | * the parcel that was written with {@link #writeBinderList} at the |
| 2658 | * current dataPosition(). Returns null if the |
| 2659 | * previously written list object was null. |
| 2660 | * |
| 2661 | * @return A newly created ArrayList containing strings with the same data |
| 2662 | * as those that were previously written. |
| 2663 | * |
| 2664 | * @see #writeBinderList |
| 2665 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2666 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2667 | public final ArrayList<IBinder> createBinderArrayList() { |
| 2668 | int N = readInt(); |
| 2669 | if (N < 0) { |
| 2670 | return null; |
| 2671 | } |
| 2672 | ArrayList<IBinder> l = new ArrayList<IBinder>(N); |
| 2673 | while (N > 0) { |
| 2674 | l.add(readStrongBinder()); |
| 2675 | N--; |
| 2676 | } |
| 2677 | return l; |
| 2678 | } |
| 2679 | |
| 2680 | /** |
| 2681 | * Read into the given List items String objects that were written with |
| 2682 | * {@link #writeStringList} at the current dataPosition(). |
| 2683 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2684 | * @see #writeStringList |
| 2685 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2686 | public final void readStringList(@NonNull List<String> list) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2687 | int M = list.size(); |
| 2688 | int N = readInt(); |
| 2689 | int i = 0; |
| 2690 | for (; i < M && i < N; i++) { |
| 2691 | list.set(i, readString()); |
| 2692 | } |
| 2693 | for (; i<N; i++) { |
| 2694 | list.add(readString()); |
| 2695 | } |
| 2696 | for (; i<M; i++) { |
| 2697 | list.remove(N); |
| 2698 | } |
| 2699 | } |
| 2700 | |
| 2701 | /** |
| 2702 | * Read into the given List items IBinder objects that were written with |
| 2703 | * {@link #writeBinderList} at the current dataPosition(). |
| 2704 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2705 | * @see #writeBinderList |
| 2706 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2707 | public final void readBinderList(@NonNull List<IBinder> list) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2708 | int M = list.size(); |
| 2709 | int N = readInt(); |
| 2710 | int i = 0; |
| 2711 | for (; i < M && i < N; i++) { |
| 2712 | list.set(i, readStrongBinder()); |
| 2713 | } |
| 2714 | for (; i<N; i++) { |
| 2715 | list.add(readStrongBinder()); |
| 2716 | } |
| 2717 | for (; i<M; i++) { |
| 2718 | list.remove(N); |
| 2719 | } |
| 2720 | } |
| 2721 | |
| 2722 | /** |
Narayan Kamath | bea4871 | 2016-12-01 15:38:28 +0000 | [diff] [blame] | 2723 | * Read the list of {@code Parcelable} objects at the current data position into the |
| 2724 | * given {@code list}. The contents of the {@code list} are replaced. If the serialized |
| 2725 | * list was {@code null}, {@code list} is cleared. |
| 2726 | * |
| 2727 | * @see #writeParcelableList(List, int) |
Narayan Kamath | bea4871 | 2016-12-01 15:38:28 +0000 | [diff] [blame] | 2728 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2729 | @NonNull |
| 2730 | public final <T extends Parcelable> List<T> readParcelableList(@NonNull List<T> list, |
| 2731 | @Nullable ClassLoader cl) { |
Narayan Kamath | bea4871 | 2016-12-01 15:38:28 +0000 | [diff] [blame] | 2732 | final int N = readInt(); |
| 2733 | if (N == -1) { |
| 2734 | list.clear(); |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 2735 | return list; |
Narayan Kamath | bea4871 | 2016-12-01 15:38:28 +0000 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | final int M = list.size(); |
| 2739 | int i = 0; |
| 2740 | for (; i < M && i < N; i++) { |
| 2741 | list.set(i, (T) readParcelable(cl)); |
| 2742 | } |
| 2743 | for (; i<N; i++) { |
| 2744 | list.add((T) readParcelable(cl)); |
| 2745 | } |
| 2746 | for (; i<M; i++) { |
| 2747 | list.remove(N); |
| 2748 | } |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 2749 | return list; |
Narayan Kamath | bea4871 | 2016-12-01 15:38:28 +0000 | [diff] [blame] | 2750 | } |
| 2751 | |
| 2752 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2753 | * Read and return a new array containing a particular object type from |
| 2754 | * the parcel at the current dataPosition(). Returns null if the |
| 2755 | * previously written array was null. The array <em>must</em> have |
| 2756 | * previously been written via {@link #writeTypedArray} with the same |
| 2757 | * object type. |
| 2758 | * |
| 2759 | * @return A newly created array containing objects with the same data |
| 2760 | * as those that were previously written. |
| 2761 | * |
| 2762 | * @see #writeTypedArray |
| 2763 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2764 | @Nullable |
| 2765 | public final <T> T[] createTypedArray(@NonNull Parcelable.Creator<T> c) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2766 | int N = readInt(); |
| 2767 | if (N < 0) { |
| 2768 | return null; |
| 2769 | } |
| 2770 | T[] l = c.newArray(N); |
| 2771 | for (int i=0; i<N; i++) { |
Sunny Goyal | 0e60f22 | 2017-09-21 21:39:20 -0700 | [diff] [blame] | 2772 | l[i] = readTypedObject(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2773 | } |
| 2774 | return l; |
| 2775 | } |
| 2776 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2777 | public final <T> void readTypedArray(@NonNull T[] val, @NonNull Parcelable.Creator<T> c) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2778 | int N = readInt(); |
| 2779 | if (N == val.length) { |
| 2780 | for (int i=0; i<N; i++) { |
Sunny Goyal | 0e60f22 | 2017-09-21 21:39:20 -0700 | [diff] [blame] | 2781 | val[i] = readTypedObject(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2782 | } |
| 2783 | } else { |
| 2784 | throw new RuntimeException("bad array lengths"); |
| 2785 | } |
| 2786 | } |
| 2787 | |
| 2788 | /** |
| 2789 | * @deprecated |
| 2790 | * @hide |
| 2791 | */ |
| 2792 | @Deprecated |
| 2793 | public final <T> T[] readTypedArray(Parcelable.Creator<T> c) { |
| 2794 | return createTypedArray(c); |
| 2795 | } |
| 2796 | |
| 2797 | /** |
Jens Ole Lauridsen | 8dea874 | 2015-04-20 11:18:51 -0700 | [diff] [blame] | 2798 | * Read and return a typed Parcelable object from a parcel. |
| 2799 | * Returns null if the previous written object was null. |
| 2800 | * The object <em>must</em> have previous been written via |
| 2801 | * {@link #writeTypedObject} with the same object type. |
| 2802 | * |
| 2803 | * @return A newly created object of the type that was previously |
| 2804 | * written. |
| 2805 | * |
| 2806 | * @see #writeTypedObject |
| 2807 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2808 | @Nullable |
| 2809 | public final <T> T readTypedObject(@NonNull Parcelable.Creator<T> c) { |
Jens Ole Lauridsen | 8dea874 | 2015-04-20 11:18:51 -0700 | [diff] [blame] | 2810 | if (readInt() != 0) { |
| 2811 | return c.createFromParcel(this); |
| 2812 | } else { |
| 2813 | return null; |
| 2814 | } |
| 2815 | } |
| 2816 | |
| 2817 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2818 | * Write a heterogeneous array of Parcelable objects into the Parcel. |
| 2819 | * Each object in the array is written along with its class name, so |
| 2820 | * that the correct class can later be instantiated. As a result, this |
| 2821 | * has significantly more overhead than {@link #writeTypedArray}, but will |
| 2822 | * correctly handle an array containing more than one type of object. |
| 2823 | * |
| 2824 | * @param value The array of objects to be written. |
| 2825 | * @param parcelableFlags Contextual flags as per |
| 2826 | * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}. |
| 2827 | * |
| 2828 | * @see #writeTypedArray |
| 2829 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2830 | public final <T extends Parcelable> void writeParcelableArray(@Nullable T[] value, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2831 | int parcelableFlags) { |
| 2832 | if (value != null) { |
| 2833 | int N = value.length; |
| 2834 | writeInt(N); |
| 2835 | for (int i=0; i<N; i++) { |
| 2836 | writeParcelable(value[i], parcelableFlags); |
| 2837 | } |
| 2838 | } else { |
| 2839 | writeInt(-1); |
| 2840 | } |
| 2841 | } |
| 2842 | |
| 2843 | /** |
| 2844 | * Read a typed object from a parcel. The given class loader will be |
| 2845 | * used to load any enclosed Parcelables. If it is null, the default class |
| 2846 | * loader will be used. |
| 2847 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2848 | @Nullable |
| 2849 | public final Object readValue(@Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2850 | int type = readInt(); |
| 2851 | |
| 2852 | switch (type) { |
| 2853 | case VAL_NULL: |
| 2854 | return null; |
| 2855 | |
| 2856 | case VAL_STRING: |
| 2857 | return readString(); |
| 2858 | |
| 2859 | case VAL_INTEGER: |
| 2860 | return readInt(); |
| 2861 | |
| 2862 | case VAL_MAP: |
| 2863 | return readHashMap(loader); |
| 2864 | |
| 2865 | case VAL_PARCELABLE: |
| 2866 | return readParcelable(loader); |
| 2867 | |
| 2868 | case VAL_SHORT: |
| 2869 | return (short) readInt(); |
| 2870 | |
| 2871 | case VAL_LONG: |
| 2872 | return readLong(); |
| 2873 | |
| 2874 | case VAL_FLOAT: |
| 2875 | return readFloat(); |
| 2876 | |
| 2877 | case VAL_DOUBLE: |
| 2878 | return readDouble(); |
| 2879 | |
| 2880 | case VAL_BOOLEAN: |
| 2881 | return readInt() == 1; |
| 2882 | |
| 2883 | case VAL_CHARSEQUENCE: |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 2884 | return readCharSequence(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2885 | |
| 2886 | case VAL_LIST: |
| 2887 | return readArrayList(loader); |
| 2888 | |
| 2889 | case VAL_BOOLEANARRAY: |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 2890 | return createBooleanArray(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2891 | |
| 2892 | case VAL_BYTEARRAY: |
| 2893 | return createByteArray(); |
| 2894 | |
| 2895 | case VAL_STRINGARRAY: |
| 2896 | return readStringArray(); |
| 2897 | |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 2898 | case VAL_CHARSEQUENCEARRAY: |
| 2899 | return readCharSequenceArray(); |
| 2900 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2901 | case VAL_IBINDER: |
| 2902 | return readStrongBinder(); |
| 2903 | |
| 2904 | case VAL_OBJECTARRAY: |
| 2905 | return readArray(loader); |
| 2906 | |
| 2907 | case VAL_INTARRAY: |
| 2908 | return createIntArray(); |
| 2909 | |
| 2910 | case VAL_LONGARRAY: |
| 2911 | return createLongArray(); |
| 2912 | |
| 2913 | case VAL_BYTE: |
| 2914 | return readByte(); |
| 2915 | |
| 2916 | case VAL_SERIALIZABLE: |
John Spurlock | 5002b8c | 2014-01-10 13:32:12 -0500 | [diff] [blame] | 2917 | return readSerializable(loader); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2918 | |
| 2919 | case VAL_PARCELABLEARRAY: |
| 2920 | return readParcelableArray(loader); |
| 2921 | |
| 2922 | case VAL_SPARSEARRAY: |
| 2923 | return readSparseArray(loader); |
| 2924 | |
| 2925 | case VAL_SPARSEBOOLEANARRAY: |
| 2926 | return readSparseBooleanArray(); |
| 2927 | |
| 2928 | case VAL_BUNDLE: |
| 2929 | return readBundle(loader); // loading will be deferred |
| 2930 | |
Craig Mautner | 719e6b1 | 2014-04-04 20:29:41 -0700 | [diff] [blame] | 2931 | case VAL_PERSISTABLEBUNDLE: |
| 2932 | return readPersistableBundle(loader); |
| 2933 | |
Jeff Sharkey | 5ef3398 | 2014-09-04 18:13:39 -0700 | [diff] [blame] | 2934 | case VAL_SIZE: |
| 2935 | return readSize(); |
| 2936 | |
| 2937 | case VAL_SIZEF: |
| 2938 | return readSizeF(); |
| 2939 | |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 2940 | case VAL_DOUBLEARRAY: |
| 2941 | return createDoubleArray(); |
| 2942 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2943 | default: |
| 2944 | int off = dataPosition() - 4; |
| 2945 | throw new RuntimeException( |
| 2946 | "Parcel " + this + ": Unmarshalling unknown type code " + type + " at offset " + off); |
| 2947 | } |
| 2948 | } |
| 2949 | |
| 2950 | /** |
| 2951 | * Read and return a new Parcelable from the parcel. The given class loader |
| 2952 | * will be used to load any enclosed Parcelables. If it is null, the default |
| 2953 | * class loader will be used. |
| 2954 | * @param loader A ClassLoader from which to instantiate the Parcelable |
| 2955 | * object, or null for the default class loader. |
| 2956 | * @return Returns the newly created Parcelable, or null if a null |
| 2957 | * object has been written. |
| 2958 | * @throws BadParcelableException Throws BadParcelableException if there |
| 2959 | * was an error trying to instantiate the Parcelable. |
| 2960 | */ |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2961 | @SuppressWarnings("unchecked") |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2962 | @Nullable |
| 2963 | public final <T extends Parcelable> T readParcelable(@Nullable ClassLoader loader) { |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2964 | Parcelable.Creator<?> creator = readParcelableCreator(loader); |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 2965 | if (creator == null) { |
| 2966 | return null; |
| 2967 | } |
| 2968 | if (creator instanceof Parcelable.ClassLoaderCreator<?>) { |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2969 | Parcelable.ClassLoaderCreator<?> classLoaderCreator = |
| 2970 | (Parcelable.ClassLoaderCreator<?>) creator; |
| 2971 | return (T) classLoaderCreator.createFromParcel(this, loader); |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 2972 | } |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2973 | return (T) creator.createFromParcel(this); |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | /** @hide */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 2977 | @UnsupportedAppUsage |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2978 | @SuppressWarnings("unchecked") |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2979 | @Nullable |
| 2980 | public final <T extends Parcelable> T readCreator(@NonNull Parcelable.Creator<?> creator, |
| 2981 | @Nullable ClassLoader loader) { |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 2982 | if (creator instanceof Parcelable.ClassLoaderCreator<?>) { |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2983 | Parcelable.ClassLoaderCreator<?> classLoaderCreator = |
| 2984 | (Parcelable.ClassLoaderCreator<?>) creator; |
| 2985 | return (T) classLoaderCreator.createFromParcel(this, loader); |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 2986 | } |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2987 | return (T) creator.createFromParcel(this); |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 2988 | } |
| 2989 | |
| 2990 | /** @hide */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 2991 | @UnsupportedAppUsage |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2992 | @Nullable |
| 2993 | public final Parcelable.Creator<?> readParcelableCreator(@Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2994 | String name = readString(); |
| 2995 | if (name == null) { |
| 2996 | return null; |
| 2997 | } |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2998 | Parcelable.Creator<?> creator; |
Andreas Gampe | 4c0df5c | 2019-05-30 14:38:43 -0700 | [diff] [blame] | 2999 | HashMap<String, Parcelable.Creator<?>> map; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3000 | synchronized (mCreators) { |
Andreas Gampe | 4c0df5c | 2019-05-30 14:38:43 -0700 | [diff] [blame] | 3001 | map = mCreators.get(loader); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3002 | if (map == null) { |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 3003 | map = new HashMap<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3004 | mCreators.put(loader, map); |
| 3005 | } |
| 3006 | creator = map.get(name); |
Andreas Gampe | 4c0df5c | 2019-05-30 14:38:43 -0700 | [diff] [blame] | 3007 | } |
| 3008 | if (creator != null) { |
| 3009 | return creator; |
| 3010 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3011 | |
Andreas Gampe | 4c0df5c | 2019-05-30 14:38:43 -0700 | [diff] [blame] | 3012 | try { |
| 3013 | // If loader == null, explicitly emulate Class.forName(String) "caller |
| 3014 | // classloader" behavior. |
| 3015 | ClassLoader parcelableClassLoader = |
| 3016 | (loader == null ? getClass().getClassLoader() : loader); |
| 3017 | // Avoid initializing the Parcelable class until we know it implements |
| 3018 | // Parcelable and has the necessary CREATOR field. http://b/1171613. |
| 3019 | Class<?> parcelableClass = Class.forName(name, false /* initialize */, |
| 3020 | parcelableClassLoader); |
| 3021 | if (!Parcelable.class.isAssignableFrom(parcelableClass)) { |
| 3022 | throw new BadParcelableException("Parcelable protocol requires subclassing " |
| 3023 | + "from Parcelable on class " + name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3024 | } |
Andreas Gampe | 4c0df5c | 2019-05-30 14:38:43 -0700 | [diff] [blame] | 3025 | Field f = parcelableClass.getField("CREATOR"); |
| 3026 | if ((f.getModifiers() & Modifier.STATIC) == 0) { |
| 3027 | throw new BadParcelableException("Parcelable protocol requires " |
| 3028 | + "the CREATOR object to be static on class " + name); |
| 3029 | } |
| 3030 | Class<?> creatorType = f.getType(); |
| 3031 | if (!Parcelable.Creator.class.isAssignableFrom(creatorType)) { |
| 3032 | // Fail before calling Field.get(), not after, to avoid initializing |
| 3033 | // parcelableClass unnecessarily. |
| 3034 | throw new BadParcelableException("Parcelable protocol requires a " |
| 3035 | + "Parcelable.Creator object called " |
| 3036 | + "CREATOR on class " + name); |
| 3037 | } |
| 3038 | creator = (Parcelable.Creator<?>) f.get(null); |
| 3039 | } catch (IllegalAccessException e) { |
| 3040 | Log.e(TAG, "Illegal access when unmarshalling: " + name, e); |
| 3041 | throw new BadParcelableException( |
| 3042 | "IllegalAccessException when unmarshalling: " + name); |
| 3043 | } catch (ClassNotFoundException e) { |
| 3044 | Log.e(TAG, "Class not found when unmarshalling: " + name, e); |
| 3045 | throw new BadParcelableException( |
| 3046 | "ClassNotFoundException when unmarshalling: " + name); |
| 3047 | } catch (NoSuchFieldException e) { |
| 3048 | throw new BadParcelableException("Parcelable protocol requires a " |
| 3049 | + "Parcelable.Creator object called " |
| 3050 | + "CREATOR on class " + name); |
| 3051 | } |
| 3052 | if (creator == null) { |
| 3053 | throw new BadParcelableException("Parcelable protocol requires a " |
| 3054 | + "non-null Parcelable.Creator object called " |
| 3055 | + "CREATOR on class " + name); |
| 3056 | } |
| 3057 | |
| 3058 | synchronized (mCreators) { |
| 3059 | map.put(name, creator); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3060 | } |
| 3061 | |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 3062 | return creator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3063 | } |
| 3064 | |
| 3065 | /** |
| 3066 | * Read and return a new Parcelable array from the parcel. |
| 3067 | * The given class loader will be used to load any enclosed |
| 3068 | * Parcelables. |
| 3069 | * @return the Parcelable array, or null if the array is null |
| 3070 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3071 | @Nullable |
| 3072 | public final Parcelable[] readParcelableArray(@Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3073 | int N = readInt(); |
| 3074 | if (N < 0) { |
| 3075 | return null; |
| 3076 | } |
| 3077 | Parcelable[] p = new Parcelable[N]; |
| 3078 | for (int i = 0; i < N; i++) { |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 3079 | p[i] = readParcelable(loader); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3080 | } |
| 3081 | return p; |
| 3082 | } |
| 3083 | |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 3084 | /** @hide */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3085 | @Nullable |
| 3086 | public final <T extends Parcelable> T[] readParcelableArray(@Nullable ClassLoader loader, |
| 3087 | @NonNull Class<T> clazz) { |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 3088 | int N = readInt(); |
| 3089 | if (N < 0) { |
| 3090 | return null; |
| 3091 | } |
| 3092 | T[] p = (T[]) Array.newInstance(clazz, N); |
| 3093 | for (int i = 0; i < N; i++) { |
| 3094 | p[i] = readParcelable(loader); |
| 3095 | } |
| 3096 | return p; |
| 3097 | } |
| 3098 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3099 | /** |
| 3100 | * Read and return a new Serializable object from the parcel. |
| 3101 | * @return the Serializable object, or null if the Serializable name |
| 3102 | * wasn't found in the parcel. |
| 3103 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3104 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3105 | public final Serializable readSerializable() { |
John Spurlock | 5002b8c | 2014-01-10 13:32:12 -0500 | [diff] [blame] | 3106 | return readSerializable(null); |
| 3107 | } |
| 3108 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3109 | @Nullable |
| 3110 | private final Serializable readSerializable(@Nullable final ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3111 | String name = readString(); |
| 3112 | if (name == null) { |
| 3113 | // For some reason we were unable to read the name of the Serializable (either there |
| 3114 | // is nothing left in the Parcel to read, or the next value wasn't a String), so |
| 3115 | // return null, which indicates that the name wasn't found in the parcel. |
| 3116 | return null; |
| 3117 | } |
| 3118 | |
| 3119 | byte[] serializedData = createByteArray(); |
| 3120 | ByteArrayInputStream bais = new ByteArrayInputStream(serializedData); |
| 3121 | try { |
John Spurlock | 5002b8c | 2014-01-10 13:32:12 -0500 | [diff] [blame] | 3122 | ObjectInputStream ois = new ObjectInputStream(bais) { |
| 3123 | @Override |
| 3124 | protected Class<?> resolveClass(ObjectStreamClass osClass) |
| 3125 | throws IOException, ClassNotFoundException { |
| 3126 | // try the custom classloader if provided |
| 3127 | if (loader != null) { |
| 3128 | Class<?> c = Class.forName(osClass.getName(), false, loader); |
| 3129 | if (c != null) { |
| 3130 | return c; |
| 3131 | } |
| 3132 | } |
| 3133 | return super.resolveClass(osClass); |
| 3134 | } |
| 3135 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3136 | return (Serializable) ois.readObject(); |
| 3137 | } catch (IOException ioe) { |
| 3138 | throw new RuntimeException("Parcelable encountered " + |
| 3139 | "IOException reading a Serializable object (name = " + name + |
| 3140 | ")", ioe); |
| 3141 | } catch (ClassNotFoundException cnfe) { |
John Spurlock | 5002b8c | 2014-01-10 13:32:12 -0500 | [diff] [blame] | 3142 | throw new RuntimeException("Parcelable encountered " + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3143 | "ClassNotFoundException reading a Serializable object (name = " |
| 3144 | + name + ")", cnfe); |
| 3145 | } |
| 3146 | } |
| 3147 | |
| 3148 | // Cache of previously looked up CREATOR.createFromParcel() methods for |
| 3149 | // particular classes. Keys are the names of the classes, values are |
| 3150 | // Method objects. |
Artur Satayev | 70507ed | 2019-07-29 13:18:27 +0100 | [diff] [blame^] | 3151 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 3152 | private static final HashMap<ClassLoader,HashMap<String,Parcelable.Creator<?>>> |
| 3153 | mCreators = new HashMap<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3154 | |
Narayan Kamath | b34a461 | 2014-01-23 14:17:11 +0000 | [diff] [blame] | 3155 | /** @hide for internal use only. */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3156 | static protected final Parcel obtain(int obj) { |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 3157 | throw new UnsupportedOperationException(); |
| 3158 | } |
| 3159 | |
| 3160 | /** @hide */ |
| 3161 | static protected final Parcel obtain(long obj) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3162 | final Parcel[] pool = sHolderPool; |
| 3163 | synchronized (pool) { |
| 3164 | Parcel p; |
| 3165 | for (int i=0; i<POOL_SIZE; i++) { |
| 3166 | p = pool[i]; |
| 3167 | if (p != null) { |
| 3168 | pool[i] = null; |
| 3169 | if (DEBUG_RECYCLE) { |
| 3170 | p.mStack = new RuntimeException(); |
| 3171 | } |
| 3172 | p.init(obj); |
| 3173 | return p; |
| 3174 | } |
| 3175 | } |
| 3176 | } |
| 3177 | return new Parcel(obj); |
| 3178 | } |
| 3179 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 3180 | private Parcel(long nativePtr) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3181 | if (DEBUG_RECYCLE) { |
| 3182 | mStack = new RuntimeException(); |
| 3183 | } |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 3184 | //Log.i(TAG, "Initializing obj=0x" + Integer.toHexString(obj), mStack); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 3185 | init(nativePtr); |
| 3186 | } |
| 3187 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 3188 | private void init(long nativePtr) { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 3189 | if (nativePtr != 0) { |
| 3190 | mNativePtr = nativePtr; |
| 3191 | mOwnsNativeParcelObject = false; |
| 3192 | } else { |
| 3193 | mNativePtr = nativeCreate(); |
| 3194 | mOwnsNativeParcelObject = true; |
| 3195 | } |
| 3196 | } |
| 3197 | |
| 3198 | private void freeBuffer() { |
| 3199 | if (mOwnsNativeParcelObject) { |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 3200 | updateNativeSize(nativeFreeBuffer(mNativePtr)); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 3201 | } |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 3202 | mReadWriteHelper = ReadWriteHelper.DEFAULT; |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | private void destroy() { |
| 3206 | if (mNativePtr != 0) { |
| 3207 | if (mOwnsNativeParcelObject) { |
| 3208 | nativeDestroy(mNativePtr); |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 3209 | updateNativeSize(0); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 3210 | } |
| 3211 | mNativePtr = 0; |
| 3212 | } |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 3213 | mReadWriteHelper = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3214 | } |
| 3215 | |
| 3216 | @Override |
| 3217 | protected void finalize() throws Throwable { |
| 3218 | if (DEBUG_RECYCLE) { |
| 3219 | if (mStack != null) { |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 3220 | Log.w(TAG, "Client did not call Parcel.recycle()", mStack); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3221 | } |
| 3222 | } |
| 3223 | destroy(); |
| 3224 | } |
| 3225 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3226 | /* package */ void readMapInternal(@NonNull Map outVal, int N, |
| 3227 | @Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3228 | while (N > 0) { |
| 3229 | Object key = readValue(loader); |
| 3230 | Object value = readValue(loader); |
| 3231 | outVal.put(key, value); |
| 3232 | N--; |
| 3233 | } |
| 3234 | } |
| 3235 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3236 | /* package */ void readArrayMapInternal(@NonNull ArrayMap outVal, int N, |
| 3237 | @Nullable ClassLoader loader) { |
Dianne Hackborn | e784d1e | 2013-09-20 18:13:52 -0700 | [diff] [blame] | 3238 | if (DEBUG_ARRAY_MAP) { |
| 3239 | RuntimeException here = new RuntimeException("here"); |
| 3240 | here.fillInStackTrace(); |
| 3241 | Log.d(TAG, "Reading " + N + " ArrayMap entries", here); |
| 3242 | } |
Dianne Hackborn | 8aee64d | 2013-10-25 10:41:50 -0700 | [diff] [blame] | 3243 | int startPos; |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 3244 | while (N > 0) { |
Dianne Hackborn | 8aee64d | 2013-10-25 10:41:50 -0700 | [diff] [blame] | 3245 | if (DEBUG_ARRAY_MAP) startPos = dataPosition(); |
Dianne Hackborn | 9c3e74f | 2014-08-13 15:39:50 -0700 | [diff] [blame] | 3246 | String key = readString(); |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 3247 | Object value = readValue(loader); |
Dianne Hackborn | 8aee64d | 2013-10-25 10:41:50 -0700 | [diff] [blame] | 3248 | if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " " |
| 3249 | + (dataPosition()-startPos) + " bytes: key=0x" |
| 3250 | + Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key); |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 3251 | outVal.append(key, value); |
| 3252 | N--; |
| 3253 | } |
Dianne Hackborn | 9c3e74f | 2014-08-13 15:39:50 -0700 | [diff] [blame] | 3254 | outVal.validate(); |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 3255 | } |
| 3256 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3257 | /* package */ void readArrayMapSafelyInternal(@NonNull ArrayMap outVal, int N, |
| 3258 | @Nullable ClassLoader loader) { |
Dianne Hackborn | e784d1e | 2013-09-20 18:13:52 -0700 | [diff] [blame] | 3259 | if (DEBUG_ARRAY_MAP) { |
| 3260 | RuntimeException here = new RuntimeException("here"); |
| 3261 | here.fillInStackTrace(); |
| 3262 | Log.d(TAG, "Reading safely " + N + " ArrayMap entries", here); |
| 3263 | } |
| 3264 | while (N > 0) { |
Dianne Hackborn | 9c3e74f | 2014-08-13 15:39:50 -0700 | [diff] [blame] | 3265 | String key = readString(); |
Dianne Hackborn | e784d1e | 2013-09-20 18:13:52 -0700 | [diff] [blame] | 3266 | if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read safe #" + (N-1) + ": key=0x" |
| 3267 | + (key != null ? key.hashCode() : 0) + " " + key); |
| 3268 | Object value = readValue(loader); |
| 3269 | outVal.put(key, value); |
| 3270 | N--; |
| 3271 | } |
| 3272 | } |
| 3273 | |
Dianne Hackborn | 9c3e74f | 2014-08-13 15:39:50 -0700 | [diff] [blame] | 3274 | /** |
| 3275 | * @hide For testing only. |
| 3276 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 3277 | @UnsupportedAppUsage |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3278 | public void readArrayMap(@NonNull ArrayMap outVal, @Nullable ClassLoader loader) { |
Dianne Hackborn | 9c3e74f | 2014-08-13 15:39:50 -0700 | [diff] [blame] | 3279 | final int N = readInt(); |
| 3280 | if (N < 0) { |
| 3281 | return; |
| 3282 | } |
| 3283 | readArrayMapInternal(outVal, N, loader); |
| 3284 | } |
| 3285 | |
Svet Ganov | ddb9488 | 2016-06-23 19:55:24 -0700 | [diff] [blame] | 3286 | /** |
| 3287 | * Reads an array set. |
| 3288 | * |
| 3289 | * @param loader The class loader to use. |
| 3290 | * |
| 3291 | * @hide |
| 3292 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 3293 | @UnsupportedAppUsage |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3294 | public @Nullable ArraySet<? extends Object> readArraySet(@Nullable ClassLoader loader) { |
Svet Ganov | ddb9488 | 2016-06-23 19:55:24 -0700 | [diff] [blame] | 3295 | final int size = readInt(); |
| 3296 | if (size < 0) { |
| 3297 | return null; |
| 3298 | } |
| 3299 | ArraySet<Object> result = new ArraySet<>(size); |
| 3300 | for (int i = 0; i < size; i++) { |
| 3301 | Object value = readValue(loader); |
| 3302 | result.append(value); |
| 3303 | } |
| 3304 | return result; |
| 3305 | } |
| 3306 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3307 | private void readListInternal(@NonNull List outVal, int N, |
| 3308 | @Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3309 | while (N > 0) { |
| 3310 | Object value = readValue(loader); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 3311 | //Log.d(TAG, "Unmarshalling value=" + value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3312 | outVal.add(value); |
| 3313 | N--; |
| 3314 | } |
| 3315 | } |
| 3316 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3317 | private void readArrayInternal(@NonNull Object[] outVal, int N, |
| 3318 | @Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3319 | for (int i = 0; i < N; i++) { |
| 3320 | Object value = readValue(loader); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 3321 | //Log.d(TAG, "Unmarshalling value=" + value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3322 | outVal[i] = value; |
| 3323 | } |
| 3324 | } |
| 3325 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3326 | private void readSparseArrayInternal(@NonNull SparseArray outVal, int N, |
| 3327 | @Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3328 | while (N > 0) { |
| 3329 | int key = readInt(); |
| 3330 | Object value = readValue(loader); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 3331 | //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3332 | outVal.append(key, value); |
| 3333 | N--; |
| 3334 | } |
| 3335 | } |
| 3336 | |
| 3337 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3338 | private void readSparseBooleanArrayInternal(@NonNull SparseBooleanArray outVal, int N) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3339 | while (N > 0) { |
| 3340 | int key = readInt(); |
| 3341 | boolean value = this.readByte() == 1; |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 3342 | //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3343 | outVal.append(key, value); |
| 3344 | N--; |
| 3345 | } |
| 3346 | } |
Dan Sandler | 5ce0430 | 2015-04-09 23:50:15 -0400 | [diff] [blame] | 3347 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3348 | private void readSparseIntArrayInternal(@NonNull SparseIntArray outVal, int N) { |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 3349 | while (N > 0) { |
| 3350 | int key = readInt(); |
| 3351 | int value = readInt(); |
| 3352 | outVal.append(key, value); |
| 3353 | N--; |
| 3354 | } |
| 3355 | } |
| 3356 | |
Dan Sandler | 5ce0430 | 2015-04-09 23:50:15 -0400 | [diff] [blame] | 3357 | /** |
| 3358 | * @hide For testing |
| 3359 | */ |
| 3360 | public long getBlobAshmemSize() { |
| 3361 | return nativeGetBlobAshmemSize(mNativePtr); |
| 3362 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3363 | } |