ObjectOutput interface provides an abstract interface to object serialization. It extends DataOutput so those methods for writing primitive data types are included in the interface.
package java.io;
public interface ObjectOutput extends DataOutput {
public void writeObject(Object obj)
throws IOException;
}
ObjectOutputStream implements object serialization. It maintains the state of the stream including the set of objects already serialized. Its methods control the traversal of objects to be serialized and makes calls to the class specific writeObject methods and to annotateClass and replaceObject methods as appropriate.
package java.io;
public class ObjectOutputStream
extends DataOutputStream
implements ObjectOutput
{
public ObjectOutputStream(OutputStream out)
throws IOException;
public final void writeObject(Object obj)
throws ClassMismatchException, MethodMissingException,
IOException;
protected void annotateClass(Class cl)
throws IOException;
protected Object replaceObject(Object obj)
throws IOException;
}
The ObjectOutputStream constructor requires an OutputStream. The constructor writes a header and version to the stream that will be read and verified by the corresponding ObjectInputStream constructor.The
writeObject method is used to serialize an object to the stream. The exceptions thrown reflect errors during the traversal or exceptions that occur on the underlying stream. If any exception is thrown, the underlying stream is left in an unknown and unusable state.The
annotateClass method is called while a Class is being serialized, after the class name and fingerprint have been written to the stream. Subclasses may extend this method and write other information to the stream about the class. This information must be read by the resolveClass method on a corresponding ObjectInputStream subclass.The
replaceObject method is called just prior to serializing an object for the first time. A subclass's implementation may return a substitute object that will be serialized instead of the original. All references in the stream to the original object will replaced by the substitute object.
writeObject method allows a class to control the serialization of its own fields. Here is its signature:
private void writeObject(ObjectOutputStream stream) throws NoAccessException, IOException
ObjectStreamException which is a subclass of IOException.
ObjectStreamException - superclass of all serialization exceptions
ClassMismatchException - when a class definition contains unexpected field or method declarations.
MethodMissingException - thrown when only one of readObject and writeObject is found. If a class implements one it must implement the other.
NoAccessException - thrown by a readObject or writeObject method to terminate serialization or deserialization.
StreamCorruptedException - thrown when the stream header is invalid or when control information in the stream is not found or found to be invalid.