Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/FileInputStream/ReadXBytes.java
41149 views
1
/*
2
* Copyright (c) 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
/*
25
* @test
26
* @library /test/lib
27
* @build jdk.test.lib.RandomFactory
28
* @run main ReadXBytes
29
* @bug 8264777
30
* @summary Test read{All,N}Bytes overrides (use -Dseed=X to set PRNG seed)
31
* @key randomness
32
*/
33
34
import java.io.File;
35
import java.io.FileInputStream;
36
import java.io.IOException;
37
import java.io.RandomAccessFile;
38
import java.util.Arrays;
39
import java.util.Random;
40
import jdk.test.lib.RandomFactory;
41
42
public class ReadXBytes {
43
private static final int ITERATIONS = 10;
44
private static final int MAX_FILE_SIZE = 1_000_000;
45
private static final Random RND = RandomFactory.getRandom();
46
47
public static void main(String args[]) throws IOException {
48
File dir = new File(System.getProperty("test.src", "."));
49
dir.deleteOnExit();
50
51
File empty = File.createTempFile("foo", "bar", dir);
52
empty.deleteOnExit();
53
try (FileInputStream fis = new FileInputStream(empty)) {
54
try {
55
fis.readNBytes(-1);
56
throw new RuntimeException("IllegalArgumentException expected");
57
} catch (IllegalArgumentException expected) {
58
}
59
byte[] nbytes = fis.readNBytes(0);
60
if (nbytes.length != 0)
61
throw new RuntimeException("readNBytes() zero length for empty");
62
63
byte[] b = fis.readNBytes(1);
64
if (b.length != 0)
65
throw new RuntimeException("readNBytes: zero-length byte[] expected");
66
67
b = fis.readAllBytes();
68
if (b.length != 0)
69
throw new RuntimeException("readAllBytes: zero-length byte[] expected");
70
}
71
72
for (int i = 0; i < ITERATIONS; i++) {
73
File file = File.createTempFile("foo", "bar", dir);
74
file.deleteOnExit();
75
76
int size = 1 + RND.nextInt(MAX_FILE_SIZE);
77
System.out.printf("size %d%n", size);
78
byte[] bytes = new byte[size];
79
RND.nextBytes(bytes);
80
try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
81
raf.write(bytes);
82
}
83
84
try (FileInputStream fis = new FileInputStream(file)) {
85
int pos = RND.nextInt(size);
86
int len = RND.nextInt(size - pos);
87
fis.getChannel().position(pos);
88
byte[] nbytes = fis.readNBytes(0);
89
if (nbytes.length != 0)
90
throw new RuntimeException("readNBytes() zero length");
91
nbytes = fis.readNBytes(len);
92
if (nbytes.length != len)
93
throw new RuntimeException("readNBytes() length");
94
if (!Arrays.equals(nbytes, 0, len, bytes, pos, pos + len))
95
throw new RuntimeException("readNBytes() content");
96
}
97
98
try (FileInputStream fis = new FileInputStream(file)) {
99
int pos = RND.nextInt(size);
100
fis.getChannel().position(pos);
101
byte[] allbytes = fis.readAllBytes();
102
if (allbytes.length != size - pos)
103
throw new RuntimeException("readAllBytes() length");
104
if (!Arrays.equals(allbytes, 0, allbytes.length,
105
bytes, pos, pos + allbytes.length))
106
throw new RuntimeException("readAllBytes() content");
107
}
108
109
file.delete();
110
}
111
dir.delete();
112
}
113
}
114
115