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

BufferedOutputStream.write() should immediately throw IOException on closed stream

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 11, 17, 21
    • core-libs
    • Fix Understood
    • x86, sparc
    • solaris_7, windows_xp

      Name: nt126004 Date: 01/06/2003


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

      FULL OPERATING SYSTEM VERSION :

      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      I am no little surprised that this has gone unreported all
      these years so I should point out that NEITHER DOWNSTREAM
      DECORATORS (OR FILTERS) NOR THE DESTINATION FILE HAVE BEEN
      CLOSED. THIS IS NOT A "BROKEN CHAIN" PROBLEM.


      Writing to a closed BufferedOutputStream does not throw an
      IOException until the byte buffer is full. The reason for
      this behavoir is obvious looking at the write methods in
      BufferedWriter (by way of comparison). They include a
      private ensureOpen() method that throws an IOException
      with a detail message of "Stream closed". This method is
      missing in BufferedOutputStream. Adding such a method is
      not a backwards compatibility issue, and so I am reporting
      this as a bug.

      The contract of OutputStream.close() claims :
      "A closed stream cannot perform output operations and cannot be reopened."

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Compile and execute sample code
      2.
      3.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      *First* write after closing stream should throw IOexception.

      REPRODUCIBILITY :
      This bug can be reproduced always.

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

      public class Test {
          public static void main(String[] args) {
              try {
                  byte[] b = { 'a', 'b' };
                  byte[] c = { 'c', 'd' };
                  OutputStream os = new BufferedOutputStream(
                                    new FileOutputStream("out.txt", false));
                  os.write(b);
                  os.close();
                  os.write(c); // this suceeds - bug

                  os = new FileOutputStream("out.txt", true);
                  os.write(b);
                  os.close();
                  os.write(c); // crashes here, correct.
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER WORKAROUND :
      In terms of silently losing data written to a closed
      BufferedOutputStream that does not reach its buffer
      capacity there is no workaround. Of course, not writing to
      closed streams would help.
      (Review ID: 179375)
      ======================================================================

            vtewari Vyom Tewari
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: