1. 程式人生 > >初識 Java的序列化與反序列化

初識 Java的序列化與反序列化

轉載-原文出處: Hollis

序列化是一種物件持久化的手段。普遍應用在網路傳輸、RMI等場景中。本文通過分析ArrayList的序列化來介紹Java序列化的相關內容。主要涉及到以下幾個問題:

怎麼實現Java的序列化

為什麼實現了java.io.Serializable接口才能被序列化

transient的作用是什麼

怎麼自定義序列化策略

自定義的序列化策略是如何被呼叫的

ArrayList對序列化的實現有什麼好處

Java物件的序列化

Java平臺允許我們在記憶體中建立可複用的Java物件,但一般情況下,只有當JVM處於執行時,這些物件才可能存在,即,這些物件的生命週期不會比JVM的生命週期更長。但在現實應用中,就可能要求在JVM停止執行之後能夠儲存(持久化)指定的物件,並在將來重新讀取被儲存的物件。Java物件序列化就能夠幫助我們實現該功能。

使用Java物件序列化,在儲存物件時,會把其狀態儲存為一組位元組,在未來,再將這些位元組組裝成物件。必須注意地是,物件序列化儲存的是物件的”狀態”,即它的成員變數。由此可知,物件序列化不會關注類中的靜態變數serialVersionUID是個例外。

除了在持久化物件時會用到物件序列化之外,當使用RMI(遠端方法呼叫),或在網路中傳遞物件時,都會用到物件序列化。Java序列化API為處理物件序列化提供了一個標準機制,該API簡單易用。

如何對Java物件進行序列化與反序列化

在Java中,只要一個類實現了java.io.Serializable介面,那麼它就可以被序列化。這裡先來一段程式碼:

code 1 建立一個User類,用於序列化及反序列化

 

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

52

53

54

55

56

package com.hollis;

import java.io.Serializable;

import java.util.Date;

 

/**

 * Created by hollis on 16/2/2.

 */

public class User implements Serializable{

    private String name;

    private int age;

    private Date birthday;

    private transient String gender;

    private static final long serialVersionUID = -6849794470754667710L;

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public int getAge() {

        return age;

    }

 

    public void setAge(int age) {

        this.age = age;

    }

 

    public Date getBirthday() {

        return birthday;

    }

 

    public void setBirthday(Date birthday) {

        this.birthday = birthday;

    }

 

    public String getGender() {

        return gender;

    }

 

    public void setGender(String gender) {

        this.gender = gender;

    }

 

    @Override

    public String toString() {

        return "User{" +

                "name='" + name + '\'' +

                ", age=" + age +

                ", gender=" + gender +

                ", birthday=" + birthday +

                '}';

    }

}

 

code 2 對User進行序列化及反序列化的Demo

 

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

52

53

54

55

56

package com.hollis;

import org.apache.commons.io.FileUtils;

import org.apache.commons.io.IOUtils;

import java.io.*;

import java.util.Date;

 

/**

 * Created by hollis on 16/2/2.

 */

public class SerializableDemo {

 

