--- old/src/java.base/share/classes/java/io/SequenceInputStream.java 2019-07-23 17:05:23.000000000 -0700 +++ new/src/java.base/share/classes/java/io/SequenceInputStream.java 2019-07-23 17:05:22.000000000 -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,13 +88,16 @@ } /** - * 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) { - in.close(); + try { + if (in != null) { + in.close(); + } + } finally { + peekNextStream(); } - peekNextStream(); } private void peekNextStream() { @@ -220,8 +223,20 @@ * @exception IOException if an I/O error occurs. */ public void close() throws IOException { + IOException ioe = null; do { - nextStream(); + try { + nextStream(); + } catch (IOException e) { + if (ioe == null) { + ioe = e; + } else { + ioe.addSuppressed(e); + } + } } while (in != null); + if (ioe != null) { + throw ioe; + } } } --- /dev/null 2019-07-23 17:05:24.000000000 -0700 +++ new/test/jdk/java/io/SequenceInputStream/Close.java 2019-07-23 17:05:23.000000000 -0700 @@ -0,0 +1,92 @@ +/* + * 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.InputStream; +import java.io.IOException; +import java.io.SequenceInputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; +import java.util.List; + +public class Close { + + public static void main(String[] argv) throws IOException { + byte[] buf = new byte[42]; + + List list = List.of(new CloseableBAOS(buf), + new CloseableBAOS(buf), new CloseableBAOS(buf), + new CloseableBAOS(buf)); + + Enumeration 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; + private int numCloses = 0; + + CloseableBAOS(byte[] buf) { + super(buf); + } + + public void close() throws IOException { + closed = true; + throw new IOException(); + } + + public boolean isClosed() { + return closed; + } + } +}