All Packages  Class Hierarchy  This Package  Previous  Next  Index

Interface Acme.Serializable

public interface Serializable
extends Versioned
Interface for serializable objects.

This implementation of serialization is much simpler and less convenient to use than the official one that will be in JDK1.1; but it has the significant albeit temporary advantage that it works in current browsers.

One thing this version does not do is handle cyclic graphs of objects. It only handles tree-shaped graphs. If you need to serialize more complicated structures, consider using an ID-based scheme - instead of having your objects contain actual references to other objects, have them contain IDs which can be translated into real references by an ID-manager class. This scheme also has the advantage that you don't have to deserialize the entire graph all at once, you can do it piece by piece as needed, and you can even set up a least-recently-used flush policy in the ID-manager.

There are five simple steps to making your class serializable.

  1. Add "implements Acme.Serializable" to the class declaration.
  2. Make sure you have a no-arguments constructor that doesn't do anything permanent.
  3. Add a getVersion() method.
  4. Add a serialize() method that serializes all your class's variables.
  5. Add a deserialize() method that deserializes all your class's variables.

Here's an example of a class that uses this interface:


 import java.io.*;
 public class Tree implements Acme.Serializable
     {
     long hash;
     Tree left, right;
     /// Real constructor.
     public Tree( long hash, Tree left, Tree right )
         {
         this.hash = hash;
         this.left = left;
         this.right = right;
         }
     /// No-args constructor, makes a blank instance to be filled in by the
     // deserializer.
     public Tree()
         {
         }
     /// Version routine.
     public String getVersion()
         {
         return "1";
         }
     /// Serialize routine.
     public void serialize( DataOutputStream dout ) throws IOException
         {
         Acme.SerialUtils.serializeLong( hash, dout );
         Acme.SerialUtils.serializeObject( left, dout );
         Acme.SerialUtils.serializeObject( right, dout );
         }
     /// Deserialize routine.
     public void deserialize( DataInputStream din ) throws IOException
         {
         hash = Acme.SerialUtils.deserializeLong( din );
         left = (Tree) Acme.SerialUtils.deserializeObject(
             this.getClass(), din );
         right = (Tree) Acme.SerialUtils.deserializeObject(
             this.getClass(), din );
         }
     }
 

Once you've got your class all set up, you'll need some code to start the serialization / deserialization process going. Here are examples of that:


     // Serialize.
     Tree t = [...];
     DataOutputStream dout = new DataOutputStream( System.out );
     try
         {
         SerialUtils.serializeObject( t, dout );
         dout.flush();
         }
     catch ( IOException e )
         {
         System.err.println( e.toString() );
         }
     // Deserialize.
     Tree t = new Tree();
     DataInputStream din = new DataInputStream( System.in );
     try
         {
         t = (Tree) SerialUtils.deserializeObject( t.getClass(), din );
         }
     catch ( IOException e )
         {
         System.err.println( e.toString() );
         }
 

Fetch the software.
Fetch the entire Acme package.

See Also:
SerialUtils

Method Index

 o deserialize(DataInputStream)
Read a serial representation of the object from the stream.
 o serialize(DataOutputStream)
Write a serial representation of the object to the stream.

Methods

 o serialize
 public abstract void serialize(DataOutputStream dout) throws IOException
Write a serial representation of the object to the stream.

 o deserialize
 public abstract void deserialize(DataInputStream din) throws IOException
Read a serial representation of the object from the stream. The object must have already been constructed, presumably by an empty no-arguments constructor. This routine just fills in the fields.


All Packages  Class Hierarchy  This Package  Previous  Next  Index

ACME Java  ACME Labs