    public static void main(String[] args) {

        //Initializes The Object

        User user = new User();

        user.setName("hollis");

        user.setGender("male");

        user.setAge(23);

        user.setBirthday(new Date());

        System.out.println(user);

 

        //Write Obj to File

        ObjectOutputStream oos = null;

        try {

            oos = new ObjectOutputStream(new FileOutputStream("tempFile"));

            oos.writeObject(user);

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            IOUtils.closeQuietly(oos);

        }

 

        //Read Obj from File

        File file = new File("tempFile");

        ObjectInputStream ois = null;

        try {

            ois = new ObjectInputStream(new FileInputStream(file));

            User newUser = (User) ois.readObject();

            System.out.println(newUser);

        } catch (IOException e) {

            e.printStackTrace();

        } catch (ClassNotFoundException e) {

            e.printStackTrace();

        } finally {

            IOUtils.closeQuietly(ois);

            try {

                FileUtils.forceDelete(file);

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

 

    }

}

//output

//User{name='hollis', age=23, gender=male, birthday=Tue Feb 02 17:37:38 CST 2016}

//User{name='hollis', age=23, gender=null, birthday=Tue Feb 02 17:37:38 CST 2016}

 

序列化及反序列化相關知識

1、在Java中,只要一個類實現了java.io.Serializable介面,那麼它就可以被序列化。

2、通過ObjectOutputStreamObjectInputStream對物件進行序列化及反序列化

3、虛擬機器是否允許反序列化,不僅取決於類路徑和功能程式碼是否一致,一個非常重要的一點是兩個類的序列化 ID 是否一致(就是 private static final long serialVersionUID

4、序列化並不儲存靜態變數(除了serialVersionUID你可以除錯看下ObjectStreamClass的原始碼中,會對serialVersionUID特殊處理並寫入到序列化檔案中的。)。

5、要想將父類物件也序列化,就需要讓父類也實現Serializable 介面。

6、Transient 關鍵字的作用是控制變數的序列化,在變數宣告前加上該關鍵字,可以阻止該變數被序列化到檔案中,在被反序列化後,transient 變數的值被設為初始值,如 int 型的是 0,物件型的是 null。

7、伺服器端給客戶端傳送序列化物件資料,物件中有一些資料是敏感的,比如密碼字串等,希望對該密碼欄位在序列化時,進行加密,而客戶端如果擁有解密的金鑰,只有在客戶端進行反序列化時,才可以對密碼進行讀取,這樣可以一定程度保證序列化物件的資料安全。

ArrayList的序列化

在介紹ArrayList序列化之前,先來考慮一個問題:

如何自定義的序列化和反序列化策略

帶著這個問題,我們來看java.util.ArrayList的原始碼

code 3

 

1

2

3

4

5

6

7

public class ArrayList<E> extends AbstractList<E>

        implements List<E>, RandomAccess, Cloneable, java.io.Serializable

{

    private static final long serialVersionUID = 8683452581122892189L;

    transient Object[] elementData; // non-private to simplify nested class access

    private int size;

}

 

筆者省略了其他成員變數,從上面的程式碼中可以知道ArrayList實現了java.io.Serializable介面,那麼我們就可以對它進行序列化及反序列化。因為elementDatatransient的,所以我們認為這個成員變數不會被序列化而保留下來。我們寫一個Demo,驗證一下我們的想法:

code 4

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

public static void main(String[] args) throws IOException, ClassNotFoundException {

        List<String> stringList = new ArrayList<String>();

        stringList.add("hello");

        stringList.add("world");

        stringList.add("hollis");

        stringList.add("chuang");

        System.out.println("init StringList" + stringList);

        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("stringlist"));

        objectOutputStream.writeObject(stringList);

 

        IOUtils.close(objectOutputStream);

        File file = new File("stringlist");

        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file));

        List<String> newStringList = (List<String>)objectInputStream.readObject();

        IOUtils.close(objectInputStream);

        if(file.exists()){

            file.delete();

        }

        System.out.println("new StringList" + newStringList);

    }

//init StringList[hello, world, hollis, chuang]

//new StringList[hello, world, hollis, chuang]

 

瞭解ArrayList的人都知道,ArrayList底層是通過陣列實現的。那麼陣列elementData其實就是用來儲存列表中的元素的。通過該屬性的宣告方式我們知道,他是無法通過序列化持久化下來的。那麼為什麼code 4的結果卻通過序列化和反序列化把List中的元素保留下來了呢?

writeObject和readObject方法

在ArrayList中定義了來個方法: writeObjectreadObject

這裡先給出結論:

在序列化過程中,如果被序列化的類中定義了writeObject readObject 方法,虛擬機器會試圖呼叫物件類裡的 writeObject readObject 方法,進行使用者自定義的序列化和反序列化。

如果沒有這樣的方法,則預設呼叫是 ObjectOutputStream defaultWriteObject 方法以及 ObjectInputStream defaultReadObject 方法。

使用者自定義的 writeObject readObject 方法可以允許使用者控制序列化的過程,比如可以在序列化的過程中動態改變序列化的數值。

來看一下這兩個方法的具體實現:

code 5

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

private void readObject(java.io.ObjectInputStream s)

        throws java.io.IOException, ClassNotFoundException {

        elementData = EMPTY_ELEMENTDATA;

 

        // Read in size, and any hidden stuff

        s.defaultReadObject();

 

        // Read in capacity

        s.readInt(); // ignored

 

        if (size > 0) {

            // be like clone(), allocate array based upon size not capacity

            ensureCapacityInternal(size);

 

            Object[] a = elementData;

            // Read in all elements in the proper order.

            for (int i=0; i<size; i++) {

                a[i] = s.readObject();

            }

        }

    }

 

code 6

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

private void writeObject(java.io.ObjectOutputStream s)

        throws java.io.IOException{

        // Write out element count, and any hidden stuff

        int expectedModCount = modCount;

        s.defaultWriteObject();

 

        // Write out size as capacity for behavioural compatibility with clone()

        s.writeInt(size);

 

        // Write out all elements in the proper order.

        for (int i=0; i<size; i++) {

            s.writeObject(elementData[i]);

        }

 

        if (modCount != expectedModCount) {

            throw new ConcurrentModificationException();

        }

    }

 

那麼為什麼ArrayList要用這種方式來實現序列化呢?

why transient

ArrayList實際上是動態陣列,每次在放滿以後自動增長設定的長度值,如果陣列自動增長長度設為100,而實際只放了一個元素,那就會序列化99個null元素。為了保證在序列化的時候不會將這麼多null同時進行序列化,ArrayList把元素陣列設定為transient。

