All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object
|
+----java.io.OutputStream
|
+----java.io.FilterOutputStream
|
+----java.io.DataOutputStream
|
+----java.io.ObjectOutputStream
The class of each object is encoded including the class name and signature of the class, the values of the object's fields and arrays, and the closure of any other objects referenced from the initial objects.
The method writeObject is used to write an object to the stream. Any object, including Strings and arrays, is written with writeObject. Multiple objects or primitives can be written to the stream. The objects must be read back from the corresponding ObjectInputstream with the same types and in the same order as they were written.
Primitive data types can also be written to the stream using the appropriate methods from DataOutputStream. Strings can also be written using the writeUTF method.
The default serialization mechanism for an object writes the class of the object, the class signature, and the values of all non-transient and non-static fields. References to other objects (except in transient or static fields) cause those objects to be written also. Multiple references to a single object are encoded using a reference sharing mechanism so that graphs of objects can be restored to the same shape as when the original was written.
For example to write an object that can be read by the example in ObjectInputStream:
FileOutputStream ostream = new FileOutputStream("t.tmp");
ObjectOutputStream p = new ObjectOutputStream(ostream);
p.writeInt(12345);
p.writeObject("Today");
p.writeObject(new Date());
p.flush();
ostream.close();
Classes that require special handling during the serialization and deserialization
process or that should NOT be writable must implement special
methods with these exact signatures:
private void readObject(java.io.ObjectInputStream stream)
throws IOException, ClassNotFoundException;
private void writeObject(java.io.ObjectOutputStream stream)
throws IOException
The writeObject method is responsible for writing the state of the object for its particular class so that the corresponding readObject method can restore it. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutputStream.
Serialization of an object can be prevented by implementing writeObject and readObject methods that throw the NoAccessException. The exception will be caught by the ObjectOutputStream and abort the serialization process.
public ObjectOutputStream(OutputStream out) throws IOException
public final void writeObject(Object obj) throws ClassMismatchException, MethodMissingException, IOException
Exceptions are thrown for problems with the OutputStream and for classes that should not be serialized. All exceptions are fatal to the OutputStream, it is left in an indeterminate state and it is up to the caller to ignore or recover the stream state.
protected void annotateClass(Class cl) throws IOException
protected Object replaceObject(Object obj) throws IOException
This method is called only once when each object is first encountered. All subsequent references to the object will be redirected to the new object. This method should return the object to be substituted or the original object.
Null can be returned as the object to be substituted, but may cause NullReferenceException in classes that contain references to the original object since they may be expecting an object instead of null.
This object substitution mechanism is needed, for example, in the case of remote method invocation. Surrogate references to remote objects can use the special methods to serialize themselves. However, references to local server objects should be serialized as the surrogate object that will be needed on the remote client. The remote method invocation infrastructure would define a subclass to map these local objects into the appropriate remote reference.
All Packages Class Hierarchy This Package Previous Next Index