Helpex - Trao đổi & giúp đỡ Đăng nhập

Serialization in Java Explained

In this post, we are going to explore an important Java concept frequently asked during Job’s interviews i.e. Java Serialization. We’ll exclusively talk about serialization and will explain its basic to advance level features with the help of code examples and sample project.


Besides knowing about serialization in Java and it’s even more important to learn the different ways to implement serialization. We’ve also addressed them in this post. You probably have faced questions like does serialization require versioning or have performance issues?


So, read this post through the end, we’ve covered all such questions. First of all, go over this generic definition of serialization


Now you can see in the next section is the list of most basic Java serialization questions and answers. It’ll surely increase your knowledge of Java serialization topic.


What is Java Serialization?


Primary purpose of java serialization is to write an object into a stream, so that it can be transported through a network and that object can be rebuilt again. When there are two different parties involved, you need a protocol to rebuild the exact same object again. Java serialization API just provides you that. Other ways you can leverage the feature of serialization is, you can use it to perform a deep copy.


Why I used ‘primary purpose’ in the above definition is, sometimes people use java serialization as a replacement for database. Just a placeholder where you can persist an object across sessions. This is not the primary purpose of java serialization. Sometimes, when I interview candidates for Java I hear them saying java serialization is used for storing (to preserve the state) an object and retrieving it. They use it synonymously with database. This is a wrong perception for serialization.


How do you serialize?


When you want to serialize an object, that respective class should implement the marker interface serializable. It just informs the compiler that this java class can be serialized. You can tag properties that should not be serialized as transient. You open a stream and write the object into it. Java API takes care of the serialization protocol and persists the java object in a file in conformance with the protocol. De-serialization is the process of getting the object back from the file to its original form.


Here protocol means, understanding between serializing person and de-serializing person. What will be the contents of file containing the serialized object? This serves as a guideline to de-serialize. Have a look at the following sample and how its serialized file looks.


Sample Source Code for Java Serialization


Code:


package com.javapapers.sample;


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 java.io.Serializable;


class SerializationBox implements Serializable {


private byte serializableProp = 10;


public byte getSerializableProp() {

return serializableProp;

}

}


public class SerializationSample {


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

FileNotFoundException, ClassNotFoundException {


SerializationBox serialB = new SerializationBox();

serialize("serial.out", serialB);

SerializationBox sb = (SerializationBox) deSerialize("serial.out");

System.out.println(sb.getSerializableProp());

}


public static void serialize(String outFile, Object serializableObject)

throws IOException {

FileOutputStream fos = new FileOutputStream(outFile);

ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(serializableObject);

}


public static Object deSerialize(String serilizedObject)

throws FileNotFoundException, IOException, ClassNotFoundException {

FileInputStream fis = new FileInputStream(serilizedObject);

ObjectInputStream ois = new ObjectInputStream(fis);

return ois.readObject();

}

}


Exploring Java Serialization


Look at following image. After serializing ‘SerializationBox’ in the above sample code, I opened the output in a hex editor. You can use Notepad++ and hex plugin to open the serialized file.

Let us look at contents byte by byte and find out what they are. It starts with “ac ed”. It is is called STREAM_MAGIC. It is a magic number (java API guys says) that is written to the stream header. It denotes that is start of serialzed content.


Serialized Output

Serialization in Java Explained


Similarly every character has a meaning. Actually the serialized file is more bulkier than you would expect, as it has a huge header the meta information of the classes involved and finally the content. Object Serialization Stream Protocol have a look at chapter 6.4.2 Terminal Symbols and Constants. It gives you list of symbols and constants used in serialization.


Thank You!

Happy Reading













0 hữu ích 0 bình luận 749 xem chia sẻ

Có thể bạn quan tâm

loading