The objectio package is now part of the DM.jar collection of utilities, please download the bits there.
The com.develop.objectio package is designed to demonstrate
various techniques for Java serialization. After you install the objectio
library, check out the com.develop.objectio.Person1
class. This class is a simple data object with a few serializable
fields. It also includes a main method that serializes an instance to a
file, which you can invoke as follows:
java com.develop.objectio.Person1 Person1.ser
After you have serialized an instance to Person1.ser, you can
read the instance back in using the ReadSerialized class, which can
read in any serialized Java file:
java com.develop.objectio.ReadSerialized Person1.ser
The Java serialization format is described in a public
spec, so it is easy to parse the format of a serialized file from any
language. The com.develop.objectio.ObjectStreamWalker class
parses the stream format and dumps its contents to System.out.
Try it:
java com.develop.objectio.ObjectStreamWalker Person1.ser
Your output will be a dump of the contents of the stream. Notice that the serialization format includes not only the class's instance data, but also the names and types of the class fields:
Field count=3
Field type 73 integer
Field name: age
Field type 76 object
Field name: firstName
Content-type TC_STRING:74
New handle: 7e0001
New String: Ljava/lang/String;
(etc.)
This extra information is helpful if you ever need to match up versions of a class after some fields have changed.
Java's serialization code requires that you have the class available at
deserialization time. Prove this to yourself by moving the Person1.class
file off of your classpath, and attempting to read the Person1.ser
file:
java com.develop.objectio.ReadSerialized Person1.ser
This time, the code will fail with a class not found. Because the ObjectStreamWalker
class does not actually reconstitute the Java objects in a stream, it does not
need the .class file to be present. Try it:
java com.develop.objectio.ObjectStreamWalker Person1.ser
will work, even if the Person1.class file is missing. In
other words, data can be extracted from a Java serialization stream even if the
original classes are unavailable.
The com.develop.objectio.Person2 class is Externalizable,
i.e. it takes responsibility for writing its own state to the stream.
While this runs noticeably faster than normal serialization, there is a
drawback. The stream format is opaque, and cannot be interpreted without
the original class. Test this by first saving a Person2.ser
file and then reading it with the ObjectStreamWalker:
java com.develop.objectio.Person2 Person2.ser
java com.develop.objectio.ObjectStreamWalker Person1.ser
This time, the instance data just shows up as a binary blob:
Content-type TC_BLOCKDATA:77
Byte count: 18
00 04 46 72 65 64 00 06
57 65 73 6C 65 79 00 00
00 83
Content-type TC_ENDBLOCKDATA:78
The objectio package is open source, subject to the following license.