Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/java/io/ByteArrayInputStream/ReadAllReadNTransferTo.java
66644 views
1
/*
2
* Copyright (c) 2018, 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
import java.io.ByteArrayInputStream;
25
import java.io.ByteArrayOutputStream;
26
import java.io.IOException;
27
import java.util.Arrays;
28
import java.util.Objects;
29
import java.util.Random;
30
import jdk.test.lib.RandomFactory;
31
32
/* @test
33
* @library /test/lib
34
* @build jdk.test.lib.RandomFactory
35
* @run main ReadAllReadNTransferTo
36
* @bug 6766844 8180451
37
* @summary Verify ByteArrayInputStream readAllBytes, readNBytes, and transferTo
38
* @key randomness
39
*/
40
public class ReadAllReadNTransferTo {
41
private static final int SIZE = 0x4d4d;
42
43
private static Random random = RandomFactory.getRandom();
44
45
public static void main(String... args) throws IOException {
46
byte[] buf = new byte[SIZE];
47
random.nextBytes(buf);
48
int position = random.nextInt(SIZE/2);
49
int size = random.nextInt(SIZE - position);
50
51
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
52
bais.readAllBytes();
53
if (bais.read(new byte[0]) != -1) {
54
throw new RuntimeException("read(byte[]) did not return -1");
55
}
56
if (bais.read(new byte[1], 0, 0) != -1) {
57
throw new RuntimeException("read(byte[],int,int) did not return -1");
58
}
59
60
bais = new ByteArrayInputStream(buf, position, size);
61
int off = size < 2 ? 0 : random.nextInt(size / 2);
62
int len = size - off < 1 ? 0 : random.nextInt(size - off);
63
64
byte[] bN = new byte[off + len];
65
if (bais.readNBytes(bN, off, len) != len) {
66
throw new RuntimeException("readNBytes return value");
67
}
68
if (!Arrays.equals(bN, off, off + len,
69
buf, position, position + len)) {
70
throw new RuntimeException("readNBytes content");
71
}
72
73
byte[] bAll = bais.readAllBytes();
74
Objects.requireNonNull(bAll, "readAllBytes return value");
75
if (bAll.length != size - len) {
76
throw new RuntimeException("readAllBytes return value length");
77
}
78
if (!Arrays.equals(bAll, 0, bAll.length,
79
buf, position + len, position + len + bAll.length)) {
80
throw new RuntimeException("readAllBytes content");
81
}
82
83
bais = new ByteArrayInputStream(buf);
84
ByteArrayOutputStream baos = new ByteArrayOutputStream(buf.length);
85
if (bais.transferTo(baos) != buf.length) {
86
throw new RuntimeException("transferTo return value length");
87
}
88
if (!Arrays.equals(buf, baos.toByteArray())) {
89
throw new RuntimeException("transferTo content");
90
}
91
}
92
}
93
94