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