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.readNBytes
  37  */
  38 public class ReadNBytes {
  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[]{1, 2, 3});
  46         test(createRandomBytes(1024));
  47         test(createRandomBytes((1 << 13) - 1));
  48         test(createRandomBytes((1 << 13)));
  49         test(createRandomBytes((1 << 13) + 1));
  50         test(createRandomBytes((1 << 15) - 1));
  51         test(createRandomBytes((1 << 15)));
  52         test(createRandomBytes((1 << 15) + 1));
  53         test(createRandomBytes((1 << 17) - 1));
  54         test(createRandomBytes((1 << 17)));
  55         test(createRandomBytes((1 << 17) + 1));
  56     }
  57 
  58     static void test(byte[] inputBytes) throws IOException {
  59         int length = inputBytes.length;
  60         InputStream in = new ByteArrayInputStream(inputBytes);
  61         byte[] readBytes = new byte[roundUp(length) >> 1];
  62         int nread = in.readNBytes(readBytes, 0, readBytes.length);
  63 
  64         int x;
  65         byte[] tmp;
  66         check(nread == readBytes.length,
  67                 "Expected number of bytes read: " + readBytes.length + ", got: " + nread);
  68         check(Arrays.equals((tmp = Arrays.copyOf(inputBytes, nread)), readBytes),
  69               "Expected[" + tmp + "], got:[" + readBytes + "]");
  70 
  71         // Read again
  72         nread = in.readNBytes(readBytes, 0, readBytes.length);
  73 
  74         check(nread == length - readBytes.length,
  75               "Expected number of bytes read: " + (length - readBytes.length) + ", got: " + nread);
  76         check(Arrays.equals((tmp = Arrays.copyOfRange(inputBytes, readBytes.length, length)),
  77                             Arrays.copyOf(readBytes, nread)),
  78               "Expected[" + tmp + "], got:[" + readBytes + "]");
  79         // Expect end of stream
  80         check((x = in.read()) == -1,
  81               "Expected end of stream from read(), got " + x);
  82         check((x = in.read(tmp)) == -1,
  83               "Expected end of stream from read(byte[]), got " + x);
  84         check((x = in.read(tmp, 0, tmp.length)) == -1,
  85               "Expected end of stream from read(byte[], int, int), got " + x);
  86         check((x = in.readNBytes(tmp, 0, tmp.length)) == 0,
  87               "Expected end of stream, 0, from readNBytes(byte[], int, int), got " + x);
  88     }
  89 
  90     static byte[] createRandomBytes(int size) {
  91         byte[] bytes = new byte[size];
  92         generator.nextBytes(bytes);
  93         return bytes;
  94     }
  95 
  96     private static int roundUp(int size) {
  97         int x = Integer.highestOneBit(size);
  98         if (x != size)
  99             x = x << 1;
 100         return x;
 101     }
 102 
 103     static void check(boolean cond, Object ... failedArgs) {
 104         if (cond)
 105             return;
 106         StringBuilder sb = new StringBuilder();
 107         for (Object o : failedArgs)
 108             sb.append(o);
 109         throw new RuntimeException(sb.toString());
 110     }
 111 }