Java中,一切都是對象,在分布式環(huán)境中經(jīng)常需要將Object從這一端網(wǎng)絡(luò)或設(shè)備傳遞到另一端。這就需要有一種可以在兩端傳輸數(shù)據(jù)的協(xié)議。Java序列化機制就是為了解決這個問題而產(chǎn)生。
將對象狀態(tài)轉(zhuǎn)換成字節(jié)流之后,可以用java.io包中各種字節(jié)流的類將其保存到文件中,管道到另一線程中或通過網(wǎng)絡(luò)連接將對象數(shù)據(jù)發(fā)送到另一主機。對象序列化功能非常簡單、強大,在RMI、Socket、JMS、EJB都有應(yīng)用。對象序列化問題在網(wǎng)絡(luò)編程中并不是最核心的課題,但卻相當(dāng)重要,具有許多實用意義。
java對象序列化不僅保留一個對象的數(shù)據(jù),而且遞歸保存對象引用的每個對象的數(shù)據(jù)。可以將整個對象層次寫入字節(jié)流中,可以保存在文件中或在網(wǎng)絡(luò)連接上傳遞。利用對象序列化可以進行對象的“深復(fù)制”,即復(fù)制對象本身及引用的對象本身。序列化一個對象可能得到整個對象的序列。
基本使用方法:
Serialization是指把類或者基本的數(shù)據(jù)類型持久化(persistence)到數(shù)據(jù)流(Stream)中,包括文件、字節(jié)流、網(wǎng)絡(luò)數(shù)據(jù)流。
JAVA中實現(xiàn)serialization主要靠兩個類:ObjectOuputStream和ObjectInputStream。他們是JAVA IO系統(tǒng)里的OutputStream和InputStream的子類。既然他們是JAVA IO中的流,那么就可以像操作一般的流一樣來操作他們。下面是他們使用方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class Pair implements Serializable{ private static final long serialVersionUID = -1874850715617681161L; private int type; private String name; public int getType() { return type; } public void setType( int type) { this .type = type; } public String getName() { return name; } public void setName(String name) { this .name = name; } public Pair( int type, String name) { super (); this .type = type; this .name = name; } public static void main(String[] args) throws IOException, ClassNotFoundException { // TODO Auto-generated method stub //serialize object pair ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); Pair pair = new Pair( 1 , "charlie" ); oos.writeObject(pair); //deserialize object, get new object newpair ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); Pair newpair = (Pair) ois.readObject(); System.out.println(newpair.getType()+ ":" +newpair.getName()); } } |
1. 這兩個類都是decorator模式的,在創(chuàng)建他們的時候,都要傳入一個基于字節(jié)的流,真正在底下存貯序列化數(shù)據(jù)的都是這些流。
2. 被持久化的類要實現(xiàn)Serializable接口,這個接口沒有任何函數(shù),只是一個標記接口。如果在一臺機器上進行序列化,把得到的數(shù)據(jù)傳送到另外一個機器上進行反序列化,那么這兩臺機器上的類應(yīng)該是完全一樣的,否則序列化是不會成功的。
3. 切記不要把上面代碼中的bos用toString得到String,然后再從這個String中得到ByteArrayInputStream,再進行反序列化。bos是以字節(jié)存貯的,轉(zhuǎn)成以字符存貯的String必然會造成數(shù)據(jù)的變化,而從String中到的byte[]也不會是之前那個byte[]了。我遇到過這個問題,是因為我想把序列化之后的數(shù)據(jù)存在xml文件中。
java虛擬機在序列化和反序列化的時候都做了些什么?
javadoc中對這兩個類的描述中對java的序列化機制進行了詳細的描述:
引用
The default serialization mechanism for an object writes the class of the object, the class signature, and the values of all non-transient and non-static fields. References to other objects (except in transient or static fields) cause those objects to be written also. Multiple references to a single object are encoded using a reference sharing mechanism so that graphs of objects can be restored to the same shape as when the original was written.
默認的序列化機制寫到流中的數(shù)據(jù)有:
1、對象所屬的類
2、類的簽名
3、所有的非transient和非static的屬性
4、對其他對象的引用也會造成對這些對象的序列化
5、如果多個引用指向一個對象,那么會使用sharing reference機制
引用
Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:
1
2
3
4
5
6
|
private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException; private void writeObject(java.io.ObjectOutputStream stream) throws IOException private void readObjectNoData() throws ObjectStreamException; |