Monday, September 19, 2005

Object Serialization and DeSerialization

This class writes an int primitive data type, a String and a Date Object which are predefined java class files that implements serializable interface into persistance storage.

import java.io.*;

import java.util.*;

class ObjectSeril {

public static void main(String[] args) {

int t =50000;

try {

FileOutputStream out = new FileOutputStream("theTime.tmp");

ObjectOutputStream s = new ObjectOutputStream(out);

s.writeObject("Today");

// s.writeObject(t); Only Objects

s.writeInt(t);

s.writeObject(new Date());

s.flush();

} catch(IOException e) {

System.out.println(e);

}

try {

FileInputStream out = new FileInputStream("theTime.tmp");

ObjectInputStream s = new ObjectInputStream(out);

String str = (String) s.readObject();

System.out.println(str);

// s.writeObject(t); Only Objects

int in = s.readInt();

System.out.println(in);

Date d = (Date) s.readObject();

System.out.println(d);

s.close();

} catch(IOException e1) {

System.out.println(e1);

}catch(ClassNotFoundException e2) {

System.out.println(e2);

}

}

}

How to Write to an ObjectOutputStream

Writing objects to a stream is a straightforward process.

ObjectOutputStream must be constructed on another stream. This code constructs an ObjectOutputStream on a FileOutputStream.

An ObjectOutputStream can be used to write primitive data types along with Java objects to an OutputStream. An object is serializable(that is its state[ instance variables] can be stored in persistance storage) only if its class implements the Serializable interface. The objects can be read (reconstituted) using an ObjectInputStream. Persistent storage of objects can be accomplished by using a file for the stream.Only objects that support the java.io.Serializable interface can be written to streams. Both String class of java.lang package and Date class of java.util package implements Serializable interface. So only we are able to write both the Objects into persistance storage using Serialization in the above code.

Next, the string Today, an int and a Date object are written to the stream with the writeInt and writeObject method of ObjectOutputStream.

Thus, the writeObject method serializes the specified object, traverses its references to other objects recursively, and writes them all. In this way, relationships between objects are maintained.

ObjectOutputStream implements the DataOutput interface that defines many methods for writing primitive data types, such as writeInt, writeFloat, or writeUTF. You can use these methods to write primitive data types to an ObjectOutputStream.

The writeObject method throws a NotSerializableException if it's given an object that is not serializable. An object is serializable only if its class implements the Serializable interface.


How to Read from an ObjectInputStream

Once you've written objects and primitive data types to a stream, you'll likely want to read them out again and reconstruct the objects. This is also straightforward. Here's code that reads in the String, int and the Date objects that were written to the file named theTime in the previous example:

Like ObjectOutputStream, ObjectInputStream must be constructed on another stream. In this example, the objects were archived in a file, so the code constructs an ObjectInputStream on a FileInputStream. Next, the code uses ObjectInputStream's readObject method to read the String and the Date objects from the file. The objects must be read from the stream in the same order in which they were written. Note that the return value from readObject is an object that is cast to and assigned to a specific type.

The readObject method deserializes the next object in the stream and traverses its references to other objects recursively to deserialize all objects that are reachable from it. In this way, it maintains the relationships between the objects.

ObjectInputStream stream implements the DataInput interface that defines methods for reading primitive data types. The methods in DataInput parallel those defined in DataOutput for writing primitive data types. They include methods such as readInt, readFloat, and readUTF. Use these methods to read primitive data types from an ObjectInputStream.

The following example creates an user-defined class that implements serializable interface, and its Object is Serialized (or) written to persistance storage.

import java.io.*;

import java.util.*;

public class ObjectSeril {

public static void main(String[] args) {

int t =50000;

try {

MyClass mclass = new MyClass(10, 20.5, "vishnu");

System.out.println("MyClass Object:");

System.out.println("~~~~~~~~~~~~~");

System.out.println("Before Serialization:");

System.out.println(mclass + "\n");


FileOutputStream out = new FileOutputStream("theTime");

ObjectOutputStream s = new ObjectOutputStream(out);

s.writeObject(mclass);

s.flush();

} catch(IOException e) {

System.out.println(e);

}

try {

MyClass myclass;

FileInputStream in = new FileInputStream("theTime");

ObjectInputStream s = new ObjectInputStream(in);

myclass = (MyClass) s.readObject();

System.out.println("After Serialization:");

System.out.println(myclass);


s.close();

} catch(IOException e1) {

System.out.println(e1);

}catch(ClassNotFoundException e2) {

System.out.println(e2);

}

}

}

class MyClass implements Serializable {

int in;

transient double d;

String str;

public MyClass(int in, double d, String str) {

this.in = in;

this.d = d;

this.str = str;

}

public String toString() {

return "int value: " + in + " double value: " + d + " string value: " + str;

}

}

Note the primitive type double thats marked with the keyword transient is not written to the persistance storage.

Output


MyClass Object:

~~~~~~~~~~~~~

Before Serialization:

int value: 10 double value: 20.5 string value: vishnu

After Serialization:

int value: 10 double value: 0.0 string value: vishnu

0 Comments:

Post a Comment

<< Home