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

Block data mode of ObjectOutputStream causing problems for the ObjectInputStream

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 1.4.0
    • core-libs



      Name: nt126004 Date: 07/17/2002


      FULL PRODUCT VERSION :
      java version "1.4.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
      Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

      FULL OPERATING SYSTEM VERSION :
      Microsoft Windows 2000 [Version 5.00.2195]

      A DESCRIPTION OF THE PROBLEM :
      When writing out many primitive types in a row to an
      ObjectOutputStream, then the reverse will fail, i.e.
      reading the primitive types in the exact same order from an
      ObjectInputStream will generate an error. This is due to
      incorrect handling of the block data mode feature.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      The program below always produces the error. The program
      will fail with a java.io.EOFException after having read the
      768th element from the ObjectInputStream.

      ---------------------------------

      import java.io.*;

      public class Javabug
      {
      public static void main(String args[]) {
      try {
      System.out.println("Starting
      Javabug...");

      ByteArrayOutputStream baos = new
      ByteArrayOutputStream();
      ObjectOutputStream oos = new
      ObjectOutputStream(baos);

      for (int i = 0; i < 1000; i++) {
      oos.writeInt(i);
      }

      baos.flush();

      byte[] buf = baos.toByteArray();

      ByteArrayInputStream bais = new
      ByteArrayInputStream(buf);
      ObjectInputStream ois = new
      ObjectInputStream(bais);

      for (int i = 0; i < 1000; i++) {
      System.out.print(i + " ");
      int x = ois.readInt();
      if (x != i) {
      System.out.println
      ("ERROR : " + i);
      }
      }

      System.out.println("END");
      } catch (Throwable t) {
      t.printStackTrace();
      }
      }
      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      The given example program about should be able to read back
      all 1000 elements.

      In ObjectInputStream, the program should recognize the
      block header data that was written internally by
      ObjectOutputStream onto the stream.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.io.EOFException
              at java.io.DataInputStream.readInt(DataInputStream.java:397)
              at java.io.ObjectInputStream$BlockDataInputStream.readInt(ObjectInputStr
      eam.java:2645)
              at java.io.ObjectInputStream.readInt(ObjectInputStream.java:892)
              at Javabug.main(Javabug.java:25)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.*;

      public class Javabug
      {
      public static void main(String args[]) {
      try {
      System.out.println("Starting Javabug...");

      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream(baos);

      for (int i = 0; i < 1000; i++) {
      oos.writeInt(i);
      }

      baos.flush();

      byte[] buf = baos.toByteArray();

      ByteArrayInputStream bais = new ByteArrayInputStream(buf);
      ObjectInputStream ois = new ObjectInputStream(bais);

      for (int i = 0; i < 1000; i++) {
      System.out.print(i + " ");
      int x = ois.readInt();
      if (x != i) {
      System.out.println("ERROR : " + i);
      }
      }

      System.out.println("END");
      } catch (Throwable t) {
      t.printStackTrace();
      }
      }
      }

      ---------- END SOURCE ----------
      (Review ID: 158451)
      ======================================================================

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: