1 /* 2 * Copyright (c) 2015, 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.ByteArrayInputStream; 25 import java.io.FilterInputStream; 26 import java.io.IOException; 27 import java.io.InputStream; 28 import java.util.Arrays; 29 import java.util.Random; 30 import jdk.test.lib.RandomFactory; 31 32 /* 33 * @test 34 * @bug 8080835 35 * @library /test/lib 36 * @run main ReadNBytes 37 * @summary Basic test for InputStream.readNBytes 38 * @key randomness 39 */ 40 41 public class ReadNBytes { 42 43 private static Random generator = RandomFactory.getRandom(); 44 45 public static void main(String[] args) throws IOException { 46 test(new byte[]{1, 2, 3}); 47 test(createRandomBytes(1024)); 48 test(createRandomBytes((1 << 13) - 1)); 49 test(createRandomBytes((1 << 13))); 50 test(createRandomBytes((1 << 13) + 1)); 51 test(createRandomBytes((1 << 15) - 1)); 52 test(createRandomBytes((1 << 15))); 53 test(createRandomBytes((1 << 15) + 1)); 54 test(createRandomBytes((1 << 17) - 1)); 55 test(createRandomBytes((1 << 17))); 56 test(createRandomBytes((1 << 17) + 1)); 57 } 58 59 static void test(byte[] inputBytes) throws IOException { 60 int length = inputBytes.length; 61 WrapperInputStream in = new WrapperInputStream(new ByteArrayInputStream(inputBytes)); 62 byte[] readBytes = new byte[(length / 2) + 1]; 63 int nread = in.readNBytes(readBytes, 0, readBytes.length); 64 65 int x; 66 byte[] tmp; 67 check(nread == readBytes.length, 68 "Expected number of bytes read: " + readBytes.length + ", got: " + nread); 69 check(Arrays.equals((tmp = Arrays.copyOf(inputBytes, nread)), readBytes), 70 "Expected[" + tmp + "], got:[" + readBytes + "]"); 71 check(!in.isClosed(), "Stream unexpectedly closed"); 72 73 // Read again 74 nread = in.readNBytes(readBytes, 0, readBytes.length); 75 76 check(nread == length - readBytes.length, 77 "Expected number of bytes read: " + (length - readBytes.length) + ", got: " + nread); 78 check(Arrays.equals((tmp = Arrays.copyOfRange(inputBytes, readBytes.length, length)), 79 Arrays.copyOf(readBytes, nread)), 80 "Expected[" + tmp + "], got:[" + readBytes + "]"); 81 // Expect end of stream 82 check((x = in.read()) == -1, 83 "Expected end of stream from read(), got " + x); 84 check((x = in.read(tmp)) == -1, 85 "Expected end of stream from read(byte[]), got " + x); 86 check((x = in.read(tmp, 0, tmp.length)) == -1, 87 "Expected end of stream from read(byte[], int, int), got " + x); 88 check((x = in.readNBytes(tmp, 0, tmp.length)) == 0, 89 "Expected end of stream, 0, from readNBytes(byte[], int, int), got " + x); 90 check(!in.isClosed(), "Stream unexpectedly closed"); 91 } 92 93 static byte[] createRandomBytes(int size) { 94 byte[] bytes = new byte[size]; 95 generator.nextBytes(bytes); 96 return bytes; 97 } 98 99 static void check(boolean cond, Object ... failedArgs) { 100 if (cond) 101 return; 102 StringBuilder sb = new StringBuilder(); 103 for (Object o : failedArgs) 104 sb.append(o); 105 throw new RuntimeException(sb.toString()); 106 } 107 108 109 static class WrapperInputStream extends FilterInputStream { 110 private boolean closed; 111 WrapperInputStream(InputStream in) { super(in); } 112 @Override public void close() throws IOException { closed = true; in.close(); } 113 boolean isClosed() { return closed; } 114 } 115 }