❮ Previous Next ❯

Java Customized Serialization

During default serialization there may be a chance of loss of information because of transient keyword.

Example

import java.io.*;
class Account implements Serializable
{
String uname="Silan";
transient String pass="Software";
}
class CustomizeDemo
{
public static void main(String args[])throws Exception
{
Account a1=new Account();
System.out.println(a1.uname+"…."+a1.pass);
FileOutputStream fos=new FileOutputStream("abc.ser");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(a1);
FileInputStream fis=new FileInputStream("abc.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
Account a2=(Account)ois.readObject();
System.out.println(a2.uname+"…."+a2.pass);
}
}
output:-

Silan Software
Silan null

In the above example before serialization Account object can provide proper username and password but after deserialization Account object can provide only username but not password,this is due to declaring password variable as transient.

Hence during default serialization there may be a chance of loss of information because transient keyword.

To recover this loss of information we should go for customized serialization.

We can implement customized serialization by using the following methods:-

Note:-

For example while performing Account object serialization if we required to do extra work in the account class we have to define above methods.

Example

import java.io.*;
class Account implements Serializable
{
String username="Silan";
transient string pwd="Software";
private void writeObject(ObjectOutputStream os)throws Exception
{
os.defaultWriteObject(); //in addition to execute method do default serialization
String epwd="abc"+pwd; //prepare encrypted password and write into a file
os.writeObject(epwd);
}
private void readObject(ObjectInputStream is)throws Exception
{
is.defaultReadObject();
String epwd=(String)is.readobject();
pwd=epwd.Substring();
}
}
class CustSerializeDemo1
{
public static void main(String[] args)throws Exception
Account a1=new Account();
System.out.println(a1.username+"..."+a1.pwd);
FileOutputStream fos=new FileOutputStream("abc.ser");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(a1);
FileInputStream fis=new FileInputStream("abc.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
Account a2=(Account)ois.readObject();
System.out.println(a2.username+"..."+a2.pwd);
}
}

On the above program before serialization and after serialization Account object can provide proper username and password.

Note:

Programmer can't call private methods directly from outside of the class,but JVM can cal call private methods directly from outside of the class.

Example

import java.io.*;
class Account implements Serializable
{
String username="Silan";
transient String pwd="Software";
transient int pin=1234;
private void writeObject(ObjectOutputStream os)throws Exception
{
os.defaultWriteObject();
String epwd="123"+pwd;
int epin=4444+pin;
os.WriteInt(epwd);
os.WriteInt(epin);
}
private void readObject(ObjectOutputStream is)throws Exception
{
is.defaultReadObject();
String epwd=(String)is.readObject();
pwd=epwd.subString(3);
int epin=is.readInt();
pin=epin-4444;
}
}
class CustSerializeDemo2
{
public static void main(String args[])throws Exception
{
Account a1=new Account();
system.out.println(a1.username+"..."+a.pwd+"..."+a1.pin);
FileOutputStream fos=new FileOutputStream("abc.ser");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.WriteObject(a1);
FileInputStream fis=new FileInputStream("abc.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
Account a2=(Account)ois.readObject();
system.out.println(a2.username+"..."+a2.pwd+"..."+a2.pin);
}
}
❮ Previous Next ❯


  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved