author | igerasim |
Sun, 25 Aug 2013 23:20:59 +0400 | |
changeset 19587 | 64631ffc11e3 |
child 24366 | 57c0a8b7a936 |
permissions | -rw-r--r-- |
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 |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
29 |
* @run main/othervm LargeCopyWithMark |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
30 |
*/ |
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 |
import java.io.BufferedInputStream; |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
33 |
import java.io.IOException; |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
34 |
import java.io.InputStream; |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
35 |
import java.io.OutputStream; |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
36 |
import static jdk.testlibrary.ProcessTools.*; |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
37 |
|
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 |
public class LargeCopyWithMark { |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
40 |
|
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
41 |
public static void main(String[] args) throws Exception { |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
42 |
if (! System.getProperty("os.arch").contains("64")) { |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
43 |
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
|
44 |
return; |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
45 |
} |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
46 |
ProcessBuilder pb = createJavaProcessBuilder("-Xmx4G", |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
47 |
"-ea:LargeCopyWithMark$Child", |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
48 |
"LargeCopyWithMark$Child"); |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
49 |
int res = pb.inheritIO().start().waitFor(); |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
50 |
if (res != 0) { |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
51 |
throw new AssertionError("Test failed: exit code = " + res); |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
52 |
} |
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 |
public static class Child { |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
56 |
static final int BUFF_SIZE = 8192; |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
57 |
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
|
58 |
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
|
59 |
|
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
60 |
static { |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
61 |
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
|
62 |
} |
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 |
public static void main(String[] args) throws Exception { |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
65 |
byte[] buff = new byte[BUFF_SIZE]; |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
66 |
|
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
67 |
try (InputStream myis = new MyInputStream(BYTES_TO_COPY); |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
68 |
InputStream bis = new BufferedInputStream(myis, BIS_BUFF_SIZE); |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
69 |
OutputStream myos = new MyOutputStream()) { |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
70 |
|
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
71 |
// 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
|
72 |
bis.mark(BIS_BUFF_SIZE + 100); |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
73 |
|
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
74 |
for (;;) { |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
75 |
int count = bis.read(buff, 0, BUFF_SIZE); |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
76 |
if (count == -1) |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
77 |
break; |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
78 |
myos.write(buff, 0, count); |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
79 |
} |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
80 |
} catch (java.lang.NegativeArraySizeException e) { |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
81 |
e.printStackTrace(); |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
82 |
System.exit(11); |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
83 |
} catch (Exception e) { |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
84 |
e.printStackTrace(); |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
85 |
} |
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 |
class MyInputStream extends InputStream { |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
91 |
private long bytesLeft; |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
92 |
public MyInputStream(long bytesLeft) { |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
93 |
this.bytesLeft = bytesLeft; |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
94 |
} |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
95 |
@Override public int read() throws IOException { |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
96 |
return 0; |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
97 |
} |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
98 |
@Override public int read(byte[] b) throws IOException { |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
99 |
return read(b, 0, b.length); |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
100 |
} |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
101 |
@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
|
102 |
if (bytesLeft <= 0) |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
103 |
return -1; |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
104 |
long result = Math.min(bytesLeft, (long)len); |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
105 |
bytesLeft -= result; |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
106 |
return (int)result; |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
107 |
} |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
108 |
@Override public int available() throws IOException { |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
109 |
return (bytesLeft > 0) ? 1 : 0; |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
110 |
} |
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 |
class MyOutputStream extends OutputStream { |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
114 |
@Override public void write(int b) throws IOException {} |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
115 |
@Override public void write(byte[] 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, int off, int len) throws IOException {} |
64631ffc11e3
7129312: BufferedInputStream calculates negative array size with large streams and mark
igerasim
parents:
diff
changeset
|
117 |
} |