All Packages Class Hierarchy This Package Previous Next Index
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.
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.
public abstract void serialize(DataOutputStream dout) throws IOException
public abstract void deserialize(DataInputStream din) throws IOException
All Packages Class Hierarchy This Package Previous Next Index
ACME Java ACME Labs