Team LiB
Previous Section Next Section

ObjectOutputStreamjava.io

Java 1.1closeable flushable

The ObjectOutputStream serializes objects, arrays, and other values to a stream. The writeObject( ) method serializes an object or array, and various other methods write primitive data values to the stream. Note that only objects that implement the Serializable or Externalizable interface can be serialized.

A class that wants to customize the way instances are serialized should declare a private writeObject(ObjectOutputStream) method. This method is invoked when an object is being serialized and can use several additional methods of ObjectOutputStream. defaultWriteObject( ) performs the same serialization that would happen if no writeObject( ) method existed. An object can call this method to serialize itself and then use other methods of ObjectOutputStream to write additional data to the serialization stream. The class must define a matching readObject( ) method to read that additional data, of course. When working with multiple versions or implementations of a class, you may have to serialize a set of fields that do not precisely match the fields of your class. In this case, give your class a static field named serialPersistentFields whose value is an array of ObjectStreamField objects that describe the fields to be serialized. In your writeObject( ) method, call putFields( ) to obtain an ObjectOutputStream.PutField object. Store field names and values into this object, and then call writeFields( ) to write them out to the serialization stream. See ObjectStreamField and ObjectOutputStream.PutField for further details.

The remaining methods of ObjectOutputStream are miscellaneous stream-manipulation methods and protected methods for use by subclasses that want to customize its serialization behavior.

Figure 9-38. java.io.ObjectOutputStream


public class ObjectOutputStream extends OutputStream implements ObjectOutput, 
     ObjectStreamConstants {
// Public Constructors
     public ObjectOutputStream(OutputStream out) throws IOException;  
// Protected Constructors
1.2  protected ObjectOutputStream( ) throws IOException, SecurityException;  
// Nested Types
1.2  public abstract static class PutField; 
// Public Instance Methods
     public void defaultWriteObject( ) throws IOException;  
1.2  public ObjectOutputStream.PutField putFields( ) throws IOException;  
     public void reset( ) throws IOException;  
1.2  public void useProtocolVersion(int version) throws IOException;  
1.2  public void writeFields( ) throws IOException;  
1.4  public void writeUnshared(Object obj) throws IOException;  
// Methods Implementing DataOutput
     public void writeBoolean(boolean val) throws IOException;  
     public void writeByte(int val) throws IOException;  
     public void writeBytes(String str) throws IOException;  
     public void writeChar(int val) throws IOException;  
     public void writeChars(String str) throws IOException;  
     public void writeDouble(double val) throws IOException;  
     public void writeFloat(float val) throws IOException;  
     public void writeInt(int val) throws IOException;  
     public void writeLong(long val) throws IOException;  
     public void writeShort(int val) throws IOException;  
     public void writeUTF(String str) throws IOException;  
// Methods Implementing ObjectOutput
     public void close( ) throws IOException;  
     public void flush( ) throws IOException;  
     public void write(int val) throws IOException;  
     public void write(byte[ ] buf) throws IOException;  
     public void write(byte[ ] buf, int off, int len) throws IOException;  
     public final void writeObject(Object obj) throws IOException;  
// Protected Instance Methods
     protected void annotateClass(Class<?> cl) throws IOException;     empty
1.3  protected void annotateProxyClass(Class<?> cl) throws IOException;     empty
     protected void drain( ) throws IOException;  
     protected boolean enableReplaceObject(boolean enable) throws SecurityException;  
     protected Object replaceObject(Object obj) throws IOException;  
1.3  protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException;  
1.2  protected void writeObjectOverride(Object obj) throws IOException;     empty
     protected void writeStreamHeader( ) throws IOException;  
}

    Team LiB
    Previous Section Next Section