Serialization and DeSerialization and Externilization understanding



2.          Introduction

  • The process of an Object from Simple Java supported from to File supported form or  network supported form call Serialization
  • The process of a File supported form or network supported form to Object supported from call De-Serialization from Simple Java

2.1.        Purpose

·         Big Balloon Carry from one place to another place is a good example of Serilization and deserilization
·         Same things pass one java supported form to File supported form

2.2.        Scope, Approach and Methods

2.2.1.         What is the process of Serilization?

·          File Output Stream
·         Object Output Stream
·         Object Output stream one method writeObjet( pass the object);

2.2.1.1.      How to do Serilization process?

Class Login  implements serializable{
int userId=10;
int userPassword=20;
}

Class SerilizationDemo extend Login{
Public static void main(String …args){
Login login=new Login();
FileOutPutStream fos=new FileOutPutStream(“abc.ser”);
ObjectOutPutStream oos=new ObjectOutPutStream(fos);
Oos.writeObject(login);
}
}

 












2.2.1.2.      Important Rules?

Ø  If Login class not implements serializable then this is compile properly but run time got exception   like Exception in thread “main” java.io.notSerializableException: Login
Ø  If Login class is implements serializable then this is compile and run properly
Ø  If Login class any variable is transient modifier used then what happen?
Class Login  implements serializable{
int userId=10;
 transient int userPassword=20;
}

Then JVM will take care and not save exact value,  but save the default value in file
Transient means not serialize.

Ø  Static with transient vs Serialization
Class Login  implements serializable{
int userId=10;
transient static int userPassword=20;
}
Then Static variable is not a part of object level. It is the part of class level. But serialization is the part of object level. So when class is loaded that time static variable object reference creates and you can use in other place. But object level you can use with out de serialization. So static variable is not part of serialization so if use static with transient then it is not impact at all.


Ø  Final with transient vs Serialization
Class Login  implements serializable{
transient final int userId=10;
 int userPassword=20;
}
In serialization every final variable directly participate by value. So final variable you can’t change the data. So if we use final variable with transient modifier then this is not impact to final variable because final variable directly participate to serialization.

Ø  Enhance with Eample
Declare
Output
Int userId =10;
Int userPassword=20;
10…. 20
Transient Int userId =10;
Int userPassword=20;
0.….20
Transient static Int userId =10;
Int userPassword=20;
10……20
Transient final Int userId =10;
Int userPassword=20;
10 ……20
Static Int userId =10;
Int userPassword=20;
10 ……20
Final Int userId =10;
Int userPassword=20;
10……20

Ø  If we don’t know the sequence object then what process we flow?
Then we use to read object by using
Object o =ois.readObject();
If(O instance Class){
}

2.2.1.3.      Customized Serilization process?


Ø  Why you will do the customized serialization?
To recover the loss of information using transient key word then we go to customized serialization
Ø  How to create customized serialization?
o   Private void writeObject(ObjectOutputStream oos) throws Exception
o   Private void readObject(ObjectInputStream ins) throws Exception
So this two method is callback Which will take care by JVM
Ø  Example of Cutomized Serilization?
Class Login  implements serializable{
int userId=10;
transient int userPassword=20;
private void writeObject(ObjectOutputStream oos) throws Exception{
oos.defaultWriteObject(); //means for default serialization
String newUserPassword=encryptedPassword(userPassword);
oos.writeObject(newUserPassword);

}
private void readObject(ObjectOutputStream ois) throws Exception{
ois.defaultReadObject();//means for default De-serilization
String newUserPassword=(String)ois.readObject();
userPassword=decryptedPassword(userPassword);
}

Private String encryptedPassword(String pass){
  pass=pass+”Kartik”;
return pass;
}
Private String decryptedPassword (String pass){
  return =pass.substring(0, pass.length()-6);
}

}

Class SerilizationDemo extend Login{
Public static void main(String …args){
Login login=new Login();
FileOutPutStream fos=new FileOutPutStream(“abc.ser”);
ObjectOutPutStream oos=new ObjectOutPutStream(fos);
oos.writeObject(login);

FileInputStream fis=new FileInputStream(“abc.ser”);
ObjectInputStream ois=new ObjectInputStream(fis);
Login d2=(Login)ois.readObject();

}
}


2.2.1.4.      De-Serilization Understanding?

Ø  What is the process of De-Serialization?
o   File Input Stream
o   Object Input Stream
o   Object input stream one method readObjet();
Ø  Example of De-Serilization?
Class Login  implements serializable{
int userId=10;
int userPassword=20;
}

Class DeSerilizationDemo extend Login{
Public static void main(String …args) throws throwable{
Login d=new Login();
FileInputStream fis=new FileInputStream(“abc.ser”);
ObjectInputStream ois=new ObjectInputStream(fis);
Login d2=(Login)ois.readObject();
}
}

