Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/misc/IOUtils/ReadNBytes.java
38839 views
1
/*
2
* Copyright (c) 2015, 2018, 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.testlibrary.RandomFactory;
31
32
import sun.misc.IOUtils;
33
34
/*
35
* @test
36
* @bug 8080835 8139206
37
* @library /lib/testlibrary
38
* @build jdk.testlibrary.*
39
* @run main ReadNBytes
40
* @summary Basic test for IOUtils.readNBytes
41
* @key randomness
42
*/
43
44
public class ReadNBytes {
45
46
private static Random generator = RandomFactory.getRandom();
47
48
public static void main(String[] args) throws IOException {
49
test(new byte[]{1, 2, 3});
50
test(createRandomBytes(1024));
51
for (int shift : new int[] {13, 15, 17}) {
52
for (int offset : new int[] {-1, 0, 1}) {
53
test(createRandomBytes((1 << shift) + offset));
54
}
55
}
56
57
test(-1);
58
test(0);
59
for (int shift : new int[] {13, 15, 17}) {
60
for (int offset : new int[] {-1, 0, 1}) {
61
test((1 << shift) + offset);
62
}
63
}
64
}
65
66
static void test(byte[] inputBytes) throws IOException {
67
int length = inputBytes.length;
68
WrapperInputStream in = new WrapperInputStream(new ByteArrayInputStream(inputBytes));
69
byte[] readBytes = new byte[(length / 2) + 1];
70
int nread = IOUtils.readNBytes(in, readBytes, 0, readBytes.length);
71
72
int x;
73
byte[] tmp;
74
check(nread == readBytes.length,
75
"Expected number of bytes read: " + readBytes.length + ", got: " + nread);
76
check(Arrays.equals((tmp = Arrays.copyOf(inputBytes, nread)), readBytes),
77
"Expected[" + tmp + "], got:[" + readBytes + "]");
78
check(!in.isClosed(), "Stream unexpectedly closed");
79
80
// Read again
81
nread = IOUtils.readNBytes(in, readBytes, 0, readBytes.length);
82
83
check(nread == length - readBytes.length,
84
"Expected number of bytes read: " + (length - readBytes.length) + ", got: " + nread);
85
check(Arrays.equals((tmp = Arrays.copyOfRange(inputBytes, readBytes.length, length)),
86
Arrays.copyOf(readBytes, nread)),
87
"Expected[" + tmp + "], got:[" + readBytes + "]");
88
// Expect end of stream
89
check((x = in.read()) == -1,
90
"Expected end of stream from read(), got " + x);
91
check((x = in.read(tmp)) == -1,
92
"Expected end of stream from read(byte[]), got " + x);
93
check((x = in.read(tmp, 0, tmp.length)) == -1,
94
"Expected end of stream from read(byte[], int, int), got " + x);
95
check((x = IOUtils.readNBytes(in, tmp, 0, tmp.length)) == 0,
96
"Expected end of stream, 0, from readNBytes(byte[], int, int), got " + x);
97
check(!in.isClosed(), "Stream unexpectedly closed");
98
}
99
100
static void test(int max) throws IOException {
101
byte[] subset1, subset2;
102
byte[] inputBytes = max <= 0 ? new byte[0] : createRandomBytes(max);
103
WrapperInputStream in =
104
new WrapperInputStream(new ByteArrayInputStream(inputBytes));
105
106
if (max < 0) {
107
try {
108
IOUtils.readNBytes(in, max);
109
check(false, "Expected IllegalArgumentException not thrown");
110
} catch (IllegalArgumentException iae) {
111
return;
112
}
113
} else if (max == 0) {
114
int x;
115
check((x = IOUtils.readNBytes(in, max).length) == 0,
116
"Expected zero bytes, got " + x);
117
return;
118
}
119
120
int off = Math.toIntExact(in.skip(generator.nextInt(max/2)));
121
int len = generator.nextInt(max - 1 - off);
122
byte[] readBytes = IOUtils.readNBytes(in, len);
123
check(readBytes.length == len,
124
"Expected " + len + " bytes, got " + readBytes.length);
125
subset1 = Arrays.copyOfRange(inputBytes, off, off + len);
126
subset2 = Arrays.copyOfRange(readBytes, 0, len);
127
check(Arrays.equals(subset1, subset2), "Expected[" + subset1 +
128
"], got:[" + readBytes + "]");
129
130
int remaining = max - (off + len);
131
readBytes = IOUtils.readNBytes(in, remaining);
132
check(readBytes.length == remaining,
133
"Expected " + remaining + "bytes, got " + readBytes.length);
134
subset1 = Arrays.copyOfRange(inputBytes, off + len, max);
135
subset2 = Arrays.copyOfRange(readBytes, 0, remaining);
136
check(Arrays.equals(subset1, subset2), "Expected[" + subset1 +
137
"], got:[" + readBytes + "]");
138
139
check(!in.isClosed(), "Stream unexpectedly closed");
140
}
141
142
static byte[] createRandomBytes(int size) {
143
byte[] bytes = new byte[size];
144
generator.nextBytes(bytes);
145
return bytes;
146
}
147
148
static void check(boolean cond, Object ... failedArgs) {
149
if (cond)
150
return;
151
StringBuilder sb = new StringBuilder();
152
for (Object o : failedArgs)
153
sb.append(o);
154
throw new RuntimeException(sb.toString());
155
}
156
157
158
static class WrapperInputStream extends FilterInputStream {
159
private boolean closed;
160
WrapperInputStream(InputStream in) { super(in); }
161
@Override public void close() throws IOException { closed = true; in.close(); }
162
boolean isClosed() { return closed; }
163
}
164
}
165
166