Path: blob/master/test/jdk/sun/net/www/http/ChunkedInputStream/TestAvailable.java
41154 views
/*1* Copyright (c) 2006, 2019, 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* @modules jdk.httpserver27* @library /test/lib28* @run main/othervm TestAvailable29* @summary HttpURLConnection#available() reads more and more data into memory30*/3132import java.net.*;33import java.util.*;34import java.io.*;35import com.sun.net.httpserver.*;36import java.util.concurrent.Executors;37import java.util.concurrent.ExecutorService;38import jdk.test.lib.net.URIBuilder;3940public class TestAvailable41{42com.sun.net.httpserver.HttpServer httpServer;43ExecutorService executorService;4445public static void main(String[] args)46{47new TestAvailable();48}4950public TestAvailable()51{52try {53startHttpServer();54doClient();55} catch (IOException ioe) {56System.err.println(ioe);57}58}5960void doClient() {61try {62InetSocketAddress address = httpServer.getAddress();6364URL url = URIBuilder.newBuilder()65.scheme("http")66.host(address.getAddress())67.port(address.getPort())68.path("/testAvailable/")69.toURLUnchecked();7071HttpURLConnection uc = (HttpURLConnection)url.openConnection();7273uc.setDoOutput(true);74uc.setRequestMethod("POST");75uc.setChunkedStreamingMode(0);76OutputStream os = uc.getOutputStream();77for (int i=0; i< (128 * 1024); i++)78os.write('X');79os.close();8081InputStream is = uc.getInputStream();82int avail = 0;83while (avail == 0) {84try { Thread.sleep(2000); } catch (Exception e) {}85avail = is.available();86}8788try { Thread.sleep(2000); } catch (Exception e) {}89int nextAvail = is.available();9091is.close();9293if (nextAvail > avail) {94throw new RuntimeException95("Failed: calling available multiple times should not return more data");96}9798} catch (IOException e) {99throw new RuntimeException(e);100} finally {101httpServer.stop(1);102executorService.shutdown();103}104105106}107108/**109* Http Server110*/111public void startHttpServer() throws IOException {112InetAddress loopback = InetAddress.getLoopbackAddress();113InetSocketAddress sockaddr = new InetSocketAddress(loopback, 0);114httpServer = com.sun.net.httpserver.HttpServer.create(sockaddr, 0);115116// create HttpServer context117HttpContext ctx = httpServer.createContext("/testAvailable/", new MyHandler());118119executorService = Executors.newCachedThreadPool();120httpServer.setExecutor(executorService);121httpServer.start();122}123124class MyHandler implements HttpHandler {125public void handle(HttpExchange t) throws IOException {126InputStream is = t.getRequestBody();127byte[] ba = new byte[1024];128while (is.read(ba) != -1);129is.close();130131t.sendResponseHeaders(200, 0);132133OutputStream os = t.getResponseBody();134for (int i=0; i< (128 * 1024); i++)135os.write('X');136os.close();137138t.close();139}140}141}142143144