jdk/test/java/net/URLClassLoader/closetest/Common.java
author dsamersoff
Thu, 17 Oct 2013 16:08:01 +0400
changeset 21069 728330d2593a
parent 7983 fb83b663cadc
child 21664 81740736e62f
permissions -rw-r--r--
8025812: tmtools/jmap/heap_config tests fail on Linux-ia32 because it Cant attach to the core file Summary: Coredump store memsz elf field rounded up to page Reviewed-by: dholmes, sla
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7983
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
     1
/*
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
     4
 *
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
     8
 *
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    13
 * accompanied this code).
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    14
 *
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    18
 *
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    21
 * questions.
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    22
 */
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    23
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    24
import java.io.*;
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    25
import java.net.*;
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    26
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    27
public class Common {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    28
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    29
    static void copyFile (String src, String dst) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    30
        copyFile (new File(src), new File(dst));
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    31
    }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    32
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    33
    static void copyDir (String src, String dst) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    34
        copyDir (new File(src), new File(dst));
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    35
    }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    36
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    37
    static void copyFile (File src, File dst) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    38
        try {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    39
            if (!src.isFile()) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    40
                throw new RuntimeException ("File not found: " + src.toString());
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    41
            }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    42
            dst.delete();
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    43
            dst.createNewFile();
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    44
            FileInputStream i = new FileInputStream (src);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    45
            FileOutputStream o = new FileOutputStream (dst);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    46
            byte[] buf = new byte [1024];
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    47
            int count;
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    48
            while ((count=i.read(buf)) >= 0) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    49
                o.write (buf, 0, count);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    50
            }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    51
            i.close();
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    52
            o.close();
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    53
        } catch (IOException e) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    54
            throw new RuntimeException (e);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    55
        }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    56
    }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    57
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    58
    static void rm_minus_rf (File path) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    59
        if (!path.exists()) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    60
            return;
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    61
        }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    62
        if (path.isFile()) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    63
            if (!path.delete()) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    64
                throw new RuntimeException ("Could not delete " + path);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    65
            }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    66
        } else if (path.isDirectory ()) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    67
            String[] names = path.list();
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    68
            File[] files = path.listFiles();
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    69
            for (int i=0; i<files.length; i++) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    70
                rm_minus_rf (new File(path, names[i]));
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    71
            }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    72
            if (!path.delete()) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    73
                throw new RuntimeException ("Could not delete " + path);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    74
            }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    75
        } else {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    76
            throw new RuntimeException ("Trying to delete something that isn't a file or a directory");
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    77
        }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    78
    }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    79
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    80
    static void copyDir (File src, File dst) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    81
        if (!src.isDirectory()) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    82
            throw new RuntimeException ("Dir not found: " + src.toString());
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    83
        }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    84
        if (dst.exists()) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    85
            throw new RuntimeException ("Dir exists: " + dst.toString());
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    86
        }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    87
        dst.mkdir();
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    88
        String[] names = src.list();
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    89
        File[] files = src.listFiles();
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    90
        for (int i=0; i<files.length; i++) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    91
            String f = names[i];
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    92
            if (files[i].isDirectory()) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    93
                copyDir (files[i], new File (dst, f));
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    94
            } else {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    95
                copyFile (new File (src, f), new File (dst, f));
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    96
            }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    97
        }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    98
    }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    99
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   100
    /* expect is true if you expect to find it, false if you expect not to */
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   101
    static Class loadClass (String name, URLClassLoader loader, boolean expect){
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   102
        try {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   103
            Class clazz = Class.forName (name, true, loader);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   104
            if (!expect) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   105
                throw new RuntimeException ("loadClass: "+name+" unexpected");
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   106
            }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   107
            return clazz;
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   108
        } catch (ClassNotFoundException e) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   109
            if (expect) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   110
                throw new RuntimeException ("loadClass: " +name + " not found");
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   111
            }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   112
        }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   113
        return null;
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   114
    }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   115
}