Writing A Simplified Version of the Serialization Algorithm

• A description of its superclass if the superclass is serializable This should, of course, immediately seem familiar. The class descriptions consist entirely of metadata that allows the instance to be read back in. In fact, this is one of the most beautiful aspects of serialization; the serialization mechanism automatically, at runtime, converts class objects into metadata so instances can be serialized with the least amount of programmer work.

10.4.2 A Simplified Version of the Serialization Algorithm

In this section, I describe a slightly simplified version of the serialization algorithm. I then proceed to a more complete description of the serialization process in the next section.

10.4.2.1 Writing

Because the class descriptions actually contain the metadata, the basic idea behind the serialization algorithm is pretty easy to describe. The only tricky part is handling circular references. The problem is this: suppose instance A refers to instance B . And instance B refers back to instance A . Completely writing out A requires you to write out B . But writing out B requires you to write out A . Because you dont want to get into an infinite loop, or even write out an instance or a class description more than once, [ 6] you need to keep track of whats already been written to the stream. [ 6] Serialization is a slow process that uses the reflection API quite heavily in addition to the bandwidth. ObjectOutputStream does this by maintaining a mapping from instances and classes to handles. When writeObject is called with an argument that has already been written to the stream, the handle is written to the stream, and no further operations are necessary. If, however, writeObject is passed an instance that has not yet been written to the stream, two things happen. First, the instance is assigned a reference handle, and the mapping from instance to reference handle is stored by ObjectOutputStream . The handle that is assigned is the next integer in a sequence. Remember the reset method on ObjectOutputStream ? It clears the mapping and resets the handle counter to 0x7E0000 .RMI also automatically resets its serialization mechanism after every remote method call. Second, the instance data is written out as per the data format described earlier. This can involve some complications if the instance has a field whose value is also a serializable instance. In this case, the serialization of the first instance is suspended, and the second instance is serialized in its place or, if the second instance has already been serialized, the reference handle for the second instance is written out. After the second instance is fully serialized, serialization of the first instance resumes. The contents of the stream look a little bit like Figur e 10- 5 . Figure 10-5. Contents of Serializations data stream

10.4.2.2 Reading