Path: blob/master/test/jdk/java/io/InputStream/ReadAllBytes.java
41149 views
/*1* Copyright (c) 2015, 2017, 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.ByteArrayInputStream;24import java.io.FilterInputStream;25import java.io.IOException;26import java.io.InputStream;27import java.util.Arrays;28import java.util.Random;29import jdk.test.lib.RandomFactory;3031/*32* @test33* @bug 8080835 819383234* @library /test/lib35* @build jdk.test.lib.RandomFactory36* @run main ReadAllBytes37* @summary Basic test for InputStream.readAllBytes38* @key randomness39*/4041public class ReadAllBytes {4243private static Random generator = RandomFactory.getRandom();4445public static void main(String[] args) throws IOException {46test(new byte[]{});47test(new byte[]{1, 2, 3});48test(createRandomBytes(1024));49for (int shift : new int[] {13, 14, 15, 17}) {50for (int offset : new int[] {-1, 0, 1}) {51test(createRandomBytes((1 << shift) + offset));52}53}54}5556static void test(byte[] expectedBytes) throws IOException {57int expectedLength = expectedBytes.length;58WrapperInputStream in = new WrapperInputStream(new ByteArrayInputStream(expectedBytes));59byte[] readBytes = in.readAllBytes();6061int x;62byte[] tmp = new byte[10];63check((x = in.read()) == -1,64"Expected end of stream from read(), got " + x);65check((x = in.read(tmp)) == -1,66"Expected end of stream from read(byte[]), got " + x);67check((x = in.read(tmp, 0, tmp.length)) == -1,68"Expected end of stream from read(byte[], int, int), got " + x);69check(in.readAllBytes().length == 0,70"Expected readAllBytes to return empty byte array");71check(expectedLength == readBytes.length,72"Expected length " + expectedLength + ", got " + readBytes.length);73check(Arrays.equals(expectedBytes, readBytes),74"Expected[" + expectedBytes + "], got:[" + readBytes + "]");75check(!in.isClosed(), "Stream unexpectedly closed");76}7778static byte[] createRandomBytes(int size) {79byte[] bytes = new byte[size];80generator.nextBytes(bytes);81return bytes;82}8384static void check(boolean cond, Object ... failedArgs) {85if (cond)86return;87StringBuilder sb = new StringBuilder();88for (Object o : failedArgs)89sb.append(o);90throw new RuntimeException(sb.toString());91}9293static class WrapperInputStream extends FilterInputStream {94private boolean closed;95WrapperInputStream(InputStream in) { super(in); }96@Override public void close() throws IOException { closed = true; in.close(); }97boolean isClosed() { return closed; }98}99}100101102