Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/ServerSocketChannel/AdaptServerSocket.java
41154 views
1
/*
2
* Copyright (c) 2001, 2016, 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 4286936 8146213
26
* @summary Unit test for server-socket-channel adaptors
27
*/
28
29
import java.io.*;
30
import java.net.*;
31
import java.nio.*;
32
import java.nio.channels.*;
33
import java.nio.charset.*;
34
35
36
public class AdaptServerSocket {
37
38
static java.io.PrintStream out = System.out;
39
static volatile boolean clientStarted = false;
40
static volatile Exception clientException = null;
41
static volatile Thread client = null;
42
43
static void startClient(final int port, final int dally)
44
throws Exception
45
{
46
Thread t = new Thread() {
47
public void run() {
48
try (Socket so = new Socket()) {
49
out.println("client: " + so);
50
clientStarted = true;
51
if (dally > 0)
52
Thread.sleep(dally);
53
so.connect(new InetSocketAddress(port));
54
if (Thread.interrupted()) {
55
out.println("client interrupted");
56
return;
57
}
58
out.println("client: " + so);
59
int a = so.getInputStream().read();
60
out.println("client: read " + a);
61
a += 1;
62
so.getOutputStream().write(a);
63
out.println("client: wrote " + a);
64
} catch (Exception x) {
65
if (x instanceof InterruptedException)
66
return;
67
clientException = x;
68
x.printStackTrace();
69
}
70
}
71
};
72
t.setDaemon(true);
73
t.start();
74
client = t;
75
}
76
77
static void test(int clientDally, int timeout, boolean shouldTimeout)
78
throws Exception
79
{
80
boolean needClient = !shouldTimeout;
81
client = null;
82
clientException = null;
83
clientStarted = false;
84
out.println();
85
86
try (ServerSocketChannel ssc = ServerSocketChannel.open();
87
ServerSocket sso = ssc.socket()) {
88
out.println("created: " + ssc);
89
out.println(" " + sso);
90
if (timeout != 0)
91
sso.setSoTimeout(timeout);
92
out.println("timeout: " + sso.getSoTimeout());
93
sso.bind(null);
94
out.println("bound: " + ssc);
95
out.println(" " + sso);
96
if (needClient) {
97
startClient(sso.getLocalPort(), clientDally);
98
while (!clientStarted) {
99
Thread.sleep(20);
100
}
101
}
102
Socket so = null;
103
try {
104
so = sso.accept();
105
} catch (SocketTimeoutException x) {
106
if (shouldTimeout)
107
out.println("Accept timed out, as expected");
108
else
109
throw x;
110
}
111
if (shouldTimeout && (so != null))
112
throw new Exception("Accept did not time out");
113
114
if (so != null) {
115
int a = 42;
116
so.getOutputStream().write(a);
117
int b = so.getInputStream().read();
118
if (b != a + 1)
119
throw new Exception("Read incorrect data");
120
out.println("server: read " + b);
121
}
122
}
123
if (needClient) {
124
client.interrupt();
125
client.join();
126
if (clientException != null)
127
throw clientException;
128
}
129
}
130
131
public static void main(String[] args) throws Exception {
132
test(0, 0, false);
133
test(50, 5000, false);
134
test(500, 50, true);
135
}
136
137
}
138
139