Team LiB
Previous Section Next Section

ObjectInputStream.GetFieldjava.io

Java 1.2

This class holds the values of named fields read by an ObjectInputStream. It gives the programmer precise control over the deserialization process and is typically used when implementing an object with a set of fields that do not match the set of fields (and the serialization stream format) of the original implementation of the object. This class allows the implementation of a class to change without breaking serialization compatibility.

In order to use the GetField class, your class must implement a private readObject( ) method that is responsible for custom deserialization. Typically, when using the GetField class, you have also specified an array of ObjectStreamField objects as the value of a private static field named serialPersistentFields. This array specifies the names and types of all fields expected to be found when reading from a serialization stream. If there is no serialPersistentField field, the array of ObjectStreamField objects is created from the actual fields (excluding static and TRansient fields) of the class.

Within the readObject( ) method of your class, call the readFields( ) method of ObjectInputStream( ). This method reads the values of all fields from the stream and stores them in an ObjectInputStream.GetField object that it returns. This GetField object is essentially a mapping from field names to field values, and you can extract the values of whatever fields you need in order to restore the proper state of the object being deserialized. The various get( ) methods return the values of named fields of specified types. Each method takes a default value as an argument, in case no value for the named field was present in the serialization stream. (This can happen when deserializing an object written by an earlier version of the class, for example.) Use the defaulted( ) method to determine whether the GetField object contains a value for the named field. If this method returns true, the named field had no value in the stream, so the get( ) method of the GetField object has to return the specified default value. The getObjectStreamClass( ) method of a GetField object returns the ObjectStreamClass object for the object being deserialized. This ObjectStreamClass can obtain the array of ObjectStreamField objects for the class.

See also

ObjectOutputStream.PutField

public abstract static class ObjectInputStream.GetField {
// Public Constructors
     public GetField( );  
// Public Instance Methods
     public abstract boolean defaulted(String name) throws IOException;  
     public abstract boolean get(String name, boolean val) throws IOException;  
     public abstract byte get(String name, byte val) throws IOException;  
     public abstract char get(String name, char val) throws IOException;  
     public abstract short get(String name, short val) throws IOException;  
     public abstract int get(String name, int val) throws IOException;  
     public abstract long get(String name, long val) throws IOException;  
     public abstract float get(String name, float val) throws IOException;  
     public abstract double get(String name, double val) throws IOException;  
     public abstract Object get(String name, Object val) throws IOException;  
     public abstract ObjectStreamClass getObjectStreamClass( );  
}

Returned By

ObjectInputStream.readFields( )

    Team LiB
    Previous Section Next Section