Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/DataInputStream/ReadFully.java
41152 views
1
/*
2
* Copyright (c) 1999, 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
/* @test
25
* @bug 4214513 8245036
26
* @summary Passing a negative offset or length,
27
* or passing a combination of offset and length too big
28
* for readFully must throw IndexOutOfBoundsException.
29
*/
30
31
import java.io.*;
32
33
public class ReadFully {
34
35
private static final void testNegativeOffset() throws Exception {
36
File file = new File(System.getProperty("test.src"),
37
"ReadFully.java");
38
try (FileInputStream in = new FileInputStream(file);
39
DataInputStream dis = new DataInputStream(in);) {
40
byte[] buffer = new byte[100];
41
dis.readFully(buffer, -1, buffer.length);
42
throw new RuntimeException("Test testNegativeOffset() failed");
43
} catch (IndexOutOfBoundsException ignore) {
44
}
45
}
46
47
private static final void testNegativeLength() throws Exception {
48
File file = new File(System.getProperty("test.src"),
49
"ReadFully.java");
50
try (FileInputStream in = new FileInputStream(file);
51
DataInputStream dis = new DataInputStream(in);) {
52
byte[] buffer = new byte[100];
53
dis.readFully(buffer, 0, -1);
54
throw new RuntimeException("Test testNegativeLength() failed");
55
} catch (IndexOutOfBoundsException ignore) {
56
}
57
}
58
59
private static final void testNegativeOffsetZeroLength() throws Exception {
60
File file = new File(System.getProperty("test.src"),
61
"ReadFully.java");
62
try (FileInputStream in = new FileInputStream(file);
63
DataInputStream dis = new DataInputStream(in);) {
64
byte[] buffer = new byte[100];
65
dis.readFully(buffer, -1, 0);
66
throw new RuntimeException("Test testNegativeOffsetZeroLength() failed");
67
} catch (IndexOutOfBoundsException ignore) {
68
}
69
}
70
71
private static final void testBigOffsetLength1() throws Exception {
72
File file = new File(System.getProperty("test.src"),
73
"ReadFully.java");
74
try (FileInputStream in = new FileInputStream(file);
75
DataInputStream dis = new DataInputStream(in);) {
76
byte[] buffer = new byte[100];
77
dis.readFully(buffer, 0, buffer.length + 1);
78
throw new RuntimeException("Test testBigOffsetLength1() failed");
79
} catch (IndexOutOfBoundsException ignore) {
80
}
81
}
82
83
private static final void testBigOffsetLength2() throws Exception {
84
File file = new File(System.getProperty("test.src"),
85
"ReadFully.java");
86
try (FileInputStream in = new FileInputStream(file);
87
DataInputStream dis = new DataInputStream(in);) {
88
byte[] buffer = new byte[100];
89
dis.readFully(buffer, 1, buffer.length);
90
throw new RuntimeException("Test testBigOffsetLength2() failed");
91
} catch (IndexOutOfBoundsException ignore) {
92
}
93
}
94
95
private static final void testBigOffsetLength3() throws Exception {
96
File file = new File(System.getProperty("test.src"),
97
"ReadFully.java");
98
try (FileInputStream in = new FileInputStream(file);
99
DataInputStream dis = new DataInputStream(in);) {
100
byte[] buffer = new byte[100];
101
dis.readFully(buffer, buffer.length, 1);
102
throw new RuntimeException("Test testBigOffsetLength3() failed");
103
} catch (IndexOutOfBoundsException ignore) {
104
}
105
}
106
107
private static final void testBigOffsetLength4() throws Exception {
108
File file = new File(System.getProperty("test.src"),
109
"ReadFully.java");
110
try (FileInputStream in = new FileInputStream(file);
111
DataInputStream dis = new DataInputStream(in);) {
112
byte[] buffer = new byte[100];
113
dis.readFully(buffer, buffer.length + 1, 0);
114
throw new RuntimeException("Test testBigOffsetLength4() failed");
115
} catch (IndexOutOfBoundsException ignore) {
116
}
117
}
118
119
public static final void main(String[] args) throws Exception {
120
testNegativeOffset();
121
testNegativeLength();
122
testNegativeOffsetZeroLength();
123
testBigOffsetLength1();
124
testBigOffsetLength2();
125
testBigOffsetLength3();
126
testBigOffsetLength4();
127
}
128
129
}
130
131