java.io.ByteArrayOutputStream allows writes after close

XMLWordPrintable

    • Type: Bug
    • Resolution: Fixed
    • Priority: P4
    • 1.4.0
    • Affects Version/s: 1.2.0
    • Component/s: docs
    • beta
    • generic
    • generic



      Name: bb33257 Date: 06/27/98


      The following program creates a PrintWriter based on ByteArrayOutputStream, and
      then closes the underlying stream. Even though the byte stream is closed, I can
      print to the PrintWriter without receiving an error.

      This problem causes a JCK test to fail.
      ---------------------------------------
      import java.io.*;
      public class PWTest {
        public static void main(String[] args) throws IOException {
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          OutputStreamWriter osw = new OutputStreamWriter(baos);
          PrintWriter pw = new PrintWriter(osw);
          baos.close();
          pw.print("foo");
          System.out.println("Should get checkError = true.");
          System.out.println("Did get checkError = " +pw.checkError());
        }
      }
      ------------------------
      The resulting output is:

      Should get checkError = true.
      Did get checkError = false
      ------------------------
      ======================================================================
      Reopened with a comment:

      If the write cannot succeed because the destination stream is closed, why would you not treat that as an error?

      Here's an interesting fact: in the test case above, if I close the OutputStreamWriter, and then write to it, the checkError is true. If I close the ByteArrayOutputStream and attempt a write, the checkError is false. So this is inconsistent API behavior.

      I also tried opening, writing to, closing and then writing again to a ByteArrayOutputStream directly (with no wrappers). I find that ByteArrayOutputStream does not throw an exception when you try to operate on it in the closed state. So perhaps this (invalid) behavior is propagating upwards?

            Assignee:
            Michael Mccloskey (Inactive)
            Reporter:
            Brian Beck (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: