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