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/FileChannel/Transfer.java
38828 views
1
/*
2
* Copyright (c) 2001, 2012, 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 4434723 4482726 4559072 4638365 4795550 5081340 5103988 6253145
26
* 6984545
27
* @summary Test FileChannel.transferFrom and transferTo
28
* @library ..
29
* @key randomness
30
*/
31
32
import java.io.*;
33
import java.net.*;
34
import java.nio.*;
35
import java.nio.channels.*;
36
import java.nio.channels.spi.SelectorProvider;
37
import java.nio.file.StandardOpenOption;
38
import java.nio.file.FileAlreadyExistsException;
39
import java.util.Random;
40
41
42
public class Transfer {
43
44
private static Random generator = new Random();
45
46
private static int[] testSizes = {
47
0, 10, 1023, 1024, 1025, 2047, 2048, 2049 };
48
49
public static void main(String[] args) throws Exception {
50
testFileChannel();
51
for (int i=0; i<testSizes.length; i++)
52
testReadableByteChannel(testSizes[i]);
53
xferTest02(); // for bug 4482726
54
xferTest03(); // for bug 4559072
55
xferTest04(); // for bug 4638365
56
xferTest05(); // for bug 4638365
57
xferTest06(); // for bug 5081340
58
xferTest07(); // for bug 5103988
59
xferTest08(); // for bug 6253145
60
xferTest09(); // for bug 6984545
61
}
62
63
private static void testFileChannel() throws Exception {
64
File source = File.createTempFile("source", null);
65
source.deleteOnExit();
66
File sink = File.createTempFile("sink", null);
67
sink.deleteOnExit();
68
69
FileOutputStream fos = new FileOutputStream(source);
70
FileChannel sourceChannel = fos.getChannel();
71
sourceChannel.write(ByteBuffer.wrap(
72
"Use the source, Luke!".getBytes()));
73
sourceChannel.close();
74
75
FileInputStream fis = new FileInputStream(source);
76
sourceChannel = fis.getChannel();
77
78
RandomAccessFile raf = new RandomAccessFile(sink, "rw");
79
FileChannel sinkChannel = raf.getChannel();
80
long oldSinkPosition = sinkChannel.position();
81
long oldSourcePosition = sourceChannel.position();
82
83
long bytesWritten = sinkChannel.transferFrom(sourceChannel, 0, 10);
84
if (bytesWritten != 10)
85
throw new RuntimeException("Transfer failed");
86
87
if (sourceChannel.position() == oldSourcePosition)
88
throw new RuntimeException("Source position didn't change");
89
90
if (sinkChannel.position() != oldSinkPosition)
91
throw new RuntimeException("Sink position changed");
92
93
if (sinkChannel.size() != 10)
94
throw new RuntimeException("Unexpected sink size");
95
96
bytesWritten = sinkChannel.transferFrom(sourceChannel, 1000, 10);
97
98
if (bytesWritten > 0)
99
throw new RuntimeException("Wrote past file size");
100
101
sourceChannel.close();
102
sinkChannel.close();
103
104
source.delete();
105
sink.delete();
106
}
107
108
private static void testReadableByteChannel(int size) throws Exception {
109
SelectorProvider sp = SelectorProvider.provider();
110
Pipe p = sp.openPipe();
111
Pipe.SinkChannel sink = p.sink();
112
Pipe.SourceChannel source = p.source();
113
sink.configureBlocking(false);
114
115
ByteBuffer outgoingdata = ByteBuffer.allocateDirect(size + 10);
116
byte[] someBytes = new byte[size + 10];
117
generator.nextBytes(someBytes);
118
outgoingdata.put(someBytes);
119
outgoingdata.flip();
120
121
int totalWritten = 0;
122
while (totalWritten < size + 10) {
123
int written = sink.write(outgoingdata);
124
if (written < 0)
125
throw new Exception("Write failed");
126
totalWritten += written;
127
}
128
129
File f = File.createTempFile("blah"+size, null);
130
f.deleteOnExit();
131
RandomAccessFile raf = new RandomAccessFile(f, "rw");
132
FileChannel fc = raf.getChannel();
133
long oldPosition = fc.position();
134
135
long bytesWritten = fc.transferFrom(source, 0, size);
136
fc.force(true);
137
if (bytesWritten != size)
138
throw new RuntimeException("Transfer failed");
139
140
if (fc.position() != oldPosition)
141
throw new RuntimeException("Position changed");
142
143
if (fc.size() != size)
144
throw new RuntimeException("Unexpected sink size "+ fc.size());
145
146
fc.close();
147
sink.close();
148
source.close();
149
150
f.delete();
151
}
152
153
public static void xferTest02() throws Exception {
154
byte[] srcData = new byte[5000];
155
for (int i=0; i<5000; i++)
156
srcData[i] = (byte)generator.nextInt();
157
158
// get filechannel for the source file.
159
File source = File.createTempFile("source", null);
160
source.deleteOnExit();
161
RandomAccessFile raf1 = new RandomAccessFile(source, "rw");
162
FileChannel fc1 = raf1.getChannel();
163
164
// write out data to the file channel
165
long bytesWritten = 0;
166
while (bytesWritten < 5000) {
167
bytesWritten = fc1.write(ByteBuffer.wrap(srcData));
168
}
169
170
// get filechannel for the dst file.
171
File dest = File.createTempFile("dest", null);
172
dest.deleteOnExit();
173
RandomAccessFile raf2 = new RandomAccessFile(dest, "rw");
174
FileChannel fc2 = raf2.getChannel();
175
176
int bytesToWrite = 3000;
177
int startPosition = 1000;
178
179
bytesWritten = fc1.transferTo(startPosition, bytesToWrite, fc2);
180
181
fc1.close();
182
fc2.close();
183
raf1.close();
184
raf2.close();
185
186
source.delete();
187
dest.delete();
188
}
189
190
public static void xferTest03() throws Exception {
191
byte[] srcData = new byte[] {1,2,3,4} ;
192
193
// get filechannel for the source file.
194
File source = File.createTempFile("source", null);
195
source.deleteOnExit();
196
RandomAccessFile raf1 = new RandomAccessFile(source, "rw");
197
FileChannel fc1 = raf1.getChannel();
198
fc1.truncate(0);
199
200
// write out data to the file channel
201
int bytesWritten = 0;
202
while (bytesWritten < 4) {
203
bytesWritten = fc1.write(ByteBuffer.wrap(srcData));
204
}
205
206
// get filechannel for the dst file.
207
File dest = File.createTempFile("dest", null);
208
dest.deleteOnExit();
209
RandomAccessFile raf2 = new RandomAccessFile(dest, "rw");
210
FileChannel fc2 = raf2.getChannel();
211
fc2.truncate(0);
212
213
fc1.transferTo(0, srcData.length + 1, fc2);
214
215
if (fc2.size() > 4)
216
throw new Exception("xferTest03 failed");
217
218
fc1.close();
219
fc2.close();
220
raf1.close();
221
raf2.close();
222
223
source.delete();
224
dest.delete();
225
}
226
227
// Test transferTo with large file
228
public static void xferTest04() throws Exception {
229
// Windows and Linux can't handle the really large file sizes for a
230
// truncate or a positional write required by the test for 4563125
231
String osName = System.getProperty("os.name");
232
if (!(osName.startsWith("SunOS") || osName.contains("OS X")))
233
return;
234
File source = File.createTempFile("blah", null);
235
source.deleteOnExit();
236
long testSize = ((long)Integer.MAX_VALUE) * 2;
237
initTestFile(source, 10);
238
RandomAccessFile raf = new RandomAccessFile(source, "rw");
239
FileChannel fc = raf.getChannel();
240
fc.write(ByteBuffer.wrap("Use the source!".getBytes()), testSize - 40);
241
fc.close();
242
raf.close();
243
244
File sink = File.createTempFile("sink", null);
245
sink.deleteOnExit();
246
247
FileInputStream fis = new FileInputStream(source);
248
FileChannel sourceChannel = fis.getChannel();
249
250
raf = new RandomAccessFile(sink, "rw");
251
FileChannel sinkChannel = raf.getChannel();
252
253
long bytesWritten = sourceChannel.transferTo(testSize -40, 10,
254
sinkChannel);
255
if (bytesWritten != 10) {
256
throw new RuntimeException("Transfer test 4 failed " +
257
bytesWritten);
258
}
259
sourceChannel.close();
260
sinkChannel.close();
261
262
source.delete();
263
sink.delete();
264
}
265
266
// Test transferFrom with large file
267
public static void xferTest05() throws Exception {
268
// Create a source file & large sink file for the test
269
File source = File.createTempFile("blech", null);
270
source.deleteOnExit();
271
initTestFile(source, 100);
272
273
// Create the sink file as a sparse file if possible
274
File sink = null;
275
FileChannel fc = null;
276
while (fc == null) {
277
sink = File.createTempFile("sink", null);
278
// re-create as a sparse file
279
sink.delete();
280
try {
281
fc = FileChannel.open(sink.toPath(),
282
StandardOpenOption.CREATE_NEW,
283
StandardOpenOption.WRITE,
284
StandardOpenOption.SPARSE);
285
} catch (FileAlreadyExistsException ignore) {
286
// someone else got it
287
}
288
}
289
sink.deleteOnExit();
290
291
long testSize = ((long)Integer.MAX_VALUE) * 2;
292
try {
293
fc.write(ByteBuffer.wrap("Use the source!".getBytes()),
294
testSize - 40);
295
} catch (IOException e) {
296
// Can't set up the test, abort it
297
System.err.println("xferTest05 was aborted.");
298
return;
299
} finally {
300
fc.close();
301
}
302
303
// Get new channels for the source and sink and attempt transfer
304
FileChannel sourceChannel = new FileInputStream(source).getChannel();
305
try {
306
FileChannel sinkChannel = new RandomAccessFile(sink, "rw").getChannel();
307
try {
308
long bytesWritten = sinkChannel.transferFrom(sourceChannel,
309
testSize - 40, 10);
310
if (bytesWritten != 10) {
311
throw new RuntimeException("Transfer test 5 failed " +
312
bytesWritten);
313
}
314
} finally {
315
sinkChannel.close();
316
}
317
} finally {
318
sourceChannel.close();
319
}
320
321
source.delete();
322
sink.delete();
323
}
324
325
static void checkFileData(File file, String expected) throws Exception {
326
FileInputStream fis = new FileInputStream(file);
327
Reader r = new BufferedReader(new InputStreamReader(fis, "ASCII"));
328
StringBuilder sb = new StringBuilder();
329
int c;
330
while ((c = r.read()) != -1)
331
sb.append((char)c);
332
String contents = sb.toString();
333
if (! contents.equals(expected))
334
throw new Exception("expected: " + expected
335
+ ", got: " + contents);
336
r.close();
337
}
338
339
// Test transferFrom asking for more bytes than remain in source
340
public static void xferTest06() throws Exception {
341
String data = "Use the source, Luke!";
342
343
File source = File.createTempFile("source", null);
344
source.deleteOnExit();
345
File sink = File.createTempFile("sink", null);
346
sink.deleteOnExit();
347
348
FileOutputStream fos = new FileOutputStream(source);
349
fos.write(data.getBytes("ASCII"));
350
fos.close();
351
352
FileChannel sourceChannel =
353
new RandomAccessFile(source, "rw").getChannel();
354
sourceChannel.position(7);
355
long remaining = sourceChannel.size() - sourceChannel.position();
356
FileChannel sinkChannel =
357
new RandomAccessFile(sink, "rw").getChannel();
358
long n = sinkChannel.transferFrom(sourceChannel, 0L,
359
sourceChannel.size()); // overflow
360
if (n != remaining)
361
throw new Exception("n == " + n + ", remaining == " + remaining);
362
363
sinkChannel.close();
364
sourceChannel.close();
365
366
checkFileData(source, data);
367
checkFileData(sink, data.substring(7,data.length()));
368
369
source.delete();
370
}
371
372
// Test transferTo to non-blocking socket channel
373
public static void xferTest07() throws Exception {
374
File source = File.createTempFile("source", null);
375
source.deleteOnExit();
376
377
FileChannel sourceChannel = new RandomAccessFile(source, "rw")
378
.getChannel();
379
sourceChannel.position(32000L)
380
.write(ByteBuffer.wrap("The End".getBytes()));
381
382
// The sink is a non-blocking socket channel
383
ServerSocketChannel ssc = ServerSocketChannel.open();
384
ssc.socket().bind(new InetSocketAddress(0));
385
InetSocketAddress sa = new InetSocketAddress(
386
InetAddress.getLocalHost(), ssc.socket().getLocalPort());
387
SocketChannel sink = SocketChannel.open(sa);
388
sink.configureBlocking(false);
389
SocketChannel other = ssc.accept();
390
391
long size = sourceChannel.size();
392
393
// keep sending until congested
394
long n;
395
do {
396
n = sourceChannel.transferTo(0, size, sink);
397
} while (n > 0);
398
399
sourceChannel.close();
400
sink.close();
401
other.close();
402
ssc.close();
403
source.delete();
404
}
405
406
407
// Test transferTo with file positions larger than 2 and 4GB
408
public static void xferTest08() throws Exception {
409
// Creating a sparse 6GB file on Windows takes too long
410
String osName = System.getProperty("os.name");
411
if (osName.startsWith("Windows"))
412
return;
413
414
final long G = 1024L * 1024L * 1024L;
415
416
// Create 6GB file
417
418
File file = File.createTempFile("source", null);
419
file.deleteOnExit();
420
421
RandomAccessFile raf = new RandomAccessFile(file, "rw");
422
FileChannel fc = raf.getChannel();
423
424
try {
425
fc.write(ByteBuffer.wrap("0123456789012345".getBytes("UTF-8")), 6*G);
426
} catch (IOException x) {
427
System.err.println("Unable to create test file:" + x);
428
fc.close();
429
return;
430
}
431
432
// Setup looback connection and echo server
433
434
ServerSocketChannel ssc = ServerSocketChannel.open();
435
ssc.socket().bind(new InetSocketAddress(0));
436
437
InetAddress lh = InetAddress.getLocalHost();
438
InetSocketAddress isa = new InetSocketAddress(lh, ssc.socket().getLocalPort());
439
SocketChannel source = SocketChannel.open(isa);
440
SocketChannel sink = ssc.accept();
441
442
Thread thr = new Thread(new EchoServer(sink));
443
thr.start();
444
445
// Test data is array of positions and counts
446
447
long testdata[][] = {
448
{ 2*G-1, 1 },
449
{ 2*G-1, 10 }, // across 2GB boundary
450
{ 2*G, 1 },
451
{ 2*G, 10 },
452
{ 2*G+1, 1 },
453
{ 4*G-1, 1 },
454
{ 4*G-1, 10 }, // across 4GB boundary
455
{ 4*G, 1 },
456
{ 4*G, 10 },
457
{ 4*G+1, 1 },
458
{ 5*G-1, 1 },
459
{ 5*G-1, 10 },
460
{ 5*G, 1 },
461
{ 5*G, 10 },
462
{ 5*G+1, 1 },
463
{ 6*G, 1 },
464
};
465
466
ByteBuffer sendbuf = ByteBuffer.allocateDirect(100);
467
ByteBuffer readbuf = ByteBuffer.allocateDirect(100);
468
469
try {
470
byte value = 0;
471
for (int i=0; i<testdata.length; i++) {
472
long position = testdata[(int)i][0];
473
long count = testdata[(int)i][1];
474
475
// generate bytes
476
for (long j=0; j<count; j++) {
477
sendbuf.put(++value);
478
}
479
sendbuf.flip();
480
481
// write to file and transfer to echo server
482
fc.write(sendbuf, position);
483
fc.transferTo(position, count, source);
484
485
// read from echo server
486
long nread = 0;
487
while (nread < count) {
488
int n = source.read(readbuf);
489
if (n < 0)
490
throw new RuntimeException("Premature EOF!");
491
nread += n;
492
}
493
494
// check reply from echo server
495
readbuf.flip();
496
sendbuf.flip();
497
if (!readbuf.equals(sendbuf))
498
throw new RuntimeException("Echo'ed bytes do not match!");
499
readbuf.clear();
500
sendbuf.clear();
501
}
502
} finally {
503
source.close();
504
ssc.close();
505
fc.close();
506
file.delete();
507
}
508
}
509
510
// Test that transferFrom with FileChannel source that is not readable
511
// throws NonReadableChannelException
512
static void xferTest09() throws Exception {
513
File source = File.createTempFile("source", null);
514
source.deleteOnExit();
515
516
File target = File.createTempFile("target", null);
517
target.deleteOnExit();
518
519
FileChannel fc1 = new FileOutputStream(source).getChannel();
520
FileChannel fc2 = new RandomAccessFile(target, "rw").getChannel();
521
try {
522
fc2.transferFrom(fc1, 0L, 0);
523
throw new RuntimeException("NonReadableChannelException expected");
524
} catch (NonReadableChannelException expected) {
525
} finally {
526
fc1.close();
527
fc2.close();
528
}
529
}
530
531
/**
532
* Creates file blah of specified size in bytes.
533
*/
534
private static void initTestFile(File blah, long size) throws Exception {
535
if (blah.exists())
536
blah.delete();
537
FileOutputStream fos = new FileOutputStream(blah);
538
BufferedWriter awriter
539
= new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
540
541
for(int i=0; i<size; i++) {
542
awriter.write("e");
543
}
544
awriter.flush();
545
awriter.close();
546
}
547
548
/**
549
* Simple in-process server to echo bytes read by a given socket channel
550
*/
551
static class EchoServer implements Runnable {
552
private SocketChannel sc;
553
554
public EchoServer(SocketChannel sc) {
555
this.sc = sc;
556
}
557
558
public void run() {
559
ByteBuffer bb = ByteBuffer.allocateDirect(1024);
560
try {
561
for (;;) {
562
int n = sc.read(bb);
563
if (n < 0)
564
break;
565
566
bb.flip();
567
while (bb.remaining() > 0) {
568
sc.write(bb);
569
}
570
bb.clear();
571
}
572
} catch (IOException x) {
573
x.printStackTrace();
574
} finally {
575
try {
576
sc.close();
577
} catch (IOException ignore) { }
578
}
579
}
580
}
581
582
}
583
584