8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
authorbpb
Mon, 29 Jul 2019 09:09:23 -0700
changeset 57569 be47f3ccdf12
parent 57568 460ac76019f4
child 57570 d7304cf430f1
8078891: java.io.SequenceInputStream.close is not atomic and not idempotent Reviewed-by: prappo, dfuchs, alanb
src/java.base/share/classes/java/io/SequenceInputStream.java
test/jdk/java/io/SequenceInputStream/Close.java
--- a/src/java.base/share/classes/java/io/SequenceInputStream.java	Mon Jul 29 08:48:52 2019 -0700
+++ b/src/java.base/share/classes/java/io/SequenceInputStream.java	Mon Jul 29 09:09:23 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -88,7 +88,7 @@
     }
 
     /**
-     *  Continues reading in the next stream if an EOF is reached.
+     * Continues reading in the next stream if an EOF is reached.
      */
     final void nextStream() throws IOException {
         if (in != null) {
@@ -220,8 +220,21 @@
      * @exception  IOException  if an I/O error occurs.
      */
     public void close() throws IOException {
-        do {
-            nextStream();
-        } while (in != null);
+        IOException ioe = null;
+        while (in != null) {
+            try {
+                in.close();
+            } catch (IOException e) {
+                if (ioe == null) {
+                    ioe = e;
+                } else {
+                    ioe.addSuppressed(e);
+                }
+            }
+            peekNextStream();
+        }
+        if (ioe != null) {
+            throw ioe;
+        }
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/io/SequenceInputStream/Close.java	Mon Jul 29 09:09:23 2019 -0700
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+ * @bug 8078891
+ * @summary Ensure close() closes all component streams
+ */
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.SequenceInputStream;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
+
+public class Close {
+
+    public static void main(String[] argv) {
+        byte[] buf = new byte[42];
+
+        List<CloseableBAOS> list = List.of(new CloseableBAOS(buf),
+            new CloseableBAOS(buf), new CloseableBAOS(buf),
+            new CloseableBAOS(buf));
+
+        Enumeration<CloseableBAOS> enumeration = Collections.enumeration(list);
+
+        SequenceInputStream sequence = new SequenceInputStream(enumeration);
+        try {
+            sequence.close();
+            throw new RuntimeException("Expected IOException not thrown");
+        } catch (IOException e) {
+            for (CloseableBAOS c : list) {
+                if (!c.isClosed()) {
+                    throw new RuntimeException("Component stream not closed");
+                }
+            }
+            Throwable[] suppressed = e.getSuppressed();
+            if (suppressed == null) {
+                throw new RuntimeException("No suppressed exceptions");
+            } else if (suppressed.length != list.size() - 1) {
+                throw new RuntimeException("Expected " + (list.size() - 1) +
+                    " suppressed exceptions but got " + suppressed.length);
+            }
+            for (Throwable t : suppressed) {
+                if (!(t instanceof IOException)) {
+                    throw new RuntimeException("Expected IOException but got " +
+                        t.getClass().getName());
+                }
+            }
+        }
+    }
+
+    static class CloseableBAOS extends ByteArrayInputStream{
+        private boolean closed;
+
+        CloseableBAOS(byte[] buf) {
+            super(buf);
+        }
+
+        @Override
+        public void close() throws IOException {
+            closed = true;
+            throw new IOException();
+        }
+
+        public boolean isClosed() {
+            return closed;
+        }
+    }
+}