< prev index next >

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

Print this page

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






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

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



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

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

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