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