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