Monday, August 4

Concept of Serialization

 What is Serialization?

Serialization is an API for encoding an objects as byte-stream and reconstructing objects from their byte-stream encodings. The serializing is process of encoding objects as byte-stream and reverse process is called deserializing.

Implementation Of Serializable

Implementation Serializable to an object allow's class instances to be serialized can be as simple as adding the words 'implements Serializable' to its declaration.


 Code Snippet :

public class SerializableObject implements java.io.Serializable{
public String name;
public String address;
//This Property Will not persist as it's mark as transient property.
public transient String age;
}

What is Serialize?

Serializable is a marker interfaces that tells the JVM it can write out the state of the object to some stream (basically read all the members, and write out their state to a stream, or to disk or something). The default mechanism is a binary format. You can also use it to clone things, or keep state between invocations, send objects across the network etc. 
 Note : Do not implement Serializable lightly, since it restricts future flexibility, and publicly exposes class implementation details which are usually private. As well, implementing Serializable correctly is not trivial.
The ObjectOutputStream class and the ObjectInputStream class implement the ObjectOutput interface and the ObjectInput interface, respectively, providing methods to write and read binary representation of objects as well as Java primitive values. An overview of how these classes can be chained to underlying streams and some selected methods they provide. The figure does not show the methods inherited from the abstract OutputStream and InputStream superclasses.



Serializing and deserializing an object.

//Reading and Writing Objects
import java.io.EOFException;
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.util.Arrays;
public class ObjectSerializationDemo {
void writeData() {
try {
// Set up the output stream:
FileOutputStream outputFile = new FileOutputStream("obj-storage.dat");
ObjectOutputStream outputStream = new ObjectOutputStream(outputFile);
// Write data:
String[] strArray = {"Seven", "Eight", "Six"};
long num = 2008;
int[] intArray = {1, 3, 1949};
String commonStr = strArray[2];
outputStream.writeObject(strArray);
outputStream.writeLong(num);
outputStream.writeObject(intArray);
outputStream.writeObject(commonStr);// "Six"
// Flush and close the output stream:
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
System.err.println("File not found: " + e);
} catch (IOException e) {
System.err.println("Write error: " + e);
}
}
void readData() {
try {
// Set up the input stream:
FileInputStream inputFile = new FileInputStream("obj-storage.dat");
ObjectInputStream inputStream = new ObjectInputStream(inputFile);
// Read the data:
String[] strArray = (String[]) inputStream.readObject();
long num = inputStream.readLong();
int[] intArray = (int[]) inputStream.readObject();
String commonStr = (String) inputStream.readObject();
// Write data to the standard output stream:
System.out.println(Arrays.toString(strArray));
System.out.println(Arrays.toString(intArray));
System.out.println(commonStr);
// Close the stream:
inputStream.close();
}catch (FileNotFoundException e) {
System.err.println("File not found: " + e);
}catch (EOFException e) {
System.err.println("End of stream: " + e);
}catch (IOException e) {
System.err.println("Read error: " + e);
}catch (ClassNotFoundException e) {
System.err.println("Class not found: " + e);
}
}
public static void main(String[] args) {
ObjectSerializationDemo demo = new ObjectSerializationDemo();
demo.writeData();
demo.readData();
}
}

 






No comments:

Post a Comment

Ads Inside Post