< prev index next >

test/jdk/java/io/InputStream/ReadNBytes.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. --- 1,7 ---- /* ! * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.
*** 29,39 **** import java.util.Random; import jdk.test.lib.RandomFactory; /* * @test ! * @bug 8080835 * @library /test/lib * @build jdk.test.lib.RandomFactory * @run main ReadNBytes * @summary Basic test for InputStream.readNBytes * @key randomness --- 29,39 ---- import java.util.Random; import jdk.test.lib.RandomFactory; /* * @test ! * @bug 8080835 8139206 * @library /test/lib * @build jdk.test.lib.RandomFactory * @run main ReadNBytes * @summary Basic test for InputStream.readNBytes * @key randomness
*** 44,62 **** private static Random generator = RandomFactory.getRandom(); public static void main(String[] args) throws IOException { test(new byte[]{1, 2, 3}); test(createRandomBytes(1024)); ! test(createRandomBytes((1 << 13) - 1)); ! test(createRandomBytes((1 << 13))); ! test(createRandomBytes((1 << 13) + 1)); ! test(createRandomBytes((1 << 15) - 1)); ! test(createRandomBytes((1 << 15))); ! test(createRandomBytes((1 << 15) + 1)); ! test(createRandomBytes((1 << 17) - 1)); ! test(createRandomBytes((1 << 17))); ! test(createRandomBytes((1 << 17) + 1)); } static void test(byte[] inputBytes) throws IOException { int length = inputBytes.length; WrapperInputStream in = new WrapperInputStream(new ByteArrayInputStream(inputBytes)); --- 44,66 ---- private static Random generator = RandomFactory.getRandom(); public static void main(String[] args) throws IOException { test(new byte[]{1, 2, 3}); test(createRandomBytes(1024)); ! for (int shift : new int[] {13, 15, 17}) { ! for (int offset : new int[] {-1, 0, 1}) { ! test(createRandomBytes((1 << shift) + offset)); ! } ! } ! ! test(-1); ! test(0); ! for (int shift : new int[] {13, 15, 17}) { ! for (int offset : new int[] {-1, 0, 1}) { ! test((1 << shift) + offset); ! } ! } } static void test(byte[] inputBytes) throws IOException { int length = inputBytes.length; WrapperInputStream in = new WrapperInputStream(new ByteArrayInputStream(inputBytes));
*** 89,98 **** --- 93,142 ---- check((x = in.readNBytes(tmp, 0, tmp.length)) == 0, "Expected end of stream, 0, from readNBytes(byte[], int, int), got " + x); check(!in.isClosed(), "Stream unexpectedly closed"); } + static void test(int max) throws IOException { + byte[] inputBytes = max <= 0 ? new byte[0] : createRandomBytes(max); + WrapperInputStream in = + new WrapperInputStream(new ByteArrayInputStream(inputBytes)); + + if (max < 0) { + try { + in.readNBytes(max); + check(false, "Expected IllegalArgumentException not thrown"); + } catch (IllegalArgumentException iae) { + return; + } + } else if (max == 0) { + int x; + check((x = in.readNBytes(max).length) == 0, + "Expected zero bytes, got " + x); + return; + } + + int off = Math.toIntExact(in.skip(generator.nextInt(max/2))); + int len = generator.nextInt(max - 1 - off); + byte[] readBytes = in.readNBytes(len); + check(readBytes.length == len, + "Expected " + len + " bytes, got " + readBytes.length); + check(Arrays.equals(inputBytes, off, off + len, readBytes, 0, len), + "Expected[" + Arrays.copyOfRange(inputBytes, off, off + len) + + "], got:[" + readBytes + "]"); + + int remaining = max - (off + len); + readBytes = in.readNBytes(remaining); + check(readBytes.length == remaining, + "Expected " + remaining + "bytes, got " + readBytes.length); + check(Arrays.equals(inputBytes, off + len, max, + readBytes, 0, remaining), + "Expected[" + Arrays.copyOfRange(inputBytes, off + len, max) + + "], got:[" + readBytes + "]"); + + check(!in.isClosed(), "Stream unexpectedly closed"); + } + static byte[] createRandomBytes(int size) { byte[] bytes = new byte[size]; generator.nextBytes(bytes); return bytes; }
< prev index next >