Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/SocketChannel/Connect.java
41154 views
1
/*
2
* Copyright (c) 2002, 2018, 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 4650679 8037360
26
* @summary Unit test for socket channels
27
* @library .. /test/lib
28
* @build jdk.test.lib.Utils TestServers
29
* @run main Connect
30
*/
31
32
import java.net.*;
33
import java.nio.*;
34
import java.nio.channels.*;
35
import java.util.*;
36
37
public class Connect {
38
39
private static final long INCREMENTAL_DELAY = 30L * 1000L;
40
41
public static void main(String args[]) throws Exception {
42
try (TestServers.EchoServer echoServer
43
= TestServers.EchoServer.startNewServer(1000)) {
44
test1(echoServer);
45
}
46
try {
47
test1(TestServers.RefusingServer.newRefusingServer());
48
throw new Exception("Refused connection throws no exception");
49
} catch (ConnectException ce) {
50
// Correct result
51
}
52
}
53
54
static void test1(TestServers.AbstractServer server) throws Exception {
55
Selector selector;
56
SocketChannel sc;
57
SelectionKey sk;
58
InetSocketAddress isa = new InetSocketAddress(
59
server.getAddress(), server.getPort());
60
sc = SocketChannel.open();
61
sc.configureBlocking(false);
62
63
selector = Selector.open();
64
sk = sc.register(selector, SelectionKey.OP_CONNECT);
65
if (sc.connect(isa)) {
66
System.err.println("Connected immediately!");
67
sc.close();
68
selector.close();
69
return;
70
} else {
71
ByteBuffer buf = ByteBuffer.allocateDirect(100);
72
buf.asCharBuffer().put(new String(
73
"The quick brown fox jumped over the lazy dog."
74
).toCharArray());
75
buf.flip();
76
long startTime = System.currentTimeMillis();
77
while(true) {
78
selector.select(INCREMENTAL_DELAY);
79
Set selectedKeys = selector.selectedKeys();
80
81
if(selectedKeys.isEmpty()) {
82
System.err.println("Elapsed time without response: " +
83
(System.currentTimeMillis() -
84
startTime) / 1000L + " seconds.");
85
}
86
else if(!selectedKeys.contains(sk))
87
{
88
System.err.println("Got wrong event about selection key.");
89
} else {
90
System.err.println("Got event for our selection key.");
91
if(sk.isConnectable()) {
92
if(sc.finishConnect()) {
93
if(sc.isConnected()) {
94
System.err.println("Successful connect.");
95
sk.interestOps(SelectionKey.OP_WRITE);
96
sc.write(buf);
97
} else {
98
System.err.println(
99
"Finish connect completed incorrectly.");
100
}
101
} else {
102
System.err.println(
103
"key incorrectly indicated socket channel connectable.");
104
}
105
}
106
if(sk.isWritable() && (buf.remaining() > 0)) {
107
sc.write(buf);
108
}
109
if(buf.remaining() == 0) {
110
System.err.println(
111
"SUCCESS! buffer contents were sent.");
112
sc.close();
113
selector.close();
114
return;
115
}
116
}
117
selectedKeys.clear();
118
}
119
}
120
}
121
}
122
123