Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/BufferedInputStream/LargeCopyWithMark.java
41152 views
1
/*
2
* Copyright (c) 2013, 2021, 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
* @requires (sun.arch.data.model == "64" & os.maxMemory > 4g)
27
* @summary BufferedInputStream calculates negative array size with large
28
* streams and mark
29
* @run main/othervm -Xmx4G -Xlog:gc,gc+heap,gc+ergo+heap -XX:+CrashOnOutOfMemoryError
30
-XX:+IgnoreUnrecognizedVMOptions -XX:+G1ExitOnExpansionFailure
31
-Xlog:cds=debug
32
LargeCopyWithMark
33
*/
34
35
import java.io.BufferedInputStream;
36
import java.io.IOException;
37
import java.io.InputStream;
38
import java.io.OutputStream;
39
40
public class LargeCopyWithMark {
41
42
static final int BUFF_SIZE = 8192;
43
static final int BIS_BUFF_SIZE = Integer.MAX_VALUE / 2 + 100;
44
static final long BYTES_TO_COPY = 2L * Integer.MAX_VALUE;
45
46
static {
47
assert BIS_BUFF_SIZE * 2 < 0 : "doubling must overflow";
48
}
49
50
public static void main(String[] args) throws Exception {
51
byte[] buff = new byte[BUFF_SIZE];
52
53
try (InputStream myis = new MyInputStream(BYTES_TO_COPY);
54
InputStream bis = new BufferedInputStream(myis, BIS_BUFF_SIZE);
55
OutputStream myos = new MyOutputStream()) {
56
57
// will require a buffer bigger than BIS_BUFF_SIZE
58
bis.mark(BIS_BUFF_SIZE + 100);
59
60
for (;;) {
61
int count = bis.read(buff, 0, BUFF_SIZE);
62
if (count == -1)
63
break;
64
myos.write(buff, 0, count);
65
}
66
}
67
}
68
}
69
70
class MyInputStream extends InputStream {
71
private long bytesLeft;
72
public MyInputStream(long bytesLeft) {
73
this.bytesLeft = bytesLeft;
74
}
75
@Override public int read() throws IOException {
76
return 0;
77
}
78
@Override public int read(byte[] b) throws IOException {
79
return read(b, 0, b.length);
80
}
81
@Override public int read(byte[] b, int off, int len) throws IOException {
82
if (bytesLeft <= 0)
83
return -1;
84
long result = Math.min(bytesLeft, (long)len);
85
bytesLeft -= result;
86
return (int)result;
87
}
88
@Override public int available() throws IOException {
89
return (bytesLeft > 0) ? 1 : 0;
90
}
91
}
92
93
class MyOutputStream extends OutputStream {
94
@Override public void write(int b) throws IOException {}
95
@Override public void write(byte[] b) throws IOException {}
96
@Override public void write(byte[] b, int off, int len) throws IOException {}
97
}
98
99