Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/http/HttpInputStream.java
41159 views
1
/*
2
* Copyright (c) 2003, 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
/* @test
25
* @bug 4937598
26
* @library /test/lib
27
* @summary http://www.clipstream.com video does not play; read() problem
28
*/
29
30
31
import java.io.IOException;
32
import java.io.InputStream;
33
import java.io.OutputStream;
34
import java.net.InetAddress;
35
import java.net.InetSocketAddress;
36
import java.net.ServerSocket;
37
import java.net.Socket;
38
import java.net.URL;
39
40
import jdk.test.lib.net.URIBuilder;
41
42
public class HttpInputStream {
43
44
private static final int CONTENT_LENGTH = 20;
45
46
static class Server implements AutoCloseable, Runnable {
47
48
final ServerSocket serverSocket;
49
static final byte[] requestEnd = new byte[]{'\r', '\n', '\r', '\n'};
50
static final int TIMEOUT = 10 * 1000;
51
52
Server() throws IOException {
53
serverSocket = new ServerSocket();
54
serverSocket.bind(new InetSocketAddress(
55
InetAddress.getLoopbackAddress(), 0));
56
}
57
58
void readOneRequest(InputStream is) throws IOException {
59
int requestEndCount = 0, r;
60
while ((r = is.read()) != -1) {
61
if (r == requestEnd[requestEndCount]) {
62
requestEndCount++;
63
if (requestEndCount == 4) {
64
break;
65
}
66
} else {
67
requestEndCount = 0;
68
}
69
}
70
}
71
72
@Override
73
public void run() {
74
try (Socket s = serverSocket.accept()) {
75
s.setSoTimeout(TIMEOUT);
76
readOneRequest(s.getInputStream());
77
try (OutputStream os =
78
s.getOutputStream()) {
79
os.write("HTTP/1.1 200 OK".getBytes());
80
os.write(("Content-Length: " + CONTENT_LENGTH).getBytes());
81
os.write("\r\n\r\n".getBytes());
82
for (int i = 0; i < CONTENT_LENGTH; i++) {
83
os.write(0xff);
84
}
85
os.flush();
86
}
87
} catch (IOException e) {
88
e.printStackTrace();
89
}
90
}
91
92
@Override
93
public void close() throws IOException {
94
if (!serverSocket.isClosed()) {
95
serverSocket.close();
96
}
97
}
98
99
public int getPort() {
100
return serverSocket.getLocalPort();
101
}
102
}
103
104
105
private static int read(InputStream is) throws IOException {
106
int len = 0;
107
while (is.read() != -1) {
108
len++;
109
}
110
return len;
111
}
112
113
public static void main(String args[]) throws IOException {
114
try (Server server = new Server()) {
115
(new Thread(server)).start();
116
URL url = URIBuilder.newBuilder()
117
.scheme("http")
118
.loopback()
119
.port(server.getPort())
120
.path("/anything")
121
.toURLUnchecked();
122
try (InputStream is = url.openConnection().getInputStream()) {
123
if (read(is) != CONTENT_LENGTH) {
124
throw new RuntimeException("HttpInputStream.read() failed with 0xff");
125
}
126
}
127
}
128
}
129
}
130
131