author | prr |
Fri, 25 May 2018 12:12:24 -0700 | |
changeset 50347 | b2f046ae8eb6 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
44273
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
1 |
/* |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
2 |
* Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved. |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
4 |
* |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
8 |
* |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
13 |
* accompanied this code). |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
14 |
* |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
18 |
* |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
21 |
* questions. |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
22 |
*/ |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
23 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
24 |
/* @test |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
25 |
* @bug 6253145 |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
26 |
* @summary Test FileChannel.transferTo with file positions up to 8GB |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
27 |
* @run testng/timeout=300 TransferTo6GBFile |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
28 |
*/ |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
29 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
30 |
import java.io.File; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
31 |
import java.io.IOException; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
32 |
import java.io.PrintStream; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
33 |
import java.io.RandomAccessFile; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
34 |
import java.net.InetAddress; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
35 |
import java.net.InetSocketAddress; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
36 |
import java.nio.ByteBuffer; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
37 |
import java.nio.channels.FileChannel; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
38 |
import java.nio.channels.ServerSocketChannel; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
39 |
import java.nio.channels.SocketChannel; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
40 |
import java.util.concurrent.TimeUnit; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
41 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
42 |
import org.testng.annotations.Test; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
43 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
44 |
public class TransferTo6GBFile { |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
45 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
46 |
private static PrintStream err = System.err; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
47 |
private static PrintStream out = System.out; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
48 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
49 |
// Test transferTo with file positions larger than 2 and 4GB |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
50 |
@Test |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
51 |
public void xferTest08() throws Exception { // for bug 6253145 |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
52 |
final long G = 1024L * 1024L * 1024L; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
53 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
54 |
// Create 6GB file |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
55 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
56 |
File file = File.createTempFile("source", null); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
57 |
file.deleteOnExit(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
58 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
59 |
RandomAccessFile raf = new RandomAccessFile(file, "rw"); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
60 |
FileChannel fc = raf.getChannel(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
61 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
62 |
out.println(" Writing large file..."); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
63 |
long t0 = System.nanoTime(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
64 |
try { |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
65 |
fc.write(ByteBuffer.wrap("0123456789012345".getBytes("UTF-8")), 6*G); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
66 |
long t1 = System.nanoTime(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
67 |
out.printf(" Wrote large file in %d ns (%d ms) %n", |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
68 |
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0)); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
69 |
} catch (IOException x) { |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
70 |
err.println(" Unable to create test file:" + x); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
71 |
fc.close(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
72 |
return; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
73 |
} |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
74 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
75 |
// Setup looback connection and echo server |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
76 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
77 |
ServerSocketChannel ssc = ServerSocketChannel.open(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
78 |
ssc.socket().bind(new InetSocketAddress(0)); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
79 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
80 |
InetAddress lh = InetAddress.getLocalHost(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
81 |
InetSocketAddress isa = new InetSocketAddress(lh, ssc.socket().getLocalPort()); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
82 |
SocketChannel source = SocketChannel.open(isa); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
83 |
SocketChannel sink = ssc.accept(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
84 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
85 |
Thread thr = new Thread(new EchoServer(sink)); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
86 |
thr.start(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
87 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
88 |
// Test data is array of positions and counts |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
89 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
90 |
long testdata[][] = { |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
91 |
{ 2*G-1, 1 }, |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
92 |
{ 2*G-1, 10 }, // across 2GB boundary |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
93 |
{ 2*G, 1 }, |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
94 |
{ 2*G, 10 }, |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
95 |
{ 2*G+1, 1 }, |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
96 |
{ 4*G-1, 1 }, |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
97 |
{ 4*G-1, 10 }, // across 4GB boundary |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
98 |
{ 4*G, 1 }, |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
99 |
{ 4*G, 10 }, |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
100 |
{ 4*G+1, 1 }, |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
101 |
{ 5*G-1, 1 }, |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
102 |
{ 5*G-1, 10 }, |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
103 |
{ 5*G, 1 }, |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
104 |
{ 5*G, 10 }, |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
105 |
{ 5*G+1, 1 }, |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
106 |
{ 6*G, 1 }, |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
107 |
}; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
108 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
109 |
ByteBuffer sendbuf = ByteBuffer.allocateDirect(100); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
110 |
ByteBuffer readbuf = ByteBuffer.allocateDirect(100); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
111 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
112 |
try { |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
113 |
byte value = 0; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
114 |
for (int i=0; i<testdata.length; i++) { |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
115 |
long position = testdata[(int)i][0]; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
116 |
long count = testdata[(int)i][1]; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
117 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
118 |
// generate bytes |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
119 |
for (long j=0; j<count; j++) { |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
120 |
sendbuf.put(++value); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
121 |
} |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
122 |
sendbuf.flip(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
123 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
124 |
// write to file and transfer to echo server |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
125 |
fc.write(sendbuf, position); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
126 |
t0 = System.nanoTime(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
127 |
fc.transferTo(position, count, source); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
128 |
out.printf(" transferTo(%d, %2d, source): %d ns%n", |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
129 |
position, count, System.nanoTime() - t0); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
130 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
131 |
// read from echo server |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
132 |
long nread = 0; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
133 |
while (nread < count) { |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
134 |
int n = source.read(readbuf); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
135 |
if (n < 0) |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
136 |
throw new RuntimeException("Premature EOF!"); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
137 |
nread += n; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
138 |
} |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
139 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
140 |
// check reply from echo server |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
141 |
readbuf.flip(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
142 |
sendbuf.flip(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
143 |
if (!readbuf.equals(sendbuf)) |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
144 |
throw new RuntimeException("Echoed bytes do not match!"); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
145 |
readbuf.clear(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
146 |
sendbuf.clear(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
147 |
} |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
148 |
} finally { |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
149 |
source.close(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
150 |
ssc.close(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
151 |
fc.close(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
152 |
file.delete(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
153 |
} |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
154 |
} |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
155 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
156 |
/** |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
157 |
* Simple in-process server to echo bytes read by a given socket channel |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
158 |
*/ |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
159 |
static class EchoServer implements Runnable { |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
160 |
private SocketChannel sc; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
161 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
162 |
public EchoServer(SocketChannel sc) { |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
163 |
this.sc = sc; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
164 |
} |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
165 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
166 |
public void run() { |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
167 |
ByteBuffer bb = ByteBuffer.allocateDirect(1024); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
168 |
try { |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
169 |
for (;;) { |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
170 |
int n = sc.read(bb); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
171 |
if (n < 0) |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
172 |
break; |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
173 |
|
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
174 |
bb.flip(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
175 |
while (bb.remaining() > 0) { |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
176 |
sc.write(bb); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
177 |
} |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
178 |
bb.clear(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
179 |
} |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
180 |
} catch (IOException x) { |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
181 |
x.printStackTrace(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
182 |
} finally { |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
183 |
try { |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
184 |
sc.close(); |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
185 |
} catch (IOException ignore) { } |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
186 |
} |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
187 |
} |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
188 |
} |
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff
changeset
|
189 |
} |