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 | |
| 19 | import android.text.TextUtils; |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 20 | import android.util.ArrayMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.util.Log; |
| 22 | import android.util.SparseArray; |
| 23 | import android.util.SparseBooleanArray; |
| 24 | |
| 25 | import java.io.ByteArrayInputStream; |
| 26 | import java.io.ByteArrayOutputStream; |
| 27 | import java.io.FileDescriptor; |
| 28 | import java.io.FileNotFoundException; |
| 29 | import java.io.IOException; |
| 30 | import java.io.ObjectInputStream; |
| 31 | import java.io.ObjectOutputStream; |
John Spurlock | 5002b8c | 2014-01-10 13:32:12 -0500 | [diff] [blame] | 32 | import java.io.ObjectStreamClass; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import java.io.Serializable; |
| 34 | import java.lang.reflect.Field; |
| 35 | import java.util.ArrayList; |
Elliott Hughes | a28b83e | 2011-02-28 14:26:13 -0800 | [diff] [blame] | 36 | import java.util.Arrays; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import java.util.HashMap; |
| 38 | import java.util.List; |
| 39 | import java.util.Map; |
| 40 | import java.util.Set; |
| 41 | |
| 42 | /** |
| 43 | * Container for a message (data and object references) that can |
| 44 | * be sent through an IBinder. A Parcel can contain both flattened data |
| 45 | * that will be unflattened on the other side of the IPC (using the various |
| 46 | * methods here for writing specific types, or the general |
| 47 | * {@link Parcelable} interface), and references to live {@link IBinder} |
| 48 | * objects that will result in the other side receiving a proxy IBinder |
| 49 | * connected with the original IBinder in the Parcel. |
| 50 | * |
| 51 | * <p class="note">Parcel is <strong>not</strong> a general-purpose |
| 52 | * serialization mechanism. This class (and the corresponding |
| 53 | * {@link Parcelable} API for placing arbitrary objects into a Parcel) is |
| 54 | * designed as a high-performance IPC transport. As such, it is not |
| 55 | * appropriate to place any Parcel data in to persistent storage: changes |
| 56 | * in the underlying implementation of any of the data in the Parcel can |
| 57 | * render older data unreadable.</p> |
| 58 | * |
| 59 | * <p>The bulk of the Parcel API revolves around reading and writing data |
| 60 | * of various types. There are six major classes of such functions available.</p> |
| 61 | * |
| 62 | * <h3>Primitives</h3> |
| 63 | * |
| 64 | * <p>The most basic data functions are for writing and reading primitive |
| 65 | * data types: {@link #writeByte}, {@link #readByte}, {@link #writeDouble}, |
| 66 | * {@link #readDouble}, {@link #writeFloat}, {@link #readFloat}, {@link #writeInt}, |
| 67 | * {@link #readInt}, {@link #writeLong}, {@link #readLong}, |
| 68 | * {@link #writeString}, {@link #readString}. Most other |
| 69 | * data operations are built on top of these. The given data is written and |
| 70 | * read using the endianess of the host CPU.</p> |
| 71 | * |
| 72 | * <h3>Primitive Arrays</h3> |
| 73 | * |
| 74 | * <p>There are a variety of methods for reading and writing raw arrays |
| 75 | * of primitive objects, which generally result in writing a 4-byte length |
| 76 | * followed by the primitive data items. The methods for reading can either |
| 77 | * read the data into an existing array, or create and return a new array. |
| 78 | * These available types are:</p> |
| 79 | * |
| 80 | * <ul> |
| 81 | * <li> {@link #writeBooleanArray(boolean[])}, |
| 82 | * {@link #readBooleanArray(boolean[])}, {@link #createBooleanArray()} |
| 83 | * <li> {@link #writeByteArray(byte[])}, |
| 84 | * {@link #writeByteArray(byte[], int, int)}, {@link #readByteArray(byte[])}, |
| 85 | * {@link #createByteArray()} |
| 86 | * <li> {@link #writeCharArray(char[])}, {@link #readCharArray(char[])}, |
| 87 | * {@link #createCharArray()} |
| 88 | * <li> {@link #writeDoubleArray(double[])}, {@link #readDoubleArray(double[])}, |
| 89 | * {@link #createDoubleArray()} |
| 90 | * <li> {@link #writeFloatArray(float[])}, {@link #readFloatArray(float[])}, |
| 91 | * {@link #createFloatArray()} |
| 92 | * <li> {@link #writeIntArray(int[])}, {@link #readIntArray(int[])}, |
| 93 | * {@link #createIntArray()} |
| 94 | * <li> {@link #writeLongArray(long[])}, {@link #readLongArray(long[])}, |
| 95 | * {@link #createLongArray()} |
| 96 | * <li> {@link #writeStringArray(String[])}, {@link #readStringArray(String[])}, |
| 97 | * {@link #createStringArray()}. |
| 98 | * <li> {@link #writeSparseBooleanArray(SparseBooleanArray)}, |
| 99 | * {@link #readSparseBooleanArray()}. |
| 100 | * </ul> |
| 101 | * |
| 102 | * <h3>Parcelables</h3> |
| 103 | * |
| 104 | * <p>The {@link Parcelable} protocol provides an extremely efficient (but |
| 105 | * low-level) protocol for objects to write and read themselves from Parcels. |
| 106 | * You can use the direct methods {@link #writeParcelable(Parcelable, int)} |
| 107 | * and {@link #readParcelable(ClassLoader)} or |
| 108 | * {@link #writeParcelableArray} and |
| 109 | * {@link #readParcelableArray(ClassLoader)} to write or read. These |
| 110 | * methods write both the class type and its data to the Parcel, allowing |
| 111 | * that class to be reconstructed from the appropriate class loader when |
| 112 | * later reading.</p> |
| 113 | * |
| 114 | * <p>There are also some methods that provide a more efficient way to work |
| 115 | * with Parcelables: {@link #writeTypedArray}, |
| 116 | * {@link #writeTypedList(List)}, |
| 117 | * {@link #readTypedArray} and {@link #readTypedList}. These methods |
| 118 | * do not write the class information of the original object: instead, the |
| 119 | * caller of the read function must know what type to expect and pass in the |
| 120 | * appropriate {@link Parcelable.Creator Parcelable.Creator} instead to |
| 121 | * properly construct the new object and read its data. (To more efficient |
| 122 | * write and read a single Parceable object, you can directly call |
| 123 | * {@link Parcelable#writeToParcel Parcelable.writeToParcel} and |
| 124 | * {@link Parcelable.Creator#createFromParcel Parcelable.Creator.createFromParcel} |
| 125 | * yourself.)</p> |
| 126 | * |
| 127 | * <h3>Bundles</h3> |
| 128 | * |
| 129 | * <p>A special type-safe container, called {@link Bundle}, is available |
| 130 | * for key/value maps of heterogeneous values. This has many optimizations |
| 131 | * for improved performance when reading and writing data, and its type-safe |
| 132 | * API avoids difficult to debug type errors when finally marshalling the |
| 133 | * data contents into a Parcel. The methods to use are |
| 134 | * {@link #writeBundle(Bundle)}, {@link #readBundle()}, and |
| 135 | * {@link #readBundle(ClassLoader)}. |
| 136 | * |
| 137 | * <h3>Active Objects</h3> |
| 138 | * |
| 139 | * <p>An unusual feature of Parcel is the ability to read and write active |
| 140 | * objects. For these objects the actual contents of the object is not |
| 141 | * written, rather a special token referencing the object is written. When |
| 142 | * reading the object back from the Parcel, you do not get a new instance of |
| 143 | * the object, but rather a handle that operates on the exact same object that |
| 144 | * was originally written. There are two forms of active objects available.</p> |
| 145 | * |
| 146 | * <p>{@link Binder} objects are a core facility of Android's general cross-process |
| 147 | * communication system. The {@link IBinder} interface describes an abstract |
| 148 | * protocol with a Binder object. Any such interface can be written in to |
| 149 | * a Parcel, and upon reading you will receive either the original object |
| 150 | * implementing that interface or a special proxy implementation |
| 151 | * that communicates calls back to the original object. The methods to use are |
| 152 | * {@link #writeStrongBinder(IBinder)}, |
| 153 | * {@link #writeStrongInterface(IInterface)}, {@link #readStrongBinder()}, |
| 154 | * {@link #writeBinderArray(IBinder[])}, {@link #readBinderArray(IBinder[])}, |
| 155 | * {@link #createBinderArray()}, |
| 156 | * {@link #writeBinderList(List)}, {@link #readBinderList(List)}, |
| 157 | * {@link #createBinderArrayList()}.</p> |
| 158 | * |
| 159 | * <p>FileDescriptor objects, representing raw Linux file descriptor identifiers, |
| 160 | * can be written and {@link ParcelFileDescriptor} objects returned to operate |
| 161 | * on the original file descriptor. The returned file descriptor is a dup |
| 162 | * of the original file descriptor: the object and fd is different, but |
| 163 | * operating on the same underlying file stream, with the same position, etc. |
| 164 | * The methods to use are {@link #writeFileDescriptor(FileDescriptor)}, |
| 165 | * {@link #readFileDescriptor()}. |
| 166 | * |
| 167 | * <h3>Untyped Containers</h3> |
| 168 | * |
| 169 | * <p>A final class of methods are for writing and reading standard Java |
| 170 | * containers of arbitrary types. These all revolve around the |
| 171 | * {@link #writeValue(Object)} and {@link #readValue(ClassLoader)} methods |
| 172 | * which define the types of objects allowed. The container methods are |
| 173 | * {@link #writeArray(Object[])}, {@link #readArray(ClassLoader)}, |
| 174 | * {@link #writeList(List)}, {@link #readList(List, ClassLoader)}, |
| 175 | * {@link #readArrayList(ClassLoader)}, |
| 176 | * {@link #writeMap(Map)}, {@link #readMap(Map, ClassLoader)}, |
| 177 | * {@link #writeSparseArray(SparseArray)}, |
| 178 | * {@link #readSparseArray(ClassLoader)}. |
| 179 | */ |
| 180 | public final class Parcel { |
| 181 | private static final boolean DEBUG_RECYCLE = false; |
Dianne Hackborn | e784d1e | 2013-09-20 18:13:52 -0700 | [diff] [blame] | 182 | private static final boolean DEBUG_ARRAY_MAP = false; |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 183 | private static final String TAG = "Parcel"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | |
| 185 | @SuppressWarnings({"UnusedDeclaration"}) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 186 | private long mNativePtr; // used by native code |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 187 | |
| 188 | /** |
| 189 | * Flag indicating if {@link #mNativePtr} was allocated by this object, |
| 190 | * indicating that we're responsible for its lifecycle. |
| 191 | */ |
| 192 | private boolean mOwnsNativeParcelObject; |
| 193 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | private RuntimeException mStack; |
| 195 | |
| 196 | private static final int POOL_SIZE = 6; |
| 197 | private static final Parcel[] sOwnedPool = new Parcel[POOL_SIZE]; |
| 198 | private static final Parcel[] sHolderPool = new Parcel[POOL_SIZE]; |
| 199 | |
| 200 | private static final int VAL_NULL = -1; |
| 201 | private static final int VAL_STRING = 0; |
| 202 | private static final int VAL_INTEGER = 1; |
| 203 | private static final int VAL_MAP = 2; |
| 204 | private static final int VAL_BUNDLE = 3; |
| 205 | private static final int VAL_PARCELABLE = 4; |
| 206 | private static final int VAL_SHORT = 5; |
| 207 | private static final int VAL_LONG = 6; |
| 208 | private static final int VAL_FLOAT = 7; |
| 209 | private static final int VAL_DOUBLE = 8; |
| 210 | private static final int VAL_BOOLEAN = 9; |
| 211 | private static final int VAL_CHARSEQUENCE = 10; |
| 212 | private static final int VAL_LIST = 11; |
| 213 | private static final int VAL_SPARSEARRAY = 12; |
| 214 | private static final int VAL_BYTEARRAY = 13; |
| 215 | private static final int VAL_STRINGARRAY = 14; |
| 216 | private static final int VAL_IBINDER = 15; |
| 217 | private static final int VAL_PARCELABLEARRAY = 16; |
| 218 | private static final int VAL_OBJECTARRAY = 17; |
| 219 | private static final int VAL_INTARRAY = 18; |
| 220 | private static final int VAL_LONGARRAY = 19; |
| 221 | private static final int VAL_BYTE = 20; |
| 222 | private static final int VAL_SERIALIZABLE = 21; |
| 223 | private static final int VAL_SPARSEBOOLEANARRAY = 22; |
| 224 | private static final int VAL_BOOLEANARRAY = 23; |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 225 | private static final int VAL_CHARSEQUENCEARRAY = 24; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 227 | // The initial int32 in a Binder call's reply Parcel header: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | private static final int EX_SECURITY = -1; |
| 229 | private static final int EX_BAD_PARCELABLE = -2; |
| 230 | private static final int EX_ILLEGAL_ARGUMENT = -3; |
| 231 | private static final int EX_NULL_POINTER = -4; |
| 232 | private static final int EX_ILLEGAL_STATE = -5; |
Dianne Hackborn | 7e71442 | 2013-09-13 17:32:57 -0700 | [diff] [blame] | 233 | private static final int EX_NETWORK_MAIN_THREAD = -6; |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 234 | private static final int EX_HAS_REPLY_HEADER = -128; // special; see below |
| 235 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 236 | private static native int nativeDataSize(long nativePtr); |
| 237 | private static native int nativeDataAvail(long nativePtr); |
| 238 | private static native int nativeDataPosition(long nativePtr); |
| 239 | private static native int nativeDataCapacity(long nativePtr); |
| 240 | private static native void nativeSetDataSize(long nativePtr, int size); |
| 241 | private static native void nativeSetDataPosition(long nativePtr, int pos); |
| 242 | private static native void nativeSetDataCapacity(long nativePtr, int size); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 243 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 244 | private static native boolean nativePushAllowFds(long nativePtr, boolean allowFds); |
| 245 | private static native void nativeRestoreAllowFds(long nativePtr, boolean lastValue); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 246 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 247 | private static native void nativeWriteByteArray(long nativePtr, byte[] b, int offset, int len); |
| 248 | private static native void nativeWriteInt(long nativePtr, int val); |
| 249 | private static native void nativeWriteLong(long nativePtr, long val); |
| 250 | private static native void nativeWriteFloat(long nativePtr, float val); |
| 251 | private static native void nativeWriteDouble(long nativePtr, double val); |
| 252 | private static native void nativeWriteString(long nativePtr, String val); |
| 253 | private static native void nativeWriteStrongBinder(long nativePtr, IBinder val); |
| 254 | private static native void nativeWriteFileDescriptor(long nativePtr, FileDescriptor val); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 255 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 256 | private static native byte[] nativeCreateByteArray(long nativePtr); |
| 257 | private static native int nativeReadInt(long nativePtr); |
| 258 | private static native long nativeReadLong(long nativePtr); |
| 259 | private static native float nativeReadFloat(long nativePtr); |
| 260 | private static native double nativeReadDouble(long nativePtr); |
| 261 | private static native String nativeReadString(long nativePtr); |
| 262 | private static native IBinder nativeReadStrongBinder(long nativePtr); |
| 263 | private static native FileDescriptor nativeReadFileDescriptor(long nativePtr); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 264 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 265 | private static native long nativeCreate(); |
| 266 | private static native void nativeFreeBuffer(long nativePtr); |
| 267 | private static native void nativeDestroy(long nativePtr); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 268 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 269 | private static native byte[] nativeMarshall(long nativePtr); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 270 | private static native void nativeUnmarshall( |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 271 | long nativePtr, byte[] data, int offest, int length); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 272 | private static native void nativeAppendFrom( |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 273 | long thisNativePtr, long otherNativePtr, int offset, int length); |
| 274 | private static native boolean nativeHasFileDescriptors(long nativePtr); |
| 275 | private static native void nativeWriteInterfaceToken(long nativePtr, String interfaceName); |
| 276 | private static native void nativeEnforceInterface(long nativePtr, String interfaceName); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 277 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | public final static Parcelable.Creator<String> STRING_CREATOR |
| 279 | = new Parcelable.Creator<String>() { |
| 280 | public String createFromParcel(Parcel source) { |
| 281 | return source.readString(); |
| 282 | } |
| 283 | public String[] newArray(int size) { |
| 284 | return new String[size]; |
| 285 | } |
| 286 | }; |
| 287 | |
| 288 | /** |
| 289 | * Retrieve a new Parcel object from the pool. |
| 290 | */ |
| 291 | public static Parcel obtain() { |
| 292 | final Parcel[] pool = sOwnedPool; |
| 293 | synchronized (pool) { |
| 294 | Parcel p; |
| 295 | for (int i=0; i<POOL_SIZE; i++) { |
| 296 | p = pool[i]; |
| 297 | if (p != null) { |
| 298 | pool[i] = null; |
| 299 | if (DEBUG_RECYCLE) { |
| 300 | p.mStack = new RuntimeException(); |
| 301 | } |
| 302 | return p; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | return new Parcel(0); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Put a Parcel object back into the pool. You must not touch |
| 311 | * the object after this call. |
| 312 | */ |
| 313 | public final void recycle() { |
| 314 | if (DEBUG_RECYCLE) mStack = null; |
| 315 | freeBuffer(); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 316 | |
| 317 | final Parcel[] pool; |
| 318 | if (mOwnsNativeParcelObject) { |
| 319 | pool = sOwnedPool; |
| 320 | } else { |
| 321 | mNativePtr = 0; |
| 322 | pool = sHolderPool; |
| 323 | } |
| 324 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | synchronized (pool) { |
| 326 | for (int i=0; i<POOL_SIZE; i++) { |
| 327 | if (pool[i] == null) { |
| 328 | pool[i] = this; |
| 329 | return; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Returns the total amount of data contained in the parcel. |
| 337 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 338 | public final int dataSize() { |
| 339 | return nativeDataSize(mNativePtr); |
| 340 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | |
| 342 | /** |
| 343 | * Returns the amount of data remaining to be read from the |
| 344 | * parcel. That is, {@link #dataSize}-{@link #dataPosition}. |
| 345 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 346 | public final int dataAvail() { |
| 347 | return nativeDataAvail(mNativePtr); |
| 348 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | |
| 350 | /** |
| 351 | * Returns the current position in the parcel data. Never |
| 352 | * more than {@link #dataSize}. |
| 353 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 354 | public final int dataPosition() { |
| 355 | return nativeDataPosition(mNativePtr); |
| 356 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | |
| 358 | /** |
| 359 | * Returns the total amount of space in the parcel. This is always |
| 360 | * >= {@link #dataSize}. The difference between it and dataSize() is the |
| 361 | * amount of room left until the parcel needs to re-allocate its |
| 362 | * data buffer. |
| 363 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 364 | public final int dataCapacity() { |
| 365 | return nativeDataCapacity(mNativePtr); |
| 366 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | |
| 368 | /** |
| 369 | * Change the amount of data in the parcel. Can be either smaller or |
| 370 | * larger than the current size. If larger than the current capacity, |
| 371 | * more memory will be allocated. |
| 372 | * |
| 373 | * @param size The new number of bytes in the Parcel. |
| 374 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 375 | public final void setDataSize(int size) { |
| 376 | nativeSetDataSize(mNativePtr, size); |
| 377 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | |
| 379 | /** |
| 380 | * Move the current read/write position in the parcel. |
| 381 | * @param pos New offset in the parcel; must be between 0 and |
| 382 | * {@link #dataSize}. |
| 383 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 384 | public final void setDataPosition(int pos) { |
| 385 | nativeSetDataPosition(mNativePtr, pos); |
| 386 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | |
| 388 | /** |
| 389 | * Change the capacity (current available space) of the parcel. |
| 390 | * |
| 391 | * @param size The new capacity of the parcel, in bytes. Can not be |
| 392 | * less than {@link #dataSize} -- that is, you can not drop existing data |
| 393 | * with this method. |
| 394 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 395 | public final void setDataCapacity(int size) { |
| 396 | nativeSetDataCapacity(mNativePtr, size); |
| 397 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 399 | /** @hide */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 400 | public final boolean pushAllowFds(boolean allowFds) { |
| 401 | return nativePushAllowFds(mNativePtr, allowFds); |
| 402 | } |
Dianne Hackborn | c04db7e | 2011-10-03 21:09:35 -0700 | [diff] [blame] | 403 | |
| 404 | /** @hide */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 405 | public final void restoreAllowFds(boolean lastValue) { |
| 406 | nativeRestoreAllowFds(mNativePtr, lastValue); |
| 407 | } |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 408 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | /** |
| 410 | * Returns the raw bytes of the parcel. |
| 411 | * |
| 412 | * <p class="note">The data you retrieve here <strong>must not</strong> |
| 413 | * be placed in any kind of persistent storage (on local disk, across |
| 414 | * a network, etc). For that, you should use standard serialization |
| 415 | * or another kind of general serialization mechanism. The Parcel |
| 416 | * marshalled representation is highly optimized for local IPC, and as |
| 417 | * such does not attempt to maintain compatibility with data created |
| 418 | * in different versions of the platform. |
| 419 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 420 | public final byte[] marshall() { |
| 421 | return nativeMarshall(mNativePtr); |
| 422 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | |
| 424 | /** |
| 425 | * Set the bytes in data to be the raw bytes of this Parcel. |
| 426 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 427 | public final void unmarshall(byte[] data, int offest, int length) { |
| 428 | nativeUnmarshall(mNativePtr, data, offest, length); |
| 429 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 431 | public final void appendFrom(Parcel parcel, int offset, int length) { |
| 432 | nativeAppendFrom(mNativePtr, parcel.mNativePtr, offset, length); |
| 433 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | |
| 435 | /** |
| 436 | * Report whether the parcel contains any marshalled file descriptors. |
| 437 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 438 | public final boolean hasFileDescriptors() { |
| 439 | return nativeHasFileDescriptors(mNativePtr); |
| 440 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | |
| 442 | /** |
| 443 | * Store or read an IBinder interface token in the parcel at the current |
| 444 | * {@link #dataPosition}. This is used to validate that the marshalled |
| 445 | * transaction is intended for the target interface. |
| 446 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 447 | public final void writeInterfaceToken(String interfaceName) { |
| 448 | nativeWriteInterfaceToken(mNativePtr, interfaceName); |
| 449 | } |
| 450 | |
| 451 | public final void enforceInterface(String interfaceName) { |
| 452 | nativeEnforceInterface(mNativePtr, interfaceName); |
| 453 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | |
| 455 | /** |
Elliott Hughes | a28b83e | 2011-02-28 14:26:13 -0800 | [diff] [blame] | 456 | * 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] | 457 | * growing {@link #dataCapacity} if needed. |
| 458 | * @param b Bytes to place into the parcel. |
| 459 | */ |
| 460 | public final void writeByteArray(byte[] b) { |
| 461 | writeByteArray(b, 0, (b != null) ? b.length : 0); |
| 462 | } |
| 463 | |
| 464 | /** |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 465 | * 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] | 466 | * growing {@link #dataCapacity} if needed. |
| 467 | * @param b Bytes to place into the parcel. |
| 468 | * @param offset Index of first byte to be written. |
| 469 | * @param len Number of bytes to write. |
| 470 | */ |
| 471 | public final void writeByteArray(byte[] b, int offset, int len) { |
| 472 | if (b == null) { |
| 473 | writeInt(-1); |
| 474 | return; |
| 475 | } |
Elliott Hughes | a28b83e | 2011-02-28 14:26:13 -0800 | [diff] [blame] | 476 | Arrays.checkOffsetAndCount(b.length, offset, len); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 477 | nativeWriteByteArray(mNativePtr, b, offset, len); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | } |
| 479 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | /** |
| 481 | * Write an integer value into the parcel at the current dataPosition(), |
| 482 | * growing dataCapacity() if needed. |
| 483 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 484 | public final void writeInt(int val) { |
| 485 | nativeWriteInt(mNativePtr, val); |
| 486 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | |
| 488 | /** |
| 489 | * Write a long integer value into the parcel at the current dataPosition(), |
| 490 | * growing dataCapacity() if needed. |
| 491 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 492 | public final void writeLong(long val) { |
| 493 | nativeWriteLong(mNativePtr, val); |
| 494 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | |
| 496 | /** |
| 497 | * Write a floating point value into the parcel at the current |
| 498 | * dataPosition(), growing dataCapacity() if needed. |
| 499 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 500 | public final void writeFloat(float val) { |
| 501 | nativeWriteFloat(mNativePtr, val); |
| 502 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 503 | |
| 504 | /** |
| 505 | * Write a double precision floating point value into the parcel at the |
| 506 | * current dataPosition(), growing dataCapacity() if needed. |
| 507 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 508 | public final void writeDouble(double val) { |
| 509 | nativeWriteDouble(mNativePtr, val); |
| 510 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | |
| 512 | /** |
| 513 | * Write a string value into the parcel at the current dataPosition(), |
| 514 | * growing dataCapacity() if needed. |
| 515 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 516 | public final void writeString(String val) { |
| 517 | nativeWriteString(mNativePtr, val); |
| 518 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | |
| 520 | /** |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 521 | * Write a CharSequence value into the parcel at the current dataPosition(), |
| 522 | * growing dataCapacity() if needed. |
| 523 | * @hide |
| 524 | */ |
| 525 | public final void writeCharSequence(CharSequence val) { |
| 526 | TextUtils.writeToParcel(val, this, 0); |
| 527 | } |
| 528 | |
| 529 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 530 | * Write an object into the parcel at the current dataPosition(), |
| 531 | * growing dataCapacity() if needed. |
| 532 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 533 | public final void writeStrongBinder(IBinder val) { |
| 534 | nativeWriteStrongBinder(mNativePtr, val); |
| 535 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | |
| 537 | /** |
| 538 | * Write an object into the parcel at the current dataPosition(), |
| 539 | * growing dataCapacity() if needed. |
| 540 | */ |
| 541 | public final void writeStrongInterface(IInterface val) { |
| 542 | writeStrongBinder(val == null ? null : val.asBinder()); |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Write a FileDescriptor into the parcel at the current dataPosition(), |
| 547 | * growing dataCapacity() if needed. |
Dan Egnor | b3e4ef3 | 2010-07-20 09:03:35 -0700 | [diff] [blame] | 548 | * |
| 549 | * <p class="caution">The file descriptor will not be closed, which may |
| 550 | * result in file descriptor leaks when objects are returned from Binder |
| 551 | * calls. Use {@link ParcelFileDescriptor#writeToParcel} instead, which |
| 552 | * accepts contextual flags and will close the original file descriptor |
| 553 | * 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] | 554 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 555 | public final void writeFileDescriptor(FileDescriptor val) { |
| 556 | nativeWriteFileDescriptor(mNativePtr, val); |
| 557 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | |
| 559 | /** |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 560 | * 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] | 561 | * growing dataCapacity() if needed. |
| 562 | */ |
| 563 | public final void writeByte(byte val) { |
| 564 | writeInt(val); |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * Please use {@link #writeBundle} instead. Flattens a Map into the parcel |
| 569 | * at the current dataPosition(), |
| 570 | * growing dataCapacity() if needed. The Map keys must be String objects. |
| 571 | * The Map values are written using {@link #writeValue} and must follow |
| 572 | * the specification there. |
| 573 | * |
| 574 | * <p>It is strongly recommended to use {@link #writeBundle} instead of |
| 575 | * this method, since the Bundle class provides a type-safe API that |
| 576 | * allows you to avoid mysterious type errors at the point of marshalling. |
| 577 | */ |
| 578 | public final void writeMap(Map val) { |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 579 | writeMapInternal((Map<String, Object>) val); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /** |
| 583 | * Flatten a Map into the parcel at the current dataPosition(), |
| 584 | * growing dataCapacity() if needed. The Map keys must be String objects. |
| 585 | */ |
Dianne Hackborn | 6aff905 | 2009-05-22 13:20:23 -0700 | [diff] [blame] | 586 | /* package */ void writeMapInternal(Map<String,Object> val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 | if (val == null) { |
| 588 | writeInt(-1); |
| 589 | return; |
| 590 | } |
| 591 | Set<Map.Entry<String,Object>> entries = val.entrySet(); |
| 592 | writeInt(entries.size()); |
| 593 | for (Map.Entry<String,Object> e : entries) { |
| 594 | writeValue(e.getKey()); |
| 595 | writeValue(e.getValue()); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | /** |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 600 | * Flatten an ArrayMap into the parcel at the current dataPosition(), |
| 601 | * growing dataCapacity() if needed. The Map keys must be String objects. |
| 602 | */ |
| 603 | /* package */ void writeArrayMapInternal(ArrayMap<String,Object> val) { |
| 604 | if (val == null) { |
| 605 | writeInt(-1); |
| 606 | return; |
| 607 | } |
| 608 | final int N = val.size(); |
| 609 | writeInt(N); |
Dianne Hackborn | e784d1e | 2013-09-20 18:13:52 -0700 | [diff] [blame] | 610 | if (DEBUG_ARRAY_MAP) { |
| 611 | RuntimeException here = new RuntimeException("here"); |
| 612 | here.fillInStackTrace(); |
| 613 | Log.d(TAG, "Writing " + N + " ArrayMap entries", here); |
| 614 | } |
Dianne Hackborn | 8aee64d | 2013-10-25 10:41:50 -0700 | [diff] [blame] | 615 | int startPos; |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 616 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 8aee64d | 2013-10-25 10:41:50 -0700 | [diff] [blame] | 617 | if (DEBUG_ARRAY_MAP) startPos = dataPosition(); |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 618 | writeValue(val.keyAt(i)); |
| 619 | writeValue(val.valueAt(i)); |
Dianne Hackborn | 8aee64d | 2013-10-25 10:41:50 -0700 | [diff] [blame] | 620 | if (DEBUG_ARRAY_MAP) Log.d(TAG, " Write #" + i + " " |
| 621 | + (dataPosition()-startPos) + " bytes: key=0x" |
| 622 | + Integer.toHexString(val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0) |
| 623 | + " " + val.keyAt(i)); |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
| 627 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | * Flatten a Bundle into the parcel at the current dataPosition(), |
| 629 | * growing dataCapacity() if needed. |
| 630 | */ |
| 631 | public final void writeBundle(Bundle val) { |
| 632 | if (val == null) { |
| 633 | writeInt(-1); |
| 634 | return; |
| 635 | } |
| 636 | |
Dianne Hackborn | 6aff905 | 2009-05-22 13:20:23 -0700 | [diff] [blame] | 637 | val.writeToParcel(this, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Flatten a List into the parcel at the current dataPosition(), growing |
| 642 | * dataCapacity() if needed. The List values are written using |
| 643 | * {@link #writeValue} and must follow the specification there. |
| 644 | */ |
| 645 | public final void writeList(List val) { |
| 646 | if (val == null) { |
| 647 | writeInt(-1); |
| 648 | return; |
| 649 | } |
| 650 | int N = val.size(); |
| 651 | int i=0; |
| 652 | writeInt(N); |
| 653 | while (i < N) { |
| 654 | writeValue(val.get(i)); |
| 655 | i++; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * Flatten an Object array into the parcel at the current dataPosition(), |
| 661 | * growing dataCapacity() if needed. The array values are written using |
| 662 | * {@link #writeValue} and must follow the specification there. |
| 663 | */ |
| 664 | public final void writeArray(Object[] val) { |
| 665 | if (val == null) { |
| 666 | writeInt(-1); |
| 667 | return; |
| 668 | } |
| 669 | int N = val.length; |
| 670 | int i=0; |
| 671 | writeInt(N); |
| 672 | while (i < N) { |
| 673 | writeValue(val[i]); |
| 674 | i++; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * Flatten a generic SparseArray into the parcel at the current |
| 680 | * dataPosition(), growing dataCapacity() if needed. The SparseArray |
| 681 | * values are written using {@link #writeValue} and must follow the |
| 682 | * specification there. |
| 683 | */ |
| 684 | public final void writeSparseArray(SparseArray<Object> val) { |
| 685 | if (val == null) { |
| 686 | writeInt(-1); |
| 687 | return; |
| 688 | } |
| 689 | int N = val.size(); |
| 690 | writeInt(N); |
| 691 | int i=0; |
| 692 | while (i < N) { |
| 693 | writeInt(val.keyAt(i)); |
| 694 | writeValue(val.valueAt(i)); |
| 695 | i++; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | public final void writeSparseBooleanArray(SparseBooleanArray val) { |
| 700 | if (val == null) { |
| 701 | writeInt(-1); |
| 702 | return; |
| 703 | } |
| 704 | int N = val.size(); |
| 705 | writeInt(N); |
| 706 | int i=0; |
| 707 | while (i < N) { |
| 708 | writeInt(val.keyAt(i)); |
| 709 | writeByte((byte)(val.valueAt(i) ? 1 : 0)); |
| 710 | i++; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | public final void writeBooleanArray(boolean[] val) { |
| 715 | if (val != null) { |
| 716 | int N = val.length; |
| 717 | writeInt(N); |
| 718 | for (int i=0; i<N; i++) { |
| 719 | writeInt(val[i] ? 1 : 0); |
| 720 | } |
| 721 | } else { |
| 722 | writeInt(-1); |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | public final boolean[] createBooleanArray() { |
| 727 | int N = readInt(); |
| 728 | // >>2 as a fast divide-by-4 works in the create*Array() functions |
| 729 | // because dataAvail() will never return a negative number. 4 is |
| 730 | // the size of a stored boolean in the stream. |
| 731 | if (N >= 0 && N <= (dataAvail() >> 2)) { |
| 732 | boolean[] val = new boolean[N]; |
| 733 | for (int i=0; i<N; i++) { |
| 734 | val[i] = readInt() != 0; |
| 735 | } |
| 736 | return val; |
| 737 | } else { |
| 738 | return null; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | public final void readBooleanArray(boolean[] val) { |
| 743 | int N = readInt(); |
| 744 | if (N == val.length) { |
| 745 | for (int i=0; i<N; i++) { |
| 746 | val[i] = readInt() != 0; |
| 747 | } |
| 748 | } else { |
| 749 | throw new RuntimeException("bad array lengths"); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | public final void writeCharArray(char[] val) { |
| 754 | if (val != null) { |
| 755 | int N = val.length; |
| 756 | writeInt(N); |
| 757 | for (int i=0; i<N; i++) { |
| 758 | writeInt((int)val[i]); |
| 759 | } |
| 760 | } else { |
| 761 | writeInt(-1); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | public final char[] createCharArray() { |
| 766 | int N = readInt(); |
| 767 | if (N >= 0 && N <= (dataAvail() >> 2)) { |
| 768 | char[] val = new char[N]; |
| 769 | for (int i=0; i<N; i++) { |
| 770 | val[i] = (char)readInt(); |
| 771 | } |
| 772 | return val; |
| 773 | } else { |
| 774 | return null; |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | public final void readCharArray(char[] val) { |
| 779 | int N = readInt(); |
| 780 | if (N == val.length) { |
| 781 | for (int i=0; i<N; i++) { |
| 782 | val[i] = (char)readInt(); |
| 783 | } |
| 784 | } else { |
| 785 | throw new RuntimeException("bad array lengths"); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | public final void writeIntArray(int[] val) { |
| 790 | if (val != null) { |
| 791 | int N = val.length; |
| 792 | writeInt(N); |
| 793 | for (int i=0; i<N; i++) { |
| 794 | writeInt(val[i]); |
| 795 | } |
| 796 | } else { |
| 797 | writeInt(-1); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | public final int[] createIntArray() { |
| 802 | int N = readInt(); |
| 803 | if (N >= 0 && N <= (dataAvail() >> 2)) { |
| 804 | int[] val = new int[N]; |
| 805 | for (int i=0; i<N; i++) { |
| 806 | val[i] = readInt(); |
| 807 | } |
| 808 | return val; |
| 809 | } else { |
| 810 | return null; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | public final void readIntArray(int[] val) { |
| 815 | int N = readInt(); |
| 816 | if (N == val.length) { |
| 817 | for (int i=0; i<N; i++) { |
| 818 | val[i] = readInt(); |
| 819 | } |
| 820 | } else { |
| 821 | throw new RuntimeException("bad array lengths"); |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | public final void writeLongArray(long[] val) { |
| 826 | if (val != null) { |
| 827 | int N = val.length; |
| 828 | writeInt(N); |
| 829 | for (int i=0; i<N; i++) { |
| 830 | writeLong(val[i]); |
| 831 | } |
| 832 | } else { |
| 833 | writeInt(-1); |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | public final long[] createLongArray() { |
| 838 | int N = readInt(); |
| 839 | // >>3 because stored longs are 64 bits |
| 840 | if (N >= 0 && N <= (dataAvail() >> 3)) { |
| 841 | long[] val = new long[N]; |
| 842 | for (int i=0; i<N; i++) { |
| 843 | val[i] = readLong(); |
| 844 | } |
| 845 | return val; |
| 846 | } else { |
| 847 | return null; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | public final void readLongArray(long[] val) { |
| 852 | int N = readInt(); |
| 853 | if (N == val.length) { |
| 854 | for (int i=0; i<N; i++) { |
| 855 | val[i] = readLong(); |
| 856 | } |
| 857 | } else { |
| 858 | throw new RuntimeException("bad array lengths"); |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | public final void writeFloatArray(float[] val) { |
| 863 | if (val != null) { |
| 864 | int N = val.length; |
| 865 | writeInt(N); |
| 866 | for (int i=0; i<N; i++) { |
| 867 | writeFloat(val[i]); |
| 868 | } |
| 869 | } else { |
| 870 | writeInt(-1); |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | public final float[] createFloatArray() { |
| 875 | int N = readInt(); |
| 876 | // >>2 because stored floats are 4 bytes |
| 877 | if (N >= 0 && N <= (dataAvail() >> 2)) { |
| 878 | float[] val = new float[N]; |
| 879 | for (int i=0; i<N; i++) { |
| 880 | val[i] = readFloat(); |
| 881 | } |
| 882 | return val; |
| 883 | } else { |
| 884 | return null; |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | public final void readFloatArray(float[] val) { |
| 889 | int N = readInt(); |
| 890 | if (N == val.length) { |
| 891 | for (int i=0; i<N; i++) { |
| 892 | val[i] = readFloat(); |
| 893 | } |
| 894 | } else { |
| 895 | throw new RuntimeException("bad array lengths"); |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | public final void writeDoubleArray(double[] val) { |
| 900 | if (val != null) { |
| 901 | int N = val.length; |
| 902 | writeInt(N); |
| 903 | for (int i=0; i<N; i++) { |
| 904 | writeDouble(val[i]); |
| 905 | } |
| 906 | } else { |
| 907 | writeInt(-1); |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | public final double[] createDoubleArray() { |
| 912 | int N = readInt(); |
| 913 | // >>3 because stored doubles are 8 bytes |
| 914 | if (N >= 0 && N <= (dataAvail() >> 3)) { |
| 915 | double[] val = new double[N]; |
| 916 | for (int i=0; i<N; i++) { |
| 917 | val[i] = readDouble(); |
| 918 | } |
| 919 | return val; |
| 920 | } else { |
| 921 | return null; |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | public final void readDoubleArray(double[] val) { |
| 926 | int N = readInt(); |
| 927 | if (N == val.length) { |
| 928 | for (int i=0; i<N; i++) { |
| 929 | val[i] = readDouble(); |
| 930 | } |
| 931 | } else { |
| 932 | throw new RuntimeException("bad array lengths"); |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | public final void writeStringArray(String[] val) { |
| 937 | if (val != null) { |
| 938 | int N = val.length; |
| 939 | writeInt(N); |
| 940 | for (int i=0; i<N; i++) { |
| 941 | writeString(val[i]); |
| 942 | } |
| 943 | } else { |
| 944 | writeInt(-1); |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | public final String[] createStringArray() { |
| 949 | int N = readInt(); |
| 950 | if (N >= 0) { |
| 951 | String[] val = new String[N]; |
| 952 | for (int i=0; i<N; i++) { |
| 953 | val[i] = readString(); |
| 954 | } |
| 955 | return val; |
| 956 | } else { |
| 957 | return null; |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | public final void readStringArray(String[] val) { |
| 962 | int N = readInt(); |
| 963 | if (N == val.length) { |
| 964 | for (int i=0; i<N; i++) { |
| 965 | val[i] = readString(); |
| 966 | } |
| 967 | } else { |
| 968 | throw new RuntimeException("bad array lengths"); |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | public final void writeBinderArray(IBinder[] val) { |
| 973 | if (val != null) { |
| 974 | int N = val.length; |
| 975 | writeInt(N); |
| 976 | for (int i=0; i<N; i++) { |
| 977 | writeStrongBinder(val[i]); |
| 978 | } |
| 979 | } else { |
| 980 | writeInt(-1); |
| 981 | } |
| 982 | } |
| 983 | |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 984 | /** |
| 985 | * @hide |
| 986 | */ |
| 987 | public final void writeCharSequenceArray(CharSequence[] val) { |
| 988 | if (val != null) { |
| 989 | int N = val.length; |
| 990 | writeInt(N); |
| 991 | for (int i=0; i<N; i++) { |
| 992 | writeCharSequence(val[i]); |
| 993 | } |
| 994 | } else { |
| 995 | writeInt(-1); |
| 996 | } |
| 997 | } |
| 998 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 999 | public final IBinder[] createBinderArray() { |
| 1000 | int N = readInt(); |
| 1001 | if (N >= 0) { |
| 1002 | IBinder[] val = new IBinder[N]; |
| 1003 | for (int i=0; i<N; i++) { |
| 1004 | val[i] = readStrongBinder(); |
| 1005 | } |
| 1006 | return val; |
| 1007 | } else { |
| 1008 | return null; |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | public final void readBinderArray(IBinder[] val) { |
| 1013 | int N = readInt(); |
| 1014 | if (N == val.length) { |
| 1015 | for (int i=0; i<N; i++) { |
| 1016 | val[i] = readStrongBinder(); |
| 1017 | } |
| 1018 | } else { |
| 1019 | throw new RuntimeException("bad array lengths"); |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | /** |
| 1024 | * Flatten a List containing a particular object type into the parcel, at |
| 1025 | * the current dataPosition() and growing dataCapacity() if needed. The |
| 1026 | * type of the objects in the list must be one that implements Parcelable. |
| 1027 | * Unlike the generic writeList() method, however, only the raw data of the |
| 1028 | * objects is written and not their type, so you must use the corresponding |
| 1029 | * readTypedList() to unmarshall them. |
| 1030 | * |
| 1031 | * @param val The list of objects to be written. |
| 1032 | * |
| 1033 | * @see #createTypedArrayList |
| 1034 | * @see #readTypedList |
| 1035 | * @see Parcelable |
| 1036 | */ |
| 1037 | public final <T extends Parcelable> void writeTypedList(List<T> val) { |
| 1038 | if (val == null) { |
| 1039 | writeInt(-1); |
| 1040 | return; |
| 1041 | } |
| 1042 | int N = val.size(); |
| 1043 | int i=0; |
| 1044 | writeInt(N); |
| 1045 | while (i < N) { |
| 1046 | T item = val.get(i); |
| 1047 | if (item != null) { |
| 1048 | writeInt(1); |
| 1049 | item.writeToParcel(this, 0); |
| 1050 | } else { |
| 1051 | writeInt(0); |
| 1052 | } |
| 1053 | i++; |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | /** |
| 1058 | * Flatten a List containing String objects into the parcel, at |
| 1059 | * the current dataPosition() and growing dataCapacity() if needed. They |
| 1060 | * can later be retrieved with {@link #createStringArrayList} or |
| 1061 | * {@link #readStringList}. |
| 1062 | * |
| 1063 | * @param val The list of strings to be written. |
| 1064 | * |
| 1065 | * @see #createStringArrayList |
| 1066 | * @see #readStringList |
| 1067 | */ |
| 1068 | public final void writeStringList(List<String> val) { |
| 1069 | if (val == null) { |
| 1070 | writeInt(-1); |
| 1071 | return; |
| 1072 | } |
| 1073 | int N = val.size(); |
| 1074 | int i=0; |
| 1075 | writeInt(N); |
| 1076 | while (i < N) { |
| 1077 | writeString(val.get(i)); |
| 1078 | i++; |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | /** |
| 1083 | * Flatten a List containing IBinder objects into the parcel, at |
| 1084 | * the current dataPosition() and growing dataCapacity() if needed. They |
| 1085 | * can later be retrieved with {@link #createBinderArrayList} or |
| 1086 | * {@link #readBinderList}. |
| 1087 | * |
| 1088 | * @param val The list of strings to be written. |
| 1089 | * |
| 1090 | * @see #createBinderArrayList |
| 1091 | * @see #readBinderList |
| 1092 | */ |
| 1093 | public final void writeBinderList(List<IBinder> val) { |
| 1094 | if (val == null) { |
| 1095 | writeInt(-1); |
| 1096 | return; |
| 1097 | } |
| 1098 | int N = val.size(); |
| 1099 | int i=0; |
| 1100 | writeInt(N); |
| 1101 | while (i < N) { |
| 1102 | writeStrongBinder(val.get(i)); |
| 1103 | i++; |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | /** |
| 1108 | * Flatten a heterogeneous array containing a particular object type into |
| 1109 | * the parcel, at |
| 1110 | * the current dataPosition() and growing dataCapacity() if needed. The |
| 1111 | * type of the objects in the array must be one that implements Parcelable. |
| 1112 | * Unlike the {@link #writeParcelableArray} method, however, only the |
| 1113 | * raw data of the objects is written and not their type, so you must use |
| 1114 | * {@link #readTypedArray} with the correct corresponding |
| 1115 | * {@link Parcelable.Creator} implementation to unmarshall them. |
| 1116 | * |
| 1117 | * @param val The array of objects to be written. |
| 1118 | * @param parcelableFlags Contextual flags as per |
| 1119 | * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}. |
| 1120 | * |
| 1121 | * @see #readTypedArray |
| 1122 | * @see #writeParcelableArray |
| 1123 | * @see Parcelable.Creator |
| 1124 | */ |
| 1125 | public final <T extends Parcelable> void writeTypedArray(T[] val, |
| 1126 | int parcelableFlags) { |
| 1127 | if (val != null) { |
| 1128 | int N = val.length; |
| 1129 | writeInt(N); |
| 1130 | for (int i=0; i<N; i++) { |
| 1131 | T item = val[i]; |
| 1132 | if (item != null) { |
| 1133 | writeInt(1); |
| 1134 | item.writeToParcel(this, parcelableFlags); |
| 1135 | } else { |
| 1136 | writeInt(0); |
| 1137 | } |
| 1138 | } |
| 1139 | } else { |
| 1140 | writeInt(-1); |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | /** |
| 1145 | * Flatten a generic object in to a parcel. The given Object value may |
| 1146 | * currently be one of the following types: |
Dan Egnor | b3e4ef3 | 2010-07-20 09:03:35 -0700 | [diff] [blame] | 1147 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1148 | * <ul> |
| 1149 | * <li> null |
| 1150 | * <li> String |
| 1151 | * <li> Byte |
| 1152 | * <li> Short |
| 1153 | * <li> Integer |
| 1154 | * <li> Long |
| 1155 | * <li> Float |
| 1156 | * <li> Double |
| 1157 | * <li> Boolean |
| 1158 | * <li> String[] |
| 1159 | * <li> boolean[] |
| 1160 | * <li> byte[] |
| 1161 | * <li> int[] |
| 1162 | * <li> long[] |
| 1163 | * <li> Object[] (supporting objects of the same type defined here). |
| 1164 | * <li> {@link Bundle} |
| 1165 | * <li> Map (as supported by {@link #writeMap}). |
| 1166 | * <li> Any object that implements the {@link Parcelable} protocol. |
| 1167 | * <li> Parcelable[] |
| 1168 | * <li> CharSequence (as supported by {@link TextUtils#writeToParcel}). |
| 1169 | * <li> List (as supported by {@link #writeList}). |
Dan Egnor | b3e4ef3 | 2010-07-20 09:03:35 -0700 | [diff] [blame] | 1170 | * <li> {@link SparseArray} (as supported by {@link #writeSparseArray(SparseArray)}). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1171 | * <li> {@link IBinder} |
| 1172 | * <li> Any object that implements Serializable (but see |
| 1173 | * {@link #writeSerializable} for caveats). Note that all of the |
| 1174 | * previous types have relatively efficient implementations for |
| 1175 | * writing to a Parcel; having to rely on the generic serialization |
| 1176 | * approach is much less efficient and should be avoided whenever |
| 1177 | * possible. |
| 1178 | * </ul> |
Dan Egnor | b3e4ef3 | 2010-07-20 09:03:35 -0700 | [diff] [blame] | 1179 | * |
| 1180 | * <p class="caution">{@link Parcelable} objects are written with |
| 1181 | * {@link Parcelable#writeToParcel} using contextual flags of 0. When |
| 1182 | * serializing objects containing {@link ParcelFileDescriptor}s, |
| 1183 | * this may result in file descriptor leaks when they are returned from |
| 1184 | * Binder calls (where {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} |
| 1185 | * should be used).</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1186 | */ |
| 1187 | public final void writeValue(Object v) { |
| 1188 | if (v == null) { |
| 1189 | writeInt(VAL_NULL); |
| 1190 | } else if (v instanceof String) { |
| 1191 | writeInt(VAL_STRING); |
| 1192 | writeString((String) v); |
| 1193 | } else if (v instanceof Integer) { |
| 1194 | writeInt(VAL_INTEGER); |
| 1195 | writeInt((Integer) v); |
| 1196 | } else if (v instanceof Map) { |
| 1197 | writeInt(VAL_MAP); |
| 1198 | writeMap((Map) v); |
| 1199 | } else if (v instanceof Bundle) { |
| 1200 | // Must be before Parcelable |
| 1201 | writeInt(VAL_BUNDLE); |
| 1202 | writeBundle((Bundle) v); |
| 1203 | } else if (v instanceof Parcelable) { |
| 1204 | writeInt(VAL_PARCELABLE); |
| 1205 | writeParcelable((Parcelable) v, 0); |
| 1206 | } else if (v instanceof Short) { |
| 1207 | writeInt(VAL_SHORT); |
| 1208 | writeInt(((Short) v).intValue()); |
| 1209 | } else if (v instanceof Long) { |
| 1210 | writeInt(VAL_LONG); |
| 1211 | writeLong((Long) v); |
| 1212 | } else if (v instanceof Float) { |
| 1213 | writeInt(VAL_FLOAT); |
| 1214 | writeFloat((Float) v); |
| 1215 | } else if (v instanceof Double) { |
| 1216 | writeInt(VAL_DOUBLE); |
| 1217 | writeDouble((Double) v); |
| 1218 | } else if (v instanceof Boolean) { |
| 1219 | writeInt(VAL_BOOLEAN); |
| 1220 | writeInt((Boolean) v ? 1 : 0); |
| 1221 | } else if (v instanceof CharSequence) { |
| 1222 | // Must be after String |
| 1223 | writeInt(VAL_CHARSEQUENCE); |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 1224 | writeCharSequence((CharSequence) v); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1225 | } else if (v instanceof List) { |
| 1226 | writeInt(VAL_LIST); |
| 1227 | writeList((List) v); |
| 1228 | } else if (v instanceof SparseArray) { |
| 1229 | writeInt(VAL_SPARSEARRAY); |
| 1230 | writeSparseArray((SparseArray) v); |
| 1231 | } else if (v instanceof boolean[]) { |
| 1232 | writeInt(VAL_BOOLEANARRAY); |
| 1233 | writeBooleanArray((boolean[]) v); |
| 1234 | } else if (v instanceof byte[]) { |
| 1235 | writeInt(VAL_BYTEARRAY); |
| 1236 | writeByteArray((byte[]) v); |
| 1237 | } else if (v instanceof String[]) { |
| 1238 | writeInt(VAL_STRINGARRAY); |
| 1239 | writeStringArray((String[]) v); |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 1240 | } else if (v instanceof CharSequence[]) { |
| 1241 | // Must be after String[] and before Object[] |
| 1242 | writeInt(VAL_CHARSEQUENCEARRAY); |
| 1243 | writeCharSequenceArray((CharSequence[]) v); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1244 | } else if (v instanceof IBinder) { |
| 1245 | writeInt(VAL_IBINDER); |
| 1246 | writeStrongBinder((IBinder) v); |
| 1247 | } else if (v instanceof Parcelable[]) { |
| 1248 | writeInt(VAL_PARCELABLEARRAY); |
| 1249 | writeParcelableArray((Parcelable[]) v, 0); |
| 1250 | } else if (v instanceof Object[]) { |
| 1251 | writeInt(VAL_OBJECTARRAY); |
| 1252 | writeArray((Object[]) v); |
| 1253 | } else if (v instanceof int[]) { |
| 1254 | writeInt(VAL_INTARRAY); |
| 1255 | writeIntArray((int[]) v); |
| 1256 | } else if (v instanceof long[]) { |
| 1257 | writeInt(VAL_LONGARRAY); |
| 1258 | writeLongArray((long[]) v); |
| 1259 | } else if (v instanceof Byte) { |
| 1260 | writeInt(VAL_BYTE); |
| 1261 | writeInt((Byte) v); |
| 1262 | } else if (v instanceof Serializable) { |
| 1263 | // Must be last |
| 1264 | writeInt(VAL_SERIALIZABLE); |
| 1265 | writeSerializable((Serializable) v); |
| 1266 | } else { |
| 1267 | throw new RuntimeException("Parcel: unable to marshal value " + v); |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | /** |
| 1272 | * Flatten the name of the class of the Parcelable and its contents |
| 1273 | * into the parcel. |
Dan Egnor | b3e4ef3 | 2010-07-20 09:03:35 -0700 | [diff] [blame] | 1274 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1275 | * @param p The Parcelable object to be written. |
| 1276 | * @param parcelableFlags Contextual flags as per |
| 1277 | * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}. |
| 1278 | */ |
| 1279 | public final void writeParcelable(Parcelable p, int parcelableFlags) { |
| 1280 | if (p == null) { |
| 1281 | writeString(null); |
| 1282 | return; |
| 1283 | } |
| 1284 | String name = p.getClass().getName(); |
| 1285 | writeString(name); |
| 1286 | p.writeToParcel(this, parcelableFlags); |
| 1287 | } |
| 1288 | |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 1289 | /** @hide */ |
| 1290 | public final void writeParcelableCreator(Parcelable p) { |
| 1291 | String name = p.getClass().getName(); |
| 1292 | writeString(name); |
| 1293 | } |
| 1294 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1295 | /** |
| 1296 | * Write a generic serializable object in to a Parcel. It is strongly |
| 1297 | * recommended that this method be avoided, since the serialization |
| 1298 | * overhead is extremely large, and this approach will be much slower than |
| 1299 | * using the other approaches to writing data in to a Parcel. |
| 1300 | */ |
| 1301 | public final void writeSerializable(Serializable s) { |
| 1302 | if (s == null) { |
| 1303 | writeString(null); |
| 1304 | return; |
| 1305 | } |
| 1306 | String name = s.getClass().getName(); |
| 1307 | writeString(name); |
| 1308 | |
| 1309 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 1310 | try { |
| 1311 | ObjectOutputStream oos = new ObjectOutputStream(baos); |
| 1312 | oos.writeObject(s); |
| 1313 | oos.close(); |
| 1314 | |
| 1315 | writeByteArray(baos.toByteArray()); |
| 1316 | } catch (IOException ioe) { |
| 1317 | throw new RuntimeException("Parcelable encountered " + |
| 1318 | "IOException writing serializable object (name = " + name + |
| 1319 | ")", ioe); |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | /** |
| 1324 | * Special function for writing an exception result at the header of |
| 1325 | * a parcel, to be used when returning an exception from a transaction. |
| 1326 | * Note that this currently only supports a few exception types; any other |
| 1327 | * exception will be re-thrown by this function as a RuntimeException |
| 1328 | * (to be caught by the system's last-resort exception handling when |
| 1329 | * dispatching a transaction). |
| 1330 | * |
| 1331 | * <p>The supported exception types are: |
| 1332 | * <ul> |
| 1333 | * <li>{@link BadParcelableException} |
| 1334 | * <li>{@link IllegalArgumentException} |
| 1335 | * <li>{@link IllegalStateException} |
| 1336 | * <li>{@link NullPointerException} |
| 1337 | * <li>{@link SecurityException} |
Dianne Hackborn | 7e71442 | 2013-09-13 17:32:57 -0700 | [diff] [blame] | 1338 | * <li>{@link NetworkOnMainThreadException} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1339 | * </ul> |
| 1340 | * |
| 1341 | * @param e The Exception to be written. |
| 1342 | * |
| 1343 | * @see #writeNoException |
| 1344 | * @see #readException |
| 1345 | */ |
| 1346 | public final void writeException(Exception e) { |
| 1347 | int code = 0; |
| 1348 | if (e instanceof SecurityException) { |
| 1349 | code = EX_SECURITY; |
| 1350 | } else if (e instanceof BadParcelableException) { |
| 1351 | code = EX_BAD_PARCELABLE; |
| 1352 | } else if (e instanceof IllegalArgumentException) { |
| 1353 | code = EX_ILLEGAL_ARGUMENT; |
| 1354 | } else if (e instanceof NullPointerException) { |
| 1355 | code = EX_NULL_POINTER; |
| 1356 | } else if (e instanceof IllegalStateException) { |
| 1357 | code = EX_ILLEGAL_STATE; |
Dianne Hackborn | 7e71442 | 2013-09-13 17:32:57 -0700 | [diff] [blame] | 1358 | } else if (e instanceof NetworkOnMainThreadException) { |
| 1359 | code = EX_NETWORK_MAIN_THREAD; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1360 | } |
| 1361 | writeInt(code); |
Brad Fitzpatrick | 703e5d3 | 2010-07-15 13:16:41 -0700 | [diff] [blame] | 1362 | StrictMode.clearGatheredViolations(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1363 | if (code == 0) { |
| 1364 | if (e instanceof RuntimeException) { |
| 1365 | throw (RuntimeException) e; |
| 1366 | } |
| 1367 | throw new RuntimeException(e); |
| 1368 | } |
| 1369 | writeString(e.getMessage()); |
| 1370 | } |
| 1371 | |
| 1372 | /** |
| 1373 | * Special function for writing information at the front of the Parcel |
| 1374 | * indicating that no exception occurred. |
| 1375 | * |
| 1376 | * @see #writeException |
| 1377 | * @see #readException |
| 1378 | */ |
| 1379 | public final void writeNoException() { |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1380 | // Despite the name of this function ("write no exception"), |
| 1381 | // it should instead be thought of as "write the RPC response |
| 1382 | // header", but because this function name is written out by |
| 1383 | // the AIDL compiler, we're not going to rename it. |
| 1384 | // |
| 1385 | // The response header, in the non-exception case (see also |
| 1386 | // writeException above, also called by the AIDL compiler), is |
| 1387 | // either a 0 (the default case), or EX_HAS_REPLY_HEADER if |
| 1388 | // StrictMode has gathered up violations that have occurred |
| 1389 | // during a Binder call, in which case we write out the number |
| 1390 | // of violations and their details, serialized, before the |
| 1391 | // actual RPC respons data. The receiving end of this is |
| 1392 | // readException(), below. |
| 1393 | if (StrictMode.hasGatheredViolations()) { |
| 1394 | writeInt(EX_HAS_REPLY_HEADER); |
| 1395 | final int sizePosition = dataPosition(); |
| 1396 | writeInt(0); // total size of fat header, to be filled in later |
| 1397 | StrictMode.writeGatheredViolationsToParcel(this); |
| 1398 | final int payloadPosition = dataPosition(); |
| 1399 | setDataPosition(sizePosition); |
| 1400 | writeInt(payloadPosition - sizePosition); // header size |
| 1401 | setDataPosition(payloadPosition); |
| 1402 | } else { |
| 1403 | writeInt(0); |
| 1404 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | /** |
| 1408 | * Special function for reading an exception result from the header of |
| 1409 | * a parcel, to be used after receiving the result of a transaction. This |
| 1410 | * will throw the exception for you if it had been written to the Parcel, |
| 1411 | * otherwise return and let you read the normal result data from the Parcel. |
| 1412 | * |
| 1413 | * @see #writeException |
| 1414 | * @see #writeNoException |
| 1415 | */ |
| 1416 | public final void readException() { |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1417 | int code = readExceptionCode(); |
| 1418 | if (code != 0) { |
| 1419 | String msg = readString(); |
| 1420 | readException(code, msg); |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | /** |
| 1425 | * Parses the header of a Binder call's response Parcel and |
| 1426 | * returns the exception code. Deals with lite or fat headers. |
| 1427 | * In the common successful case, this header is generally zero. |
| 1428 | * In less common cases, it's a small negative number and will be |
| 1429 | * followed by an error string. |
| 1430 | * |
| 1431 | * This exists purely for android.database.DatabaseUtils and |
| 1432 | * insulating it from having to handle fat headers as returned by |
| 1433 | * e.g. StrictMode-induced RPC responses. |
| 1434 | * |
| 1435 | * @hide |
| 1436 | */ |
| 1437 | public final int readExceptionCode() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1438 | int code = readInt(); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1439 | if (code == EX_HAS_REPLY_HEADER) { |
| 1440 | int headerSize = readInt(); |
| 1441 | if (headerSize == 0) { |
| 1442 | Log.e(TAG, "Unexpected zero-sized Parcel reply header."); |
| 1443 | } else { |
| 1444 | // Currently the only thing in the header is StrictMode stacks, |
| 1445 | // but discussions around event/RPC tracing suggest we might |
| 1446 | // put that here too. If so, switch on sub-header tags here. |
| 1447 | // But for now, just parse out the StrictMode stuff. |
| 1448 | StrictMode.readAndHandleBinderCallViolations(this); |
| 1449 | } |
| 1450 | // And fat response headers are currently only used when |
| 1451 | // there are no exceptions, so return no error: |
| 1452 | return 0; |
| 1453 | } |
| 1454 | return code; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | /** |
| 1458 | * Use this function for customized exception handling. |
| 1459 | * customized method call this method for all unknown case |
| 1460 | * @param code exception code |
| 1461 | * @param msg exception message |
| 1462 | */ |
| 1463 | public final void readException(int code, String msg) { |
| 1464 | switch (code) { |
| 1465 | case EX_SECURITY: |
| 1466 | throw new SecurityException(msg); |
| 1467 | case EX_BAD_PARCELABLE: |
| 1468 | throw new BadParcelableException(msg); |
| 1469 | case EX_ILLEGAL_ARGUMENT: |
| 1470 | throw new IllegalArgumentException(msg); |
| 1471 | case EX_NULL_POINTER: |
| 1472 | throw new NullPointerException(msg); |
| 1473 | case EX_ILLEGAL_STATE: |
| 1474 | throw new IllegalStateException(msg); |
Dianne Hackborn | 7e71442 | 2013-09-13 17:32:57 -0700 | [diff] [blame] | 1475 | case EX_NETWORK_MAIN_THREAD: |
| 1476 | throw new NetworkOnMainThreadException(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1477 | } |
| 1478 | throw new RuntimeException("Unknown exception code: " + code |
| 1479 | + " msg " + msg); |
| 1480 | } |
| 1481 | |
| 1482 | /** |
| 1483 | * Read an integer value from the parcel at the current dataPosition(). |
| 1484 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 1485 | public final int readInt() { |
| 1486 | return nativeReadInt(mNativePtr); |
| 1487 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1488 | |
| 1489 | /** |
| 1490 | * Read a long integer value from the parcel at the current dataPosition(). |
| 1491 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 1492 | public final long readLong() { |
| 1493 | return nativeReadLong(mNativePtr); |
| 1494 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1495 | |
| 1496 | /** |
| 1497 | * Read a floating point value from the parcel at the current |
| 1498 | * dataPosition(). |
| 1499 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 1500 | public final float readFloat() { |
| 1501 | return nativeReadFloat(mNativePtr); |
| 1502 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1503 | |
| 1504 | /** |
| 1505 | * Read a double precision floating point value from the parcel at the |
| 1506 | * current dataPosition(). |
| 1507 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 1508 | public final double readDouble() { |
| 1509 | return nativeReadDouble(mNativePtr); |
| 1510 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1511 | |
| 1512 | /** |
| 1513 | * Read a string value from the parcel at the current dataPosition(). |
| 1514 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 1515 | public final String readString() { |
| 1516 | return nativeReadString(mNativePtr); |
| 1517 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1518 | |
| 1519 | /** |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 1520 | * Read a CharSequence value from the parcel at the current dataPosition(). |
| 1521 | * @hide |
| 1522 | */ |
| 1523 | public final CharSequence readCharSequence() { |
| 1524 | return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(this); |
| 1525 | } |
| 1526 | |
| 1527 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1528 | * Read an object from the parcel at the current dataPosition(). |
| 1529 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 1530 | public final IBinder readStrongBinder() { |
| 1531 | return nativeReadStrongBinder(mNativePtr); |
| 1532 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1533 | |
| 1534 | /** |
| 1535 | * Read a FileDescriptor from the parcel at the current dataPosition(). |
| 1536 | */ |
| 1537 | public final ParcelFileDescriptor readFileDescriptor() { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 1538 | FileDescriptor fd = nativeReadFileDescriptor(mNativePtr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1539 | return fd != null ? new ParcelFileDescriptor(fd) : null; |
| 1540 | } |
| 1541 | |
Jeff Sharkey | da5a3e1 | 2013-08-11 12:54:42 -0700 | [diff] [blame] | 1542 | /** {@hide} */ |
| 1543 | public final FileDescriptor readRawFileDescriptor() { |
| 1544 | return nativeReadFileDescriptor(mNativePtr); |
| 1545 | } |
| 1546 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1547 | /*package*/ static native FileDescriptor openFileDescriptor(String file, |
| 1548 | int mode) throws FileNotFoundException; |
Dianne Hackborn | 9a84983 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 1549 | /*package*/ static native FileDescriptor dupFileDescriptor(FileDescriptor orig) |
| 1550 | throws IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1551 | /*package*/ static native void closeFileDescriptor(FileDescriptor desc) |
| 1552 | throws IOException; |
Dianne Hackborn | c9119f5 | 2011-02-28 18:03:26 -0800 | [diff] [blame] | 1553 | /*package*/ static native void clearFileDescriptor(FileDescriptor desc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1554 | |
| 1555 | /** |
| 1556 | * Read a byte value from the parcel at the current dataPosition(). |
| 1557 | */ |
| 1558 | public final byte readByte() { |
| 1559 | return (byte)(readInt() & 0xff); |
| 1560 | } |
| 1561 | |
| 1562 | /** |
| 1563 | * Please use {@link #readBundle(ClassLoader)} instead (whose data must have |
| 1564 | * been written with {@link #writeBundle}. Read into an existing Map object |
| 1565 | * from the parcel at the current dataPosition(). |
| 1566 | */ |
| 1567 | public final void readMap(Map outVal, ClassLoader loader) { |
| 1568 | int N = readInt(); |
| 1569 | readMapInternal(outVal, N, loader); |
| 1570 | } |
| 1571 | |
| 1572 | /** |
| 1573 | * Read into an existing List object from the parcel at the current |
| 1574 | * dataPosition(), using the given class loader to load any enclosed |
| 1575 | * Parcelables. If it is null, the default class loader is used. |
| 1576 | */ |
| 1577 | public final void readList(List outVal, ClassLoader loader) { |
| 1578 | int N = readInt(); |
| 1579 | readListInternal(outVal, N, loader); |
| 1580 | } |
| 1581 | |
| 1582 | /** |
| 1583 | * Please use {@link #readBundle(ClassLoader)} instead (whose data must have |
| 1584 | * been written with {@link #writeBundle}. Read and return a new HashMap |
| 1585 | * object from the parcel at the current dataPosition(), using the given |
| 1586 | * class loader to load any enclosed Parcelables. Returns null if |
| 1587 | * the previously written map object was null. |
| 1588 | */ |
| 1589 | public final HashMap readHashMap(ClassLoader loader) |
| 1590 | { |
| 1591 | int N = readInt(); |
| 1592 | if (N < 0) { |
| 1593 | return null; |
| 1594 | } |
| 1595 | HashMap m = new HashMap(N); |
| 1596 | readMapInternal(m, N, loader); |
| 1597 | return m; |
| 1598 | } |
| 1599 | |
| 1600 | /** |
| 1601 | * Read and return a new Bundle object from the parcel at the current |
| 1602 | * dataPosition(). Returns null if the previously written Bundle object was |
| 1603 | * null. |
| 1604 | */ |
| 1605 | public final Bundle readBundle() { |
| 1606 | return readBundle(null); |
| 1607 | } |
| 1608 | |
| 1609 | /** |
| 1610 | * Read and return a new Bundle object from the parcel at the current |
| 1611 | * dataPosition(), using the given class loader to initialize the class |
| 1612 | * loader of the Bundle for later retrieval of Parcelable objects. |
| 1613 | * Returns null if the previously written Bundle object was null. |
| 1614 | */ |
| 1615 | public final Bundle readBundle(ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1616 | int length = readInt(); |
| 1617 | if (length < 0) { |
Dianne Hackborn | 4a7d824 | 2013-10-03 10:19:20 -0700 | [diff] [blame] | 1618 | 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] | 1619 | return null; |
| 1620 | } |
Dianne Hackborn | 6aff905 | 2009-05-22 13:20:23 -0700 | [diff] [blame] | 1621 | |
| 1622 | final Bundle bundle = new Bundle(this, length); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1623 | if (loader != null) { |
| 1624 | bundle.setClassLoader(loader); |
| 1625 | } |
| 1626 | return bundle; |
| 1627 | } |
| 1628 | |
| 1629 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1630 | * Read and return a byte[] object from the parcel. |
| 1631 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 1632 | public final byte[] createByteArray() { |
| 1633 | return nativeCreateByteArray(mNativePtr); |
| 1634 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1635 | |
| 1636 | /** |
| 1637 | * Read a byte[] object from the parcel and copy it into the |
| 1638 | * given byte array. |
| 1639 | */ |
| 1640 | public final void readByteArray(byte[] val) { |
| 1641 | // TODO: make this a native method to avoid the extra copy. |
| 1642 | byte[] ba = createByteArray(); |
| 1643 | if (ba.length == val.length) { |
| 1644 | System.arraycopy(ba, 0, val, 0, ba.length); |
| 1645 | } else { |
| 1646 | throw new RuntimeException("bad array lengths"); |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | /** |
| 1651 | * Read and return a String[] object from the parcel. |
| 1652 | * {@hide} |
| 1653 | */ |
| 1654 | public final String[] readStringArray() { |
| 1655 | String[] array = null; |
| 1656 | |
| 1657 | int length = readInt(); |
| 1658 | if (length >= 0) |
| 1659 | { |
| 1660 | array = new String[length]; |
| 1661 | |
| 1662 | for (int i = 0 ; i < length ; i++) |
| 1663 | { |
| 1664 | array[i] = readString(); |
| 1665 | } |
| 1666 | } |
| 1667 | |
| 1668 | return array; |
| 1669 | } |
| 1670 | |
| 1671 | /** |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 1672 | * Read and return a CharSequence[] object from the parcel. |
| 1673 | * {@hide} |
| 1674 | */ |
| 1675 | public final CharSequence[] readCharSequenceArray() { |
| 1676 | CharSequence[] array = null; |
| 1677 | |
| 1678 | int length = readInt(); |
| 1679 | if (length >= 0) |
| 1680 | { |
| 1681 | array = new CharSequence[length]; |
| 1682 | |
| 1683 | for (int i = 0 ; i < length ; i++) |
| 1684 | { |
| 1685 | array[i] = readCharSequence(); |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | return array; |
| 1690 | } |
| 1691 | |
| 1692 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1693 | * Read and return a new ArrayList object from the parcel at the current |
| 1694 | * dataPosition(). Returns null if the previously written list object was |
| 1695 | * null. The given class loader will be used to load any enclosed |
| 1696 | * Parcelables. |
| 1697 | */ |
| 1698 | public final ArrayList readArrayList(ClassLoader loader) { |
| 1699 | int N = readInt(); |
| 1700 | if (N < 0) { |
| 1701 | return null; |
| 1702 | } |
| 1703 | ArrayList l = new ArrayList(N); |
| 1704 | readListInternal(l, N, loader); |
| 1705 | return l; |
| 1706 | } |
| 1707 | |
| 1708 | /** |
| 1709 | * Read and return a new Object array from the parcel at the current |
| 1710 | * dataPosition(). Returns null if the previously written array was |
| 1711 | * null. The given class loader will be used to load any enclosed |
| 1712 | * Parcelables. |
| 1713 | */ |
| 1714 | public final Object[] readArray(ClassLoader loader) { |
| 1715 | int N = readInt(); |
| 1716 | if (N < 0) { |
| 1717 | return null; |
| 1718 | } |
| 1719 | Object[] l = new Object[N]; |
| 1720 | readArrayInternal(l, N, loader); |
| 1721 | return l; |
| 1722 | } |
| 1723 | |
| 1724 | /** |
| 1725 | * Read and return a new SparseArray object from the parcel at the current |
| 1726 | * dataPosition(). Returns null if the previously written list object was |
| 1727 | * null. The given class loader will be used to load any enclosed |
| 1728 | * Parcelables. |
| 1729 | */ |
| 1730 | public final SparseArray readSparseArray(ClassLoader loader) { |
| 1731 | int N = readInt(); |
| 1732 | if (N < 0) { |
| 1733 | return null; |
| 1734 | } |
| 1735 | SparseArray sa = new SparseArray(N); |
| 1736 | readSparseArrayInternal(sa, N, loader); |
| 1737 | return sa; |
| 1738 | } |
| 1739 | |
| 1740 | /** |
| 1741 | * Read and return a new SparseBooleanArray object from the parcel at the current |
| 1742 | * dataPosition(). Returns null if the previously written list object was |
| 1743 | * null. |
| 1744 | */ |
| 1745 | public final SparseBooleanArray readSparseBooleanArray() { |
| 1746 | int N = readInt(); |
| 1747 | if (N < 0) { |
| 1748 | return null; |
| 1749 | } |
| 1750 | SparseBooleanArray sa = new SparseBooleanArray(N); |
| 1751 | readSparseBooleanArrayInternal(sa, N); |
| 1752 | return sa; |
| 1753 | } |
| 1754 | |
| 1755 | /** |
| 1756 | * Read and return a new ArrayList containing a particular object type from |
| 1757 | * the parcel that was written with {@link #writeTypedList} at the |
| 1758 | * current dataPosition(). Returns null if the |
| 1759 | * previously written list object was null. The list <em>must</em> have |
| 1760 | * previously been written via {@link #writeTypedList} with the same object |
| 1761 | * type. |
| 1762 | * |
| 1763 | * @return A newly created ArrayList containing objects with the same data |
| 1764 | * as those that were previously written. |
| 1765 | * |
| 1766 | * @see #writeTypedList |
| 1767 | */ |
| 1768 | public final <T> ArrayList<T> createTypedArrayList(Parcelable.Creator<T> c) { |
| 1769 | int N = readInt(); |
| 1770 | if (N < 0) { |
| 1771 | return null; |
| 1772 | } |
| 1773 | ArrayList<T> l = new ArrayList<T>(N); |
| 1774 | while (N > 0) { |
| 1775 | if (readInt() != 0) { |
| 1776 | l.add(c.createFromParcel(this)); |
| 1777 | } else { |
| 1778 | l.add(null); |
| 1779 | } |
| 1780 | N--; |
| 1781 | } |
| 1782 | return l; |
| 1783 | } |
| 1784 | |
| 1785 | /** |
| 1786 | * Read into the given List items containing a particular object type |
| 1787 | * that were written with {@link #writeTypedList} at the |
| 1788 | * current dataPosition(). The list <em>must</em> have |
| 1789 | * previously been written via {@link #writeTypedList} with the same object |
| 1790 | * type. |
| 1791 | * |
| 1792 | * @return A newly created ArrayList containing objects with the same data |
| 1793 | * as those that were previously written. |
| 1794 | * |
| 1795 | * @see #writeTypedList |
| 1796 | */ |
| 1797 | public final <T> void readTypedList(List<T> list, Parcelable.Creator<T> c) { |
| 1798 | int M = list.size(); |
| 1799 | int N = readInt(); |
| 1800 | int i = 0; |
| 1801 | for (; i < M && i < N; i++) { |
| 1802 | if (readInt() != 0) { |
| 1803 | list.set(i, c.createFromParcel(this)); |
| 1804 | } else { |
| 1805 | list.set(i, null); |
| 1806 | } |
| 1807 | } |
| 1808 | for (; i<N; i++) { |
| 1809 | if (readInt() != 0) { |
| 1810 | list.add(c.createFromParcel(this)); |
| 1811 | } else { |
| 1812 | list.add(null); |
| 1813 | } |
| 1814 | } |
| 1815 | for (; i<M; i++) { |
| 1816 | list.remove(N); |
| 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | /** |
| 1821 | * Read and return a new ArrayList containing String objects from |
| 1822 | * the parcel that was written with {@link #writeStringList} at the |
| 1823 | * current dataPosition(). Returns null if the |
| 1824 | * previously written list object was null. |
| 1825 | * |
| 1826 | * @return A newly created ArrayList containing strings with the same data |
| 1827 | * as those that were previously written. |
| 1828 | * |
| 1829 | * @see #writeStringList |
| 1830 | */ |
| 1831 | public final ArrayList<String> createStringArrayList() { |
| 1832 | int N = readInt(); |
| 1833 | if (N < 0) { |
| 1834 | return null; |
| 1835 | } |
| 1836 | ArrayList<String> l = new ArrayList<String>(N); |
| 1837 | while (N > 0) { |
| 1838 | l.add(readString()); |
| 1839 | N--; |
| 1840 | } |
| 1841 | return l; |
| 1842 | } |
| 1843 | |
| 1844 | /** |
| 1845 | * Read and return a new ArrayList containing IBinder objects from |
| 1846 | * the parcel that was written with {@link #writeBinderList} at the |
| 1847 | * current dataPosition(). Returns null if the |
| 1848 | * previously written list object was null. |
| 1849 | * |
| 1850 | * @return A newly created ArrayList containing strings with the same data |
| 1851 | * as those that were previously written. |
| 1852 | * |
| 1853 | * @see #writeBinderList |
| 1854 | */ |
| 1855 | public final ArrayList<IBinder> createBinderArrayList() { |
| 1856 | int N = readInt(); |
| 1857 | if (N < 0) { |
| 1858 | return null; |
| 1859 | } |
| 1860 | ArrayList<IBinder> l = new ArrayList<IBinder>(N); |
| 1861 | while (N > 0) { |
| 1862 | l.add(readStrongBinder()); |
| 1863 | N--; |
| 1864 | } |
| 1865 | return l; |
| 1866 | } |
| 1867 | |
| 1868 | /** |
| 1869 | * Read into the given List items String objects that were written with |
| 1870 | * {@link #writeStringList} at the current dataPosition(). |
| 1871 | * |
| 1872 | * @return A newly created ArrayList containing strings with the same data |
| 1873 | * as those that were previously written. |
| 1874 | * |
| 1875 | * @see #writeStringList |
| 1876 | */ |
| 1877 | public final void readStringList(List<String> list) { |
| 1878 | int M = list.size(); |
| 1879 | int N = readInt(); |
| 1880 | int i = 0; |
| 1881 | for (; i < M && i < N; i++) { |
| 1882 | list.set(i, readString()); |
| 1883 | } |
| 1884 | for (; i<N; i++) { |
| 1885 | list.add(readString()); |
| 1886 | } |
| 1887 | for (; i<M; i++) { |
| 1888 | list.remove(N); |
| 1889 | } |
| 1890 | } |
| 1891 | |
| 1892 | /** |
| 1893 | * Read into the given List items IBinder objects that were written with |
| 1894 | * {@link #writeBinderList} at the current dataPosition(). |
| 1895 | * |
| 1896 | * @return A newly created ArrayList containing strings with the same data |
| 1897 | * as those that were previously written. |
| 1898 | * |
| 1899 | * @see #writeBinderList |
| 1900 | */ |
| 1901 | public final void readBinderList(List<IBinder> list) { |
| 1902 | int M = list.size(); |
| 1903 | int N = readInt(); |
| 1904 | int i = 0; |
| 1905 | for (; i < M && i < N; i++) { |
| 1906 | list.set(i, readStrongBinder()); |
| 1907 | } |
| 1908 | for (; i<N; i++) { |
| 1909 | list.add(readStrongBinder()); |
| 1910 | } |
| 1911 | for (; i<M; i++) { |
| 1912 | list.remove(N); |
| 1913 | } |
| 1914 | } |
| 1915 | |
| 1916 | /** |
| 1917 | * Read and return a new array containing a particular object type from |
| 1918 | * the parcel at the current dataPosition(). Returns null if the |
| 1919 | * previously written array was null. The array <em>must</em> have |
| 1920 | * previously been written via {@link #writeTypedArray} with the same |
| 1921 | * object type. |
| 1922 | * |
| 1923 | * @return A newly created array containing objects with the same data |
| 1924 | * as those that were previously written. |
| 1925 | * |
| 1926 | * @see #writeTypedArray |
| 1927 | */ |
| 1928 | public final <T> T[] createTypedArray(Parcelable.Creator<T> c) { |
| 1929 | int N = readInt(); |
| 1930 | if (N < 0) { |
| 1931 | return null; |
| 1932 | } |
| 1933 | T[] l = c.newArray(N); |
| 1934 | for (int i=0; i<N; i++) { |
| 1935 | if (readInt() != 0) { |
| 1936 | l[i] = c.createFromParcel(this); |
| 1937 | } |
| 1938 | } |
| 1939 | return l; |
| 1940 | } |
| 1941 | |
| 1942 | public final <T> void readTypedArray(T[] val, Parcelable.Creator<T> c) { |
| 1943 | int N = readInt(); |
| 1944 | if (N == val.length) { |
| 1945 | for (int i=0; i<N; i++) { |
| 1946 | if (readInt() != 0) { |
| 1947 | val[i] = c.createFromParcel(this); |
| 1948 | } else { |
| 1949 | val[i] = null; |
| 1950 | } |
| 1951 | } |
| 1952 | } else { |
| 1953 | throw new RuntimeException("bad array lengths"); |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | /** |
| 1958 | * @deprecated |
| 1959 | * @hide |
| 1960 | */ |
| 1961 | @Deprecated |
| 1962 | public final <T> T[] readTypedArray(Parcelable.Creator<T> c) { |
| 1963 | return createTypedArray(c); |
| 1964 | } |
| 1965 | |
| 1966 | /** |
| 1967 | * Write a heterogeneous array of Parcelable objects into the Parcel. |
| 1968 | * Each object in the array is written along with its class name, so |
| 1969 | * that the correct class can later be instantiated. As a result, this |
| 1970 | * has significantly more overhead than {@link #writeTypedArray}, but will |
| 1971 | * correctly handle an array containing more than one type of object. |
| 1972 | * |
| 1973 | * @param value The array of objects to be written. |
| 1974 | * @param parcelableFlags Contextual flags as per |
| 1975 | * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}. |
| 1976 | * |
| 1977 | * @see #writeTypedArray |
| 1978 | */ |
| 1979 | public final <T extends Parcelable> void writeParcelableArray(T[] value, |
| 1980 | int parcelableFlags) { |
| 1981 | if (value != null) { |
| 1982 | int N = value.length; |
| 1983 | writeInt(N); |
| 1984 | for (int i=0; i<N; i++) { |
| 1985 | writeParcelable(value[i], parcelableFlags); |
| 1986 | } |
| 1987 | } else { |
| 1988 | writeInt(-1); |
| 1989 | } |
| 1990 | } |
| 1991 | |
| 1992 | /** |
| 1993 | * Read a typed object from a parcel. The given class loader will be |
| 1994 | * used to load any enclosed Parcelables. If it is null, the default class |
| 1995 | * loader will be used. |
| 1996 | */ |
| 1997 | public final Object readValue(ClassLoader loader) { |
| 1998 | int type = readInt(); |
| 1999 | |
| 2000 | switch (type) { |
| 2001 | case VAL_NULL: |
| 2002 | return null; |
| 2003 | |
| 2004 | case VAL_STRING: |
| 2005 | return readString(); |
| 2006 | |
| 2007 | case VAL_INTEGER: |
| 2008 | return readInt(); |
| 2009 | |
| 2010 | case VAL_MAP: |
| 2011 | return readHashMap(loader); |
| 2012 | |
| 2013 | case VAL_PARCELABLE: |
| 2014 | return readParcelable(loader); |
| 2015 | |
| 2016 | case VAL_SHORT: |
| 2017 | return (short) readInt(); |
| 2018 | |
| 2019 | case VAL_LONG: |
| 2020 | return readLong(); |
| 2021 | |
| 2022 | case VAL_FLOAT: |
| 2023 | return readFloat(); |
| 2024 | |
| 2025 | case VAL_DOUBLE: |
| 2026 | return readDouble(); |
| 2027 | |
| 2028 | case VAL_BOOLEAN: |
| 2029 | return readInt() == 1; |
| 2030 | |
| 2031 | case VAL_CHARSEQUENCE: |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 2032 | return readCharSequence(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2033 | |
| 2034 | case VAL_LIST: |
| 2035 | return readArrayList(loader); |
| 2036 | |
| 2037 | case VAL_BOOLEANARRAY: |
| 2038 | return createBooleanArray(); |
| 2039 | |
| 2040 | case VAL_BYTEARRAY: |
| 2041 | return createByteArray(); |
| 2042 | |
| 2043 | case VAL_STRINGARRAY: |
| 2044 | return readStringArray(); |
| 2045 | |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 2046 | case VAL_CHARSEQUENCEARRAY: |
| 2047 | return readCharSequenceArray(); |
| 2048 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2049 | case VAL_IBINDER: |
| 2050 | return readStrongBinder(); |
| 2051 | |
| 2052 | case VAL_OBJECTARRAY: |
| 2053 | return readArray(loader); |
| 2054 | |
| 2055 | case VAL_INTARRAY: |
| 2056 | return createIntArray(); |
| 2057 | |
| 2058 | case VAL_LONGARRAY: |
| 2059 | return createLongArray(); |
| 2060 | |
| 2061 | case VAL_BYTE: |
| 2062 | return readByte(); |
| 2063 | |
| 2064 | case VAL_SERIALIZABLE: |
John Spurlock | 5002b8c | 2014-01-10 13:32:12 -0500 | [diff] [blame] | 2065 | return readSerializable(loader); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2066 | |
| 2067 | case VAL_PARCELABLEARRAY: |
| 2068 | return readParcelableArray(loader); |
| 2069 | |
| 2070 | case VAL_SPARSEARRAY: |
| 2071 | return readSparseArray(loader); |
| 2072 | |
| 2073 | case VAL_SPARSEBOOLEANARRAY: |
| 2074 | return readSparseBooleanArray(); |
| 2075 | |
| 2076 | case VAL_BUNDLE: |
| 2077 | return readBundle(loader); // loading will be deferred |
| 2078 | |
| 2079 | default: |
| 2080 | int off = dataPosition() - 4; |
| 2081 | throw new RuntimeException( |
| 2082 | "Parcel " + this + ": Unmarshalling unknown type code " + type + " at offset " + off); |
| 2083 | } |
| 2084 | } |
| 2085 | |
| 2086 | /** |
| 2087 | * Read and return a new Parcelable from the parcel. The given class loader |
| 2088 | * will be used to load any enclosed Parcelables. If it is null, the default |
| 2089 | * class loader will be used. |
| 2090 | * @param loader A ClassLoader from which to instantiate the Parcelable |
| 2091 | * object, or null for the default class loader. |
| 2092 | * @return Returns the newly created Parcelable, or null if a null |
| 2093 | * object has been written. |
| 2094 | * @throws BadParcelableException Throws BadParcelableException if there |
| 2095 | * was an error trying to instantiate the Parcelable. |
| 2096 | */ |
| 2097 | public final <T extends Parcelable> T readParcelable(ClassLoader loader) { |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 2098 | Parcelable.Creator<T> creator = readParcelableCreator(loader); |
| 2099 | if (creator == null) { |
| 2100 | return null; |
| 2101 | } |
| 2102 | if (creator instanceof Parcelable.ClassLoaderCreator<?>) { |
| 2103 | return ((Parcelable.ClassLoaderCreator<T>)creator).createFromParcel(this, loader); |
| 2104 | } |
| 2105 | return creator.createFromParcel(this); |
| 2106 | } |
| 2107 | |
| 2108 | /** @hide */ |
| 2109 | public final <T extends Parcelable> T readCreator(Parcelable.Creator<T> creator, |
| 2110 | ClassLoader loader) { |
| 2111 | if (creator instanceof Parcelable.ClassLoaderCreator<?>) { |
| 2112 | return ((Parcelable.ClassLoaderCreator<T>)creator).createFromParcel(this, loader); |
| 2113 | } |
| 2114 | return creator.createFromParcel(this); |
| 2115 | } |
| 2116 | |
| 2117 | /** @hide */ |
| 2118 | public final <T extends Parcelable> Parcelable.Creator<T> readParcelableCreator( |
| 2119 | ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2120 | String name = readString(); |
| 2121 | if (name == null) { |
| 2122 | return null; |
| 2123 | } |
| 2124 | Parcelable.Creator<T> creator; |
| 2125 | synchronized (mCreators) { |
| 2126 | HashMap<String,Parcelable.Creator> map = mCreators.get(loader); |
| 2127 | if (map == null) { |
| 2128 | map = new HashMap<String,Parcelable.Creator>(); |
| 2129 | mCreators.put(loader, map); |
| 2130 | } |
| 2131 | creator = map.get(name); |
| 2132 | if (creator == null) { |
| 2133 | try { |
| 2134 | Class c = loader == null ? |
| 2135 | Class.forName(name) : Class.forName(name, true, loader); |
| 2136 | Field f = c.getField("CREATOR"); |
| 2137 | creator = (Parcelable.Creator)f.get(null); |
| 2138 | } |
| 2139 | catch (IllegalAccessException e) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 2140 | Log.e(TAG, "Illegal access when unmarshalling: " |
| 2141 | + name, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2142 | throw new BadParcelableException( |
| 2143 | "IllegalAccessException when unmarshalling: " + name); |
| 2144 | } |
| 2145 | catch (ClassNotFoundException e) { |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 2146 | Log.e(TAG, "Class not found when unmarshalling: " |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 2147 | + name, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2148 | throw new BadParcelableException( |
| 2149 | "ClassNotFoundException when unmarshalling: " + name); |
| 2150 | } |
| 2151 | catch (ClassCastException e) { |
| 2152 | throw new BadParcelableException("Parcelable protocol requires a " |
| 2153 | + "Parcelable.Creator object called " |
| 2154 | + " CREATOR on class " + name); |
| 2155 | } |
| 2156 | catch (NoSuchFieldException e) { |
| 2157 | throw new BadParcelableException("Parcelable protocol requires a " |
| 2158 | + "Parcelable.Creator object called " |
| 2159 | + " CREATOR on class " + name); |
| 2160 | } |
Irfan Sheriff | 97a72f6 | 2013-01-11 10:45:51 -0800 | [diff] [blame] | 2161 | catch (NullPointerException e) { |
| 2162 | throw new BadParcelableException("Parcelable protocol requires " |
| 2163 | + "the CREATOR object to be static on class " + name); |
| 2164 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2165 | if (creator == null) { |
| 2166 | throw new BadParcelableException("Parcelable protocol requires a " |
| 2167 | + "Parcelable.Creator object called " |
| 2168 | + " CREATOR on class " + name); |
| 2169 | } |
| 2170 | |
| 2171 | map.put(name, creator); |
| 2172 | } |
| 2173 | } |
| 2174 | |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 2175 | return creator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2176 | } |
| 2177 | |
| 2178 | /** |
| 2179 | * Read and return a new Parcelable array from the parcel. |
| 2180 | * The given class loader will be used to load any enclosed |
| 2181 | * Parcelables. |
| 2182 | * @return the Parcelable array, or null if the array is null |
| 2183 | */ |
| 2184 | public final Parcelable[] readParcelableArray(ClassLoader loader) { |
| 2185 | int N = readInt(); |
| 2186 | if (N < 0) { |
| 2187 | return null; |
| 2188 | } |
| 2189 | Parcelable[] p = new Parcelable[N]; |
| 2190 | for (int i = 0; i < N; i++) { |
| 2191 | p[i] = (Parcelable) readParcelable(loader); |
| 2192 | } |
| 2193 | return p; |
| 2194 | } |
| 2195 | |
| 2196 | /** |
| 2197 | * Read and return a new Serializable object from the parcel. |
| 2198 | * @return the Serializable object, or null if the Serializable name |
| 2199 | * wasn't found in the parcel. |
| 2200 | */ |
| 2201 | public final Serializable readSerializable() { |
John Spurlock | 5002b8c | 2014-01-10 13:32:12 -0500 | [diff] [blame] | 2202 | return readSerializable(null); |
| 2203 | } |
| 2204 | |
| 2205 | private final Serializable readSerializable(final ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2206 | String name = readString(); |
| 2207 | if (name == null) { |
| 2208 | // For some reason we were unable to read the name of the Serializable (either there |
| 2209 | // is nothing left in the Parcel to read, or the next value wasn't a String), so |
| 2210 | // return null, which indicates that the name wasn't found in the parcel. |
| 2211 | return null; |
| 2212 | } |
| 2213 | |
| 2214 | byte[] serializedData = createByteArray(); |
| 2215 | ByteArrayInputStream bais = new ByteArrayInputStream(serializedData); |
| 2216 | try { |
John Spurlock | 5002b8c | 2014-01-10 13:32:12 -0500 | [diff] [blame] | 2217 | ObjectInputStream ois = new ObjectInputStream(bais) { |
| 2218 | @Override |
| 2219 | protected Class<?> resolveClass(ObjectStreamClass osClass) |
| 2220 | throws IOException, ClassNotFoundException { |
| 2221 | // try the custom classloader if provided |
| 2222 | if (loader != null) { |
| 2223 | Class<?> c = Class.forName(osClass.getName(), false, loader); |
| 2224 | if (c != null) { |
| 2225 | return c; |
| 2226 | } |
| 2227 | } |
| 2228 | return super.resolveClass(osClass); |
| 2229 | } |
| 2230 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2231 | return (Serializable) ois.readObject(); |
| 2232 | } catch (IOException ioe) { |
| 2233 | throw new RuntimeException("Parcelable encountered " + |
| 2234 | "IOException reading a Serializable object (name = " + name + |
| 2235 | ")", ioe); |
| 2236 | } catch (ClassNotFoundException cnfe) { |
John Spurlock | 5002b8c | 2014-01-10 13:32:12 -0500 | [diff] [blame] | 2237 | throw new RuntimeException("Parcelable encountered " + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2238 | "ClassNotFoundException reading a Serializable object (name = " |
| 2239 | + name + ")", cnfe); |
| 2240 | } |
| 2241 | } |
| 2242 | |
| 2243 | // Cache of previously looked up CREATOR.createFromParcel() methods for |
| 2244 | // particular classes. Keys are the names of the classes, values are |
| 2245 | // Method objects. |
| 2246 | private static final HashMap<ClassLoader,HashMap<String,Parcelable.Creator>> |
| 2247 | mCreators = new HashMap<ClassLoader,HashMap<String,Parcelable.Creator>>(); |
| 2248 | |
Narayan Kamath | b34a461 | 2014-01-23 14:17:11 +0000 | [diff] [blame] | 2249 | /** @hide for internal use only. */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2250 | static protected final Parcel obtain(int obj) { |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 2251 | throw new UnsupportedOperationException(); |
| 2252 | } |
| 2253 | |
| 2254 | /** @hide */ |
| 2255 | static protected final Parcel obtain(long obj) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2256 | final Parcel[] pool = sHolderPool; |
| 2257 | synchronized (pool) { |
| 2258 | Parcel p; |
| 2259 | for (int i=0; i<POOL_SIZE; i++) { |
| 2260 | p = pool[i]; |
| 2261 | if (p != null) { |
| 2262 | pool[i] = null; |
| 2263 | if (DEBUG_RECYCLE) { |
| 2264 | p.mStack = new RuntimeException(); |
| 2265 | } |
| 2266 | p.init(obj); |
| 2267 | return p; |
| 2268 | } |
| 2269 | } |
| 2270 | } |
| 2271 | return new Parcel(obj); |
| 2272 | } |
| 2273 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 2274 | private Parcel(long nativePtr) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2275 | if (DEBUG_RECYCLE) { |
| 2276 | mStack = new RuntimeException(); |
| 2277 | } |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 2278 | //Log.i(TAG, "Initializing obj=0x" + Integer.toHexString(obj), mStack); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 2279 | init(nativePtr); |
| 2280 | } |
| 2281 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 2282 | private void init(long nativePtr) { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 2283 | if (nativePtr != 0) { |
| 2284 | mNativePtr = nativePtr; |
| 2285 | mOwnsNativeParcelObject = false; |
| 2286 | } else { |
| 2287 | mNativePtr = nativeCreate(); |
| 2288 | mOwnsNativeParcelObject = true; |
| 2289 | } |
| 2290 | } |
| 2291 | |
| 2292 | private void freeBuffer() { |
| 2293 | if (mOwnsNativeParcelObject) { |
| 2294 | nativeFreeBuffer(mNativePtr); |
| 2295 | } |
| 2296 | } |
| 2297 | |
| 2298 | private void destroy() { |
| 2299 | if (mNativePtr != 0) { |
| 2300 | if (mOwnsNativeParcelObject) { |
| 2301 | nativeDestroy(mNativePtr); |
| 2302 | } |
| 2303 | mNativePtr = 0; |
| 2304 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2305 | } |
| 2306 | |
| 2307 | @Override |
| 2308 | protected void finalize() throws Throwable { |
| 2309 | if (DEBUG_RECYCLE) { |
| 2310 | if (mStack != null) { |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 2311 | 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] | 2312 | } |
| 2313 | } |
| 2314 | destroy(); |
| 2315 | } |
| 2316 | |
Dianne Hackborn | 6aff905 | 2009-05-22 13:20:23 -0700 | [diff] [blame] | 2317 | /* package */ void readMapInternal(Map outVal, int N, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2318 | ClassLoader loader) { |
| 2319 | while (N > 0) { |
| 2320 | Object key = readValue(loader); |
| 2321 | Object value = readValue(loader); |
| 2322 | outVal.put(key, value); |
| 2323 | N--; |
| 2324 | } |
| 2325 | } |
| 2326 | |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 2327 | /* package */ void readArrayMapInternal(ArrayMap outVal, int N, |
| 2328 | ClassLoader loader) { |
Dianne Hackborn | e784d1e | 2013-09-20 18:13:52 -0700 | [diff] [blame] | 2329 | if (DEBUG_ARRAY_MAP) { |
| 2330 | RuntimeException here = new RuntimeException("here"); |
| 2331 | here.fillInStackTrace(); |
| 2332 | Log.d(TAG, "Reading " + N + " ArrayMap entries", here); |
| 2333 | } |
Dianne Hackborn | 8aee64d | 2013-10-25 10:41:50 -0700 | [diff] [blame] | 2334 | int startPos; |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 2335 | while (N > 0) { |
Dianne Hackborn | 8aee64d | 2013-10-25 10:41:50 -0700 | [diff] [blame] | 2336 | if (DEBUG_ARRAY_MAP) startPos = dataPosition(); |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 2337 | Object key = readValue(loader); |
| 2338 | Object value = readValue(loader); |
Dianne Hackborn | 8aee64d | 2013-10-25 10:41:50 -0700 | [diff] [blame] | 2339 | if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " " |
| 2340 | + (dataPosition()-startPos) + " bytes: key=0x" |
| 2341 | + Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key); |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 2342 | outVal.append(key, value); |
| 2343 | N--; |
| 2344 | } |
| 2345 | } |
| 2346 | |
Dianne Hackborn | e784d1e | 2013-09-20 18:13:52 -0700 | [diff] [blame] | 2347 | /* package */ void readArrayMapSafelyInternal(ArrayMap outVal, int N, |
| 2348 | ClassLoader loader) { |
| 2349 | if (DEBUG_ARRAY_MAP) { |
| 2350 | RuntimeException here = new RuntimeException("here"); |
| 2351 | here.fillInStackTrace(); |
| 2352 | Log.d(TAG, "Reading safely " + N + " ArrayMap entries", here); |
| 2353 | } |
| 2354 | while (N > 0) { |
| 2355 | Object key = readValue(loader); |
| 2356 | if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read safe #" + (N-1) + ": key=0x" |
| 2357 | + (key != null ? key.hashCode() : 0) + " " + key); |
| 2358 | Object value = readValue(loader); |
| 2359 | outVal.put(key, value); |
| 2360 | N--; |
| 2361 | } |
| 2362 | } |
| 2363 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2364 | private void readListInternal(List outVal, int N, |
| 2365 | ClassLoader loader) { |
| 2366 | while (N > 0) { |
| 2367 | Object value = readValue(loader); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 2368 | //Log.d(TAG, "Unmarshalling value=" + value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2369 | outVal.add(value); |
| 2370 | N--; |
| 2371 | } |
| 2372 | } |
| 2373 | |
| 2374 | private void readArrayInternal(Object[] outVal, int N, |
| 2375 | ClassLoader loader) { |
| 2376 | for (int i = 0; i < N; i++) { |
| 2377 | Object value = readValue(loader); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 2378 | //Log.d(TAG, "Unmarshalling value=" + value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2379 | outVal[i] = value; |
| 2380 | } |
| 2381 | } |
| 2382 | |
| 2383 | private void readSparseArrayInternal(SparseArray outVal, int N, |
| 2384 | ClassLoader loader) { |
| 2385 | while (N > 0) { |
| 2386 | int key = readInt(); |
| 2387 | Object value = readValue(loader); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 2388 | //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2389 | outVal.append(key, value); |
| 2390 | N--; |
| 2391 | } |
| 2392 | } |
| 2393 | |
| 2394 | |
| 2395 | private void readSparseBooleanArrayInternal(SparseBooleanArray outVal, int N) { |
| 2396 | while (N > 0) { |
| 2397 | int key = readInt(); |
| 2398 | boolean value = this.readByte() == 1; |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 2399 | //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2400 | outVal.append(key, value); |
| 2401 | N--; |
| 2402 | } |
| 2403 | } |
| 2404 | } |