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/ReadAllBytes.java
38839 views
1
/*
2
* Copyright (c) 2015, 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
31
import jdk.testlibrary.RandomFactory;
32
33
import sun.misc.IOUtils;
34
35
/*
36
* @test
37
* @bug 8080835 8193832
38
* @library /lib/testlibrary
39
* @build jdk.testlibrary.*
40
* @run main ReadAllBytes
41
* @summary Basic test for IOUtils.readAllBytes
42
* @key randomness
43
*/
44
45
public class ReadAllBytes {
46
47
private static Random generator = RandomFactory.getRandom();
48
49
public static void main(String[] args) throws IOException {
50
test(new byte[]{});
51
test(new byte[]{1, 2, 3});
52
test(createRandomBytes(1024));
53
for (int shift : new int[] {13, 14, 15, 17}) {
54
for (int offset : new int[] {-1, 0, 1}) {
55
test(createRandomBytes((1 << shift) + offset));
56
}
57
}
58
}
59
60
static void test(byte[] expectedBytes) throws IOException {
61
int expectedLength = expectedBytes.length;
62
WrapperInputStream in = new WrapperInputStream(new ByteArrayInputStream(expectedBytes));
63
byte[] readBytes = IOUtils.readAllBytes(in);
64
65
int x;
66
byte[] tmp = new byte[10];
67
check((x = in.read()) == -1,
68
"Expected end of stream from read(), got " + x);
69
check((x = in.read(tmp)) == -1,
70
"Expected end of stream from read(byte[]), got " + x);
71
check((x = in.read(tmp, 0, tmp.length)) == -1,
72
"Expected end of stream from read(byte[], int, int), got " + x);
73
check(IOUtils.readAllBytes(in).length == 0,
74
"Expected readAllBytes to return empty byte array");
75
check(expectedLength == readBytes.length,
76
"Expected length " + expectedLength + ", got " + readBytes.length);
77
check(Arrays.equals(expectedBytes, readBytes),
78
"Expected[" + expectedBytes + "], got:[" + readBytes + "]");
79
check(!in.isClosed(), "Stream unexpectedly closed");
80
}
81
82
static byte[] createRandomBytes(int size) {
83
byte[] bytes = new byte[size];
84
generator.nextBytes(bytes);
85
return bytes;
86
}
87
88
static void check(boolean cond, Object ... failedArgs) {
89
if (cond)
90
return;
91
StringBuilder sb = new StringBuilder();
92
for (Object o : failedArgs)
93
sb.append(o);
94
throw new RuntimeException(sb.toString());
95
}
96
97
static class WrapperInputStream extends FilterInputStream {
98
private boolean closed;
99
WrapperInputStream(InputStream in) { super(in); }
100
@Override public void close() throws IOException { closed = true; in.close(); }
101
boolean isClosed() { return closed; }
102
}
103
}
104
105