Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/etc/FailingFlushAndClose.java
38813 views
/*1* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.*;2425/**26* @test27* @bug 701558928* @summary Test that buffering streams are considered closed even when the29* close or flush from the underlying stream fails.30*/3132public class FailingFlushAndClose {3334static int failed;3536static void fail(String msg) {37System.err.println("FAIL: " + msg);38failed++;39}4041static void failWithIOE(String msg) throws IOException {42fail(msg);43throw new IOException(msg);44}4546static class FailingCloseInputStream extends InputStream {47boolean closed;48@Override49public int read()throws IOException {50if (closed)51failWithIOE("input stream is closed");52return 1;53}54@Override55public void close() throws IOException {56if (!closed) {57closed = true;58throw new IOException("close failed");59}60}61}6263static class FailingCloseOutputStream extends OutputStream {64boolean closed;65@Override66public void write(int b) throws IOException {67if (closed)68failWithIOE("output stream is closed");69}70@Override71public void flush() throws IOException {72if (closed)73failWithIOE("output stream is closed");74}75@Override76public void close() throws IOException {77if (!closed) {78closed = true;79throw new IOException("close failed");80}81}82}8384static class FailingFlushOutputStream extends OutputStream {85boolean closed;86@Override87public void write(int b) throws IOException {88if (closed)89failWithIOE("output stream is closed");90}91@Override92public void flush() throws IOException {93if (closed) {94failWithIOE("output stream is closed");95} else {96throw new IOException("flush failed");97}98}99@Override100public void close() throws IOException {101closed = true;102}103}104105static class FailingCloseReader extends Reader {106boolean closed;107@Override108public int read(char[] cbuf, int off, int len) throws IOException {109if (closed)110failWithIOE("reader is closed");111return 1;112}113@Override114public void close() throws IOException {115if (!closed) {116closed = true;117throw new IOException("close failed");118}119}120}121122static class FailingCloseWriter extends Writer {123boolean closed;124@Override125public void write(char[] cbuf, int off, int len) throws IOException {126if (closed)127failWithIOE("writer is closed");128}129@Override130public void flush() throws IOException {131if (closed)132failWithIOE("writer is closed");133}134@Override135public void close() throws IOException {136if (!closed) {137closed = true;138throw new IOException("close failed");139}140}141}142143static class FailingFlushWriter extends Writer {144boolean closed;145@Override146public void write(char[] cbuf, int off, int len) throws IOException {147if (closed)148failWithIOE("writer is closed");149}150@Override151public void flush() throws IOException {152if (closed) {153failWithIOE("writer is closed");154} else {155throw new IOException("flush failed");156}157}158@Override159public void close() throws IOException {160if (!closed) {161closed = true;162throw new IOException("close failed");163}164}165}166167static void testFailingClose(InputStream in) throws IOException {168System.out.println(in.getClass());169in.read(new byte[100]);170try {171in.close();172fail("close did not fail");173} catch (IOException expected) { }174try {175in.read(new byte[100]);176fail("read did not fail");177} catch (IOException expected) { }178}179180static void testFailingClose(OutputStream out) throws IOException {181System.out.println(out.getClass());182out.write(1);183try {184out.close();185fail("close did not fail");186} catch (IOException expected) { }187try {188out.write(1);189if (!(out instanceof BufferedOutputStream))190fail("write did not fail");191} catch (IOException expected) { }192}193194static void testFailingFlush(OutputStream out) throws IOException {195System.out.println(out.getClass());196out.write(1);197try {198out.flush();199fail("flush did not fail");200} catch (IOException expected) { }201if (out instanceof BufferedOutputStream) {202out.write(1);203try {204out.close();205fail("close did not fail");206} catch (IOException expected) { }207}208}209210static void testFailingClose(Reader r) throws IOException {211System.out.println(r.getClass());212r.read(new char[100]);213try {214r.close();215fail("close did not fail");216} catch (IOException expected) { }217try {218r.read(new char[100]);219fail("read did not fail");220} catch (IOException expected) { }221}222223static void testFailingClose(Writer w) throws IOException {224System.out.println(w.getClass());225w.write("message");226try {227w.close();228fail("close did not fail");229} catch (IOException expected) { }230try {231w.write("another message");232fail("write did not fail");233} catch (IOException expected) { }234}235236static void testFailingFlush(Writer w) throws IOException {237System.out.println(w.getClass());238w.write("message");239try {240w.flush();241fail("flush did not fail");242} catch (IOException expected) { }243if (w instanceof BufferedWriter) {244// assume this message will be buffered245w.write("another message");246try {247w.close();248fail("close did not fail");249} catch (IOException expected) { }250}251}252253public static void main(String[] args) throws IOException {254255testFailingClose(new BufferedInputStream(new FailingCloseInputStream()));256testFailingClose(new BufferedOutputStream(new FailingCloseOutputStream()));257258testFailingClose(new BufferedReader(new FailingCloseReader()));259testFailingClose(new BufferedWriter(new FailingCloseWriter()));260261testFailingFlush(new BufferedOutputStream(new FailingFlushOutputStream()));262testFailingFlush(new BufferedWriter(new FailingFlushWriter()));263264if (failed > 0)265throw new RuntimeException(failed + " test(s) failed - see log for details");266}267}268269270