Path: blob/master/test/jdk/java/io/ByteArrayInputStream/ReadAllReadNTransferTo.java
41149 views
/*1* Copyright (c) 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.ByteArrayOutputStream;25import java.io.IOException;26import java.util.Arrays;27import java.util.Objects;28import java.util.Random;29import jdk.test.lib.RandomFactory;3031/* @test32* @library /test/lib33* @build jdk.test.lib.RandomFactory34* @run main ReadAllReadNTransferTo35* @bug 818045136* @summary Verify ByteArrayInputStream readAllBytes, readNBytes, and transferTo37* @key randomness38*/39public class ReadAllReadNTransferTo {40private static final int SIZE = 0x4d4d;4142private static Random random = RandomFactory.getRandom();4344public static void main(String... args) throws IOException {45byte[] buf = new byte[SIZE];46random.nextBytes(buf);47int position = random.nextInt(SIZE/2);48int size = random.nextInt(SIZE - position);4950ByteArrayInputStream bais =51new ByteArrayInputStream(buf, position, size);52int off = size < 2 ? 0 : random.nextInt(size / 2);53int len = size - off < 1 ? 0 : random.nextInt(size - off);5455byte[] bN = new byte[off + len];56if (bais.readNBytes(bN, off, len) != len) {57throw new RuntimeException("readNBytes return value");58}59if (!Arrays.equals(bN, off, off + len,60buf, position, position + len)) {61throw new RuntimeException("readNBytes content");62}6364byte[] bAll = bais.readAllBytes();65Objects.requireNonNull(bAll, "readAllBytes return value");66if (bAll.length != size - len) {67throw new RuntimeException("readAllBytes return value length");68}69if (!Arrays.equals(bAll, 0, bAll.length,70buf, position + len, position + len + bAll.length)) {71throw new RuntimeException("readAllBytes content");72}7374// XXX transferTo()75bais = new ByteArrayInputStream(buf);76ByteArrayOutputStream baos = new ByteArrayOutputStream(buf.length);77if (bais.transferTo(baos) != buf.length) {78throw new RuntimeException("transferTo return value length");79}80if (!Arrays.equals(buf, baos.toByteArray())) {81throw new RuntimeException("transferTo content");82}83}84}858687