1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package test.java.io.ByteArrayInputStream; 24 25 import java.io.ByteArrayInputStream; 26 import java.io.ByteArrayOutputStream; 27 import java.io.IOException; 28 import java.util.Arrays; 29 import java.util.Objects; 30 import java.util.Random; 31 32 import org.testng.annotations.Test; 33 34 /* @test 35 * @library /test/lib 36 * @build jdk.test.lib.RandomFactory 37 * @run main ReadAllReadNTransferTo 38 * @bug 8180451 39 * @summary Verify ByteArrayInputStream readAllBytes, readNBytes, and transferTo 40 * @key randomness 41 */ 42 public class ReadAllReadNTransferTo { 43 private static final int SIZE = 0x4d4d; 44 45 private static Random random = new Random(); 46 47 @Test testRead()48 public void testRead() throws IOException { 49 byte[] buf = new byte[SIZE]; 50 random.nextBytes(buf); 51 int position = random.nextInt(SIZE/2); 52 int size = random.nextInt(SIZE - position); 53 54 ByteArrayInputStream bais = 55 new ByteArrayInputStream(buf, position, size); 56 int off = size < 2 ? 0 : random.nextInt(size / 2); 57 int len = size - off < 1 ? 0 : random.nextInt(size - off); 58 59 byte[] bN = new byte[off + len]; 60 if (bais.readNBytes(bN, off, len) != len) { 61 throw new RuntimeException("readNBytes return value"); 62 } 63 64 if (!Arrays.equals(Arrays.copyOfRange(bN, off, off+len), 65 Arrays.copyOfRange(buf, position, position+len))) { 66 throw new RuntimeException("readNBytes content"); 67 } 68 69 byte[] bAll = bais.readAllBytes(); 70 Objects.requireNonNull(bAll, "readAllBytes return value"); 71 if (bAll.length != size - len) { 72 throw new RuntimeException("readAllBytes return value length"); 73 } 74 if (!Arrays.equals(bAll, 75 Arrays.copyOfRange( buf, position + len, position + len + bAll.length))) { 76 throw new RuntimeException("readAllBytes content"); 77 } 78 79 // XXX transferTo() 80 bais = new ByteArrayInputStream(buf); 81 ByteArrayOutputStream baos = new ByteArrayOutputStream(buf.length); 82 if (bais.transferTo(baos) != buf.length) { 83 throw new RuntimeException("transferTo return value length"); 84 } 85 if (!Arrays.equals(buf, baos.toByteArray())) { 86 throw new RuntimeException("transferTo content"); 87 } 88 } 89 }