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