Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socket/CloseAvailable.java
41149 views
1
/*
2
* Copyright (c) 1998, 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 4091859 8189366
27
* @library /test/lib
28
* @summary Test Socket.available()
29
* @run main CloseAvailable
30
* @run main/othervm -Djava.net.preferIPv4Stack=true CloseAvailable
31
*/
32
33
import java.net.*;
34
import java.io.*;
35
import jdk.test.lib.net.IPSupport;
36
37
38
public class CloseAvailable {
39
40
public static void main(String[] args) throws Exception {
41
IPSupport.throwSkippedExceptionIfNonOperational();
42
43
testClose();
44
45
testEOF(true);
46
testEOF(false);
47
testIOEOnClosed(true);
48
testIOEOnClosed(false);
49
}
50
51
static void testClose() throws IOException {
52
boolean error = true;
53
InetAddress addr = InetAddress.getLocalHost();
54
ServerSocket ss = new ServerSocket(0, 0, addr);
55
int port = ss.getLocalPort();
56
57
Thread t = new Thread(new Thread("Close-Available-1") {
58
public void run() {
59
try {
60
Socket s = new Socket(addr, port);
61
s.close();
62
} catch (Exception e) {
63
e.printStackTrace();
64
}
65
}
66
});
67
68
t.start();
69
70
Socket soc = ss.accept();
71
ss.close();
72
73
DataInputStream is = new DataInputStream(soc.getInputStream());
74
is.close();
75
76
try {
77
is.available();
78
}
79
catch (IOException ex) {
80
error = false;
81
}
82
if (error)
83
throw new RuntimeException("Available() can be called after stream closed.");
84
}
85
86
// Verifies consistency of `available` behaviour when EOF reached, both
87
// explicitly and implicitly.
88
static void testEOF(boolean readUntilEOF) throws IOException {
89
System.out.println("testEOF, readUntilEOF: " + readUntilEOF);
90
InetAddress addr = InetAddress.getLoopbackAddress();
91
ServerSocket ss = new ServerSocket();
92
ss.bind(new InetSocketAddress(addr, 0), 0);
93
int port = ss.getLocalPort();
94
95
try (Socket s = new Socket(addr, port)) {
96
s.getOutputStream().write(0x42);
97
s.shutdownOutput();
98
99
try (Socket soc = ss.accept()) {
100
ss.close();
101
102
InputStream is = soc.getInputStream();
103
int b = is.read();
104
assert b == 0x42;
105
assert !s.isClosed();
106
if (readUntilEOF) {
107
b = is.read();
108
assert b == -1;
109
}
110
111
int a;
112
for (int i = 0; i < 100; i++) {
113
a = is.available();
114
System.out.print(a + ", ");
115
if (a != 0)
116
throw new RuntimeException("Unexpected non-zero available: " + a);
117
}
118
assert !s.isClosed();
119
assert is.read() == -1;
120
}
121
}
122
System.out.println("\ncomplete");
123
}
124
125
// Verifies IOException thrown by `available`, on a closed input stream
126
// that may, or may not, have reached EOF prior to closure.
127
static void testIOEOnClosed(boolean readUntilEOF) throws IOException {
128
System.out.println("testIOEOnClosed, readUntilEOF: " + readUntilEOF);
129
InetAddress addr = InetAddress.getLoopbackAddress();
130
ServerSocket ss = new ServerSocket();
131
ss.bind(new InetSocketAddress(addr, 0), 0);
132
int port = ss.getLocalPort();
133
134
try (Socket s = new Socket(addr, port)) {
135
s.getOutputStream().write(0x43);
136
s.shutdownOutput();
137
138
try (Socket soc = ss.accept()) {
139
ss.close();
140
141
InputStream is = soc.getInputStream();
142
int b = is.read();
143
assert b == 0x43;
144
assert !s.isClosed();
145
if (readUntilEOF) {
146
b = is.read();
147
assert b == -1;
148
}
149
is.close();
150
try {
151
b = is.available();
152
throw new RuntimeException("UNEXPECTED successful read: " + b);
153
} catch (IOException expected) {
154
System.out.println("caught expected IOException:" + expected);
155
}
156
}
157
}
158
System.out.println("\ncomplete");
159
}
160
}
161
162