Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/InputStream/Skip.java
41149 views
1
/*
2
* Copyright (c) 1997, 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
/**
25
* @test
26
* @bug 4016710 6516099
27
* @summary check for correct implementation of InputStream.skip{NBytes}
28
*/
29
30
import java.io.EOFException;
31
import java.io.InputStream;
32
import java.io.IOException;
33
34
public class Skip {
35
private static final int EOF = -1;
36
37
private static void dotest(InputStream in, int curpos, long total,
38
long toskip, long expected) throws Exception {
39
try {
40
System.err.println("\n\nCurrently at pos = " + curpos +
41
"\nTotal bytes in the Stream = " + total +
42
"\nNumber of bytes to skip = " + toskip +
43
"\nNumber of bytes that should be skipped = " +
44
expected);
45
46
long skipped = in.skip(toskip);
47
48
System.err.println("actual number skipped: "+ skipped);
49
50
if ((skipped < 0) || (skipped > expected)) {
51
throw new RuntimeException("Unexpected byte count skipped");
52
}
53
} catch (IOException e) {
54
System.err.println("IOException is thrown: " + e);
55
} catch (Throwable e) {
56
throw new RuntimeException("Unexpected " + e + " is thrown!");
57
}
58
}
59
60
private static void dotestExact(MyInputStream in, long curpos, long total,
61
long toskip, boolean expectIOE, boolean expectEOFE) {
62
63
System.err.println("\n\nCurrently at pos = " + curpos +
64
"\nTotal bytes in the Stream = " + total +
65
"\nNumber of bytes to skip = " + toskip);
66
67
try {
68
long pos = in.position();
69
assert pos == curpos : pos + " != " + curpos;
70
in.skipNBytes(toskip);
71
if (in.position() != pos + (toskip < 0 ? 0 : toskip)) {
72
throw new RuntimeException((in.position() - pos) +
73
" bytes skipped; expected " + toskip);
74
}
75
} catch (EOFException eofe) {
76
if (!expectEOFE) {
77
throw new RuntimeException("Unexpected EOFException", eofe);
78
}
79
System.err.println("Caught expected EOFException");
80
} catch (IOException ioe) {
81
if (!expectIOE) {
82
throw new RuntimeException("Unexpected IOException", ioe);
83
}
84
System.err.println("Caught expected IOException");
85
}
86
}
87
88
public static void main( String argv[] ) throws Exception {
89
MyInputStream in = new MyInputStream(11);
90
91
// test for negative skip
92
dotest(in, 0, 11, -23, 0);
93
94
// check for skip beyond EOF starting from before EOF
95
dotest(in, 0, 11, 20, 11);
96
97
// check for skip after EOF
98
dotest(in, EOF, 11, 20, 0);
99
100
in = new MyInputStream(9000);
101
102
// check for skip equal to the read chunk size in InputStream.java
103
dotest(in, 0, 9000, 2048, 2048);
104
105
// check for skip larger than the read chunk size in InputStream.java
106
dotest(in, 2048, 9000, 5000, 5000);
107
108
// check for skip beyond EOF starting from before EOF
109
dotest(in, 7048, 9000, 5000, 1952);
110
111
in = new MyInputStream(5000);
112
113
// check for multiple chunk reads
114
dotest(in, 0, 5000, 6000, 5000);
115
116
/*
117
* check for skip larger than Integer.MAX_VALUE
118
* (Takes about 2 hrs on a sparc ultra-1)
119
* long total = (long)Integer.MAX_VALUE + (long)10;
120
* long toskip = total - (long)6;
121
* in = new MyInputStream(total);
122
* dotest(in, 0, total, toskip, toskip);
123
*/
124
125
// tests for skipping an exact number of bytes
126
127
final long streamLength = Long.MAX_VALUE;
128
in = new MyInputStream(streamLength);
129
130
// negative skip: OK
131
dotestExact(in, 0, streamLength, -1, false, false);
132
133
// negative skip at EOF: OK
134
in.position(streamLength);
135
dotestExact(in, streamLength, streamLength, -1, false, false);
136
in.position(0);
137
138
// zero skip: OK
139
dotestExact(in, 0, streamLength, 0, false, false);
140
141
// zero skip at EOF: OK
142
in.position(streamLength);
143
dotestExact(in, streamLength, streamLength, 0, false, false);
144
145
// skip(1) at EOF: EOFE
146
dotestExact(in, streamLength, streamLength, 1, false, true);
147
in.position(0);
148
149
final long n = 31; // skip count
150
long pos = 0;
151
152
// skip(n) returns negative value: IOE
153
in.setState(-1, 100);
154
dotestExact(in, pos, streamLength, n, true, false);
155
156
// skip(n) returns n + 1: IOE
157
in.setState(n + 1, 100);
158
dotestExact(in, pos, streamLength, n, true, false);
159
pos += n + 1;
160
161
// skip(n) returns n/2 but only n/4 subsequent reads succeed: EOFE
162
in.setState(n/2, n/2 + n/4);
163
dotestExact(in, pos, streamLength, n, false, true);
164
pos += n/2 + n/4;
165
166
// skip(n) returns n/2 but n - n/2 subsequent reads succeed: OK
167
in.setState(n/2, n);
168
dotestExact(in, pos, streamLength, n, false, false);
169
pos += n;
170
}
171
}
172
173
class MyInputStream extends InputStream {
174
private static final int EOF = -1;
175
176
private final long endoffile;
177
178
private long readctr = 0;
179
180
private boolean isStateSet = false;
181
private long skipReturn;
182
private long readLimit;
183
184
public MyInputStream(long endoffile) {
185
this.endoffile = endoffile;
186
}
187
188
/**
189
* Limits the behavior of skip() and read().
190
*
191
* @param skipReturn the value to be returned by skip()
192
* @param maxReads the maximum number of reads past the current position
193
* before EOF is reached
194
*/
195
public void setState(long skipReturn, long maxReads) {
196
this.skipReturn = skipReturn;
197
this.readLimit = readctr + maxReads;
198
isStateSet = true;
199
}
200
201
public int read() {
202
if (readctr == endoffile ||
203
(isStateSet && readctr >= readLimit)) {
204
return EOF;
205
}
206
else {
207
readctr++;
208
return 0;
209
}
210
}
211
212
public int available() { return 0; }
213
214
public long position() { return readctr; }
215
216
public void position(long pos) {
217
readctr = pos < 0 ? 0 : Math.min(pos, endoffile);
218
}
219
220
public long skip(long n) throws IOException {
221
if (isStateSet) {
222
return skipReturn < 0 ? skipReturn : super.skip(skipReturn);
223
}
224
225
// InputStream skip implementation.
226
return super.skip(n); // readctr is implicitly incremented
227
}
228
}
229
230