Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/misc/IOUtils/ReadNBytes.java
38839 views
/*1* Copyright (c) 2015, 2018, 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.testlibrary.RandomFactory;3031import sun.misc.IOUtils;3233/*34* @test35* @bug 8080835 813920636* @library /lib/testlibrary37* @build jdk.testlibrary.*38* @run main ReadNBytes39* @summary Basic test for IOUtils.readNBytes40* @key randomness41*/4243public class ReadNBytes {4445private static Random generator = RandomFactory.getRandom();4647public static void main(String[] args) throws IOException {48test(new byte[]{1, 2, 3});49test(createRandomBytes(1024));50for (int shift : new int[] {13, 15, 17}) {51for (int offset : new int[] {-1, 0, 1}) {52test(createRandomBytes((1 << shift) + offset));53}54}5556test(-1);57test(0);58for (int shift : new int[] {13, 15, 17}) {59for (int offset : new int[] {-1, 0, 1}) {60test((1 << shift) + offset);61}62}63}6465static void test(byte[] inputBytes) throws IOException {66int length = inputBytes.length;67WrapperInputStream in = new WrapperInputStream(new ByteArrayInputStream(inputBytes));68byte[] readBytes = new byte[(length / 2) + 1];69int nread = IOUtils.readNBytes(in, readBytes, 0, readBytes.length);7071int x;72byte[] tmp;73check(nread == readBytes.length,74"Expected number of bytes read: " + readBytes.length + ", got: " + nread);75check(Arrays.equals((tmp = Arrays.copyOf(inputBytes, nread)), readBytes),76"Expected[" + tmp + "], got:[" + readBytes + "]");77check(!in.isClosed(), "Stream unexpectedly closed");7879// Read again80nread = IOUtils.readNBytes(in, readBytes, 0, readBytes.length);8182check(nread == length - readBytes.length,83"Expected number of bytes read: " + (length - readBytes.length) + ", got: " + nread);84check(Arrays.equals((tmp = Arrays.copyOfRange(inputBytes, readBytes.length, length)),85Arrays.copyOf(readBytes, nread)),86"Expected[" + tmp + "], got:[" + readBytes + "]");87// Expect end of stream88check((x = in.read()) == -1,89"Expected end of stream from read(), got " + x);90check((x = in.read(tmp)) == -1,91"Expected end of stream from read(byte[]), got " + x);92check((x = in.read(tmp, 0, tmp.length)) == -1,93"Expected end of stream from read(byte[], int, int), got " + x);94check((x = IOUtils.readNBytes(in, tmp, 0, tmp.length)) == 0,95"Expected end of stream, 0, from readNBytes(byte[], int, int), got " + x);96check(!in.isClosed(), "Stream unexpectedly closed");97}9899static void test(int max) throws IOException {100byte[] subset1, subset2;101byte[] inputBytes = max <= 0 ? new byte[0] : createRandomBytes(max);102WrapperInputStream in =103new WrapperInputStream(new ByteArrayInputStream(inputBytes));104105if (max < 0) {106try {107IOUtils.readNBytes(in, max);108check(false, "Expected IllegalArgumentException not thrown");109} catch (IllegalArgumentException iae) {110return;111}112} else if (max == 0) {113int x;114check((x = IOUtils.readNBytes(in, max).length) == 0,115"Expected zero bytes, got " + x);116return;117}118119int off = Math.toIntExact(in.skip(generator.nextInt(max/2)));120int len = generator.nextInt(max - 1 - off);121byte[] readBytes = IOUtils.readNBytes(in, len);122check(readBytes.length == len,123"Expected " + len + " bytes, got " + readBytes.length);124subset1 = Arrays.copyOfRange(inputBytes, off, off + len);125subset2 = Arrays.copyOfRange(readBytes, 0, len);126check(Arrays.equals(subset1, subset2), "Expected[" + subset1 +127"], got:[" + readBytes + "]");128129int remaining = max - (off + len);130readBytes = IOUtils.readNBytes(in, remaining);131check(readBytes.length == remaining,132"Expected " + remaining + "bytes, got " + readBytes.length);133subset1 = Arrays.copyOfRange(inputBytes, off + len, max);134subset2 = Arrays.copyOfRange(readBytes, 0, remaining);135check(Arrays.equals(subset1, subset2), "Expected[" + subset1 +136"], got:[" + readBytes + "]");137138check(!in.isClosed(), "Stream unexpectedly closed");139}140141static byte[] createRandomBytes(int size) {142byte[] bytes = new byte[size];143generator.nextBytes(bytes);144return bytes;145}146147static void check(boolean cond, Object ... failedArgs) {148if (cond)149return;150StringBuilder sb = new StringBuilder();151for (Object o : failedArgs)152sb.append(o);153throw new RuntimeException(sb.toString());154}155156157static class WrapperInputStream extends FilterInputStream {158private boolean closed;159WrapperInputStream(InputStream in) { super(in); }160@Override public void close() throws IOException { closed = true; in.close(); }161boolean isClosed() { return closed; }162}163}164165166