Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/StreamingOutputStream.java
38867 views
/*1* Copyright (c) 2006, 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/*24* @test25* @bug 647225026* @run main/othervm StreamingOutputStream27* @summary HttpURLConnection.getOutputStream streaming mode bug when called multiple times28*/2930import java.net.*;31import java.io.*;32import com.sun.net.httpserver.*;3334/*35* Calling HttpURLConnection.getOutputStream multiple times when streaming36* should not create a new OutputStream each time.37*/3839public class StreamingOutputStream40{41com.sun.net.httpserver.HttpServer httpServer;4243public static void main(String[] args) {44new StreamingOutputStream();45}4647public StreamingOutputStream() {48try {49startHttpServer();50doClient();51} catch (IOException ioe) {52ioe.printStackTrace();53}54}5556void doClient() {57try {58InetSocketAddress address = httpServer.getAddress();5960URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/");61HttpURLConnection uc = (HttpURLConnection)url.openConnection();6263uc.setDoOutput(true);64uc.setFixedLengthStreamingMode(1);65OutputStream os1 = uc.getOutputStream();66OutputStream os2 = uc.getOutputStream();6768if (os1 != os2)69throw new RuntimeException("Failed: OutputStreams should reference the same object");7071os2.write('b');72os2.close();7374int resp = uc.getResponseCode();75if (resp != 200)76throw new RuntimeException("Failed: Server should return 200 OK");7778} catch (IOException e) {79e.printStackTrace();80} finally {81httpServer.stop(1);82}83}84/**85* Http Server86*/87void startHttpServer() throws IOException {88httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);89HttpContext ctx = httpServer.createContext("/test/", new MyHandler());90httpServer.start();91}9293class MyHandler implements HttpHandler {94public void handle(HttpExchange t) throws IOException {95InputStream is = t.getRequestBody();96while(is.read() != -1);97is.close();9899t.sendResponseHeaders(200, -1);100t.close();101}102}103}104105106