首页 > 代码库 > Binder-Java Parcel

Binder-Java Parcel

Parcel是Binder用在在两个进程间的共享内存里做对象引用与值序列化(不同于传统的序列化)的类。序列化在binder程序里又加flatten,把数据结构散开,直接以blob的形式放进内存里。

Parcel对象是数据的容器,通过IBinder来传送。

用来读写数据的API有六个主要数据类型,分别是Byte,Double,Float,Int,Long,String。Parcel提供了处理这些类型的API。

也有创建、读、写数组的相应API。

Parcelable是一个协议,为了通过在Parcel里传递传递对象引用从而在进程间传递而创建。可用的方法有writeParcelable,readParcelable和writeParcelableArray,readParcelableArray。

一种更有效的办法是create,read,write Typed List/Object/Array,不会传入类型名,但要自己做初始化。更有效的办法是Parcelable里内置的接口。

代表底层文件的FlieDescriptor对象,也有相应的类方法来读写创建。

代码位于frameworks/base/core/java/android/os

先是几个debug flag,recycle和app_map

1 189public final class Parcel {
2 190    private static final boolean DEBUG_RECYCLE = false;
3 191    private static final boolean DEBUG_ARRAY_MAP = false;
4 192    private static final String TAG = "Parcel";

然后是jni的native指针

1 194    @SuppressWarnings({"UnusedDeclaration"})
2 195    private long mNativePtr; // used by native code

然后是说明指针对象是否是被本对象创建的flag和指针指向的大小

1 197    /**
2 198     * Flag indicating if {@link #mNativePtr} was allocated by this object,
3 199     * indicating that we‘re responsible for its lifecycle.
4 200     */
5 201    private boolean mOwnsNativeParcelObject;
6 202    private long mNativeSize;

然后是RuntimeException对象,mStack

1 204    private RuntimeException mStack;

然后是自有的和占有的Parcel对象数组池,数字大小为16

1 206    private static final int POOL_SIZE = 6;
2 207    private static final Parcel[] sOwnedPool = new Parcel[POOL_SIZE];
3 208    private static final Parcel[] sHolderPool = new Parcel[POOL_SIZE];

然后是表示Parcel可以传递的类型定义,native的C++ code可以以此知道有多少个字节

 1 210    // Keep in sync with frameworks/native/libs/binder/PersistableBundle.cpp.
 2 211    private static final int VAL_NULL = -1;
 3 212    private static final int VAL_STRING = 0;
 4 213    private static final int VAL_INTEGER = 1;
 5 214    private static final int VAL_MAP = 2;
 6 215    private static final int VAL_BUNDLE = 3;
 7 216    private static final int VAL_PARCELABLE = 4;
 8 217    private static final int VAL_SHORT = 5;
 9 218    private static final int VAL_LONG = 6;
10 219    private static final int VAL_FLOAT = 7;
11 220    private static final int VAL_DOUBLE = 8;
12 221    private static final int VAL_BOOLEAN = 9;
13 222    private static final int VAL_CHARSEQUENCE = 10;
14 223    private static final int VAL_LIST  = 11;
15 224    private static final int VAL_SPARSEARRAY = 12;
16 225    private static final int VAL_BYTEARRAY = 13;
17 226    private static final int VAL_STRINGARRAY = 14;
18 227    private static final int VAL_IBINDER = 15;
19 228    private static final int VAL_PARCELABLEARRAY = 16;
20 229    private static final int VAL_OBJECTARRAY = 17;
21 230    private static final int VAL_INTARRAY = 18;
22 231    private static final int VAL_LONGARRAY = 19;
23 232    private static final int VAL_BYTE = 20;
24 233    private static final int VAL_SERIALIZABLE = 21;
25 234    private static final int VAL_SPARSEBOOLEANARRAY = 22;
26 235    private static final int VAL_BOOLEANARRAY = 23;
27 236    private static final int VAL_CHARSEQUENCEARRAY = 24;
28 237    private static final int VAL_PERSISTABLEBUNDLE = 25;
29 238    private static final int VAL_SIZE = 26;
30 239    private static final int VAL_SIZEF = 27;
31 240    private static final int VAL_DOUBLEARRAY = 28;

然后是reply回来的parcel头部里的错误码定义

 1 242    // The initial int32 in a Binder call‘s reply Parcel header:
 2 243    // Keep these in sync with libbinder‘s binder/Status.h.
 3 244    private static final int EX_SECURITY = -1;
 4 245    private static final int EX_BAD_PARCELABLE = -2;
 5 246    private static final int EX_ILLEGAL_ARGUMENT = -3;
 6 247    private static final int EX_NULL_POINTER = -4;
 7 248    private static final int EX_ILLEGAL_STATE = -5;
 8 249    private static final int EX_NETWORK_MAIN_THREAD = -6;
 9 250    private static final int EX_UNSUPPORTED_OPERATION = -7;
10 251    private static final int EX_SERVICE_SPECIFIC = -8;
11 252    private static final int EX_HAS_REPLY_HEADER = -128;  // special; see below
12 253    // EX_TRANSACTION_FAILED is used exclusively in native code.
13 254    // see libbinder‘s binder/Status.h
14 255    private static final int EX_TRANSACTION_FAILED = -129;

 然后是几个获取与设置native对象大小,游标位置的jni方法

1 257    private static native int nativeDataSize(long nativePtr);
2 258    private static native int nativeDataAvail(long nativePtr);
3 259    private static native int nativeDataPosition(long nativePtr);
4 260    private static native int nativeDataCapacity(long nativePtr);
5 261    private static native long nativeSetDataSize(long nativePtr, int size);
6 262    private static native void nativeSetDataPosition(long nativePtr, int pos);
7 263    private static native void nativeSetDataCapacity(long nativePtr, int size);

然后是操作fd的native方法

1 265    private static native boolean nativePushAllowFds(long nativePtr, boolean allowFds);
2 266    private static native void nativeRestoreAllowFds(long nativePtr, boolean lastValue);

然后是写具体类型数据到native对象的native方法

1 268    private static native void nativeWriteByteArray(long nativePtr, byte[] b, int offset, int len);
2 269    private static native void nativeWriteBlob(long nativePtr, byte[] b, int offset, int len);
3 270    private static native void nativeWriteInt(long nativePtr, int val);
4 271    private static native void nativeWriteLong(long nativePtr, long val);
5 272    private static native void nativeWriteFloat(long nativePtr, float val);
6 273    private static native void nativeWriteDouble(long nativePtr, double val);
7 274    private static native void nativeWriteString(long nativePtr, String val);
8 275    private static native void nativeWriteStrongBinder(long nativePtr, IBinder val);
9 276    private static native long nativeWriteFileDescriptor(long nativePtr, FileDescriptor val);

然后是从native对象读具体类型数据的native方法

1 278    private static native byte[] nativeCreateByteArray(long nativePtr);
2 279    private static native byte[] nativeReadBlob(long nativePtr);
3 280    private static native int nativeReadInt(long nativePtr);
4 281    private static native long nativeReadLong(long nativePtr);
5 282    private static native float nativeReadFloat(long nativePtr);
6 283    private static native double nativeReadDouble(long nativePtr);
7 284    private static native String nativeReadString(long nativePtr);
8 285    private static native IBinder nativeReadStrongBinder(long nativePtr);
9 286    private static native FileDescriptor nativeReadFileDescriptor(long nativePtr);

然后是创造、销毁buffer的native方法

1 288    private static native long nativeCreate();
2 289    private static native long nativeFreeBuffer(long nativePtr);
3 290    private static native void nativeDestroy(long nativePtr);

然后是序列化反序列化的native方法

1 292    private static native byte[] nativeMarshall(long nativePtr);
2 293    private static native long nativeUnmarshall(
3 294            long nativePtr, byte[] data, int offset, int length);

然后是合并native对象的native方法

1 295    private static native long nativeAppendFrom(
2 296            long thisNativePtr, long otherNativePtr, int offset, int length);

然后是判断native buffer里是否存在文件描述符的native方法

1 297    private static native boolean nativeHasFileDescriptors(long nativePtr);

然后是write和enforce interface的native方法

1 298    private static native void nativeWriteInterfaceToken(long nativePtr, String interfaceName);
2 299    private static native void nativeEnforceInterface(long nativePtr, String interfaceName);

 

Binder-Java Parcel