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 6446990 27 * @modules jdk.httpserver 28 * @run main/othervm TestAvailable 29 * @summary HttpURLConnection#available() reads more and more data into memory 30 */ 31 32import java.net.*; 33import java.util.*; 34import java.io.*; 35import com.sun.net.httpserver.*; 36import java.util.concurrent.Executors; 37import java.util.concurrent.ExecutorService; 38 39public class TestAvailable 40{ 41 com.sun.net.httpserver.HttpServer httpServer; 42 ExecutorService executorService; 43 44 public static void main(String[] args) 45 { 46 new TestAvailable(); 47 } 48 49 public TestAvailable() 50 { 51 try { 52 startHttpServer(); 53 doClient(); 54 } catch (IOException ioe) { 55 System.err.println(ioe); 56 } 57 } 58 59 void doClient() { 60 try { 61 InetSocketAddress address = httpServer.getAddress(); 62 63 URL url = new URL("http://localhost:" + address.getPort() + "/testAvailable/"); 64 HttpURLConnection uc = (HttpURLConnection)url.openConnection(); 65 66 uc.setDoOutput(true); 67 uc.setRequestMethod("POST"); 68 uc.setChunkedStreamingMode(0); 69 OutputStream os = uc.getOutputStream(); 70 for (int i=0; i< (128 * 1024); i++) 71 os.write('X'); 72 os.close(); 73 74 InputStream is = uc.getInputStream(); 75 int avail = 0; 76 while (avail == 0) { 77 try { Thread.sleep(2000); } catch (Exception e) {} 78 avail = is.available(); 79 } 80 81 try { Thread.sleep(2000); } catch (Exception e) {} 82 int nextAvail = is.available(); 83 84 is.close(); 85 86 if (nextAvail > avail) { 87 throw new RuntimeException 88 ("Failed: calling available multiple times should not return more data"); 89 } 90 91 } catch (IOException e) { 92 throw new RuntimeException(e); 93 } finally { 94 httpServer.stop(1); 95 executorService.shutdown(); 96 } 97 98 99 } 100 101 /** 102 * Http Server 103 */ 104 public void startHttpServer() throws IOException { 105 httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0); 106 107 // create HttpServer context 108 HttpContext ctx = httpServer.createContext("/testAvailable/", new MyHandler()); 109 110 executorService = Executors.newCachedThreadPool(); 111 httpServer.setExecutor(executorService); 112 httpServer.start(); 113 } 114 115 class MyHandler implements HttpHandler { 116 public void handle(HttpExchange t) throws IOException { 117 InputStream is = t.getRequestBody(); 118 byte[] ba = new byte[1024]; 119 while (is.read(ba) != -1); 120 is.close(); 121 122 t.sendResponseHeaders(200, 0); 123 124 OutputStream os = t.getResponseBody(); 125 for (int i=0; i< (128 * 1024); i++) 126 os.write('X'); 127 os.close(); 128 129 t.close(); 130 } 131 } 132} 133