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