1、整个读写全是在内存中进行,主要是通过malloc()、realloc()、memcpy()等内存操作进行
2、读写时是PAD_SIZE=4字节对齐, #define PAD_SIZE(s) (((s)+3)&~3)
3、预分配的空间不够时newSize = ((mDataSize+len)*3)/2;会一次多分配50%
4、普通数据,使用的是mData内存地址,对于IBinder类型的数据以及FileDescriptor使用的是mObjects内存地址
下面举个小例子说明一下如何使用:
- package test.com.parcel;
- import Android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.Parcel;
- import android.os.Parcelable;
- import android.util.Log;
- public class TestParcelActivity extends Activity {
- ComplexDataStruct complexData = null;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- testComplexDS();
- }
- public void testComplexDS(){
- complexData = new ComplexDataStruct();
- Intent intent=new Intent(TestParcelActivity.this,TestParcelActivity.class);
- complexData.a = 10;
- complexData.b = 20;
- complexData.str1 = " hello...";
- complexData.str2 = " world...";
- intent.putExtra("complexData", complexData);
- ComplexDataStruct test = (ComplexDataStruct)intent.getParcelableExtra("complexData");
- Log.i("","a="+test.a);
- Log.i("","b="+test.b);
- Log.i("","str1="+test.str1);
- Log.i("","str2="+test.str2);
- }
- /* 一个复杂的Parcelable对象传递 */
- public class ComplexDataStruct {
- public int a = 0;
- public int b = 0;
- public String str1 = null;
- public String str2 = null;
- /* 内部实现静态CREATOR类 */
- public static final Parcelable.Creator<ComplexDataStruct> CREATOR =
- new Parcelable.Creator<ComplexDataStruct>() {
- // 从Parcel中读取数据,返回ComplexDataStruct对象
- public ComplexDataStruct createFromParcel(Parcel in) {
- return new ComplexDataStruct(in);
- }
- public ComplexDataStruct[] newArray(int size) {
- return new ComplexDataStruct[size]; // 传递数组
- }
- };
- ComplexDataStruct(){
- //TODO
- }
- public ComplexDataStruct(ComplexDataStruct other) {
- this.a = other.a;
- this.b = other.b;
- this.str1 = other.str1;
- this.str2 = other.str2;
- }
- private ComplexDataStruct(Parcel in) {
- readFromParcel(in);
- }
- public void readFromParcel(Parcel in) {
- a = in.readInt();
- b = in.readInt();
- str1 = in.readString();
- str2 = in.readString();
- }
- public int describeContents() {
- // TODO Auto-generated method stub
- return 0;
- }
- public void writeToParcel(Parcel arg0, int arg1) {
- // TODO Auto-generated method stub
- arg0.writeInt(a);
- arg0.writeInt(b);
- arg0.writeString(str1);
- arg0.writeString(str2);
- }
- }
- }
