[PATCH] JDK-8054565: FilterOutputStream.close may throw IOException if called twice and underlying flush or close fails

Chris Hegarty chris.hegarty at oracle.com
Fri Dec 5 15:43:33 UTC 2014


On 05/12/14 15:33, Alan Bateman wrote:
> On 05/12/2014 14:04, Chris Hegarty wrote:
>>  ...
> Right, no need for it to be protected. I think what you have seems right
> but we probably need a small spec clarification so that it reads "When
> not already closed, the close method of FilterOutputStream ...". That
> removes the testable assertion that flush that is always called.

Right. Just to close the loop, the change, excluding testcase, looks like:

diff --git a/src/java.base/share/classes/java/io/FilterOutputStream.java 
b/src/java.base/share/classes/java/io/FilterOutputStream.java
--- a/src/java.base/share/classes/java/io/FilterOutputStream.java
+++ b/src/java.base/share/classes/java/io/FilterOutputStream.java
@@ -48,6 +48,8 @@
       */
      protected OutputStream out;

+    private boolean closed;
+
      /**
       * Creates an output stream filter built on top of the specified
       * underlying output stream.
@@ -144,9 +146,9 @@
       * Closes this output stream and releases any system resources
       * associated with the stream.
       * <p>
-     * The <code>close</code> method of <code>FilterOutputStream</code>
-     * calls its <code>flush</code> method, and then calls the
-     * <code>close</code> method of its underlying output stream.
+     * When not already closed, the {@code close} method of {@code
+     * FilterOutputStream} calls its {@code flush} method, and then
+     * calls the {@code close} method of its underlying output stream.
       *
       * @exception  IOException  if an I/O error occurs.
       * @see        java.io.FilterOutputStream#flush()
@@ -154,6 +156,9 @@
       */
      @SuppressWarnings("try")
      public void close() throws IOException {
+        if (closed)
+            return;
+        closed = true;
          try (OutputStream ostream = out) {
              flush();
          }

-Chris.



More information about the core-libs-dev mailing list