Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/SocketChannel/VectorIO.java
41154 views
1
/*
2
* Copyright (c) 2000, 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 8191025
26
* @summary Test socketchannel vector IO (use -Dseed=X to set PRNG seed)
27
* @library .. /test/lib
28
* @build jdk.test.lib.RandomFactory
29
* @run main VectorIO
30
* @key randomness
31
*/
32
33
import java.io.*;
34
import java.net.*;
35
import java.nio.*;
36
import java.nio.channels.*;
37
import java.util.*;
38
import jdk.test.lib.RandomFactory;
39
40
public class VectorIO {
41
42
private static Random generator = RandomFactory.getRandom();
43
44
static int testSize;
45
46
// whether to use the write/read variant with a length parameter
47
static boolean setLength;
48
49
public static void main(String[] args) throws Exception {
50
testSize = 1;
51
setLength = false;
52
runTest();
53
for(int i=15; i<18; i++) {
54
testSize = i;
55
setLength = !setLength;
56
runTest();
57
}
58
}
59
60
static void runTest() throws Exception {
61
System.err.println("Length " + testSize);
62
Server sv = new Server(testSize);
63
sv.start();
64
bufferTest(sv.port());
65
if (sv.finish(8000) == 0)
66
throw new Exception("Failed: Length = " + testSize);
67
}
68
69
static void bufferTest(int port) throws Exception {
70
ByteBuffer[] bufs = new ByteBuffer[testSize];
71
long total = 0L;
72
for(int i=0; i<testSize; i++) {
73
String source = "buffer" + i;
74
if (generator.nextBoolean())
75
bufs[i] = ByteBuffer.allocateDirect(source.length());
76
else
77
bufs[i] = ByteBuffer.allocate(source.length());
78
79
bufs[i].put(source.getBytes("8859_1"));
80
bufs[i].flip();
81
total += bufs[i].remaining();
82
}
83
84
ByteBuffer[] bufsPlus1 = new ByteBuffer[bufs.length + 1];
85
System.arraycopy(bufs, 0, bufsPlus1, 0, bufs.length);
86
87
// Get a connection to the server
88
InetAddress lh = InetAddress.getLocalHost();
89
InetSocketAddress isa = new InetSocketAddress(lh, port);
90
SocketChannel sc = SocketChannel.open();
91
sc.connect(isa);
92
sc.configureBlocking(generator.nextBoolean());
93
94
// Write the data out
95
long rem = total;
96
while (rem > 0L) {
97
long bytesWritten;
98
if (setLength) {
99
bytesWritten = sc.write(bufsPlus1, 0, bufs.length);
100
} else {
101
bytesWritten = sc.write(bufs);
102
}
103
if (bytesWritten == 0) {
104
if (sc.isBlocking()) {
105
throw new RuntimeException("write did not block");
106
} else {
107
System.err.println("Non-blocking write() wrote zero bytes");
108
}
109
Thread.sleep(50);
110
} else {
111
rem -= bytesWritten;
112
}
113
}
114
115
// Clean up
116
sc.close();
117
}
118
119
static class Server
120
extends TestThread
121
{
122
final int testSize;
123
final ServerSocketChannel ssc;
124
125
Server(int testSize) throws IOException {
126
super("Server " + testSize);
127
this.testSize = testSize;
128
this.ssc = ServerSocketChannel.open().bind(new InetSocketAddress(0));
129
}
130
131
int port() {
132
return ssc.socket().getLocalPort();
133
}
134
135
void go() throws Exception {
136
bufferTest();
137
}
138
139
void bufferTest() throws Exception {
140
long total = 0L;
141
ByteBuffer[] bufs = new ByteBuffer[testSize];
142
for(int i=0; i<testSize; i++) {
143
String source = "buffer" + i;
144
if (generator.nextBoolean())
145
bufs[i] = ByteBuffer.allocateDirect(source.length());
146
else
147
bufs[i] = ByteBuffer.allocate(source.length());
148
total += bufs[i].capacity();
149
}
150
151
ByteBuffer[] bufsPlus1 = new ByteBuffer[bufs.length + 1];
152
System.arraycopy(bufs, 0, bufsPlus1, 0, bufs.length);
153
154
// Get a connection from client
155
SocketChannel sc = null;
156
157
try {
158
159
ssc.configureBlocking(false);
160
161
for (;;) {
162
sc = ssc.accept();
163
if (sc != null) {
164
System.err.println("accept() succeeded");
165
break;
166
}
167
Thread.sleep(50);
168
}
169
170
sc.configureBlocking(generator.nextBoolean());
171
172
// Read data into multiple buffers
173
long avail = total;
174
while (avail > 0) {
175
long bytesRead;
176
if (setLength) {
177
bytesRead = sc.read(bufsPlus1, 0, bufs.length);
178
} else {
179
bytesRead = sc.read(bufs);
180
}
181
if (bytesRead < 0)
182
break;
183
if (bytesRead == 0) {
184
if (sc.isBlocking()) {
185
throw new RuntimeException("read did not block");
186
} else {
187
System.err.println
188
("Non-blocking read() read zero bytes");
189
}
190
Thread.sleep(50);
191
}
192
avail -= bytesRead;
193
}
194
195
// Check results
196
for(int i=0; i<testSize; i++) {
197
String expected = "buffer" + i;
198
bufs[i].flip();
199
int size = bufs[i].capacity();
200
byte[] data = new byte[size];
201
for(int j=0; j<size; j++)
202
data[j] = bufs[i].get();
203
String message = new String(data, "8859_1");
204
if (!message.equals(expected))
205
throw new Exception("Wrong data: Got "
206
+ message + ", expected "
207
+ expected);
208
}
209
210
} finally {
211
// Clean up
212
ssc.close();
213
if (sc != null)
214
sc.close();
215
}
216
217
}
218
219
}
220
221
}
222
223