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 package test.java.io.InputStream; 24 25 import java.io.ByteArrayInputStream; 26 import java.io.FilterInputStream; 27 import java.io.IOException; 28 import java.io.InputStream; 29 import java.util.Arrays; 30 import java.util.Random; 31 import org.testng.annotations.Test; 32 33 /* 34 * @test 35 * @bug 8080835 8193832 36 * @library /test/lib 37 * @build jdk.test.lib.RandomFactory 38 * @run main ReadAllBytes 39 * @summary Basic test for InputStream.readAllBytes 40 * @key randomness 41 */ 42 43 public class ReadAllBytes { 44 45 private static Random generator = new Random(); 46 47 @Test testReadAllBytes()48 public void testReadAllBytes() throws IOException { 49 test(new byte[]{}); 50 test(new byte[]{1, 2, 3}); 51 test(createRandomBytes(1024)); 52 for (int shift : new int[] {13, 14, 15, 17}) { 53 for (int offset : new int[] {-1, 0, 1}) { 54 test(createRandomBytes((1 << shift) + offset)); 55 } 56 } 57 } 58 test(byte[] expectedBytes)59 static void test(byte[] expectedBytes) throws IOException { 60 int expectedLength = expectedBytes.length; 61 WrapperInputStream in = new WrapperInputStream(new ByteArrayInputStream(expectedBytes)); 62 byte[] readBytes = in.readAllBytes(); 63 64 int x; 65 byte[] tmp = new byte[10]; 66 check((x = in.read()) == -1, 67 "Expected end of stream from read(), got " + x); 68 check((x = in.read(tmp)) == -1, 69 "Expected end of stream from read(byte[]), got " + x); 70 check((x = in.read(tmp, 0, tmp.length)) == -1, 71 "Expected end of stream from read(byte[], int, int), got " + x); 72 check(in.readAllBytes().length == 0, 73 "Expected readAllBytes to return empty byte array"); 74 check(expectedLength == readBytes.length, 75 "Expected length " + expectedLength + ", got " + readBytes.length); 76 check(Arrays.equals(expectedBytes, readBytes), 77 "Expected[" + expectedBytes + "], got:[" + readBytes + "]"); 78 check(!in.isClosed(), "Stream unexpectedly closed"); 79 } 80 createRandomBytes(int size)81 static byte[] createRandomBytes(int size) { 82 byte[] bytes = new byte[size]; 83 generator.nextBytes(bytes); 84 return bytes; 85 } 86 check(boolean cond, Object ... failedArgs)87 static void check(boolean cond, Object ... failedArgs) { 88 if (cond) 89 return; 90 StringBuilder sb = new StringBuilder(); 91 for (Object o : failedArgs) 92 sb.append(o); 93 throw new RuntimeException(sb.toString()); 94 } 95 96 static class WrapperInputStream extends FilterInputStream { 97 private boolean closed; WrapperInputStream(InputStream in)98 WrapperInputStream(InputStream in) { super(in); } close()99 @Override public void close() throws IOException { closed = true; in.close(); } isClosed()100 boolean isClosed() { return closed; } 101 } 102 }