Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/bugs/B6526158.java
38867 views
/*1* Copyright (c) 2007, 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 652615826* @summary HttpExchange.getRequestBody().close() throws Exception27*/2829import com.sun.net.httpserver.*;3031import java.util.*;32import java.util.concurrent.*;33import java.io.*;34import java.net.*;35import java.security.*;36import java.security.cert.*;37import javax.net.ssl.*;3839public class B6526158 {4041/* keep under 64 k */42final static int SIZE = 60 * 1024;4344public static void main (String[] args) throws Exception {45Handler handler = new Handler();46InetSocketAddress addr = new InetSocketAddress (0);47HttpServer server = HttpServer.create (addr, 0);48HttpContext ctx = server.createContext ("/test", handler);4950ExecutorService executor = Executors.newCachedThreadPool();51server.setExecutor (executor);52server.start ();5354URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");55HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();56urlc.setDoOutput (true);57try {58OutputStream os = new BufferedOutputStream (urlc.getOutputStream());59for (int i=0; i< SIZE; i++) {60os.write (i);61}62os.close();63InputStream is = urlc.getInputStream();64int c = 0;65while (is.read()!= -1) {66c ++;67}68is.close();69} finally {70server.stop(2);71executor.shutdown();72}73if (error) {74throw new RuntimeException ("Test failed");75}76}7778public static boolean error = false;7980static class Handler implements HttpHandler {81int invocation = 1;82public void handle (HttpExchange t)83throws IOException84{85InputStream is = t.getRequestBody();86try {87is.close();88} catch (IOException e) {89e.printStackTrace();90error = true;91}92t.sendResponseHeaders (200, -1);93t.close();94}95}96}979899