why writeObject and readObject

前面說過,為了防止一個包含大量空物件的陣列被序列化,為了優化儲存,所以,ArrayList使用transient來宣告elementData。但是,作為一個集合,在序列化過程中還必須保證其中的元素可以被持久化下來,所以,通過重寫writeObject 和 readObject方法的方式把其中的元素保留下來。

writeObject方法把elementData陣列中的元素遍歷的儲存到輸出流(ObjectOutputStream)中。

readObject方法從輸入流(ObjectInputStream)中讀出物件並儲存賦值到elementData陣列中。

至此,我們先試著來回答剛剛提出的問題:

如何自定義的序列化和反序列化策略

答:可以通過在被序列化的類中增加writeObject 和 readObject方法。那麼問題又來了:

雖然ArrayList中寫了writeObject readObject 方法,但是這兩個方法並沒有顯示的被呼叫啊。

那麼如果一個類中包含writeObject readObject 方法,那麼這兩個方法是怎麼被呼叫的呢?

ObjectOutputStream

從code 4中,我們可以看出,物件的序列化過程通過ObjectOutputStream和ObjectInputputStream來實現的,那麼帶著剛剛的問題,我們來分析一下ArrayList中的writeObject 和 readObject 方法到底是如何被呼叫的呢?

為了節省篇幅,這裡給出ObjectOutputStream的writeObject的呼叫棧:

writeObject --->writeObject0 --->writeOrdinaryObject--->writeSerialData--->invokeWriteObject

這裡看一下invokeWriteObject

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

void invokeWriteObject(Object obj, ObjectOutputStream out)

        throws IOException, UnsupportedOperationException

    {

        if (writeObjectMethod != null) {

            try {

                writeObjectMethod.invoke(obj, new Object[]{ out });

            } catch (InvocationTargetException ex) {

                Throwable th = ex.getTargetException();

                if (th instanceof IOException) {

                    throw (IOException) th;

                } else {

                    throwMiscException(th);

                }

            } catch (IllegalAccessException ex) {

                // should not occur, as access checks have been suppressed

                throw new InternalError(ex);

            }

        } else {

            throw new UnsupportedOperationException();

        }

    }

 

其中writeObjectMethod.invoke(obj,new Object[]{ out });是關鍵,通過反射的方式呼叫writeObjectMethod方法。官方是這麼解釋這個writeObjectMethod的:

class-definedwriteObject method, or null if none

在我們的例子中,這個方法就是我們在ArrayList中定義的writeObject方法。通過反射的方式被呼叫了。

至此,我們先試著來回答剛剛提出的問題:

如果一個類中包含writeObject readObject 方法,那麼這兩個方法是怎麼被呼叫的?

答:在使用ObjectOutputStream的writeObject方法和ObjectInputStream的readObject方法時,會通過反射的方式呼叫。


至此,我們已經介紹完了ArrayList的序列化方式。那麼,不知道有沒有人提出這樣的疑問:

Serializable明明就是一個空的介面,它是怎麼保證只有實現了該介面的方法才能進行序列化與反序列化的呢?

Serializable介面的定義:

 

1

2

public interface Serializable {

}

 

讀者可以嘗試把code1中的繼承Serializable的程式碼去掉,再執行code2,會丟擲java.io.NotSerializableException

其實這個問題也很好回答,我們再回到剛剛ObjectOutputStream的writeObject的呼叫棧:

writeObject --->writeObject0--->writeOrdinaryObject--->writeSerialData--->invokeWriteObject

writeObject0方法中有這麼一段程式碼:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

if (obj instanceof String) {

                writeString((String) obj, unshared);

            } else if (cl.isArray()) {

                writeArray(obj, desc, unshared);

            } else if (obj instanceof Enum) {

                writeEnum((Enum<?>) obj, desc, unshared);

            } else if (obj instanceof Serializable) {

                writeOrdinaryObject(obj, desc, unshared);

            } else {

                if (extendedDebugInfo) {

                    throw new NotSerializableException(

                        cl.getName() + "\n" + debugInfoStack.toString());

                } else {

                    throw new NotSerializableException(cl.getName());

                }

            }

 

在進行序列化操作時,會判斷要被序列化的類是否是Enum、Array和Serializable型別,如果不是則直接丟擲NotSerializableException

總結

1、如果一個類想被序列化,需要實現Serializable介面。否則將丟擲NotSerializableException異常,這是因為,在序列化操作過程中會對型別進行檢查,要求被序列化的類必須屬於Enum、Array或者實現Serializable介面類的其中一種。

2、在變數宣告前加上transient關鍵字,可以阻止該變數被序列化到檔案中。

3、在類中增加writeObject 和 readObject 方法可以實現自定義序列化策略

參考資料

Java序列化的高階認識