1 /*
   2  * Copyright (c) 2015, 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.IOException;
  26 import java.io.InputStream;
  27 import java.util.Arrays;
  28 import java.util.Random;
  29 
  30 /*
  31  * @test
  32  * @bug 8765432
  33  * @library /lib/testlibrary
  34  * @build jdk.testlibrary.*
  35  * @key randomness
  36  * @summary Basic test for InputStream.readAllBytes
  37  */
  38 public class ReadAllBytes {
  39 
  40     // TODO: Improve test coverage...
  41 
  42     private static Random generator = RandomFactory.getRandom();
  43 
  44     public static void main(String[] args) throws IOException {
  45         test(new byte[]{});
  46         test(new byte[]{1, 2, 3});
  47         test(createRandomBytes(1024));
  48         test(createRandomBytes((1 << 13) - 1));
  49         test(createRandomBytes((1 << 13)));
  50         test(createRandomBytes((1 << 13) + 1));
  51         test(createRandomBytes((1 << 15) - 1));
  52         test(createRandomBytes((1 << 15)));
  53         test(createRandomBytes((1 << 15) + 1));
  54         test(createRandomBytes((1 << 17) - 1));
  55         test(createRandomBytes((1 << 17)));
  56         test(createRandomBytes((1 << 17) + 1));
  57     }
  58 
  59     static void test(byte[] expectedBytes) throws IOException {
  60         int expectedLength = expectedBytes.length;
  61         InputStream in = new ByteArrayInputStream(expectedBytes);
  62         byte[] readBytes = in.readAllBytes();
  63 
  64         int x;
  65         byte[] tmp = new byte[10];
  66         check((x = in.read()) == -1,
  67               "Expected end of stream from read(), got " + x);
  68         check((x = in.read(tmp)) == -1,
  69               "Expected end of stream from read(byte[]), got " + x);
  70         check((x = in.read(tmp, 0, tmp.length)) == -1,
  71               "Expected end of stream from read(byte[], int, int), got " + x);
  72         check(in.readAllBytes().length == 0,
  73               "Expected readAllBytes to return empty byte array");
  74         check(expectedLength == readBytes.length,
  75               "Expected length " + expectedLength + ", got " + readBytes.length);
  76         check(Arrays.equals(expectedBytes, readBytes),
  77               "Expected[" + expectedBytes + "], got:[" + readBytes + "]");
  78     }
  79 
  80     static byte[] createRandomBytes(int size) {
  81         byte[] bytes = new byte[size];
  82         generator.nextBytes(bytes);
  83         return bytes;
  84     }
  85 
  86     static void check(boolean cond, Object ... failedArgs) {
  87         if (cond)
  88             return;
  89         StringBuilder sb = new StringBuilder();
  90         for (Object o : failedArgs)
  91             sb.append(o);
  92         throw new RuntimeException(sb.toString());
  93     }
  94 }