blob: 74aeda92932e629866e72b82bbdb92b6284aeddc [file] [log] [blame]
igerasim1788f902013-08-25 23:20:59 +04001/*
2 * Copyright (c) 2013, 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/* @test
25 * @bug 7129312
26 * @summary BufferedInputStream calculates negative array size with large
27 * streams and mark
28 * @library /lib/testlibrary
jbachorikec886a02014-05-22 11:23:46 +020029 * @build jdk.testlibrary.*
igerasim1788f902013-08-25 23:20:59 +040030 * @run main/othervm LargeCopyWithMark
31 */
32
33import java.io.BufferedInputStream;
34import java.io.IOException;
35import java.io.InputStream;
36import java.io.OutputStream;
37import static jdk.testlibrary.ProcessTools.*;
38
39
40public class LargeCopyWithMark {
41
42 public static void main(String[] args) throws Exception {
43 if (! System.getProperty("os.arch").contains("64")) {
44 System.out.println("Test runs on 64 bit platforms");
45 return;
46 }
47 ProcessBuilder pb = createJavaProcessBuilder("-Xmx4G",
48 "-ea:LargeCopyWithMark$Child",
49 "LargeCopyWithMark$Child");
50 int res = pb.inheritIO().start().waitFor();
51 if (res != 0) {
52 throw new AssertionError("Test failed: exit code = " + res);
53 }
54 }
55
56 public static class Child {
57 static final int BUFF_SIZE = 8192;
58 static final int BIS_BUFF_SIZE = Integer.MAX_VALUE / 2 + 100;
59 static final long BYTES_TO_COPY = 2L * Integer.MAX_VALUE;
60
61 static {
62 assert BIS_BUFF_SIZE * 2 < 0 : "doubling must overflow";
63 }
64
65 public static void main(String[] args) throws Exception {
66 byte[] buff = new byte[BUFF_SIZE];
67
68 try (InputStream myis = new MyInputStream(BYTES_TO_COPY);
69 InputStream bis = new BufferedInputStream(myis, BIS_BUFF_SIZE);
70 OutputStream myos = new MyOutputStream()) {
71
72 // will require a buffer bigger than BIS_BUFF_SIZE
73 bis.mark(BIS_BUFF_SIZE + 100);
74
75 for (;;) {
76 int count = bis.read(buff, 0, BUFF_SIZE);
77 if (count == -1)
78 break;
79 myos.write(buff, 0, count);
80 }
81 } catch (java.lang.NegativeArraySizeException e) {
82 e.printStackTrace();
83 System.exit(11);
84 } catch (Exception e) {
85 e.printStackTrace();
86 }
87 }
88 }
89}
90
91class MyInputStream extends InputStream {
92 private long bytesLeft;
93 public MyInputStream(long bytesLeft) {
94 this.bytesLeft = bytesLeft;
95 }
96 @Override public int read() throws IOException {
97 return 0;
98 }
99 @Override public int read(byte[] b) throws IOException {
100 return read(b, 0, b.length);
101 }
102 @Override public int read(byte[] b, int off, int len) throws IOException {
103 if (bytesLeft <= 0)
104 return -1;
105 long result = Math.min(bytesLeft, (long)len);
106 bytesLeft -= result;
107 return (int)result;
108 }
109 @Override public int available() throws IOException {
110 return (bytesLeft > 0) ? 1 : 0;
111 }
112}
113
114class MyOutputStream extends OutputStream {
115 @Override public void write(int b) throws IOException {}
116 @Override public void write(byte[] b) throws IOException {}
117 @Override public void write(byte[] b, int off, int len) throws IOException {}
118}