`
daqing15
  • 浏览: 40919 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

英语Java学习资料

阅读更多
Serialization is the process of saving an object's state to a sequence of bytes; deserialization is the process of rebuilding those bytes into a live object. The Java Serialization API provides a standard mechanism for developers to handle object serialization. In this tip, you will see how to serialize an object, and why serialization is sometimes necessary. You'll learn about the serialization algorithm used in Java, and see an example that illustrates the serialized format of an object. By the time you're done, you should have a solid knowledge of how the serialization algorithm works and what entities are serialized as part of the object at a low level.
Why is serialization required?

In today's world, a typical enterprise application will have multiple components and will be distributed across various systems and networks. In Java, everything is represented as objects; if two Java components want to communicate with each other, there needs be a mechanism to exchange data. One way to achieve this is to define your own protocol and transfer an object. This means that the receiving end must know the protocol used by the sender to re-create the object, which would make it very difficult to talk to third-party components. Hence, there needs to be a generic and efficient protocol to transfer the object between components. Serialization is defined for this purpose, and Java components use this protocol to transfer objects.

Figure 1 shows a high-level view of client/server communication, where an object is transferred from the client to the server through serialization.
A high-level view of serialization in action
Figure 1. A high-level view of serialization in action (click to enlarge)



How to serialize an object

In order to serialize an object, you need to ensure that the class of the object implements the java.io.Serializable interface, as shown in Listing 1.
Listing 1. Implementing Serializable

import java.io.Serializable;
class TestSerial implements Serializable {
public byte version = 100;
public byte count = 0;
}


In Listing 1, the only thing you had to do differently from creating a normal class is implement the java.io.Serializable interface. The Serializable interface is a marker interface; it declares no methods at all. It tells the serialization mechanism that the class can be serialized.

Now that you have made the class eligible for serialization, the next step is to actually serialize the object. That is done by calling the writeObject() method of the java.io.ObjectOutputStream class, as shown in Listing 2.
Listing 2. Calling writeObject()
public static void main(String args[]) throws IOException {
FileOutputStream fos = new FileOutputStream("temp.out");
ObjectOutputStream oos = new ObjectOutputStream(fos);
TestSerial ts = new TestSerial();
oos.writeObject(ts);
oos.flush();
oos.close();
}


Listing 2 stores the state of the TestSerial object in a file called temp.out. oos.writeObject(ts); actually kicks off the serialization algorithm, which in turn writes the object to temp.out.

To re-create the object from the persistent file, you would employ the code in Listing 3.
Listing 3. Recreating a serialized object
public static void main(String args[]) throws IOException {
FileInputStream fis = new FileInputStream("temp.out");
ObjectInputStream oin = new ObjectInputStream(fis);
TestSerial ts = (TestSerial) oin.readObject();
System.out.println("version="+ts.version);
}


In Listing 3, the object's restoration occurs with the oin.readObject() method call. This method call reads in the raw bytes that we previously persisted and creates a live object that is an exact replica of the original object graph. Because readObject() can read any serializable object, a cast to the correct type is required.

Executing this code will print version=100 on the standard output.
The serialized format of an object

What does the serialized version of the object look like? Remember, the sample code in the previous section saved the serialized version of the TestSerial object into the file temp.out. Listing 4 shows the contents of temp.out, displayed in hexadecimal. (You need a hexadecimal editor to see the output in hexadecimal format.)
Listing 4. Hexadecimal form of TestSerial
AC ED 00 05 73 72 00 0A 53 65 72 69 61 6C 54 65
73 74 A0 0C 34 00 FE B1 DD F9 02 00 02 42 00 05
63 6F 75 6E 74 42 00 07 76 65 72 73 69 6F 6E 78
70 00 64


If you look again at the actual TestSerial object, you'll see that it has only two byte members, as shown in Listing 5.
Listing 5. TestSerial's byte members
public byte version = 100;
public byte count = 0;


The size of a byte variable is one byte, and hence the total size of the object (without the header) is two bytes. But if you look at the size of the serialized object in Listing 4, you'll see 51 bytes. Surprise! Where did the extra bytes come from, and what is their significance? They are introduced by the serialization algorithm, and are required in order to to re-create the object. In the next section, you'll explore this algorithm in detail.
Java's serialization algorithm

By now, you should have a pretty good knowledge of how to serialize an object. But how does the process work under the hood? In general the serialization algorithm does the following:

    * It writes out the metadata of the class associated with an instance.
    * It recursively writes out the description of the superclass until it finds java.lang.object.
    * Once it finishes writing the metadata information, it then starts with the actual data associated with the instance. But this time, it starts from the topmost superclass.
    * It recursively writes the data associated with the instance, starting from the least superclass to the most-derived class.

I've written a different example object for this section that will cover all possible cases. The new sample object to be serialized is shown in Listing 6.
Listing 6. Sample serialized object
class parent implements Serializable {
int parentVersion = 10;
}

class contain implements Serializable{
int containVersion = 11;
}
public class SerialTest extends parent implements Serializable {
int version = 66;
contain con = new contain();

public int getVersion() {
return version;
}
public static void main(String args[]) throws IOException {
FileOutputStream fos = new FileOutputStream("temp.out");
ObjectOutputStream oos = new ObjectOutputStream(fos);
SerialTest st = new SerialTest();
oos.writeObject(st);
oos.flush();
oos.close();
}
}

This example is a straightforward one. It serializes an object of type SerialTest, which is derived from parent and has a container object, contain. The serialized format of this object is shown in Listing 7.
Listing 7. Serialized form of sample object
AC ED 00 05 73 72 00 0A 53 65 72 69 61 6C 54 65
73 74 05 52 81 5A AC 66 02 F6 02 00 02 49 00 07
76 65 72 73 69 6F 6E 4C 00 03 63 6F 6E 74 00 09
4C 63 6F 6E 74 61 69 6E 3B 78 72 00 06 70 61 72
65 6E 74 0E DB D2 BD 85 EE 63 7A 02 00 01 49 00
0D 70 61 72 65 6E 74 56 65 72 73 69 6F 6E 78 70
00 00 00 0A 00 00 00 42 73 72 00 07 63 6F 6E 74
61 69 6E FC BB E6 0E FB CB 60 C7 02 00 01 49 00
0E 63 6F 6E 74 61 69 6E 56 65 72 73 69 6F 6E 78
70 00 00 00 0B


Figure 2 offers a high-level look at the serialization algorithm for this scenario.
An outline of the serialization algorithm
Figure 2. An outline of the serialization algorithm


Let's go through the serialized format of the object in detail and see what each byte represents. Begin with the serialization protocol information:
    * AC ED: STREAM_MAGIC. Specifies that this is a serialization protocol.
    * 00 05: STREAM_VERSION. The serialization version.
    * 0x73: TC_OBJECT. Specifies that this is a new Object.


The first step of the serialization algorithm is to write the description of the class associated with an instance. The example serializes an object of type SerialTest, so the algorithm starts by writing the description of the SerialTest class.

 
  * 0x72: TC_CLASSDESC. Specifies that this is a new class.
    * 00 0A: Length of the class name.
    * 53 65 72 69 61 6c 54 65 73 74: SerialTest, the name of the class.
    * 05 52 81 5A AC 66 02 F6: SerialVersionUID, the serial version identifier of this class.
    * 0x02: Various flags. This particular flag says that the object supports serialization.
    * 00 02: Number of fields in this class.


Next, the algorithm writes the field int version = 66;.

   * 0x49: Field type code. 49 represents "I", which stands for Int.
    * 00 07: Length of the field name.
    * 76 65 72 73 69 6F 6E: version, the name of the field.


And then the algorithm writes the next field, contain con = new contain();. This is an object, so it will write the canonical JVM signature of this field.
    * 0x74: TC_STRING. Represents a new string.
    * 00 09: Length of the string.
    * 4C 63 6F 6E 74 61 69 6E 3B: Lcontain;, the canonical JVM signature.
    * 0x78: TC_ENDBLOCKDATA, the end of the optional block data for an object.


The next step of the algorithm is to write the description of the parent class, which is the immediate superclass of SerialTest.
    * 0x72: TC_CLASSDESC. Specifies that this is a new class.
    * 00 06: Length of the class name.
    * 70 61 72 65 6E 74: SerialTest, the name of the class
    * 0E DB D2 BD 85 EE 63 7A: SerialVersionUID, the serial version identifier of this class.
    * 0x02: Various flags. This flag notes that the object supports serialization.
    * 00 01: Number of fields in this class.


Now the algorithm will write the field description for the parent class. parent has one field, int parentVersion = 100;.

   * 0x49: Field type code. 49 represents "I", which stands for Int.
    * 00 0D: Length of the field name.
    * 70 61 72 65 6E 74 56 65 72 73 69 6F 6E: parentVersion, the name of the field.
    * 0x78: TC_ENDBLOCKDATA, the end of block data for this object.
    * 0x70: TC_NULL, which represents the fact that there are no more superclasses because we have reached the top of the class hierarchy.


So far, the serialization algorithm has written the description of the class associated with the instance and all its superclasses. Next, it will write the actual data associated with the instance. It writes the parent class members first:
    * 00 00 00 0A: 10, the value of parentVersion.


Then it moves on to SerialTest.

   
* 00 00 00 42: 66, the value of version.


The next few bytes are interesting. The algorithm needs to write the information about the contain object, shown in Listing 8.
Listing 8. The contain object

contain con = new contain();


Remember, the serialization algorithm hasn't written the class description for the contain class yet. This is the opportunity to write this description.

   * 0x73: TC_OBJECT, designating a new object.
    * 0x72: TC_CLASSDESC.
    * 00 07: Length of the class name.
    * 63 6F 6E 74 61 69 6E: contain, the name of the class.
    * FC BB E6 0E FB CB 60 C7: SerialVersionUID, the serial version identifier of this class.
    * 0x02: Various flags. This flag indicates that this class supports serialization.
    * 00 01: Number of fields in this class.


Next, the algorithm must write the description for contain's only field, int containVersion = 11;.

    * 0x49: Field type code. 49 represents "I", which stands for Int.
    * 00 0E: Length of the field name.
    * 63 6F 6E 74 61 69 6E 56 65 72 73 69 6F 6E: containVersion, the name of the field.
    * 0x78: TC_ENDBLOCKDATA.


Next, the serialization algorithm checks to see if contain has any parent classes. If it did, the algorithm would start writing that class; but in this case there is no superclass for contain, so the algorithm writes TC_NULL.

    * 0x70: TC_NULL.

Finally, the algorithm writes the actual data associated with contain.

    * 00 00 00 0B: 11, the value of containVersion.

Conclusion

In this tip, you have seen how to serialize an object, and learned how the serialization algorithm works in detail. I hope this article gives you more detail on what happens when you actually serialize an object.

没有时间去翻译,再说自己英文是菜鸟,在这里标志一下,狠学英语!

文章出处:http://www.javaworld.com/community/node/2915
分享到:
评论

相关推荐

    java学习资料 英文版

    在学习过程中,结合英文版的《Thinking in Java》,不仅可以锻炼编程技术,还能提升英语阅读能力,对于想要在全球范围内发展的程序员来说,这是一笔宝贵的财富。因此,无论是初学者还是经验丰富的开发者,都应该珍视...

    英文版JAVA学习教程

    根据提供的文件信息,我们可以从中提炼出关于Java Servlet API Specification的相关知识点。下面将详细介绍这些知识点。 ### Java Servlet API Specification概述 #### 版本信息 - **版本号:** 2.4 - **状态:** ...

    Java学习资料 英文版

    Java学习资料英文版"Thinking in Java Fourth Edition"是Java学习者推荐的重要文献,由Bruce Eckel所著,他是MindView, Inc.的总裁。 在Bruce Eckel的著作中,他详细地介绍Java的编程思想,强调面向对象的程序设计...

    英语单词学习软件(Java开发)

    【标题】"英语单词学习软件(Java开发)"所涉及的知识点主要集中在Java编程语言、软件开发和英语学习工具的设计上。这个项目是为初学者提供一个实践平台,让他们能够运用Java技能开发出一个实用的应用。 1. **Java...

    Java学习书籍资源

    学习Java语言的书籍和教材,有中文的和英文的,仅供参考。

    java 英语学习词汇

    根据提供的文件信息,我们可以整理出一系列与Java编程语言及其英语术语相关的知识点。下面将对这些词汇进行详细解读,以便更好地理解和掌握Java编程的基础概念及专业术语。 ### A. 数组(Array) 数组是一种基本的...

    Thinking in java学习资料

    Thinking in java第三版中文版,第四版英文版以及对应的习题答案! 博文链接:https://easonfans.iteye.com/blog/456901

    学习Java时候必须学习的英语

    总的来说,学习Java英语不仅可以提高阅读英文技术文档的能力,还能帮助开发者更快地学习新技术,跟上开源社区的步伐。通过记忆和实践,将Java英语融入日常编程习惯,你的编程技能将会得到显著提升。在日后的学习和...

    基于用Java实现的一个英语学习APP.rar

    基于用Java实现的一个英语学习APP.rar 功能点 背单词 这个功能点有点难搞,词库应该是没有什么办法了,现在手头上的数据就只有人人词典上的那些单词,而且存储方式也是一个问题,所有数据都是以 JSON 方式存储的,...

    JAVA学习之路:不走弯路,就是捷径(一)

    ### JAVA学习之路:不走弯路,就是捷径(一) #### 1. 前言与背景 本文旨在为那些即将踏上或已经走在JAVA学习道路上的朋友提供一系列实用且宝贵的建议,帮助大家避免不必要的弯路,使学习过程更加高效、顺畅。 ####...

    JAVA学习方法JAVA学习方法JAVA学习方法

    ### JAVA学习方法详解 在当前信息技术飞速发展的背景下,JAVA作为一种重要的编程语言,在软件开发、系统构建等领域扮演着举足轻重的角色。对于初学者来说,掌握正确的JAVA学习方法至关重要。下面将根据提供的标题和...

    java学习心得java学习心得

    ### Java学习心得 在深入探讨Java的学习过程中,我们首先需要明确Java作为一种广泛使用的编程语言,在软件开发、Web应用、移动应用(尤其是Android平台)等领域都有着举足轻重的地位。接下来,我们将围绕“Java学习...

    10来本java学习书籍

    标题中的"10来本java学习书籍"表明这是一个关于Java编程语言的学习资源集合,而描述中的重复信息似乎是为了强调书籍的数量。从标签中我们可以看出,这些书籍不仅涵盖了Java语言的基础和进阶,还涉及到"算法"这一核心...

    JAVA编程学习的正确顺序

    JAVA编程学习的正确顺序 JAVA编程学习的正确顺序是指学习JAVA及相关技术的步骤和方法,本文将从四个阶段对JAVA编程学习的正确顺序进行详细的解释。 第1阶段:掌握JAVA语言基础知识 在学习JAVA编程之前,需要掌握...

    Java学习资料

    这份"Java学习资料"包含了两个核心的资源,可以帮助初学者及有经验的开发者深入理解和提升Java编程技能。 首先,"Java白皮书初始最新版(Java10).pdf"提供了Java语言的规范,这是Java开发的基础。白皮书详细阐述了...

    java学习必备1200单词

    java学习中所需的1200单词 1 file n 文件;v 保存文件 2 command n 命令 指令 3 use v 使用 用途 4 program n 程序 5 line n 数据 程序 行 线路 6 if conj 如果 7 display vt 显示 显示器 8 set v 设置 n ...

    java后端学习课程整理

    JAVA 后端学习课程总结 JAVA 是一种广泛使用的编程语言,广泛应用于 Android 应用程序开发、Web 应用程序开发、桌面应用程序开发等领域。JAVA 后端开发是指使用 JAVA 语言开发服务器端应用程序,例如 Web 服务、...

    java学习必读书目

    以下是一些推荐的Java学习必读书目及其详细解析: 1. **《Java2核心技术(第6版)卷I:基础知识》** - 由Cay S. Horstmann和Gary Cornell合著,这是一本经典的Java入门书籍。它详细介绍了Java的基础概念,包括语法、...

Global site tag (gtag.js) - Google Analytics