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