Java serialization – Algorithm Behind It

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. In order to be serializable the class has ti implement marker interface Serializable.

The serialized format of an object

What does the serialized version of the object look like? What bytes get stored once you serialize an object.
This is a simple Serializable ‘SerializationExample’ class which extends another Serializable class ‘Parent’.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class SerializationExample extends Parent implements Serializable  {
 
	private static final long serialVersionUID = 20;
	public int var = 22;
 
 
	public static void main(String[] args) throws Exception{
		SerializationExample se = new SerializationExample();
		File file = new File("c:/downloads/class.ser");
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
		oos.writeObject(se);
		oos.close();
	}
 
 
}
class Parent implements Serializable{
	public int parentvar = 42;
}

Once the object is serialized we can see its hexadecimal values using any hexadecimal editor

Lets analyze each hexadecimal value one by one :

ac ed : Specifies that this is a serialization protocol.
00 05: STREAM_VERSION. The serialization version.
0x73: TC_OBJECT. Specifies that this is a new Object.
0x72: TC_CLASSDESC. Specifies that this is a new class.
00 14: Length of the class name (20 characters – 16*1 + 4)
53 65 72 69 61 6c 69 7a 61 74 69 6f 6e 45 78 61
6d 70 6c 65
: SerializationExample, the name of the class.
00 00 00 00 00 00 00 14 : SerialVersionUID, the serial version identifier of this class.16*1+4 = 20
0x02: Various flags. This particular flag says that the object supports serialization.
00 01: Number of fields in this class.
0x49: Field type code. 49 represents “I”, which stands for Int.
00 03: Length of the field name.
76 61 72: var, the name of the field
0x78: TC_ENDBLOCKDATA, the end of the optional block data for an object

Now definition of parent class will start

0x72: TC_CLASSDESC. Specifies that this is a new class.
00 06 : Length of name of parent class
50 61 72 65 6e 74 : Name of parent class,Parent
Next few bytes : SerialVersionUID
0x02: Various flags. This particular flag says that the object supports serialization.
00 01: Number of fields in this class.
0x49: Field type code. 49 represents “I”, which stands for Int.
00 09: Length of the field name.
70 61 72 65 6e 74 76 61 72: parentvar, the name of the field
0x78: TC_ENDBLOCKDATA, the end of the optional block data for an object
0x70: TC_NULL, which represents the fact that there are no more superclasses because we have reached the top of the class hierarchy
00 00 00 2a: value of variable parentvar
00 00 00 16: value of variable var

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *