Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/http/ChunkedOutputStream/CheckError.java
41154 views
1
/*
2
* Copyright (c) 2004, 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 5054016
27
* @library /test/lib
28
* @summary get the failure immediately when writing individual chunks over socket fail
29
* @run main CheckError
30
* @run main/othervm -Djava.net.preferIPv6Addresses=true CheckError
31
*/
32
33
import java.io.BufferedReader;
34
import java.io.IOException;
35
import java.io.InputStream;
36
import java.io.InputStreamReader;
37
import java.io.OutputStream;
38
import java.net.HttpURLConnection;
39
import java.net.InetAddress;
40
import java.net.InetSocketAddress;
41
import java.net.Proxy;
42
import java.net.ServerSocket;
43
import java.net.Socket;
44
import java.net.URL;
45
import static java.lang.System.out;
46
47
import jdk.test.lib.net.URIBuilder;
48
49
public class CheckError {
50
51
static int BUFFER_SIZE = 8192; // 8k
52
static int TOTAL_BYTES = 1 * 1024 * 1024; // 1M
53
54
public static void main(String[] args) throws Exception {
55
56
HTTPServer server = new HTTPServer();
57
server.start();
58
int port = server.getPort();
59
out.println("Server listening on " + port);
60
61
62
URL url = URIBuilder.newBuilder()
63
.scheme("http")
64
.host(server.getAddress())
65
.port(port)
66
.toURL();
67
68
HttpURLConnection conn = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
69
conn.setRequestMethod("POST");
70
conn.setDoOutput(true);
71
conn.setChunkedStreamingMode(1024);
72
73
out.println("sending " + TOTAL_BYTES + " bytes");
74
75
int byteAtOnce;
76
int sendingBytes = TOTAL_BYTES;
77
byte[] buffer = getBuffer(BUFFER_SIZE);
78
try (OutputStream toServer = conn.getOutputStream()) {
79
while (sendingBytes > 0) {
80
if (sendingBytes > BUFFER_SIZE) {
81
byteAtOnce = BUFFER_SIZE;
82
} else {
83
byteAtOnce = sendingBytes;
84
}
85
toServer.write(buffer, 0, byteAtOnce);
86
sendingBytes -= byteAtOnce;
87
out.print((TOTAL_BYTES - sendingBytes) + " was sent. ");
88
toServer.flush();
89
// gives the server thread time to read, and eventually close;
90
Thread.sleep(500);
91
}
92
} catch (IOException expected) {
93
// Expected IOException due to server.close()
94
out.println("PASSED. Caught expected: " + expected);
95
return;
96
}
97
98
// Expected IOException not received. FAIL
99
throw new RuntimeException("Failed: Expected IOException not received");
100
}
101
102
static byte[] getBuffer(int size) {
103
byte[] buffer = new byte[size];
104
for (int i = 0; i < size; i++)
105
buffer[i] = (byte)i;
106
return buffer;
107
}
108
109
static class HTTPServer extends Thread {
110
111
final ServerSocket serverSocket;
112
113
HTTPServer() throws IOException {
114
InetAddress loopback = InetAddress.getLoopbackAddress();
115
serverSocket = new ServerSocket();
116
serverSocket.bind(new InetSocketAddress(loopback, 0));
117
}
118
119
int getPort() {
120
return serverSocket.getLocalPort();
121
}
122
123
InetAddress getAddress() {
124
return serverSocket.getInetAddress();
125
}
126
127
public void run() {
128
try (Socket client = serverSocket.accept()) {
129
130
InputStream in = client.getInputStream();
131
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
132
String line;
133
do {
134
line = reader.readLine();
135
out.println("Server: " + line);
136
} while (line != null && line.length() > 0);
137
138
System.out.println("Server: receiving some data");
139
// just read some data, then close the connection
140
in.read(new byte[1024]);
141
142
in.close();
143
client.close();
144
out.println("Server closed socket");
145
} catch (IOException e) {
146
e.printStackTrace();
147
}
148
}
149
}
150
}
151
152