< prev index next >

src/java.base/share/classes/java/io/ObjectOutputStream.java

Print this page

 110  * implement the java.io.Serializable interface.  Subclasses of Objects that
 111  * are not serializable can be serializable. In this case the non-serializable
 112  * class must have a no-arg constructor to allow its fields to be initialized.
 113  * In this case it is the responsibility of the subclass to save and restore
 114  * the state of the non-serializable class. It is frequently the case that the
 115  * fields of that class are accessible (public, package, or protected) or that
 116  * there are get and set methods that can be used to restore the state.
 117  *
 118  * <p>Serialization of an object can be prevented by implementing writeObject
 119  * and readObject methods that throw the NotSerializableException.  The
 120  * exception will be caught by the ObjectOutputStream and abort the
 121  * serialization process.
 122  *
 123  * <p>Implementing the Externalizable interface allows the object to assume
 124  * complete control over the contents and format of the object's serialized
 125  * form.  The methods of the Externalizable interface, writeExternal and
 126  * readExternal, are called to save and restore the objects state.  When
 127  * implemented by a class they can write and read their own state using all of
 128  * the methods of ObjectOutput and ObjectInput.  It is the responsibility of
 129  * the objects to handle any versioning that occurs.






 130  *
 131  * <p>Enum constants are serialized differently than ordinary serializable or
 132  * externalizable objects.  The serialized form of an enum constant consists
 133  * solely of its name; field values of the constant are not transmitted.  To
 134  * serialize an enum constant, ObjectOutputStream writes the string returned by
 135  * the constant's name method.  Like other serializable or externalizable
 136  * objects, enum constants can function as the targets of back references
 137  * appearing subsequently in the serialization stream.  The process by which
 138  * enum constants are serialized cannot be customized; any class-specific
 139  * writeObject and writeReplace methods defined by enum types are ignored
 140  * during serialization.  Similarly, any serialPersistentFields or
 141  * serialVersionUID field declarations are also ignored--all enum types have a
 142  * fixed serialVersionUID of 0L.
 143  *
 144  * <p>Primitive data, excluding serializable fields and externalizable data, is
 145  * written to the ObjectOutputStream in block-data records. A block data record
 146  * is composed of a header and data. The block data header consists of a marker
 147  * and the number of bytes to follow the header.  Consecutive primitive data
 148  * writes are merged into one block-data record.  The blocking factor used for
 149  * a block-data record will be 1024 bytes.  Each block-data record will be

