CONTENTS | PREV | NEXT | Java Object Serialization Specification |
A basic structure is needed to represent objects in a stream. Each attribute of the object needs to be represented: its classes, its fields, and data written and later read by class-specific methods. The representation of objects in the stream can be described with a grammar. There are special representations for null objects, new objects, classes, arrays, strings, and back references to any object already in the stream. Each object written to the stream is assigned a handle that is used to refer back to the object. Handles are assigned sequentially starting from 0x7E0000. The handles restart at 0x7E0000 when the stream is reset.A class object is represented by the following:
AnObjectStreamClass
object for a Class that is not a dynamic proxy class is represented by the following:
- The Stream Unique Identifier (SUID) of compatible classes.
- A flag indicating if the class defined a
writeObject
method- The number of serializable fields
- The array of fields of the class that are serialized by the default mechanismFor arrays and object fields, the type of the field is included as a string which must be in "field descriptor" format (e.g., "
Ljava/lang/Object;
") as specified in section 4.3.2 of The JavaTM Virtual Machine Specification, Second Edition.- Optional block-data records or objects written by the
annotateClass
method- The
ObjectStreamClass
of its supertype (null if the superclass is not serializable)
AnObjectStreamClass
object for a dynamic proxy class is represented by the following:
- The number of interfaces that the dynamic proxy class implements
- The names of all of the interfaces implemented by the dynamic proxy class, listed in the order that they are returned by invoking the
getInterfaces
method on the Class object.- Optional block-data records or objects written by the
annotateProxyClass
method.- The ObjectStreamClass of its supertype,
java.lang.reflect.Proxy
.
The representation ofString
objects depends on the length of the UTF encoded string. If the UTF encoding of the givenString
is less than 65536 bytes in length, theString
is written in the standard Java UTF-8 format. Starting with the JavaTM 2 SDK, Standard Edition, v1.3, strings for which the UTF encoding length is greater than or equal to 65536 bytes are written in a variant "long" UTF format. The "long" UTF format is identical to the standard Java UTF-8 format, except that it uses 8 bytes to write the length of the UTF string, instead of 2 bytes. The typecode preceding theString
in the serialization stream indicates which format was used to write theString
.Arrays are represented by the following:
New objects in the stream are represented by the following:
- The serializable fields.See Section 1.5, "Defining Serializable Fields for a Class."
- If the class has
writeObject
/readObject
methods, there may be optional objects and/or block-data records of primitive types written by thewriteObject
method followed by anendBlockData
code.
All primitive data written by classes is buffered and wrapped in block-data records, regardless if the data is written to the stream within awriteObject
method or written directly to the stream from outside awriteObject
method. This data can only be read by the correspondingreadObject
methods or be read directly from the stream. Objects written by thewriteObject
method terminate any previous block-data record and are written either as regular objects or null or back references, as appropriate. The block-data records allow error recovery to discard any optional data. When called from within a class, the stream can discard any data or objects until theendBlockData
.