Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/InputStream/ReadNBytes.java
41149 views
1
/*
2
* Copyright (c) 2015, 2020, 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
import java.io.ByteArrayInputStream;
25
import java.io.FilterInputStream;
26
import java.io.IOException;
27
import java.io.InputStream;
28
import java.util.Arrays;
29
import java.util.Random;
30
import jdk.test.lib.RandomFactory;
31
32
/*
33
* @test
34
* @bug 8080835 8139206 8254742
35
* @library /test/lib
36
* @build jdk.test.lib.RandomFactory
37
* @run main ReadNBytes
38
* @summary Basic test for InputStream.readNBytes
39
* @key randomness
40
*/
41
42
public class ReadNBytes {
43
44
private static Random generator = RandomFactory.getRandom();
45
46
public static void main(String[] args) throws IOException {
47
test();
48
test(new byte[]{1, 2, 3});
49
test(createRandomBytes(1024));
50
for (int shift : new int[] {13, 15, 17}) {
51
for (int offset : new int[] {-1, 0, 1}) {
52
test(createRandomBytes((1 << shift) + offset));
53
}
54
}
55
56
test(-1);
57
test(0);
58
for (int shift : new int[] {13, 15, 17}) {
59
for (int offset : new int[] {-1, 0, 1}) {
60
test((1 << shift) + offset);
61
}
62
}
63
}
64
65
static void test(byte[] inputBytes) throws IOException {
66
int length = inputBytes.length;
67
WrapperInputStream in = new WrapperInputStream(new ByteArrayInputStream(inputBytes));
68
byte[] readBytes = new byte[(length / 2) + 1];
69
int nread = in.readNBytes(readBytes, 0, readBytes.length);
70
71
int x;
72
byte[] tmp;
73
check(nread == readBytes.length,
74
"Expected number of bytes read: " + readBytes.length + ", got: " + nread);
75
check(Arrays.equals((tmp = Arrays.copyOf(inputBytes, nread)), readBytes),
76
"Expected[" + tmp + "], got:[" + readBytes + "]");
77
check(!in.isClosed(), "Stream unexpectedly closed");
78
79
// Read again
80
nread = in.readNBytes(readBytes, 0, readBytes.length);
81
82
check(nread == length - readBytes.length,
83
"Expected number of bytes read: " + (length - readBytes.length) + ", got: " + nread);
84
check(Arrays.equals((tmp = Arrays.copyOfRange(inputBytes, readBytes.length, length)),
85
Arrays.copyOf(readBytes, nread)),
86
"Expected[" + tmp + "], got:[" + readBytes + "]");
87
// Expect end of stream
88
check((x = in.read()) == -1,
89
"Expected end of stream from read(), got " + x);
90
check((x = in.read(tmp)) == -1,
91
"Expected end of stream from read(byte[]), got " + x);
92
check((x = in.read(tmp, 0, tmp.length)) == -1,
93
"Expected end of stream from read(byte[], int, int), got " + x);
94
check((x = in.readNBytes(tmp, 0, tmp.length)) == 0,
95
"Expected end of stream, 0, from readNBytes(byte[], int, int), got " + x);
96
check(!in.isClosed(), "Stream unexpectedly closed");
97
}
98
99
static void test(int max) throws IOException {
100
byte[] inputBytes = max <= 0 ? new byte[0] : createRandomBytes(max);
101
WrapperInputStream in =
102
new WrapperInputStream(new ByteArrayInputStream(inputBytes));
103
104
if (max < 0) {
105
try {
106
in.readNBytes(max);
107
check(false, "Expected IllegalArgumentException not thrown");
108
} catch (IllegalArgumentException iae) {
109
return;
110
}
111
} else if (max == 0) {
112
int x;
113
check((x = in.readNBytes(max).length) == 0,
114
"Expected zero bytes, got " + x);
115
return;
116
}
117
118
int off = Math.toIntExact(in.skip(generator.nextInt(max/2)));
119
int len = generator.nextInt(max - 1 - off);
120
byte[] readBytes = in.readNBytes(len);
121
check(readBytes.length == len,
122
"Expected " + len + " bytes, got " + readBytes.length);
123
check(Arrays.equals(inputBytes, off, off + len, readBytes, 0, len),
124
"Expected[" + Arrays.copyOfRange(inputBytes, off, off + len) +
125
"], got:[" + readBytes + "]");
126
127
int remaining = max - (off + len);
128
readBytes = in.readNBytes(remaining);
129
check(readBytes.length == remaining,
130
"Expected " + remaining + "bytes, got " + readBytes.length);
131
check(Arrays.equals(inputBytes, off + len, max,
132
readBytes, 0, remaining),
133
"Expected[" + Arrays.copyOfRange(inputBytes, off + len, max) +
134
"], got:[" + readBytes + "]");
135
136
check(!in.isClosed(), "Stream unexpectedly closed");
137
}
138
139
static void test() throws IOException {
140
final int chunkSize = 8192;
141
int size = (10 + generator.nextInt(11))*chunkSize;
142
143
byte[] buf = new byte[size];
144
generator.nextBytes(buf);
145
InputStream s = new ThrottledByteArrayInputStream(buf);
146
147
byte[] b = s.readNBytes(size);
148
149
check(Arrays.equals(b, buf), "Arrays not equal");
150
}
151
152
static byte[] createRandomBytes(int size) {
153
byte[] bytes = new byte[size];
154
generator.nextBytes(bytes);
155
return bytes;
156
}
157
158
static void check(boolean cond, Object ... failedArgs) {
159
if (cond)
160
return;
161
StringBuilder sb = new StringBuilder();
162
for (Object o : failedArgs)
163
sb.append(o);
164
throw new RuntimeException(sb.toString());
165
}
166
167
static class WrapperInputStream extends FilterInputStream {
168
private boolean closed;
169
WrapperInputStream(InputStream in) { super(in); }
170
@Override public void close() throws IOException { closed = true; in.close(); }
171
boolean isClosed() { return closed; }
172
}
173
174
static class ThrottledByteArrayInputStream extends ByteArrayInputStream {
175
private int count = 0;
176
177
ThrottledByteArrayInputStream(byte[] buf) {
178
super(buf);
179
}
180
181
@Override
182
//
183
// Sometimes return zero or a smaller count than requested.
184
//
185
public int read(byte[] buf, int off, int len) {
186
if (generator.nextBoolean()) {
187
return 0;
188
} else if (++count / 3 == 0) {
189
len /= 3;
190
}
191
192
return super.read(buf, off, len);
193
}
194
}
195
}
196
197