Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/http/StreamingOutputStream.java
41159 views
1
/*
2
* Copyright (c) 2006, 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 6472250
27
* @modules jdk.httpserver
28
* @run main/othervm StreamingOutputStream
29
* @summary HttpURLConnection.getOutputStream streaming mode bug when called multiple times
30
*/
31
32
import java.net.*;
33
import java.io.*;
34
import com.sun.net.httpserver.*;
35
36
/*
37
* Calling HttpURLConnection.getOutputStream multiple times when streaming
38
* should not create a new OutputStream each time.
39
*/
40
41
public class StreamingOutputStream
42
{
43
com.sun.net.httpserver.HttpServer httpServer;
44
45
public static void main(String[] args) {
46
new StreamingOutputStream();
47
}
48
49
public StreamingOutputStream() {
50
try {
51
startHttpServer();
52
doClient();
53
} catch (IOException ioe) {
54
ioe.printStackTrace();
55
}
56
}
57
58
void doClient() {
59
try {
60
InetSocketAddress address = httpServer.getAddress();
61
62
URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/");
63
HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
64
65
uc.setDoOutput(true);
66
uc.setFixedLengthStreamingMode(1);
67
OutputStream os1 = uc.getOutputStream();
68
OutputStream os2 = uc.getOutputStream();
69
70
if (os1 != os2)
71
throw new RuntimeException("Failed: OutputStreams should reference the same object");
72
73
os2.write('b');
74
os2.close();
75
76
int resp = uc.getResponseCode();
77
if (resp != 200)
78
throw new RuntimeException("Failed: Server should return 200 OK");
79
80
} catch (IOException e) {
81
e.printStackTrace();
82
} finally {
83
httpServer.stop(1);
84
}
85
}
86
/**
87
* Http Server
88
*/
89
void startHttpServer() throws IOException {
90
InetAddress address = InetAddress.getLocalHost();
91
if (!InetAddress.getByName(address.getHostName()).equals(address)) {
92
// if this happens then we should possibly change the client
93
// side to use the address literal in its URL instead of
94
// the host name.
95
throw new IOException(address.getHostName()
96
+ " resolves to "
97
+ InetAddress.getByName(address.getHostName())
98
+ " not to "
99
+ address + ": check host configuration.");
100
}
101
httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(address, 0), 0);
102
HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
103
httpServer.start();
104
}
105
106
class MyHandler implements HttpHandler {
107
public void handle(HttpExchange t) throws IOException {
108
InputStream is = t.getRequestBody();
109
while(is.read() != -1);
110
is.close();
111
112
t.sendResponseHeaders(200, -1);
113
t.close();
114
}
115
}
116
}
117
118