persistence by having its properties or fields and state information saved and
restored to and from storage.
why Serialization ?
The serialization mechanism has been added into the Java language for two reasons:
(1) the JavaBeans mechanism uses serialization.
(2) remote method invocation (RMI) allows you to automatically use objects located at another host in the network just like any local objects.
---------------------------------------------------------------------------------------------
How to make a class or a bean serializable?
By implementing the java.io.Serializable interface.
There is no method in the Serializable interface. The Serializable
interface acts as a marker, telling the object serialization
tools that your class is serializable
or
By implementing the java.io.Externalizable interface .
There are two methods in the Externalizable interface. You have
to implement these two methods in order to make your class
externalizable. These two methods are readExternal() and writeExternal().
---------------------------------------------------------------------------------------------
What is the difference between Serializalble and Externalizable interface?When you use Serializable interface, your class is serialized automatically
by default. But you can override writeObject() and readObject() two methods to
control more complex object serailization process. When you use Externalizable
interface, you have a complete control over your class’s serialization
process(for example, when writing and reading a specific file format).
Serialization is a Marker interface -Marker Interface is used by java runtime engine (JVM) to identify the class for special processing.
Use serialization when you need to add data to the serialization stream
that is not an object data member.
Externalization allows you to customize how serialization is done. By implementing externalization you are controlling what gets serialized ( and what doesnot ) as versus default serialization where all non-transient attributes get serialized.
For “fat data” classes with a large number of attributes only a few of which needs to persisted, externalization will help you reduce the size of serialized stream and the time taken to serialize the object. But there will be an overhead involved because the runtime has to call your methods to read/write objects.
Performance issue –
1. Further more if you are subclassing your externalizable class you will want to invoke your superclass’s implementation. So this causes overhead while you subclass your externalizable class.
2. methods in externalizable interface are public. So any malicious program can invoke which results into lossing the prior serialized state.
---------------------------------------------------------------------------------------------
Note : If you don’t want some field not to be serialized, you can mark
that field transient or static.
---------------------------------------------------------------------------------------------
Example for Serializable :
import java.io.Serializable;
public class PersonDetails implements Serializable {
private String name;
private int age;
public PersonDetails(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}
import java.io.*;
class SerializeandDeserialize
{
// create a Persondetails and serialize it to a file persondetails.txt
private static void serializeObject()
{
PersonDetails amir = new PersonDetails();
try
{
FileOutputStream fos = new FileOutputStream("persondetails.txt");
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(amir);
os.close();
fos.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
// For Deserialized object from a file persondetails.txt
private static void deserializeObject()
{
PersonDetails amir = null;
try
{
FileInputStream fis = new FileInputStream("persondetails.txt");
ObjectInputStream is = new ObjectInputStream(fis);
amir = (PersonDetails)is.readObject();
is.close();
fis.close();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println("Name : "+amir.getName());
System.out.println("Age : "+amir.getAge());
}
public static void main(String arg[])
{
serializeObject();
deserializeObject();
}
}
Example for Externizable:
import java.io.*;
public class PersonDetails_Ext implements Externalizable {
private String name;
private int age;
public PersonDetails_Ext(String name, int age) {
this.name = name;
this.age = age;
}
public PersonDetails_Ext() {
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void writeExternal(ObjectOutput out)
throws IOException
{
out.writeObject(this.name);
out.writeInt(this.age);
}
public void readExternal(ObjectInput in)
throws IOException, ClassNotFoundException
{
this.name = (String)in.readObject();
this.age = in.readInt();
}
}
import java.io.*;
class SerializeandDeserialize1
{
// create a PersonDetails_Ext and serialize it to a file persondetails.txt
private static void serializeObject()
{
PersonDetails_Ext amir = new PersonDetails_Ext("Amirtharaj", 27);
try
{
FileOutputStream fos = new FileOutputStream("persondetails.txt");
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(amir);
os.close();
fos.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
// For Deserialized object from a file persondetails.txt
private static void deserializeObject()
{
PersonDetails_Ext amir = null;
try
{
FileInputStream fis = new FileInputStream("persondetails.txt");
ObjectInputStream is = new ObjectInputStream(fis);
amir = (PersonDetails_Ext)is.readObject();
is.close();
fis.close();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println("Name : "+amir.getName());
System.out.println("Age : "+amir.getAge());
}
public static void main(String arg[])
{
serializeObject();
deserializeObject();
}
}
No comments:
Post a Comment