1/* 2 * Copyright (c) 2006, 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/* 25 * @test 26 * @bug 6472250 27 * @modules jdk.httpserver 28 * @run main/othervm StreamingOutputStream 29 * @summary HttpURLConnection.getOutputStream streaming mode bug when called multiple times 30 */ 31 32import java.net.*; 33import java.io.*; 34import com.sun.net.httpserver.*; 35 36/* 37 * Calling HttpURLConnection.getOutputStream multiple times when streaming 38 * should not create a new OutputStream each time. 39 */ 40 41public class StreamingOutputStream 42{ 43 com.sun.net.httpserver.HttpServer httpServer; 44 45 public static void main(String[] args) { 46 new StreamingOutputStream(); 47 } 48 49 public StreamingOutputStream() { 50 try { 51 startHttpServer(); 52 doClient(); 53 } catch (IOException ioe) { 54 ioe.printStackTrace(); 55 } 56 } 57 58 void doClient() { 59 try { 60 InetSocketAddress address = httpServer.getAddress(); 61 62 URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/"); 63 HttpURLConnection uc = (HttpURLConnection)url.openConnection(); 64 65 uc.setDoOutput(true); 66 uc.setFixedLengthStreamingMode(1); 67 OutputStream os1 = uc.getOutputStream(); 68 OutputStream os2 = uc.getOutputStream(); 69 70 if (os1 != os2) 71 throw new RuntimeException("Failed: OutputStreams should reference the same object"); 72 73 os2.write('b'); 74 os2.close(); 75 76 int resp = uc.getResponseCode(); 77 if (resp != 200) 78 throw new RuntimeException("Failed: Server should return 200 OK"); 79 80 } catch (IOException e) { 81 e.printStackTrace(); 82 } finally { 83 httpServer.stop(1); 84 } 85 } 86 /** 87 * Http Server 88 */ 89 void startHttpServer() throws IOException { 90 httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0); 91 HttpContext ctx = httpServer.createContext("/test/", new MyHandler()); 92 httpServer.start(); 93 } 94 95 class MyHandler implements HttpHandler { 96 public void handle(HttpExchange t) throws IOException { 97 InputStream is = t.getRequestBody(); 98 while(is.read() != -1); 99 is.close(); 100 101 t.sendResponseHeaders(200, -1); 102 t.close(); 103 } 104 } 105} 106