关于parcel,我们先来讲讲它的“父辈” Serialize。
Serialize 是java提供的一套序列化机制。但是为什么要序列化,怎么序列化,序列化是怎么做到的,我们将在本文探讨下。
一:java 中的serialize
关于Serialize这个东东,think in java其实说的很详细,大意如下:
1.Serialize的目的
当你创建对象时,你需要,它一直存在,但是当程序终止时,它就消失了。
如果程序不运行的情况下,可以保存某些信息,这将非常有用。
如何我程序在下次运行的时候,可以把上次运行的某些信息恢复回来.
2.Serialize的使用:
使用一个嵌套的Serializable对象
package com.joyfulmath.androidstudy.bind.worm; import java.io.Serializable; public class Data implements Serializable { private int n; public Data(int n) { this.n = n; } @Override public String toString() { return Integer.toString(n); } }
package com.joyfulmath.androidstudy.bind.worm; import java.io.Serializable; import java.util.Random; import com.joyfulmath.androidstudy.TraceLog; public class Worm implements Serializable { static Random rand = new Random(47); Data[] d = { new Data(rand.nextInt(10)), new Data(rand.nextInt(10)), new Data(rand.nextInt(10)) }; private Worm next; private char c; public Worm(int i, char x) { TraceLog.i("Worm construct:"+i); c = x; if(--i>0) { next = new Worm(i,(char) (x+1)); } } public Worm() { TraceLog.i("default Worm construct"); } @Override public String toString() { StringBuilder result = new StringBuilder(":"); result.append(c); result.append("("); for(Data dat:d) { result.append(dat+" "); } result.append(")"); if(next!=null) { result.append(next); } return result.toString(); } }
验证序列化的读写:
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import com.joyfulmath.androidstudy.TraceLog; public class WormSample { static final String path = "/mnt/sdcard/worm.out"; public void doAction() { Worm w = new Worm(6, 'a'); TraceLog.i(" "+w.toString()); try { ObjectOutputStream opt = new ObjectOutputStream(new FileOutputStream(path)); opt.writeObject("Worm object "); opt.writeObject(w); opt.close(); ObjectInputStream in = new ObjectInputStream(new FileInputStream(path)); String s = (String) in.readObject(); Worm w2 = (Worm) in.readObject(); TraceLog.i(s+w); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
最后log:
08-15 09:18:20.384: I/Worm(28437): <init>: Worm construct:6 [at (Worm.java:21)] 08-15 09:18:20.384: I/Worm(28437): <init>: Worm construct:5 [at (Worm.java:21)] 08-15 09:18:20.384: I/Worm(28437): <init>: Worm construct:4 [at (Worm.java:21)] 08-15 09:18:20.384: I/Worm(28437): <init>: Worm construct:3 [at (Worm.java:21)] 08-15 09:18:20.384: I/Worm(28437): <init>: Worm construct:2 [at (Worm.java:21)] 08-15 09:18:20.384: I/Worm(28437): <init>: Worm construct:1 [at (Worm.java:21)] 08-15 09:18:20.384: I/WormSample(28437): doAction: 08-15 09:18:20.384: I/WormSample(28437): :a(853):b(119):c(802):d(788):e(199):f(881) [at (WormSample.java:18)] 08-15 09:18:20.414: I/WormSample(28437): doAction: Worm object 08-15 09:18:20.414: I/WormSample(28437): :a(853):b(119):c(802):d(788):e(199):f(881) [at (WormSample.java:28)]
可以看到,数据被很好的还原了,包含内部的序列化对象!
二:parcel
Serializable是java定义的一套序列化机制,但是他是操作文件来执行的。或者说,它的性能无法满足android上的要求,
这样,parcel被google发明出来,用以取代Serializable。
1.Parcelable 的使用
package com.joyfulmath.androidstudy.bind.worm; import android.os.Parcel; import android.os.Parcelable; public class DataP implements Parcelable { public int n; public DataP(int n) { this.n = n; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(n); } public static final Parcelable.Creator<DataP> CREATOR = new Parcelable.Creator<DataP>() { public DataP createFromParcel(Parcel in) { return new DataP(in); } public DataP[] newArray(int size) { return new DataP[size]; } }; private DataP(Parcel in) { n = in.readInt(); } @Override public String toString() { return Integer.toString(n); } }
package com.joyfulmath.androidstudy.bind.worm; import java.util.Random; import com.joyfulmath.androidstudy.TraceLog; import android.os.Parcel; import android.os.Parcelable; public class WormP implements Parcelable { static Random rand = new Random(47); public DataP[] d = { new DataP(rand.nextInt(10)), new DataP(rand.nextInt(10)), new DataP(rand.nextInt(10)) }; private WormP next; public byte c; public WormP(int i,byte x) { TraceLog.i("Wormp construct:"+i); c = x; if(--i>0) { next = new WormP(i,(byte) (x+1)); } } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeByte(c); dest.writeParcelableArray(d, 0); if (next != null) { dest.writeParcelable(next, 0); } } public static final Parcelable.Creator<WormP> CREATOR = new Parcelable.Creator<WormP>() { public WormP createFromParcel(Parcel in) { return new WormP(in); } public WormP[] newArray(int size) { return new WormP[size]; } }; private WormP(Parcel in) { c = in.readByte(); d = (DataP[]) in.readParcelableArray(DataP.class.getClassLoader()); } @Override public String toString() { StringBuilder result = new StringBuilder(":"); result.append(c); result.append("("); for(DataP dat:d) { result.append(dat+" "); } result.append(")"); if(next!=null) { result.append(next); } return result.toString(); } }
parcel一般使用在intent的内容的传递,所以本处做一个简单的模拟:
public void doActionP() { TraceLog.i(); byte a = 'a'; WormP w = new WormP(6, a); TraceLog.i(w.toString()); Intent intent = new Intent(); intent.putExtra("wormp", w); ///... Intent newIntent = new Intent(intent); WormP w2 = newIntent.getParcelableExtra("wormp"); TraceLog.i(w2.toString()); TraceLog.i("end"); }
08-15 10:14:11.924: I/WormSample(20183): doActionP: [at (WormSample.java:47)] 08-15 10:14:11.934: I/WormP(20183): <init>: Wormp construct:6 [at (WormP.java:21)] 08-15 10:14:11.934: I/WormP(20183): <init>: Wormp construct:5 [at (WormP.java:21)] 08-15 10:14:11.934: I/WormP(20183): <init>: Wormp construct:4 [at (WormP.java:21)] 08-15 10:14:11.934: I/WormP(20183): <init>: Wormp construct:3 [at (WormP.java:21)] 08-15 10:14:11.934: I/WormP(20183): <init>: Wormp construct:2 [at (WormP.java:21)] 08-15 10:14:11.934: I/WormP(20183): <init>: Wormp construct:1 [at (WormP.java:21)] 08-15 10:14:11.934: I/WormSample(20183): doActionP: :97(8 5 3 ):98(1 1 9 ):99(8 0 2 ):100(7 8 8 ):101(1 9 9 ):102(8 8 1 ) [at (WormSample.java:50)] 08-15 10:14:11.934: I/WormSample(20183): doActionP: :97(8 5 3 ):98(1 1 9 ):99(8 0 2 ):100(7 8 8 ):101(1 9 9 ):102(8 8 1 ) [at (WormSample.java:59)] 08-15 10:14:11.934: I/WormSample(20183): doActionP: end [at (WormSample.java:61)]
可以看到结果,数据完全正确。
以上就是parcel的使用方式,在下一篇,将探索parcel的实现方式。
参考:
http://blog.csdn.net/niu_gao/article/details/6451699