jdk/test/java/io/BufferedInputStream/LargeCopyWithMark.java
author ykantser
Fri, 09 May 2014 15:24:57 +0200
changeset 24366 57c0a8b7a936
parent 19587 64631ffc11e3
permissions -rw-r--r--
8034960: Serviceability tests using @library failing with java.lang.NoClassDefFoundError Reviewed-by: sla, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19587
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
     1
/*
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
     4
 *
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
     7
 * published by the Free Software Foundation.
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
     8
 *
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    13
 * accompanied this code).
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    14
 *
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    18
 *
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    21
 * questions.
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    22
 */
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    23
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    24
/* @test
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    25
 * @bug 7129312
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    26
 * @summary BufferedInputStream calculates negative array size with large
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    27
 *          streams and mark
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    28
 * @library /lib/testlibrary
24366
57c0a8b7a936 8034960: Serviceability tests using @library failing with java.lang.NoClassDefFoundError
ykantser
parents: 19587
diff changeset
    29
 * @build jdk.testlibrary.*
19587
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    30
 * @run main/othervm LargeCopyWithMark
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    31
 */
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    32
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    33
import java.io.BufferedInputStream;
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    34
import java.io.IOException;
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    35
import java.io.InputStream;
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    36
import java.io.OutputStream;
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    37
import static jdk.testlibrary.ProcessTools.*;
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    38
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    39
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    40
public class LargeCopyWithMark {
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    41
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    42
    public static void main(String[] args) throws Exception {
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    43
        if (! System.getProperty("os.arch").contains("64")) {
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    44
            System.out.println("Test runs on 64 bit platforms");
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    45
            return;
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    46
        }
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    47
        ProcessBuilder pb = createJavaProcessBuilder("-Xmx4G",
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    48
                "-ea:LargeCopyWithMark$Child",
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    49
                "LargeCopyWithMark$Child");
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    50
        int res = pb.inheritIO().start().waitFor();
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    51
        if (res != 0) {
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    52
            throw new AssertionError("Test failed: exit code = " + res);
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    53
        }
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    54
    }
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    55
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    56
    public static class Child {
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    57
        static final int BUFF_SIZE = 8192;
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    58
        static final int BIS_BUFF_SIZE = Integer.MAX_VALUE / 2 + 100;
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    59
        static final long BYTES_TO_COPY = 2L * Integer.MAX_VALUE;
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    60
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    61
        static {
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    62
            assert BIS_BUFF_SIZE * 2 < 0 : "doubling must overflow";
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    63
        }
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    64
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    65
        public static void main(String[] args) throws Exception {
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    66
            byte[] buff = new byte[BUFF_SIZE];
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    67
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    68
            try (InputStream myis = new MyInputStream(BYTES_TO_COPY);
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    69
                 InputStream bis = new BufferedInputStream(myis, BIS_BUFF_SIZE);
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    70
                 OutputStream myos = new MyOutputStream()) {
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    71
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    72
                // will require a buffer bigger than BIS_BUFF_SIZE
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    73
                bis.mark(BIS_BUFF_SIZE + 100);
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    74
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    75
                for (;;) {
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    76
                    int count = bis.read(buff, 0, BUFF_SIZE);
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    77
                    if (count == -1)
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    78
                        break;
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    79
                    myos.write(buff, 0, count);
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    80
                }
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    81
            } catch (java.lang.NegativeArraySizeException e) {
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    82
                e.printStackTrace();
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    83
                System.exit(11);
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    84
            } catch (Exception e) {
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    85
                e.printStackTrace();
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    86
            }
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    87
        }
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    88
    }
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    89
}
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    90
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    91
class MyInputStream extends InputStream {
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    92
    private long bytesLeft;
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    93
    public MyInputStream(long bytesLeft) {
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    94
        this.bytesLeft = bytesLeft;
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    95
    }
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    96
    @Override public int read() throws IOException {
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    97
        return 0;
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    98
    }
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
    99
    @Override public int read(byte[] b) throws IOException {
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
   100
        return read(b, 0, b.length);
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
   101
    }
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
   102
    @Override public int read(byte[] b, int off, int len) throws IOException {
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
   103
        if (bytesLeft <= 0)
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
   104
            return -1;
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
   105
        long result = Math.min(bytesLeft, (long)len);
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
   106
        bytesLeft -= result;
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
   107
        return (int)result;
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
   108
    }
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
   109
    @Override public int available() throws IOException {
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
   110
        return (bytesLeft > 0) ? 1 : 0;
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
   111
    }
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
   112
}
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
   113
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
   114
class MyOutputStream extends OutputStream {
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
   115
    @Override public void write(int b) throws IOException {}
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
   116
    @Override public void write(byte[] b) throws IOException {}
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
   117
    @Override public void write(byte[] b, int off, int len) throws IOException {}
64631ffc11e3 7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff changeset
   118
}