Saturday, January 9, 2010

Object Serialization

Categories: ,

Quite simply, object serialization provides a program the ability to read or write a whole object to and from a raw byte stream. It allows Java objects and primitives to be encoded into a byte stream suitable for streaming to some type of network or to a file-system, or more generally, to a transmission medium or storage facility.
          To do I/O on a text file for the purpose of storing and loading data. The parsing these files always seems to reduce to a basic set of hurdles that you must get over: Did I reach the EOF marker? Is the file pointer on top of the integer or the string? What happens when I reach the EOLN mark, and what if the marker is missing? Now consider a situation where a programming language suddenly made these problems go away? Object serialization has taken a step in the direction of being able to store objects instead of reading and writing their state in some foreign and possibly unfriendly format.
        Serializing an object requires only that it meets one of two criteria. The class must either implement the Serializable interface (java.io.Serializable) which has no methods that you need to write or the class must implement the Externalizable interface which defines two methods. As long as you do not have any special requirements, making a serializable is as simple as adding the ``implements Serializable'' clause.
         An important modifier that your can use for object data is the transient  keyword. If a field in a class is marked as transient, it will automatically be skipped when serializing any objects that are instances of that class. This is very useful for references that are ``relative to an address space'' they were created in, such as stream objects, file references and network connections. This is a key attribute of a class that should be considered when attaching the Serialization or Externalizable interface to a class you are writing.
         One thing to remember about de-serializing an object is that the class must have a public constructor that takes no arguments. This is to allow the serialization mechanism to create an empty object and then fill in the data.
          The serialization mechanism uses an identification value to keep track of all classes. This identifier, called the serialVersionUID (SUID), is computed from the structure of the class file to form a unique 64-bit identifier. It is possible to explicitly indicate which serialVersionUID your class is compatible with. In plain English, you can tell the serialization mechanism which version of your class you are compatible with.


static final long serialVersionUID = 7110175216435025451L;
If you include this in the class data, the serialization mechanism will understand which past version of the class you intend to be compatible with the current implementation.



Applications

There are already numerous projects in progress that will take advantage of the serialization process, two of which have resulted in Java 1.1 features. One is remote method invocation (RMI)  where objects seem to be transparently transported over the network and reconstituted for use on a machine other than the one it originally was created on. RMI is the rough equivalent of the remote procedure call(RPC) , done ``object-oriented'' style. In effect, with minimal code overhead, you can invoke methods on objects which exist on a machine which your program has access to via a network. Object serialization plays a key role in being able to pass arguments and return values over the network in a seemingly transparent manner.
A second application of serialization appears in the implementation of JavaBeans. The designers wanted it to be easy and intuitive to have Beans save their state and then be recalled with that state at a later date. For this reason, all Beans must be serializable, implementing either the Serializable or Externalizable interface.

Conclusion

Object serialization is not a technology that will impress an audience during a presentation, nor will it boost product sales all by itself. Serialization is a building block that will allow the creation of potentially earth-shaking technologies for Java. We have seen some of these technologies, like JavaBeans and RMI, surface already
Example:

import java.io.*;

  public class SerializingObject{
  
  
public static void main(String[] argsthrows IOException{
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Please enter File name : ");
  
    
String file = in.readLine();

   
System.out.print("Enter extention : ");
  
    
String ext = in.readLine();
 
  
String filename = file + "." + ext;

   
File f = new File(filename);
 
  
try{
  
   
ObjectOutput ObjOut = new ObjectOutputStream(new FileOutputStream(f));

     
ObjOut.writeObject(f);
 
    
 ObjOut.close();

     
System.out.println("Serializing an Object Creation completed successfully.");
  
    
}
   
catch(IOException e){
  
   
System.out.println(e.getMessage());

      }
  
}


  }


Output of the Program:

C:\nisha>javac SerializingObject.java

C:\nisha>java SerializingObject
Please enter File name : Filterfile
Enter extention : txt
Serializing an Object Creation is completly Successfully.

C:\nisha>



Spread The Love, Share Our Article

Related Posts

No Response to "Object Serialization"

Post a Comment