Path: blob/master/test/jdk/sun/net/www/protocol/http/Finalizer.java
41159 views
/*1* Copyright (c) 2000, 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/* @test24@bug 437224825@summary Java2D demo throws exceptions on MerlinB3226*/27import java.io.*;28import java.net.*;29class XServer extends Thread {30ServerSocket srv;31Socket s;32InputStream is;33OutputStream os;3435XServer (ServerSocket s) {36srv = s;37}3839Socket getSocket () {40return (s);41}4243public void run() {44try {45String x;46String msg = "Message from the server\r\n";47s = srv.accept ();48is = s.getInputStream ();49BufferedReader r = new BufferedReader(new InputStreamReader(is));50os = s.getOutputStream ();51BufferedWriter w = new BufferedWriter(new OutputStreamWriter(os));52while ((x=r.readLine()) != null) {53if (x.length() == 0)54break;55}56w.write("HTTP/1.1 200\r\n");57w.write("Content-Type: text/html\r\n");58w.write("Content-Length: "+msg.length()+"\r\n");59w.write("\r\n");60w.write(msg);61w.flush();6263// second round64while ((x=r.readLine()) != null) {65if (x.length() == 0)66break;67}68w.write("HTTP/1.1 200\r\n");69w.write("Content-Type: text/html\r\n");70w.write("Content-Length: "+msg.length()+"\r\n");71w.write("\r\n");72w.write(msg);73w.flush();74s.close ();75srv.close ();76} catch (IOException e) {}77}78}7980public class Finalizer {81public static void main (String args[]) {82ServerSocket serversocket = null;83try {84InetAddress loopback = InetAddress.getLoopbackAddress();85serversocket = new ServerSocket();86serversocket.bind(new InetSocketAddress(loopback, 0));87int port = serversocket.getLocalPort ();88XServer server = new XServer (serversocket);89server.start ();90Thread.sleep (2000);91URL url = new URL ("http://localhost:"+port+"/index.html");92URLConnection urlc = url.openConnection ();93urlc.connect ();94InputStream is = urlc.getInputStream ();95is = urlc.getInputStream ();96sink (is);97HttpURLConnection ht = (HttpURLConnection) urlc;9899URLConnection url1 = url.openConnection ();100is = url1.getInputStream ();101102ht = null; urlc = null;103System.gc();104System.runFinalization ();105106sink (is);107108System.out.println("Passed!");109} catch (IOException e) {110throw new RuntimeException("finalize method failure."+e);111} catch (InterruptedException ie) {112} finally {113if (serversocket != null) {114try {serversocket.close();} catch (IOException io) {}115}116}117}118119static void sink (InputStream is) {120int c;121byte [] b = new byte [1024];122try {123while ((c = is.read (b)) != -1);124} catch (Exception e) {125throw new RuntimeException("finalize method failure."+e);126}127}128}129130131