Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/httpclient/ByteArrayPublishers.java
41149 views
1
/*
2
* Copyright (c) 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 8222968
27
* @summary ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
28
* @run main/othervm ByteArrayPublishers
29
*/
30
31
import java.net.InetAddress;
32
import java.net.URI;
33
import java.net.URISyntaxException;
34
import java.net.http.HttpClient;
35
import java.net.http.HttpRequest;
36
import java.net.http.HttpResponse;
37
import java.util.ArrayList;
38
import java.util.concurrent.LinkedBlockingQueue;
39
import java.util.concurrent.CompletableFuture;
40
41
import com.sun.net.httpserver.HttpExchange;
42
import com.sun.net.httpserver.HttpServer;
43
44
import java.io.IOException;
45
import java.net.InetSocketAddress;
46
import java.nio.charset.StandardCharsets;
47
import static java.net.http.HttpRequest.BodyPublisher;
48
import static java.net.http.HttpRequest.BodyPublishers;
49
50
public class ByteArrayPublishers {
51
private static final BodyPublisher BODY_PUBLISHER =
52
BodyPublishers.ofByteArray("abcdefghijklmnopqrstuvwxyz".getBytes());
53
54
static int LOOPS = 100;
55
56
public static void main(String[] args) throws Exception {
57
HttpServer server = null;
58
try {
59
InetAddress loopBack = InetAddress.getLoopbackAddress();
60
String lpBackStr = loopBack.getHostAddress();
61
InetSocketAddress serverAddr = new InetSocketAddress(loopBack, 0);
62
server = HttpServer.create(serverAddr, 500);
63
server.createContext("/", (HttpExchange e) -> {
64
e.getRequestBody().readAllBytes();
65
String response = "Hello world";
66
e.sendResponseHeaders(200, response.length());
67
e.getResponseBody().write(response.getBytes(StandardCharsets.ISO_8859_1));
68
e.close();
69
});
70
server.start();
71
var address = server.getAddress();
72
URI dest = new URI("http://" + lpBackStr + ":"
73
+ Integer.toString(address.getPort()) + "/");
74
75
HttpClient client = createClient();
76
77
ArrayList<CompletableFuture<HttpResponse<Void>>> futures = new ArrayList<>(LOOPS);
78
LinkedBlockingQueue<Object> results = new LinkedBlockingQueue<Object>();
79
for (int i=0;i<LOOPS;i++) {
80
futures.add(
81
client.sendAsync(createRequest(dest), HttpResponse.BodyHandlers.discarding())
82
.handle((v, t) -> {
83
if (t != null)
84
results.add(t);
85
else
86
results.add(v);
87
return null;
88
}));
89
}
90
91
for (int i=0; i<LOOPS; i++) {
92
Object o = results.take();
93
if (o instanceof Exception) {
94
throw new RuntimeException((Exception)o);
95
}
96
}
97
} finally {
98
server.stop(1);
99
}
100
}
101
102
private static HttpRequest createRequest(URI uri) throws URISyntaxException {
103
HttpRequest.Builder builder = HttpRequest.newBuilder(uri)
104
.method("POST", BODY_PUBLISHER)
105
.version(HttpClient.Version.HTTP_1_1);
106
builder.header("content-type", "text/plain");
107
return builder.build();
108
}
109
110
private static HttpClient createClient() {
111
return HttpClient.newBuilder()
112
.version(HttpClient.Version.HTTP_1_1)
113
.build();
114
115
}
116
}
117
118