next up previous contents
Next: writeObjectメソッド Up: Serialization Previous: オブジェクトを「保存」「復元」するとは?

ObjectStreamClass

あるオブジェクトの状態を保存しようとするならば、まず、そのオブジェクトが属する クラスが、どのようなフィールドを含んでいるのかを知らなければなりません。逆に、 あるオブジェクトをストリームから復元しようとする時にも、ストリーム中にどの ようなフィールド情報が含まれているのかを、あらかじめ知っておく必要があります。 こうした目的のために、JDK1.1では、ObjectStreamClassクラスという新しいクラスが 導入されました。このクラスは、あるクラスのオブジェクトをストリームに読み書き する際に、そのクラスに対応して作られます。このクラスは、対応するクラスの 名前、フィールドの一覧、クラスのバージョン、上位クラスの情報など、ストリーム に読み書きされるオブジェクトの特徴を記述する Class Descriptorクラスで、 ObjectInputStream、ObjectOutputStreamの双方で利用されます。 次のリストは、このObjectStreamClassクラスのフィールドの一部です。

==========================================================================
public class ObjectStreamClass implements java.io.Serializable {
    .....
    /* Cache of Class -> ClassDescriptor Mappings. */
    static private ObjectStreamClassEntry[] descriptorFor = new ObjectStreamClassEntry[61];

    /* The name of this descriptor */
    private String name;

    /* The descriptor of the supertype. */
    private ObjectStreamClass superclass;
    .......
    /* Array of persistent fields of this class, sorted by type and name. */
    private ObjectStreamField[] fields;
    
    /* Class that is a descriptor for in this virtual machine.*/
    private Class ofClass;
    
    /* SerialVersionUID for this class. */
    private long suid;
    
    /* This sequence of type, byte offset of the fields 
       to be serialized and deserialized.*/
    private int[] fieldSequence;
    .......
    .......
}

class ObjectStreamField {
    String name;                // the name of the field
    char type;                        // type first byte of the type signature
    int  offset;                // Offset into the object of the field
    String typeString;                // iff object, typename
    .......
}
==========================================================================

リストのうしろにあげたのは、このクラスの中で使われる、一つ一つのフィールドを 記述するために導入されたObjectStreamFieldクラスの構造です。ここでは、フィールド のオブジェクトの値が直接は含まれていないことに注意してください。フィールドの 値は、あるテーブルに一括して収納され、ObjectStreamFieldクラスは、そのテーブル 上のoffsetを記憶します。



maruyama@wakhok.ac.jp