1 /*
   2  * Copyright (c) 2017, 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 
  24 import java.io.ByteArrayInputStream;
  25 import java.io.ByteArrayOutputStream;
  26 import java.io.IOException;
  27 import java.util.Arrays;
  28 import java.util.Objects;
  29 import java.util.Random;
  30 import jdk.test.lib.RandomFactory;
  31 
  32 /* @test
  33  * @library /test/lib
  34  * @build jdk.test.lib.RandomFactory
  35  * @run main ReadAllReadNTransferTo
  36  * @bug 8180451
  37  * @summary Verify ByteArrayInputStream readAllBytes, readNBytes, and transferTo
  38  * @key randomness
  39  */
  40 public class ReadAllReadNTransferTo {
  41     private static final int SIZE = 0x4d4d;
  42 
  43     private static Random random = RandomFactory.getRandom();
  44 
  45     public static void main(String... args) throws IOException {
  46         byte[] buf = new byte[SIZE];
  47         random.nextBytes(buf);
  48         int offset = random.nextInt(SIZE/2);
  49         int length = random.nextInt(SIZE - offset);
  50 
  51         ByteArrayInputStream bais =
  52             new ByteArrayInputStream(buf, offset, length);
  53         int off = random.nextInt(length/2);
  54         int len = random.nextInt(length - off);
  55 
  56         byte[] bN = new byte[off + len];
  57         if (bais.readNBytes(bN, off, len) != len) {
  58             throw new RuntimeException("readNBytes return value");
  59         }
  60         if (!Arrays.equals(bN, off, off + len,
  61             buf, offset, offset + len)) {
  62             throw new RuntimeException("readNBytes content");
  63         }
  64 
  65         byte[] bAll = bais.readAllBytes();
  66         Objects.requireNonNull(bAll, "readAllBytes return value");
  67         if (bAll.length != length - len) {
  68             throw new RuntimeException("readAllBytes return value length");
  69         }
  70         if (!Arrays.equals(bAll, 0, bAll.length,
  71             buf, offset + len, offset + len + bAll.length)) {
  72             throw new RuntimeException("readAllBytes content");
  73         }
  74 
  75         // XXX transferTo()
  76         bais = new ByteArrayInputStream(buf);
  77         ByteArrayOutputStream baos = new ByteArrayOutputStream(buf.length);
  78         if (bais.transferTo(baos) != buf.length) {
  79             throw new RuntimeException("transferTo return value length");
  80         }
  81         if (!Arrays.equals(buf, baos.toByteArray())) {
  82             throw new RuntimeException("transferTo content");
  83         }
  84     }
  85 }