Shevek (shevek) wrote,

We have derived the axioms of Java OutputStreams. They are far more subtle than they appear.

  • Any OutputStream must be safe against double-close.
  • Any buffering I/O layer must flush before close IF the close is not a no-op or double-close.

The options are:

  • Double-close is invalid.
  • The user is required to flush a buffer before close.

The interesting question is:

May flush() throw an IOException if the stream is closed, even if there is no buffered data to send?

The two axioms above make this question answerable either way. It doesn't matter. If you're closing, close all underlying streams you have access to, even if you then go on to close a network layer which has pointers to inner/filtered streams within them; it will be safe.
  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.