Path: blob/master/test/jdk/java/io/InputStream/ReadNBytes.java
41149 views
/*1* Copyright (c) 2015, 2020, 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 8139206 825474234* @library /test/lib35* @build jdk.test.lib.RandomFactory36* @run main ReadNBytes37* @summary Basic test for InputStream.readNBytes38* @key randomness39*/4041public class ReadNBytes {4243private static Random generator = RandomFactory.getRandom();4445public static void main(String[] args) throws IOException {46test();47test(new byte[]{1, 2, 3});48test(createRandomBytes(1024));49for (int shift : new int[] {13, 15, 17}) {50for (int offset : new int[] {-1, 0, 1}) {51test(createRandomBytes((1 << shift) + offset));52}53}5455test(-1);56test(0);57for (int shift : new int[] {13, 15, 17}) {58for (int offset : new int[] {-1, 0, 1}) {59test((1 << shift) + offset);60}61}62}6364static void test(byte[] inputBytes) throws IOException {65int length = inputBytes.length;66WrapperInputStream in = new WrapperInputStream(new ByteArrayInputStream(inputBytes));67byte[] readBytes = new byte[(length / 2) + 1];68int nread = in.readNBytes(readBytes, 0, readBytes.length);6970int x;71byte[] tmp;72check(nread == readBytes.length,73"Expected number of bytes read: " + readBytes.length + ", got: " + nread);74check(Arrays.equals((tmp = Arrays.copyOf(inputBytes, nread)), readBytes),75"Expected[" + tmp + "], got:[" + readBytes + "]");76check(!in.isClosed(), "Stream unexpectedly closed");7778// Read again79nread = in.readNBytes(readBytes, 0, readBytes.length);8081check(nread == length - readBytes.length,82"Expected number of bytes read: " + (length - readBytes.length) + ", got: " + nread);83check(Arrays.equals((tmp = Arrays.copyOfRange(inputBytes, readBytes.length, length)),84Arrays.copyOf(readBytes, nread)),85"Expected[" + tmp + "], got:[" + readBytes + "]");86// Expect end of stream87check((x = in.read()) == -1,88"Expected end of stream from read(), got " + x);89check((x = in.read(tmp)) == -1,90"Expected end of stream from read(byte[]), got " + x);91check((x = in.read(tmp, 0, tmp.length)) == -1,92"Expected end of stream from read(byte[], int, int), got " + x);93check((x = in.readNBytes(tmp, 0, tmp.length)) == 0,94"Expected end of stream, 0, from readNBytes(byte[], int, int), got " + x);95check(!in.isClosed(), "Stream unexpectedly closed");96}9798static void test(int max) throws IOException {99byte[] inputBytes = max <= 0 ? new byte[0] : createRandomBytes(max);100WrapperInputStream in =101new WrapperInputStream(new ByteArrayInputStream(inputBytes));102103if (max < 0) {104try {105in.readNBytes(max);106check(false, "Expected IllegalArgumentException not thrown");107} catch (IllegalArgumentException iae) {108return;109}110} else if (max == 0) {111int x;112check((x = in.readNBytes(max).length) == 0,113"Expected zero bytes, got " + x);114return;115}116117int off = Math.toIntExact(in.skip(generator.nextInt(max/2)));118int len = generator.nextInt(max - 1 - off);119byte[] readBytes = in.readNBytes(len);120check(readBytes.length == len,121"Expected " + len + " bytes, got " + readBytes.length);122check(Arrays.equals(inputBytes, off, off + len, readBytes, 0, len),123"Expected[" + Arrays.copyOfRange(inputBytes, off, off + len) +124"], got:[" + readBytes + "]");125126int remaining = max - (off + len);127readBytes = in.readNBytes(remaining);128check(readBytes.length == remaining,129"Expected " + remaining + "bytes, got " + readBytes.length);130check(Arrays.equals(inputBytes, off + len, max,131readBytes, 0, remaining),132"Expected[" + Arrays.copyOfRange(inputBytes, off + len, max) +133"], got:[" + readBytes + "]");134135check(!in.isClosed(), "Stream unexpectedly closed");136}137138static void test() throws IOException {139final int chunkSize = 8192;140int size = (10 + generator.nextInt(11))*chunkSize;141142byte[] buf = new byte[size];143generator.nextBytes(buf);144InputStream s = new ThrottledByteArrayInputStream(buf);145146byte[] b = s.readNBytes(size);147148check(Arrays.equals(b, buf), "Arrays not equal");149}150151static byte[] createRandomBytes(int size) {152byte[] bytes = new byte[size];153generator.nextBytes(bytes);154return bytes;155}156157static void check(boolean cond, Object ... failedArgs) {158if (cond)159return;160StringBuilder sb = new StringBuilder();161for (Object o : failedArgs)162sb.append(o);163throw new RuntimeException(sb.toString());164}165166static class WrapperInputStream extends FilterInputStream {167private boolean closed;168WrapperInputStream(InputStream in) { super(in); }169@Override public void close() throws IOException { closed = true; in.close(); }170boolean isClosed() { return closed; }171}172173static class ThrottledByteArrayInputStream extends ByteArrayInputStream {174private int count = 0;175176ThrottledByteArrayInputStream(byte[] buf) {177super(buf);178}179180@Override181//182// Sometimes return zero or a smaller count than requested.183//184public int read(byte[] buf, int off, int len) {185if (generator.nextBoolean()) {186return 0;187} else if (++count / 3 == 0) {188len /= 3;189}190191return super.read(buf, off, len);192}193}194}195196197