Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/Selector/OutOfBand.java
41153 views
1
/*
2
* Copyright (c) 2010, 2021, 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 6213702
26
* @summary OOB data causes a SocketChannel, with OOBINLINE disabled, to be selected
27
*/
28
29
/* @test
30
* @requires (os.family == "windows")
31
* @run main/othervm -Djava.nio.channels.spi.SelectorProvider=sun.nio.ch.WindowsSelectorProvider OutOfBand
32
*/
33
34
import java.net.*;
35
import java.nio.ByteBuffer;
36
import java.nio.channels.*;
37
import java.io.IOException;
38
39
public class OutOfBand {
40
41
public static void main(String[] args) throws Exception {
42
ServerSocketChannel ssc = null;
43
SocketChannel sc = null;
44
Selector sel = null;
45
Socket s = null;
46
47
try {
48
// establish loopback connection.
49
ssc = ServerSocketChannel.open().bind(new InetSocketAddress(0));
50
s = new Socket(InetAddress.getLocalHost(),
51
ssc.socket().getLocalPort());
52
sc = ssc.accept();
53
54
sel = Selector.open();
55
sc.configureBlocking(false);
56
sc.register(sel, SelectionKey.OP_READ);
57
58
// OOB data should be disabled by default
59
if (sc.socket().getOOBInline())
60
throw new RuntimeException("SO_OOBINLINE enabled");
61
test(s, false, 0, 0, sel);
62
test(s, false, 512, 0, sel);
63
test(s, false, 0, 512, sel);
64
test(s, false, 512, 512, sel);
65
66
// enable SO_OOBINLINE
67
sc.socket().setOOBInline(true);
68
69
// OOB data should be received
70
test(s, true, 0, 0, sel);
71
test(s, true, 512, 0, sel);
72
test(s, true, 0, 512, sel);
73
test(s, true, 512, 512, sel);
74
75
} finally {
76
if (sel != null) sel.close();
77
if (sc != null) sc.close();
78
if (ssc != null) ssc.close();
79
if (s != null) sc.close();
80
}
81
}
82
83
static void test(Socket s, boolean urgentExpected,
84
int bytesBefore, int bytesAfter,
85
Selector sel)
86
throws IOException
87
{
88
// send data
89
int bytesExpected = 0;
90
if (bytesBefore > 0) {
91
s.getOutputStream().write(new byte[bytesBefore]);
92
bytesExpected += bytesBefore;
93
}
94
s.sendUrgentData(0xff);
95
if (urgentExpected)
96
bytesExpected++;
97
if (bytesAfter > 0) {
98
s.getOutputStream().write(new byte[bytesAfter]);
99
bytesExpected += bytesAfter;
100
}
101
102
// receive data, checking for spurious wakeups and reads
103
int spuriousWakeups = 0;
104
int spuriousReads = 0;
105
int bytesRead = 0;
106
ByteBuffer bb = ByteBuffer.allocate(100);
107
for (;;) {
108
int n = sel.select(2000);
109
if (n == 0) {
110
if (bytesRead == bytesExpected) {
111
System.out.format("Selector wakeups %d\tSpurious reads %d%n",
112
spuriousWakeups, spuriousReads);
113
return;
114
}
115
if (++spuriousWakeups >= 3)
116
throw new RuntimeException("Selector appears to be spinning" +
117
" or data not received");
118
continue;
119
}
120
if (n > 1)
121
throw new RuntimeException("More than one key selected????");
122
SelectionKey key = sel.selectedKeys().iterator().next();
123
bb.clear();
124
n = ((SocketChannel)key.channel()).read(bb);
125
if (n == 0) {
126
if (++spuriousReads >=3)
127
throw new RuntimeException("Too many spurious reads");
128
} else {
129
bytesRead += n;
130
if (bytesRead > bytesExpected)
131
throw new RuntimeException("Received more than expected");
132
}
133
sel.selectedKeys().clear();
134
}
135
}
136
}
137
138