Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/ChunkedInputStream/TestAvailable.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 644699026* @run main/othervm TestAvailable27* @summary HttpURLConnection#available() reads more and more data into memory28*/2930import java.net.*;31import java.util.*;32import java.io.*;33import com.sun.net.httpserver.*;34import java.util.concurrent.Executors;35import java.util.concurrent.ExecutorService;3637public class TestAvailable38{39com.sun.net.httpserver.HttpServer httpServer;40ExecutorService executorService;4142public static void main(String[] args)43{44new TestAvailable();45}4647public TestAvailable()48{49try {50startHttpServer();51doClient();52} catch (IOException ioe) {53System.err.println(ioe);54}55}5657void doClient() {58try {59InetSocketAddress address = httpServer.getAddress();6061URL url = new URL("http://localhost:" + address.getPort() + "/testAvailable/");62HttpURLConnection uc = (HttpURLConnection)url.openConnection();6364uc.setDoOutput(true);65uc.setRequestMethod("POST");66uc.setChunkedStreamingMode(0);67OutputStream os = uc.getOutputStream();68for (int i=0; i< (128 * 1024); i++)69os.write('X');70os.close();7172InputStream is = uc.getInputStream();73int avail = 0;74while (avail == 0) {75try { Thread.sleep(2000); } catch (Exception e) {}76avail = is.available();77}7879try { Thread.sleep(2000); } catch (Exception e) {}80int nextAvail = is.available();8182is.close();8384if (nextAvail > avail) {85throw new RuntimeException86("Failed: calling available multiple times should not return more data");87}8889} catch (IOException e) {90throw new RuntimeException(e);91} finally {92httpServer.stop(1);93executorService.shutdown();94}959697}9899/**100* Http Server101*/102public void startHttpServer() throws IOException {103httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);104105// create HttpServer context106HttpContext ctx = httpServer.createContext("/testAvailable/", new MyHandler());107108executorService = Executors.newCachedThreadPool();109httpServer.setExecutor(executorService);110httpServer.start();111}112113class MyHandler implements HttpHandler {114public void handle(HttpExchange t) throws IOException {115InputStream is = t.getRequestBody();116byte[] ba = new byte[1024];117while (is.read(ba) != -1);118is.close();119120t.sendResponseHeaders(200, 0);121122OutputStream os = t.getResponseBody();123for (int i=0; i< (128 * 1024); i++)124os.write('X');125os.close();126127t.close();128}129}130}131132133