Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/unixdomain/Shutdown.java
41153 views
1
/*
2
* Copyright (c) 2020, 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
/**
25
* @test
26
* @bug 8245194
27
* @run main/othervm Shutdown
28
*/
29
30
import java.io.Closeable;
31
import java.io.IOException;
32
import java.net.*;
33
import java.nio.ByteBuffer;
34
import java.nio.channels.*;
35
import java.nio.charset.StandardCharsets;
36
import java.nio.file.Files;
37
import java.util.Arrays;
38
39
/**
40
* Check that half close works
41
*/
42
public class Shutdown {
43
44
public static void main(String args[]) throws Exception {
45
if (!supported()) {
46
System.out.println("Unix domain channels not supported");
47
return;
48
}
49
runTest();
50
}
51
52
static boolean supported() {
53
try {
54
SocketChannel.open(StandardProtocolFamily.UNIX).close();
55
} catch (UnsupportedOperationException e) {
56
return false;
57
} catch (Exception e) {
58
return true; // continue test to see what problem is
59
}
60
return true;
61
}
62
63
static void assertTrue(boolean condition, String error) {
64
if (!condition)
65
throw new RuntimeException(error);
66
}
67
68
public static void runTest() throws IOException {
69
ServerSocketChannel server = null;
70
SocketChannel client = null;
71
SocketChannel acceptee = null;
72
UnixDomainSocketAddress usa = null;
73
74
try {
75
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
76
server.bind(null);
77
usa = (UnixDomainSocketAddress)server.getLocalAddress();
78
System.out.println("Local address " + usa);
79
client = SocketChannel.open(usa);
80
acceptee = server.accept();
81
ByteBuffer buf = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.ISO_8859_1));
82
ByteBuffer rx = ByteBuffer.allocate(buf.capacity());
83
client.write(buf);
84
buf.rewind();
85
while (rx.hasRemaining())
86
acceptee.read(rx);
87
88
assertTrue(Arrays.equals(buf.array(), rx.array()), "array contents not equal");
89
90
client.shutdownOutput();
91
try {
92
client.write(buf);
93
throw new RuntimeException("shutdown error");
94
} catch (ClosedChannelException e) {
95
}
96
97
rx.clear();
98
int c = acceptee.read(rx);
99
assertTrue(c == -1, "read after remote shutdown");
100
101
client.configureBlocking(false);
102
c = client.read(rx);
103
assertTrue(c == 0, "expected c == 0");
104
client.shutdownInput();
105
c = client.read(rx);
106
assertTrue(c == -1, "expected c == -1");
107
} finally {
108
close(server);
109
close(client);
110
close(acceptee);
111
if (usa != null)
112
Files.delete(usa.getPath());
113
}
114
System.out.println("OK");
115
}
116
117
static void close(Closeable c) {
118
try {
119
if (c != null)
120
c.close();
121
} catch (IOException e) {}
122
}
123
}
124
125