Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 151
نمايش فراداده

ObjectInputStream deserializes objects, arrays, and other values from a stream that was previously created with an ObjectOutputStream. The readObject( ) method deserializes objects and arrays (which should then be cast to the appropriate type); various other methods read primitive data values from the stream. Note that only objects that implement the Serializable or Externalizable interface can be serialized and deserialized.

A class may implement its own private readObject(ObjectInputStream) method to customize the way it is deserialized. If you define such a method, there are several ObjectInputStream methods you can use to help deserialize the object. defaultReadObject( ) is the easiest. It reads the content of the object just as an ObjectInputStream would normally do. If you wrote additional data before or after the default object contents, you should read that data before or after calling defaultReadObject( ). When working with multiple versions or implementations of a class, you may have to deserialize a set of fields that do not 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 deserialized. If you do this, your readObject( ) method can call readFields( ) to read the specified fields from the stream and return them in a ObjectInputStream.GetField object. See ObjectStreamField and ObjectInputStream.GetField for more details. Finally, you can call registerValidation( ) from a custom readObject( ) method. This method registers an ObjectInputValidation object (typically the object being deserialized) to be notified when a complete tree of objects has been deserialized, and the original call to the readObject( ) method of the ObjectInputStream is about to return to its caller.

The remaining methods include miscellaneous stream-manipulation methods and several protected methods for use by subclasses that want to customize the deserialization behavior of ObjectInputStream.

Figure 9-36. java.io.ObjectInputStream

public class 

ObjectInputStream extends InputStream implements ObjectInput, ObjectStreamConstants { // Public Constructors public

ObjectInputStream (InputStream

in ) throws IOException; // Protected Constructors

1.2 protected

ObjectInputStream ( ) throws IOException, SecurityException; // Nested Types

1.2 public abstract static class

GetField ; // Public Instance Methods public void

defaultReadObject ( ) throws IOException, ClassNotFoundException;

1.2 public ObjectInputStream.GetField

readFields ( ) throws IOException, ClassNotFoundException;

1.4 public Object

readUnshared ( ) throws IOException, ClassNotFoundException; public void

registerValidation (ObjectInputValidation

obj , int

prio ) throws NotActiveException, InvalidObjectException; // Methods Implementing DataInput public boolean

readBoolean ( ) throws IOException; public byte

readByte ( ) throws IOException; public char

readChar ( ) throws IOException; public double

readDouble ( ) throws IOException; public float

readFloat ( ) throws IOException; public void

readFully (byte[ ]

buf ) throws IOException; public void

readFully (byte[ ]

buf , int

off , int

len ) throws IOException; public int

readInt ( ) throws IOException; public long

readLong ( ) throws IOException; public short

readShort ( ) throws IOException; public int

readUnsignedByte ( ) throws IOException; public int

readUnsignedShort ( ) throws IOException; public String

readUTF ( ) throws IOException; public int

skipBytes (int

len ) throws IOException; // Methods Implementing ObjectInput public int

available ( ) throws IOException; public void

close ( ) throws IOException; public int

read ( ) throws IOException; public int

read (byte[ ]

buf , int

off , int

len ) throws IOException; public final Object

readObject ( ) throws IOException, ClassNotFoundException; // Protected Instance Methods protected boolean

enableResolveObject (boolean

enable ) throws SecurityException;

1.3 protected ObjectStreamClass

readClassDescriptor ( ) throws IOException, ClassNotFoundException;

1.2 protected Object

readObjectOverride ( ) throws IOException, ClassNotFoundException; constant protected void

readStreamHeader ( ) throws IOException, StreamCorruptedException; protected Class<?>

resolveClass (ObjectStreamClass

desc ) throws IOException, ClassNotFoundException; protected Object

resolveObject (Object

obj ) throws IOException;

1.3 protected Class<?>

resolveProxyClass (String[ ]

interfaces ) throws IOException, ClassNotFoundException; // Deprecated Public Methods

# public String

readLine ( ) throws IOException; Implements:DataInput }