Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socket/ShutdownInput.java
41149 views
1
/*
2
* Copyright (c) 2011, 2019, 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 7014860
27
* @library /test/lib
28
* @summary Socket.getInputStream().available() not clear for
29
* case that connection is shutdown for reading
30
* @run main ShutdownInput
31
* @run main/othervm -Djava.net.preferIPv4Stack=true ShutdownInput
32
*/
33
34
import java.io.InputStream;
35
import java.io.OutputStream;
36
import java.net.InetAddress;
37
import java.net.InetSocketAddress;
38
import java.net.ServerSocket;
39
import java.net.Socket;
40
import java.nio.channels.ServerSocketChannel;
41
import java.nio.channels.SocketChannel;
42
import jdk.test.lib.net.IPSupport;
43
44
public class ShutdownInput {
45
static boolean failed = false;
46
47
public static void main(String args[]) throws Exception {
48
IPSupport.throwSkippedExceptionIfNonOperational();
49
50
InetAddress iaddr = InetAddress.getLoopbackAddress();
51
52
try (ServerSocket ss = new ServerSocket(0, 0, iaddr);
53
Socket s1 = new Socket(iaddr, ss.getLocalPort());
54
Socket s2 = ss.accept() ) {
55
56
test(s1, s2, "Testing NET");
57
}
58
59
// check the NIO socket adapter
60
InetSocketAddress socketAddress = new InetSocketAddress(iaddr, 0);
61
try (ServerSocketChannel sc = ServerSocketChannel.open().bind(socketAddress);
62
SocketChannel s1 = SocketChannel.open(
63
new InetSocketAddress(iaddr, sc.socket().getLocalPort()));
64
SocketChannel s2 = sc.accept() ) {
65
66
test(s1.socket(), s2.socket(), "Testing NIO");
67
}
68
69
if (failed) {
70
throw new RuntimeException("Failed: check output");
71
}
72
}
73
74
public static void test(Socket s1, Socket s2, String mesg) throws Exception {
75
OutputStream os = s1.getOutputStream();
76
os.write("This is a message".getBytes("US-ASCII"));
77
78
InputStream in = s2.getInputStream();
79
s2.shutdownInput();
80
81
if (in.available() != 0) {
82
failed = true;
83
System.out.println(mesg + ":" + s2 + " in.available() should be 0, " +
84
"but returns "+ in.available());
85
}
86
87
byte[] ba = new byte[2];
88
if (in.read() != -1 ||
89
in.read(ba) != -1 ||
90
in.read(ba, 0, ba.length) != -1) {
91
92
failed = true;
93
System.out.append(mesg + ":" + s2 + " in.read() should be -1");
94
}
95
}
96
}
97
98