0N/A/*
3261N/A * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 6401598
0N/A * @summary new HttpServer cannot serve binary stream data
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.HttpURLConnection;
0N/Aimport java.net.MalformedURLException;
0N/Aimport java.net.URL;
0N/Aimport java.net.InetSocketAddress;
0N/Aimport java.util.concurrent.*;
0N/A
0N/Aimport com.sun.net.httpserver.HttpExchange;
0N/Aimport com.sun.net.httpserver.HttpHandler;
0N/Aimport com.sun.net.httpserver.HttpServer;
0N/A
0N/Apublic class B6401598 {
0N/A
0N/A static class MyHandler implements HttpHandler {
0N/A
0N/A public MyHandler() {
0N/A
0N/A }
0N/A
0N/A public void handle(HttpExchange arg0) throws IOException {
0N/A try {
0N/A InputStream is = arg0.getRequestBody();
0N/A OutputStream os = arg0.getResponseBody();
0N/A
0N/A DataInputStream dis = new DataInputStream(is);
0N/A
0N/A short input = dis.readShort();
0N/A while (dis.read() != -1) ;
0N/A dis.close();
0N/A
0N/A DataOutputStream dos = new DataOutputStream(os);
0N/A
0N/A arg0.sendResponseHeaders(200, 0);
0N/A
0N/A dos.writeShort(input);
0N/A
0N/A dos.flush();
0N/A dos.close();
0N/A } catch (IOException e) {
0N/A e.printStackTrace();
0N/A error = true;
0N/A }
0N/A }
0N/A
0N/A }
0N/A
0N/A static int port;
0N/A static boolean error = false;
0N/A static ExecutorService exec;
0N/A static HttpServer server;
0N/A
0N/A public static void main(String[] args) {
0N/A try {
0N/A server = HttpServer.create(new InetSocketAddress(0), 400);
0N/A server.createContext("/server/", new MyHandler());
0N/A exec = Executors.newFixedThreadPool(3);
3105N/A server.setExecutor(exec);
0N/A port = server.getAddress().getPort();
0N/A server.start();
0N/A
0N/A short counter;
0N/A
0N/A for (counter = 0; counter < 1000; counter++) {
0N/A HttpURLConnection connection = getHttpURLConnection(new URL("http://127.0.0.1:"+port+"/server/"), 10000);
0N/A
0N/A OutputStream os = connection.getOutputStream();
0N/A
0N/A DataOutputStream dos = new DataOutputStream(os);
0N/A
0N/A dos.writeShort(counter);
0N/A
0N/A dos.flush();
0N/A dos.close();
0N/A
0N/A counter++;
0N/A
0N/A InputStream is = connection.getInputStream();
0N/A
0N/A DataInputStream dis = new DataInputStream(is);
0N/A
0N/A short ret = dis.readShort();
0N/A
0N/A dis.close();
0N/A }
0N/A System.out.println ("Stopping");
0N/A server.stop (1);
0N/A exec.shutdown();
0N/A } catch (IOException e) {
0N/A // TODO Auto-generated catch block
0N/A e.printStackTrace();
0N/A server.stop (1);
0N/A exec.shutdown();
0N/A }
0N/A }
0N/A
0N/A
0N/A
0N/A static HttpURLConnection getHttpURLConnection(URL url, int timeout) throws IOException {
0N/A
0N/A HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
0N/A
0N/A httpURLConnection.setConnectTimeout(40000);
0N/A httpURLConnection.setReadTimeout(timeout);
0N/A httpURLConnection.setDoOutput(true);
0N/A httpURLConnection.setDoInput(true);
0N/A httpURLConnection.setUseCaches(false);
0N/A httpURLConnection.setAllowUserInteraction(false);
0N/A httpURLConnection.setRequestMethod("POST");
0N/A
0N/A // HttpURLConnection httpURLConnection = new MyHttpURLConnection(url);
0N/A
0N/A return httpURLConnection;
0N/A }
0N/A}