jdk/test/sun/net/www/protocol/http/Finalizer.java
author jjiang
Wed, 25 May 2016 03:06:24 +0100
changeset 38557 5c485e1ea6fa
parent 5506 202f599c92aa
permissions -rw-r--r--
8157635: Fix module dependencies for /sun/* tests Summary: Module declarations for the tests in /sun/* Reviewed-by: alanb Contributed-by: John Jiang <sha.jiang@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
   @bug 4372248
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
   @summary Java2D demo throws exceptions on MerlinB32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
class XServer extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
    ServerSocket srv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
    Socket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    InputStream is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    OutputStream os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    XServer (ServerSocket s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        srv = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    Socket getSocket () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        return (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            String x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            String msg = "Message from the server\r\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            s = srv.accept ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            is = s.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            BufferedReader r = new BufferedReader(new InputStreamReader(is));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            os = s.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            BufferedWriter w = new BufferedWriter(new OutputStreamWriter(os));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            while ((x=r.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                if (x.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            w.write("HTTP/1.1 200\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            w.write("Content-Type: text/html\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            w.write("Content-Length: "+msg.length()+"\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            w.write("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            w.write(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            w.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            // second round
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            while ((x=r.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                if (x.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            w.write("HTTP/1.1 200\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            w.write("Content-Type: text/html\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            w.write("Content-Length: "+msg.length()+"\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            w.write("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            w.write(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            w.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            s.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            srv.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
public class Finalizer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static void main (String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            ServerSocket serversocket = new ServerSocket (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            int port = serversocket.getLocalPort ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            XServer server = new XServer (serversocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            server.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            Thread.sleep (2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            URL url = new URL ("http://localhost:"+port+"/index.html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            URLConnection urlc = url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            urlc.connect ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            InputStream is = urlc.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            is = urlc.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            sink (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            HttpURLConnection ht = (HttpURLConnection) urlc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            URLConnection url1 = url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            is = url1.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            ht = null; urlc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            System.runFinalization ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            sink (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            System.out.println("Passed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            throw new RuntimeException("finalize method failure."+e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static void sink (InputStream is) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        byte [] b = new byte [1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            while ((c = is.read (b)) != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            throw new RuntimeException("finalize method failure."+e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
}