StreamingOutputStream.java revision 0
325N/A/*
325N/A * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
325N/A * CA 95054 USA or visit www.sun.com if you need additional information or
325N/A * have any questions.
325N/A */
325N/A
325N/A/*
325N/A * @test
325N/A * @bug 6472250
325N/A * @run main/othervm StreamingOutputStream
325N/A * @summary HttpURLConnection.getOutputStream streaming mode bug when called multiple times
325N/A */
325N/A
325N/Aimport java.net.*;
325N/Aimport java.io.*;
325N/Aimport com.sun.net.httpserver.*;
325N/A
325N/A/*
325N/A * Calling HttpURLConnection.getOutputStream multiple times when streaming
325N/A * should not create a new OutputStream each time.
325N/A */
325N/A
325N/Apublic class StreamingOutputStream
325N/A{
325N/A com.sun.net.httpserver.HttpServer httpServer;
325N/A
325N/A public static void main(String[] args) {
325N/A new StreamingOutputStream();
325N/A }
325N/A
325N/A public StreamingOutputStream() {
325N/A try {
325N/A startHttpServer();
325N/A doClient();
325N/A } catch (IOException ioe) {
325N/A ioe.printStackTrace();
325N/A }
325N/A }
325N/A
325N/A void doClient() {
325N/A try {
325N/A InetSocketAddress address = httpServer.getAddress();
325N/A
325N/A URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/");
325N/A HttpURLConnection uc = (HttpURLConnection)url.openConnection();
325N/A
325N/A uc.setDoOutput(true);
325N/A uc.setFixedLengthStreamingMode(1);
325N/A OutputStream os1 = uc.getOutputStream();
325N/A OutputStream os2 = uc.getOutputStream();
325N/A
325N/A if (os1 != os2)
325N/A throw new RuntimeException("Failed: OutputStreams should reference the same object");
325N/A
325N/A os2.write('b');
325N/A os2.close();
325N/A
325N/A int resp = uc.getResponseCode();
325N/A if (resp != 200)
325N/A throw new RuntimeException("Failed: Server should return 200 OK");
325N/A
325N/A } catch (IOException e) {
325N/A e.printStackTrace();
325N/A } finally {
325N/A httpServer.stop(1);
325N/A }
325N/A }
325N/A /**
325N/A * Http Server
325N/A */
325N/A void startHttpServer() throws IOException {
325N/A httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
325N/A HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
325N/A httpServer.start();
325N/A }
325N/A
325N/A class MyHandler implements HttpHandler {
325N/A public void handle(HttpExchange t) throws IOException {
325N/A InputStream is = t.getRequestBody();
325N/A while(is.read() != -1);
325N/A is.close();
325N/A
325N/A t.sendResponseHeaders(200, -1);
325N/A t.close();
325N/A }
325N/A }
325N/A}
325N/A