1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.io.ByteArrayOutputStream; 25 import java.io.InputStream; 26 import java.io.IOException; 27 28 /* 29 * @test 30 * @bug 4358774 31 * @summary Check for expected behavior of InputStream.nullStream(). 32 */ 33 public class NullInputStream { 34 public static void main(String[] args) throws IOException { 35 int failures = 0; 36 37 InputStream in = InputStream.nullStream(); 38 if (in == null) { 39 throw new RuntimeException 40 ("InputStream.nullStream() returned null"); 41 } 42 43 if (in.available() != 0) { 44 System.err.println("available() != 0"); 45 failures++; 46 } 47 48 if (in.read() != -1) { 49 System.err.println("read() != -1"); 50 failures++; 51 } 52 53 if (in.read(new byte[1]) != -1) { 54 System.err.println("read(byte[]) != -1"); 55 failures++; 56 } 57 58 if (in.read(new byte[1], 0, 1) != -1) { 59 System.err.println("read(byte[],int,int) != -1"); 60 failures++; 61 } 62 63 if (in.readAllBytes().length != 0) { 64 System.err.println("readAllBytes().length != 0"); 65 failures++; 66 } 67 68 if (in.readNBytes(new byte[1], 0, 1) != 0) { 69 System.err.println("readNBytes(byte[],int,int) != -1"); 70 failures++; 71 } 72 73 if (in.transferTo(new ByteArrayOutputStream(7)) != 0) { 74 System.err.println("transferTo() != 0"); 75 failures++; 76 } 77 78 if (in.skip(1) != 0) { 79 System.err.println("skip() != 0"); 80 failures++; 81 } 82 83 in.close(); 84 try { 85 in.available(); 86 System.err.println("No IOException from available() after closing"); 87 failures++; 88 } catch (IOException e) { 89 } 90 try { 91 in.read(); 92 System.err.println("No IOException from read() after closing"); 93 failures++; 94 } catch (IOException e) { 95 } 96 try { 97 in.read(new byte[1], 0, 1); 98 System.err.println("No IOException from read(b,i,i) after closing"); 99 failures++; 100 } catch (IOException e) { 101 } 102 try { 103 in.readAllBytes(); 104 System.err.println("No IOException from readAllBytes() after closing"); 105 failures++; 106 } catch (IOException e) { 107 } 108 try { 109 in.readNBytes(new byte[1], 0, 1); 110 System.err.println("No IOException from readNBytes() after closing"); 111 failures++; 112 } catch (IOException e) { 113 } 114 try { 115 in.skip(1); 116 System.err.println("No IOException from skip() after closing"); 117 failures++; 118 } catch (IOException e) { 119 } 120 try { 121 in.transferTo(new ByteArrayOutputStream(7)); 122 System.err.println("No IOException from transferTo() after closing"); 123 failures++; 124 } catch (IOException e) { 125 } 126 127 if (failures != 0) { 128 throw new RuntimeException("Test failed"); 129 } 130 } 131 }