Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4289070

ObjectOutputStream.PutField.write() broken

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.3.0
    • core-libs
    • beta2
    • generic
    • generic
    • Verified

      The PutField API appears to provide 2 ways of writing an object's field values
      to a serialization stream. The recommended method is to call
      ObjectOutputStream.writeFields() after setting values in the PutField object:

          private void writeObject(ObjectOutputStream out) throws IOException {
              ObjectOutputStream.PutField fields = out.putFields();
              // set field values ...
              out.writeFields();
          }

      However, ObjectOutputStream.PutField also provides a public write() method,
      which accepts an object implementing ObjectOutput as a parameter. This presents
      the following problems:

          1. It's unclear why there are two different methods of flushing a PutField
          to an ObjectOutputStream (javadoc/spec doesn't elaborate).

          2. PutField.write() allows the field values to be written to an
          ObjectOutputStream other than the one in which the object is being
          serialized.

          3. There is no corresponding GetField.read().

          4. Unlike ObjectOutputStream.writeFields(), PutField.write() doesn't unset
          block data mode before writing field values, nor does it reset block data
          mode afterwards. As a result, there is no way for an ObjectInputStream to
          make any use of the values written by PutField.write()
          (ObjectInputStream.readFields() expects the fields to be written in
          "non-block-data" mode).

      Attached code demonstrates #4.

      The implementation of PutField.write() could be changed to unset block data mode
      on the given stream before writing field values, and then (re)set it afterwards.
      Since PutField.write() accepts ObjectOutput (not ObjectOutputStream), it must
      first explicitly check to see if the stream it's passed is an
      ObjectOutputStream, and then toggle block data mode.

      A better (IMO) solution is to deprecate PutField.write(), and possibly change
      its implementation to throw an exception, since it never worked in the first
      place.

            mwarressunw Michael Warres (Inactive)
            mwarressunw Michael Warres (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: