Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/Channels/Basic.java
38821 views
1
/*
2
* Copyright (c) 2001, 2008, 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 4417152 4481572 6248930 6725399 6884800
26
* @summary Test Channels basic functionality
27
*/
28
29
import java.io.*;
30
import java.nio.*;
31
import java.nio.charset.*;
32
import java.nio.channels.*;
33
34
35
public class Basic {
36
37
static String message;
38
39
static String encoding;
40
41
static File blah;
42
43
static int ITERATIONS = 500;
44
45
public static void main(String[] args) throws Exception {
46
message = "ascii data for a test";
47
encoding = "ISO-8859-1";
48
test();
49
message = "\ucafe\ubabe\ucafe\ubabe\ucafe\ubabe";
50
encoding = "UTF-8";
51
test();
52
}
53
54
static void failNpeExpected() {
55
throw new RuntimeException("Did not get the expected NullPointerException.");
56
}
57
58
private static void test() throws Exception {
59
//Test if methods of Channels throw NPE with null argument(s)
60
try {
61
Channels.newInputStream((ReadableByteChannel)null);
62
failNpeExpected();
63
} catch (NullPointerException npe) {}
64
65
try {
66
Channels.newOutputStream((WritableByteChannel)null);
67
failNpeExpected();
68
} catch (NullPointerException npe) {}
69
70
try {
71
ReadableByteChannel channel = Channels.newChannel((InputStream)null);
72
failNpeExpected();
73
} catch (NullPointerException ne) {} // OK. As expected.
74
75
try {
76
WritableByteChannel channel = Channels.newChannel((OutputStream)null);
77
failNpeExpected();
78
} catch (NullPointerException ne) {} // OK. As expected.
79
80
WritableByteChannel wbc = new WritableByteChannel() {
81
public int write(ByteBuffer src) { return 0; }
82
public void close() throws IOException { }
83
public boolean isOpen() { return true; }
84
};
85
86
ReadableByteChannel rbc = new ReadableByteChannel() {
87
public int read(ByteBuffer dst) { return 0; }
88
public void close() {}
89
public boolean isOpen() { return true; }
90
};
91
92
try {
93
Channels.newReader((ReadableByteChannel)null,
94
Charset.defaultCharset().newDecoder(),
95
-1);
96
failNpeExpected();
97
} catch (NullPointerException npe) {}
98
99
try {
100
Channels.newReader(rbc, (CharsetDecoder)null, -1);
101
failNpeExpected();
102
} catch (NullPointerException npe) {}
103
104
try {
105
Channels.newReader((ReadableByteChannel)null,
106
Charset.defaultCharset().name());
107
failNpeExpected();
108
} catch (NullPointerException npe) {}
109
110
try {
111
Channels.newReader(rbc, null);
112
failNpeExpected();
113
} catch (NullPointerException npe) {}
114
115
116
try {
117
Channels.newReader(null, null);
118
failNpeExpected();
119
} catch (NullPointerException npe) {}
120
121
try {
122
Channels.newWriter((WritableByteChannel)null,
123
Charset.defaultCharset().newEncoder(),
124
-1);
125
failNpeExpected();
126
} catch (NullPointerException npe) {}
127
128
try {
129
Channels.newWriter(null, null, -1);
130
failNpeExpected();
131
} catch (NullPointerException npe) {}
132
133
try {
134
Channels.newWriter(wbc, null, -1);
135
failNpeExpected();
136
} catch (NullPointerException npe) {}
137
138
try {
139
Channels.newWriter((WritableByteChannel)null,
140
Charset.defaultCharset().name());
141
failNpeExpected();
142
} catch (NullPointerException npe) {}
143
144
try {
145
Channels.newWriter(wbc, null);
146
failNpeExpected();
147
} catch (NullPointerException npe) {}
148
149
try {
150
Channels.newWriter(null, null);
151
failNpeExpected();
152
} catch (NullPointerException npe) {}
153
154
155
try {
156
blah = File.createTempFile("blah", null);
157
158
testNewOutputStream(blah);
159
readAndCheck(blah);
160
blah.delete();
161
162
writeOut(blah, ITERATIONS);
163
testNewInputStream(blah);
164
blah.delete();
165
166
testNewChannelOut(blah);
167
readAndCheck(blah);
168
blah.delete();
169
170
writeOut(blah, ITERATIONS);
171
testNewChannelIn(blah);
172
test4481572(blah);
173
blah.delete();
174
175
testNewWriter(blah);
176
readAndCheck(blah);
177
blah.delete();
178
179
writeOut(blah, ITERATIONS);
180
testNewReader(blah);
181
182
} finally {
183
blah.delete();
184
}
185
}
186
187
private static void readAndCheck(File blah) throws Exception {
188
FileInputStream fis = new FileInputStream(blah);
189
int messageSize = message.length() * ITERATIONS * 3 + 1;
190
byte bb[] = new byte[messageSize];
191
int bytesRead = 0;
192
int totalRead = 0;
193
while (bytesRead != -1) {
194
totalRead += bytesRead;
195
bytesRead = fis.read(bb, totalRead, messageSize - totalRead);
196
}
197
String result = new String(bb, 0, totalRead, encoding);
198
int len = message.length();
199
for (int i=0; i<ITERATIONS; i++) {
200
String segment = result.substring(i++ * len, i * len);
201
if (!segment.equals(message))
202
throw new RuntimeException("Test failed");
203
}
204
fis.close();
205
}
206
207
private static void writeOut(File blah, int limit) throws Exception {
208
FileOutputStream fos = new FileOutputStream(blah);
209
for (int i=0; i<limit; i++)
210
fos.write(message.getBytes(encoding));
211
fos.close();
212
}
213
214
private static void testNewOutputStream(File blah) throws Exception {
215
FileOutputStream fos = new FileOutputStream(blah);
216
FileChannel fc = fos.getChannel();
217
WritableByteChannel wbc = (WritableByteChannel)fc;
218
OutputStream os = Channels.newOutputStream(wbc);
219
for (int i=0; i<ITERATIONS; i++)
220
os.write(message.getBytes(encoding));
221
os.close();
222
fos.close();
223
}
224
225
private static void testNewInputStream(File blah) throws Exception {
226
FileInputStream fis = new FileInputStream(blah);
227
FileChannel fc = fis.getChannel();
228
InputStream is = Channels.newInputStream(fc);
229
int messageSize = message.length() * ITERATIONS * 3 + 1;
230
byte bb[] = new byte[messageSize];
231
232
int bytesRead = 0;
233
int totalRead = 0;
234
while (bytesRead != -1) {
235
totalRead += bytesRead;
236
long rem = Math.min(fc.size() - totalRead, (long)Integer.MAX_VALUE);
237
if (is.available() != (int)rem)
238
throw new RuntimeException("available not useful or not maximally useful");
239
bytesRead = is.read(bb, totalRead, messageSize - totalRead);
240
}
241
if (is.available() != 0)
242
throw new RuntimeException("available() should return 0 at EOF");
243
244
String result = new String(bb, 0, totalRead, encoding);
245
int len = message.length();
246
for (int i=0; i<ITERATIONS; i++) {
247
String segment = result.substring(i++ * len, i * len);
248
if (!segment.equals(message))
249
throw new RuntimeException("Test failed");
250
}
251
is.close();
252
fis.close();
253
}
254
255
private static void testNewChannelOut(File blah) throws Exception {
256
ExtendedFileOutputStream fos = new ExtendedFileOutputStream(blah);
257
WritableByteChannel wbc = Channels.newChannel(fos);
258
for (int i=0; i<ITERATIONS; i++)
259
wbc.write(ByteBuffer.wrap(message.getBytes(encoding)));
260
wbc.close();
261
fos.close();
262
}
263
264
private static void testNewChannelIn(File blah) throws Exception {
265
ExtendedFileInputStream fis = new ExtendedFileInputStream(blah);
266
ReadableByteChannel rbc = Channels.newChannel(fis);
267
268
int messageSize = message.length() * ITERATIONS * 3;
269
byte data[] = new byte[messageSize+1];
270
ByteBuffer bb = ByteBuffer.wrap(data);
271
272
int bytesRead = 0;
273
int totalRead = 0;
274
while (bytesRead != -1) {
275
totalRead += bytesRead;
276
bytesRead = rbc.read(bb);
277
}
278
279
String result = new String(data, 0, totalRead, encoding);
280
int len = message.length();
281
for (int i=0; i<ITERATIONS; i++) {
282
String segment = result.substring(i++ * len, i * len);
283
if (!segment.equals(message))
284
throw new RuntimeException("Test failed");
285
}
286
rbc.close();
287
fis.close();
288
}
289
290
// Causes BufferOverflowException if bug 4481572 is present.
291
private static void test4481572(File blah) throws Exception {
292
ExtendedFileInputStream fis = new ExtendedFileInputStream(blah);
293
ReadableByteChannel rbc = Channels.newChannel(fis);
294
295
byte data[] = new byte[9000];
296
ByteBuffer bb = ByteBuffer.wrap(data);
297
298
int bytesRead = 1;
299
int totalRead = 0;
300
while (bytesRead > 0) {
301
totalRead += bytesRead;
302
bytesRead = rbc.read(bb);
303
}
304
rbc.close();
305
fis.close();
306
}
307
308
private static void testNewWriter(File blah) throws Exception {
309
FileOutputStream fos = new FileOutputStream(blah);
310
WritableByteChannel wbc = (WritableByteChannel)fos.getChannel();
311
Writer w = Channels.newWriter(wbc, encoding);
312
char data[] = new char[40];
313
message.getChars(0, message.length(), data, 0);
314
for (int i=0; i<ITERATIONS; i++)
315
w.write(data, 0, message.length());
316
w.flush();
317
w.close();
318
fos.close();
319
}
320
321
private static void testNewReader(File blah) throws Exception {
322
FileInputStream fis = new FileInputStream(blah);
323
ReadableByteChannel rbc = (ReadableByteChannel)fis.getChannel();
324
Reader r = Channels.newReader(rbc, encoding);
325
326
int messageSize = message.length() * ITERATIONS;
327
char data[] = new char[messageSize];
328
329
int totalRead = 0;
330
int charsRead = 0;
331
while (totalRead < messageSize) {
332
totalRead += charsRead;
333
charsRead = r.read(data, totalRead, messageSize - totalRead);
334
}
335
String result = new String(data, 0, totalRead);
336
int len = message.length();
337
for (int i=0; i<ITERATIONS; i++) {
338
String segment = result.substring(i++ * len, i * len);
339
if (!segment.equals(message))
340
throw new RuntimeException("Test failed");
341
}
342
r.close();
343
fis.close();
344
}
345
}
346
347
class ExtendedFileInputStream extends java.io.FileInputStream {
348
ExtendedFileInputStream(File file) throws FileNotFoundException {
349
super(file);
350
}
351
}
352
353
class ExtendedFileOutputStream extends java.io.FileOutputStream {
354
ExtendedFileOutputStream(File file) throws FileNotFoundException {
355
super(file);
356
}
357
}
358
359