1429     private void writeOrdinaryObject(Object obj,
1430                                      ObjectStreamClass desc,
1431                                      boolean unshared)
1432         throws IOException
1433     {
1434         if (extendedDebugInfo) {
1435             debugInfoStack.push(
1436                 (depth == 1 ? "root " : "") + "object (class \"" +
1437                 obj.getClass().getName() + "\", " + obj.toString() + ")");
1438         }
1439         try {
1440             desc.checkSerialize();
1441 
1442             bout.writeByte(TC_OBJECT);
1443             writeClassDesc(desc, false);
1444             handles.assign(unshared ? null : obj);
1445 
1446             if (desc.isRecord()) {
1447                 writeRecordData(obj, desc);
1448             } else if (desc.isExternalizable() && !desc.isProxy()) {



1449                 writeExternalData((Externalizable) obj);
1450             } else {
1451                 writeSerialData(obj, desc);
1452             }
1453         } finally {
1454             if (extendedDebugInfo) {
1455                 debugInfoStack.pop();
1456             }
1457         }
1458     }
1459 
1460     /**
1461      * Writes externalizable data of given object by invoking its
1462      * writeExternal() method.
1463      */
1464     private void writeExternalData(Externalizable obj) throws IOException {
1465         PutFieldImpl oldPut = curPut;
1466         curPut = null;
1467 
1468         if (extendedDebugInfo) {

 110  * implement the java.io.Serializable interface.  Subclasses of Objects that
 111  * are not serializable can be serializable. In this case the non-serializable
 112  * class must have a no-arg constructor to allow its fields to be initialized.
 113  * In this case it is the responsibility of the subclass to save and restore
 114  * the state of the non-serializable class. It is frequently the case that the
 115  * fields of that class are accessible (public, package, or protected) or that
 116  * there are get and set methods that can be used to restore the state.
 117  *
 118  * <p>Serialization of an object can be prevented by implementing writeObject
 119  * and readObject methods that throw the NotSerializableException.  The
 120  * exception will be caught by the ObjectOutputStream and abort the
 121  * serialization process.
 122  *
 123  * <p>Implementing the Externalizable interface allows the object to assume
 124  * complete control over the contents and format of the object's serialized
 125  * form.  The methods of the Externalizable interface, writeExternal and
 126  * readExternal, are called to save and restore the objects state.  When
 127  * implemented by a class they can write and read their own state using all of
 128  * the methods of ObjectOutput and ObjectInput.  It is the responsibility of
 129  * the objects to handle any versioning that occurs.
 130  * Value classes implementing {@link Externalizable} cannot be serialized
 131  * or deserialized, the value object is immutable and the state cannot be restored.
 132  * Use {@link Serializable} {@code writeReplace} to delegate to another serializable
 133  * object such as a record.
 134  *
 135  * Value objects cannot be {@code java.io.Externalizable}.
 136  *
 137  * <p>Enum constants are serialized differently than ordinary serializable or
 138  * externalizable objects.  The serialized form of an enum constant consists
 139  * solely of its name; field values of the constant are not transmitted.  To
 140  * serialize an enum constant, ObjectOutputStream writes the string returned by
 141  * the constant's name method.  Like other serializable or externalizable
 142  * objects, enum constants can function as the targets of back references
 143  * appearing subsequently in the serialization stream.  The process by which
 144  * enum constants are serialized cannot be customized; any class-specific
 145  * writeObject and writeReplace methods defined by enum types are ignored
 146  * during serialization.  Similarly, any serialPersistentFields or
 147  * serialVersionUID field declarations are also ignored--all enum types have a
 148  * fixed serialVersionUID of 0L.
 149  *
 150  * <p>Primitive data, excluding serializable fields and externalizable data, is
 151  * written to the ObjectOutputStream in block-data records. A block data record
 152  * is composed of a header and data. The block data header consists of a marker
 153  * and the number of bytes to follow the header.  Consecutive primitive data
 154  * writes are merged into one block-data record.  The blocking factor used for
 155  * a block-data record will be 1024 bytes.  Each block-data record will be

1435     private void writeOrdinaryObject(Object obj,
1436                                      ObjectStreamClass desc,
1437                                      boolean unshared)
1438         throws IOException
1439     {
1440         if (extendedDebugInfo) {
1441             debugInfoStack.push(
1442                 (depth == 1 ? "root " : "") + "object (class \"" +
1443                 obj.getClass().getName() + "\", " + obj.toString() + ")");
1444         }
1445         try {
1446             desc.checkSerialize();
1447 
1448             bout.writeByte(TC_OBJECT);
1449             writeClassDesc(desc, false);
1450             handles.assign(unshared ? null : obj);
1451 
1452             if (desc.isRecord()) {
1453                 writeRecordData(obj, desc);
1454             } else if (desc.isExternalizable() && !desc.isProxy()) {
1455                 if (desc.forClass().isValue())
1456                     throw new NotSerializableException("Externalizable not valid for value class "
1457                             + desc.forClass().getName());
1458                 writeExternalData((Externalizable) obj);
1459             } else {
1460                 writeSerialData(obj, desc);
1461             }
1462         } finally {
1463             if (extendedDebugInfo) {
1464                 debugInfoStack.pop();
1465             }
1466         }
1467     }
1468 
1469     /**
1470      * Writes externalizable data of given object by invoking its
1471      * writeExternal() method.
1472      */
1473     private void writeExternalData(Externalizable obj) throws IOException {
1474         PutFieldImpl oldPut = curPut;
1475         curPut = null;
1476 
1477         if (extendedDebugInfo) {
< prev index next >