test/jdk/java/io/InputStream/ReadNBytes.java
changeset 48696 917868f73209
parent 47216 71c04702a3d5
equal deleted inserted replaced
48695:a7ce228abcd7 48696:917868f73209
     1 /*
     1 /*
     2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    29 import java.util.Random;
    29 import java.util.Random;
    30 import jdk.test.lib.RandomFactory;
    30 import jdk.test.lib.RandomFactory;
    31 
    31 
    32 /*
    32 /*
    33  * @test
    33  * @test
    34  * @bug 8080835
    34  * @bug 8080835 8139206
    35  * @library /test/lib
    35  * @library /test/lib
    36  * @build jdk.test.lib.RandomFactory
    36  * @build jdk.test.lib.RandomFactory
    37  * @run main ReadNBytes
    37  * @run main ReadNBytes
    38  * @summary Basic test for InputStream.readNBytes
    38  * @summary Basic test for InputStream.readNBytes
    39  * @key randomness
    39  * @key randomness
    44     private static Random generator = RandomFactory.getRandom();
    44     private static Random generator = RandomFactory.getRandom();
    45 
    45 
    46     public static void main(String[] args) throws IOException {
    46     public static void main(String[] args) throws IOException {
    47         test(new byte[]{1, 2, 3});
    47         test(new byte[]{1, 2, 3});
    48         test(createRandomBytes(1024));
    48         test(createRandomBytes(1024));
    49         test(createRandomBytes((1 << 13) - 1));
    49         for (int shift : new int[] {13, 15, 17}) {
    50         test(createRandomBytes((1 << 13)));
    50             for (int offset : new int[] {-1, 0, 1}) {
    51         test(createRandomBytes((1 << 13) + 1));
    51                 test(createRandomBytes((1 << shift) + offset));
    52         test(createRandomBytes((1 << 15) - 1));
    52             }
    53         test(createRandomBytes((1 << 15)));
    53         }
    54         test(createRandomBytes((1 << 15) + 1));
    54 
    55         test(createRandomBytes((1 << 17) - 1));
    55         test(-1);
    56         test(createRandomBytes((1 << 17)));
    56         test(0);
    57         test(createRandomBytes((1 << 17) + 1));
    57         for (int shift : new int[] {13, 15, 17}) {
       
    58             for (int offset : new int[] {-1, 0, 1}) {
       
    59                 test((1 << shift) + offset);
       
    60             }
       
    61         }
    58     }
    62     }
    59 
    63 
    60     static void test(byte[] inputBytes) throws IOException {
    64     static void test(byte[] inputBytes) throws IOException {
    61         int length = inputBytes.length;
    65         int length = inputBytes.length;
    62         WrapperInputStream in = new WrapperInputStream(new ByteArrayInputStream(inputBytes));
    66         WrapperInputStream in = new WrapperInputStream(new ByteArrayInputStream(inputBytes));
    89         check((x = in.readNBytes(tmp, 0, tmp.length)) == 0,
    93         check((x = in.readNBytes(tmp, 0, tmp.length)) == 0,
    90               "Expected end of stream, 0, from readNBytes(byte[], int, int), got " + x);
    94               "Expected end of stream, 0, from readNBytes(byte[], int, int), got " + x);
    91         check(!in.isClosed(), "Stream unexpectedly closed");
    95         check(!in.isClosed(), "Stream unexpectedly closed");
    92     }
    96     }
    93 
    97 
       
    98     static void test(int max) throws IOException {
       
    99         byte[] inputBytes = max <= 0 ? new byte[0] : createRandomBytes(max);
       
   100         WrapperInputStream in =
       
   101             new WrapperInputStream(new ByteArrayInputStream(inputBytes));
       
   102 
       
   103         if (max < 0) {
       
   104             try {
       
   105                 in.readNBytes(max);
       
   106                 check(false, "Expected IllegalArgumentException not thrown");
       
   107             } catch (IllegalArgumentException iae) {
       
   108                 return;
       
   109             }
       
   110         } else if (max == 0) {
       
   111             int x;
       
   112             check((x = in.readNBytes(max).length) == 0,
       
   113                   "Expected zero bytes, got " + x);
       
   114             return;
       
   115         }
       
   116 
       
   117         int off = Math.toIntExact(in.skip(generator.nextInt(max/2)));
       
   118         int len = generator.nextInt(max - 1 - off);
       
   119         byte[] readBytes = in.readNBytes(len);
       
   120         check(readBytes.length == len,
       
   121               "Expected " + len + " bytes, got " + readBytes.length);
       
   122         check(Arrays.equals(inputBytes, off, off + len, readBytes, 0, len),
       
   123               "Expected[" + Arrays.copyOfRange(inputBytes, off, off + len) +
       
   124               "], got:[" + readBytes + "]");
       
   125 
       
   126         int remaining = max - (off + len);
       
   127         readBytes = in.readNBytes(remaining);
       
   128         check(readBytes.length == remaining,
       
   129               "Expected " + remaining + "bytes, got " + readBytes.length);
       
   130         check(Arrays.equals(inputBytes, off + len, max,
       
   131               readBytes, 0, remaining),
       
   132           "Expected[" + Arrays.copyOfRange(inputBytes, off + len, max) +
       
   133           "], got:[" + readBytes + "]");
       
   134 
       
   135         check(!in.isClosed(), "Stream unexpectedly closed");
       
   136     }
       
   137 
    94     static byte[] createRandomBytes(int size) {
   138     static byte[] createRandomBytes(int size) {
    95         byte[] bytes = new byte[size];
   139         byte[] bytes = new byte[size];
    96         generator.nextBytes(bytes);
   140         generator.nextBytes(bytes);
    97         return bytes;
   141         return bytes;
    98     }
   142     }