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