/* * Copyright (c) 2017, 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. */ import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.IOException; /* * @test * @bug 4358774 * @summary Check for expected behavior of InputStream.nullStream(). */ public class NullInputStream { public static void main(String[] args) throws IOException { int failures = 0; InputStream in = InputStream.nullStream(); if (in == null) { throw new RuntimeException ("InputStream.nullStream() returned null"); } if (in.available() != 0) { System.err.println("available() != 0"); failures++; } if (in.read() != -1) { System.err.println("read() != -1"); failures++; } if (in.read(new byte[1]) != -1) { System.err.println("read(byte[]) != -1"); failures++; } if (in.read(new byte[1], 0, 1) != -1) { System.err.println("read(byte[],int,int) != -1"); failures++; } if (in.readAllBytes().length != 0) { System.err.println("readAllBytes().length != 0"); failures++; } if (in.readNBytes(new byte[1], 0, 1) != 0) { System.err.println("readNBytes(byte[],int,int) != -1"); failures++; } if (in.transferTo(new ByteArrayOutputStream(7)) != 0) { System.err.println("transferTo() != 0"); failures++; } if (in.skip(1) != 0) { System.err.println("skip() != 0"); failures++; } in.close(); try { in.available(); System.err.println("No IOException from available() after closing"); failures++; } catch (IOException e) { } try { in.read(); System.err.println("No IOException from read() after closing"); failures++; } catch (IOException e) { } try { in.read(new byte[1], 0, 1); System.err.println("No IOException from read(b,i,i) after closing"); failures++; } catch (IOException e) { } try { in.readAllBytes(); System.err.println("No IOException from readAllBytes() after closing"); failures++; } catch (IOException e) { } try { in.readNBytes(new byte[1], 0, 1); System.err.println("No IOException from readNBytes() after closing"); failures++; } catch (IOException e) { } try { in.skip(1); System.err.println("No IOException from skip() after closing"); failures++; } catch (IOException e) { } try { in.transferTo(new ByteArrayOutputStream(7)); System.err.println("No IOException from transferTo() after closing"); failures++; } catch (IOException e) { } if (failures != 0) { throw new RuntimeException("Test failed"); } } }