Serialization in java
I will explain
you what is java serialization, then provide you with a sample for
serialization. Finally most importantly, lets explore what is inside a
serialized object and what it means. That is internals of java
serialization and how does it works. If you want to have your own
implementation of java serialization, this article will provide you good knowledge on Serialization concept.
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.
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.
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.
package
com.serial.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
SerializationEx
implements
Serializable {
private
byte
serializableProp =
10
;
public
byte
getSerializableProp() {
return
serializableProp;
}
}
public
class
SerializationSample {
public
static
void
main(String args[])
throws
IOException,
FileNotFoundException, ClassNotFoundException {
SerializationEx serialB =
new
SerializationEx();
serialize(
"serial.out"
, serialB);
SerializationEx sb = (SerializationEx) 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();
}
}
0 comments:
Post a Comment