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