Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/FileChannel/TransferTo6GBFile.java
41154 views
1
/*
2
* Copyright (c) 2001, 2017, 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 6253145
26
* @summary Test FileChannel.transferTo with file positions up to 8GB
27
* @run testng/timeout=300 TransferTo6GBFile
28
*/
29
30
import java.io.File;
31
import java.io.IOException;
32
import java.io.PrintStream;
33
import java.io.RandomAccessFile;
34
import java.net.InetAddress;
35
import java.net.InetSocketAddress;
36
import java.nio.ByteBuffer;
37
import java.nio.channels.FileChannel;
38
import java.nio.channels.ServerSocketChannel;
39
import java.nio.channels.SocketChannel;
40
import java.util.concurrent.TimeUnit;
41
42
import org.testng.annotations.Test;
43
44
public class TransferTo6GBFile {
45
46
private static PrintStream err = System.err;
47
private static PrintStream out = System.out;
48
49
// Test transferTo with file positions larger than 2 and 4GB
50
@Test
51
public void xferTest08() throws Exception { // for bug 6253145
52
final long G = 1024L * 1024L * 1024L;
53
54
// Create 6GB file
55
56
File file = File.createTempFile("source", null);
57
file.deleteOnExit();
58
59
RandomAccessFile raf = new RandomAccessFile(file, "rw");
60
FileChannel fc = raf.getChannel();
61
62
out.println(" Writing large file...");
63
long t0 = System.nanoTime();
64
try {
65
fc.write(ByteBuffer.wrap("0123456789012345".getBytes("UTF-8")), 6*G);
66
long t1 = System.nanoTime();
67
out.printf(" Wrote large file in %d ns (%d ms) %n",
68
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
69
} catch (IOException x) {
70
err.println(" Unable to create test file:" + x);
71
fc.close();
72
return;
73
}
74
75
// Setup looback connection and echo server
76
77
ServerSocketChannel ssc = ServerSocketChannel.open();
78
ssc.socket().bind(new InetSocketAddress(0));
79
80
InetAddress lh = InetAddress.getLocalHost();
81
InetSocketAddress isa = new InetSocketAddress(lh, ssc.socket().getLocalPort());
82
SocketChannel source = SocketChannel.open(isa);
83
SocketChannel sink = ssc.accept();
84
85
Thread thr = new Thread(new EchoServer(sink));
86
thr.start();
87
88
// Test data is array of positions and counts
89
90
long testdata[][] = {
91
{ 2*G-1, 1 },
92
{ 2*G-1, 10 }, // across 2GB boundary
93
{ 2*G, 1 },
94
{ 2*G, 10 },
95
{ 2*G+1, 1 },
96
{ 4*G-1, 1 },
97
{ 4*G-1, 10 }, // across 4GB boundary
98
{ 4*G, 1 },
99
{ 4*G, 10 },
100
{ 4*G+1, 1 },
101
{ 5*G-1, 1 },
102
{ 5*G-1, 10 },
103
{ 5*G, 1 },
104
{ 5*G, 10 },
105
{ 5*G+1, 1 },
106
{ 6*G, 1 },
107
};
108
109
ByteBuffer sendbuf = ByteBuffer.allocateDirect(100);
110
ByteBuffer readbuf = ByteBuffer.allocateDirect(100);
111
112
try {
113
byte value = 0;
114
for (int i=0; i<testdata.length; i++) {
115
long position = testdata[(int)i][0];
116
long count = testdata[(int)i][1];
117
118
// generate bytes
119
for (long j=0; j<count; j++) {
120
sendbuf.put(++value);
121
}
122
sendbuf.flip();
123
124
// write to file and transfer to echo server
125
fc.write(sendbuf, position);
126
t0 = System.nanoTime();
127
fc.transferTo(position, count, source);
128
out.printf(" transferTo(%d, %2d, source): %d ns%n",
129
position, count, System.nanoTime() - t0);
130
131
// read from echo server
132
long nread = 0;
133
while (nread < count) {
134
int n = source.read(readbuf);
135
if (n < 0)
136
throw new RuntimeException("Premature EOF!");
137
nread += n;
138
}
139
140
// check reply from echo server
141
readbuf.flip();
142
sendbuf.flip();
143
if (!readbuf.equals(sendbuf))
144
throw new RuntimeException("Echoed bytes do not match!");
145
readbuf.clear();
146
sendbuf.clear();
147
}
148
} finally {
149
source.close();
150
ssc.close();
151
fc.close();
152
file.delete();
153
}
154
}
155
156
/**
157
* Simple in-process server to echo bytes read by a given socket channel
158
*/
159
static class EchoServer implements Runnable {
160
private SocketChannel sc;
161
162
public EchoServer(SocketChannel sc) {
163
this.sc = sc;
164
}
165
166
public void run() {
167
ByteBuffer bb = ByteBuffer.allocateDirect(1024);
168
try {
169
for (;;) {
170
int n = sc.read(bb);
171
if (n < 0)
172
break;
173
174
bb.flip();
175
while (bb.remaining() > 0) {
176
sc.write(bb);
177
}
178
bb.clear();
179
}
180
} catch (IOException x) {
181
x.printStackTrace();
182
} finally {
183
try {
184
sc.close();
185
} catch (IOException ignore) { }
186
}
187
}
188
}
189
}
190
191