Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/HttpInputStream.java
38867 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 493759825* @summary http://www.clipstream.com video does not play; read() problem26*/272829import java.io.IOException;30import java.io.InputStream;31import java.io.OutputStream;32import java.net.ServerSocket;33import java.net.Socket;34import java.net.URL;3536public class HttpInputStream {3738private static final int CONTENT_LENGTH = 20;3940static class Server implements AutoCloseable, Runnable {4142final ServerSocket serverSocket;43static final byte[] requestEnd = new byte[]{'\r', '\n', '\r', '\n'};44static final int TIMEOUT = 10 * 1000;4546Server() throws IOException {47serverSocket = new ServerSocket(0);48}4950void readOneRequest(InputStream is) throws IOException {51int requestEndCount = 0, r;52while ((r = is.read()) != -1) {53if (r == requestEnd[requestEndCount]) {54requestEndCount++;55if (requestEndCount == 4) {56break;57}58} else {59requestEndCount = 0;60}61}62}6364@Override65public void run() {66try (Socket s = serverSocket.accept()) {67s.setSoTimeout(TIMEOUT);68readOneRequest(s.getInputStream());69try (OutputStream os =70s.getOutputStream()) {71os.write("HTTP/1.1 200 OK".getBytes());72os.write(("Content-Length: " + CONTENT_LENGTH).getBytes());73os.write("\r\n\r\n".getBytes());74for (int i = 0; i < CONTENT_LENGTH; i++) {75os.write(0xff);76}77os.flush();78}79} catch (IOException e) {80e.printStackTrace();81}82}8384@Override85public void close() throws IOException {86if (!serverSocket.isClosed()) {87serverSocket.close();88}89}9091public int getPort() {92return serverSocket.getLocalPort();93}94}959697private static int read(InputStream is) throws IOException {98int len = 0;99while (is.read() != -1) {100len++;101}102return len;103}104105public static void main(String args[]) throws IOException {106try (Server server = new Server()) {107(new Thread(server)).start();108URL url = new URL("http://localhost:" + server.getPort() + "/anything");109try (InputStream is = url.openConnection().getInputStream()) {110if (read(is) != CONTENT_LENGTH) {111throw new RuntimeException("HttpInputStream.read() failed with 0xff");112}113}114}115}116}117118119