Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/misc/IOUtils/ReadAllBytes.java
38839 views
/*1* Copyright (c) 2015, 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;2930import jdk.testlibrary.RandomFactory;3132import sun.misc.IOUtils;3334/*35* @test36* @bug 8080835 819383237* @library /lib/testlibrary38* @build jdk.testlibrary.*39* @run main ReadAllBytes40* @summary Basic test for IOUtils.readAllBytes41* @key randomness42*/4344public class ReadAllBytes {4546private static Random generator = RandomFactory.getRandom();4748public static void main(String[] args) throws IOException {49test(new byte[]{});50test(new byte[]{1, 2, 3});51test(createRandomBytes(1024));52for (int shift : new int[] {13, 14, 15, 17}) {53for (int offset : new int[] {-1, 0, 1}) {54test(createRandomBytes((1 << shift) + offset));55}56}57}5859static void test(byte[] expectedBytes) throws IOException {60int expectedLength = expectedBytes.length;61WrapperInputStream in = new WrapperInputStream(new ByteArrayInputStream(expectedBytes));62byte[] readBytes = IOUtils.readAllBytes(in);6364int x;65byte[] tmp = new byte[10];66check((x = in.read()) == -1,67"Expected end of stream from read(), got " + x);68check((x = in.read(tmp)) == -1,69"Expected end of stream from read(byte[]), got " + x);70check((x = in.read(tmp, 0, tmp.length)) == -1,71"Expected end of stream from read(byte[], int, int), got " + x);72check(IOUtils.readAllBytes(in).length == 0,73"Expected readAllBytes to return empty byte array");74check(expectedLength == readBytes.length,75"Expected length " + expectedLength + ", got " + readBytes.length);76check(Arrays.equals(expectedBytes, readBytes),77"Expected[" + expectedBytes + "], got:[" + readBytes + "]");78check(!in.isClosed(), "Stream unexpectedly closed");79}8081static byte[] createRandomBytes(int size) {82byte[] bytes = new byte[size];83generator.nextBytes(bytes);84return bytes;85}8687static void check(boolean cond, Object ... failedArgs) {88if (cond)89return;90StringBuilder sb = new StringBuilder();91for (Object o : failedArgs)92sb.append(o);93throw new RuntimeException(sb.toString());94}9596static class WrapperInputStream extends FilterInputStream {97private boolean closed;98WrapperInputStream(InputStream in) { super(in); }99@Override public void close() throws IOException { closed = true; in.close(); }100boolean isClosed() { return closed; }101}102}103104105