2.2.1.5.      Inheritance with serilization Understanding?


Ø  If Parent class is serializable but Child is not serialization then what happen?
Then program is execute properly because parent is serializable so every child by default serializable.
Ø  If Parent class is not serializable but Child class is serializable then what happen?
o   Parent class is not Serializable. If any instance variable inheriting from non serializable  area then JVM ignore original value and put the default value
o   in deserialization time you got instance –control flow:
§  identified of instance variable which is non serializable
§  Execution of non serializable instance variable assignment to de-seriazable
§  Execution of no argument constructor
§  If run time no argument constructor is not found then got one exception like InvalidClassCustException
§  Example
Class Animal{
Int i=10;
Animal(){
Syso(“Animal Constructor called”);
}
}

Class Dog extends Animal implements seriazable{
Int j=10;
Public static void main(String …args){
Dog d=new Dog();
d.i=200;
d.j=400
FileOutPutStream fos=new FileOutPutStream(“abc.ser”);
ObjectOutPutStream oos=new ObjectOutPutStream(fos);
Oos.writeObject(d);

FileInputStream fis=new FileInputStream(“abc.ser”);
ObjectInputStream ois=new ObjectInputStream(fis);
Dog d2=(Dog)ois.readObject();

}
}

Out put Step:
i=10,j=10,   Ã after assign i=200,j=400--à after serialization  i=0, j=400à after de-serilization Ã  i=10, j=400


2.2.2.         What is the process of Externalization?

Ø  If you save total object to a file then call to Serilization and if you need part of the object to a file then call Externilization
Ø  Serilization will take care by JVM but Externilization will take care by Programmer
Ø  Performance wise Externilization is better then Serilization
Ø  Externalization there should be must have no-arguments constructor but in serilization it is not requird
Ø  So when we call Deserilization then JVM call to no-argument constructor  after then call to De-serilization methods
Ø  Tresient key word not play the role in Externilization

2.2.2.1.      How to do Externilization process?

Ø  Example of  Externilization
class Result implements Externilization{
String name;
int roleNo;
int marks
public result(){}
public result(String name,int roleNo,int marks){
this.name=name;this.roleNo=roleNo;this.marks=marks;}

Public void writeExternal(ObjectOutput out) throws IOE {
out.setObject(this.name);
out.setInt(this.marks);
}
Public void readExternal(ObjectInput in) throws IOE,ClassNotFound {
name=(String)in.readObject();
marks=in.readInt();
}
}

After than do as it is same of Seilization and deserilization process like
Class SerilizationDemo extend Login{
Public static void main(String …args){
Result login=new Result ();
FileOutPutStream fos=new FileOutPutStream(“abc.ser”);
ObjectOutPutStream oos=new ObjectOutPutStream(fos);
Oos.writeObject(login);

FileInputStream fis=new FileInputStream(“abc.ser”);
ObjectInputStream ois=new ObjectInputStream(fis);
Result d2=( Result)ois.readObject();

}
}

Ø  Difference between Serilization and Externalization
Serialization
Externalization
  • It is meant for Default Serialization
  • Here everything take care by JVM and programmer doesn’t have any control
  • In serialization total object will be saved to the file always whether it is required or not.
  • Relatively performance is low
  • Serialization is best choice if we want to save total object to the file
  • It doesn’t contain any method. It is Marker Interface
  • It’s implemented class not required to contain public no-argument constructor.
  • Transient key is a vital things in serialization
  • It is meant for Customized serialization
  • Here everything takes care by programmer and JVM doesn’t have any control.
  • In Externilization based on our required. We can save either total object or partial object.
  • Relatively performance is high
  • It is the best choice when if want to save part of the object to a file
  • It contains two method writeExternal() and readExternal(). So it is not a marker interface
  • It’s implanting time it is mandatory to create a no-argument constructor. Otherwise we will get run time exception saying InvalidClassException
  • This place transient key word is not play any role

2.2.3.         Understand of SerilizationVersionUid?

Ø  What is SerilizationVersionUid?
o   when we used serialization any place then so highly recommended to add SerilizationVersionUid to overcome of many issue.

o   When sender send an object data to receiver. Then first create serialization of that object and save to a file.
o   Now receiver read this object by using De-serialization and convert file to object.
Ø  Example
Sender
Object Ã  File
If any modification of .class file in receiver side or jvm
File Ã  Object
receiver
Object A
Object B
Object C
Object d with default serilize version id

Proper save
Proper save
Proper save
Proper save
No
Yes (modification)
Yes (JVM)
no issue if you changes jvm or .class file by default class will matching by Serilal Version Uid
No issue
Issue InvalidClassException
May be/May not be
No issue
Correct data
Fail
Unambiguous answer
Correct data with default serilize version id



Example 1 Serialization And De-Serialization :







Previous
Next Post »