Wed, 21 Jan 2015 12:38:11 +0100
8068013: [TESTBUG] Aix support in hotspot jtreg tests
Reviewed-by: ctornqvi, fzhinkin, farvidsson
drchase@6134 | 1 | import java.io.BufferedOutputStream; |
drchase@6134 | 2 | import java.io.FileNotFoundException; |
drchase@6134 | 3 | import java.io.FileOutputStream; |
drchase@6134 | 4 | import java.io.IOException; |
drchase@6134 | 5 | import java.net.URL; |
drchase@6134 | 6 | import java.net.URLClassLoader; |
drchase@6134 | 7 | import java.util.jar.JarEntry; |
drchase@6134 | 8 | import java.util.jar.JarOutputStream; |
drchase@6134 | 9 | |
drchase@5800 | 10 | /* |
drchase@5800 | 11 | * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
drchase@5800 | 12 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
drchase@5800 | 13 | * |
drchase@5800 | 14 | * This code is free software; you can redistribute it and/or modify it |
drchase@5800 | 15 | * under the terms of the GNU General Public License version 2 only, as |
drchase@5800 | 16 | * published by the Free Software Foundation. |
drchase@5800 | 17 | * |
drchase@5800 | 18 | * This code is distributed in the hope that it will be useful, but WITHOUT |
drchase@5800 | 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
drchase@5800 | 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
drchase@5800 | 21 | * version 2 for more details (a copy is included in the LICENSE file that |
drchase@5800 | 22 | * accompanied this code). |
drchase@5800 | 23 | * |
drchase@5800 | 24 | * You should have received a copy of the GNU General Public License version |
drchase@5800 | 25 | * 2 along with this work; if not, write to the Free Software Foundation, |
drchase@5800 | 26 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
drchase@5800 | 27 | * |
drchase@5800 | 28 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
drchase@5800 | 29 | * or visit www.oracle.com if you need additional information or have any |
drchase@5800 | 30 | * questions. |
drchase@5800 | 31 | * |
drchase@5800 | 32 | */ |
drchase@5800 | 33 | |
drchase@5800 | 34 | /** |
drchase@6134 | 35 | * A ByteClassLoader is used to define classes from collections of bytes, as |
drchase@6134 | 36 | * well as loading classes in the usual way. It includes options to write the |
drchase@6134 | 37 | * classes to files in a jar, or to read the classes from jars in a later or |
drchase@6134 | 38 | * debugging run. |
drchase@5800 | 39 | * |
drchase@6134 | 40 | * If Boolean property byteclassloader.verbose is true, be chatty about jar |
drchase@6134 | 41 | * file operations. |
drchase@6134 | 42 | * |
drchase@5800 | 43 | */ |
drchase@6134 | 44 | public class ByteClassLoader extends URLClassLoader { |
drchase@6134 | 45 | |
drchase@6134 | 46 | final static boolean verbose |
drchase@6134 | 47 | = Boolean.getBoolean("byteclassloader.verbose"); |
drchase@6134 | 48 | |
drchase@6134 | 49 | final boolean read; |
drchase@6134 | 50 | final JarOutputStream jos; |
drchase@6134 | 51 | final String jar_name; |
drchase@6134 | 52 | |
drchase@6134 | 53 | /** |
drchase@6134 | 54 | * Make a new ByteClassLoader. |
drchase@6134 | 55 | * |
drchase@6134 | 56 | * @param jar_name Basename of jar file to be read/written by this classloader. |
drchase@6134 | 57 | * @param read If true, read classes from jar file instead of from parameter. |
drchase@6134 | 58 | * @param write If true, write classes to jar files for offline study/use. |
drchase@6134 | 59 | * |
drchase@6134 | 60 | * @throws FileNotFoundException |
drchase@6134 | 61 | * @throws IOException |
drchase@6134 | 62 | */ |
drchase@6134 | 63 | public ByteClassLoader(String jar_name, boolean read, boolean write) |
drchase@6134 | 64 | throws FileNotFoundException, IOException { |
drchase@6134 | 65 | super(read |
drchase@6134 | 66 | ? new URL[]{new URL("file:" + jar_name + ".jar")} |
drchase@6134 | 67 | : new URL[0]); |
drchase@6134 | 68 | this.read = read; |
drchase@6134 | 69 | this.jar_name = jar_name; |
drchase@6134 | 70 | this.jos = write |
drchase@6134 | 71 | ? new JarOutputStream( |
drchase@6134 | 72 | new BufferedOutputStream( |
drchase@6134 | 73 | new FileOutputStream(jar_name + ".jar"))) : null; |
drchase@6134 | 74 | if (read && write) { |
drchase@6134 | 75 | throw new Error("At most one of read and write may be true."); |
drchase@6134 | 76 | } |
drchase@6134 | 77 | } |
drchase@6134 | 78 | |
drchase@6134 | 79 | private static void writeJarredFile(JarOutputStream jos, String file, String suffix, byte[] bytes) { |
drchase@6134 | 80 | String fileName = file.replace(".", "/") + "." + suffix; |
drchase@6134 | 81 | JarEntry ze = new JarEntry(fileName); |
drchase@6134 | 82 | try { |
drchase@6134 | 83 | ze.setSize(bytes.length); |
drchase@6134 | 84 | jos.putNextEntry(ze); |
drchase@6134 | 85 | jos.write(bytes); |
drchase@6134 | 86 | jos.closeEntry(); |
drchase@6134 | 87 | } catch (IOException e) { |
drchase@6134 | 88 | throw new RuntimeException(e); |
drchase@6134 | 89 | } |
drchase@6134 | 90 | } |
drchase@6134 | 91 | |
drchase@5800 | 92 | /** |
drchase@5800 | 93 | * (pre)load class name using classData for the definition. |
drchase@5800 | 94 | * |
drchase@5800 | 95 | * @param name |
drchase@5800 | 96 | * @param classData |
drchase@5800 | 97 | * @return |
drchase@5800 | 98 | */ |
drchase@6134 | 99 | public Class<?> loadBytes(String name, byte[] classData) throws ClassNotFoundException { |
drchase@6134 | 100 | if (jos != null) { |
drchase@6134 | 101 | if (verbose) { |
drchase@6134 | 102 | System.out.println("ByteClassLoader: writing " + name); |
drchase@6134 | 103 | } |
drchase@6134 | 104 | writeJarredFile(jos, name, "class", classData); |
drchase@6134 | 105 | } |
drchase@6134 | 106 | |
drchase@6134 | 107 | Class<?> clazz = null; |
drchase@6134 | 108 | if (read) { |
drchase@6134 | 109 | if (verbose) { |
drchase@6134 | 110 | System.out.println("ByteClassLoader: reading " + name + " from " + jar_name); |
drchase@6134 | 111 | } |
drchase@6134 | 112 | clazz = loadClass(name); |
drchase@6134 | 113 | } else { |
drchase@6134 | 114 | clazz = defineClass(name, classData, 0, classData.length); |
drchase@6134 | 115 | resolveClass(clazz); |
drchase@6134 | 116 | } |
drchase@6134 | 117 | return clazz; |
drchase@6134 | 118 | } |
drchase@6134 | 119 | |
drchase@6134 | 120 | public void close() { |
drchase@6134 | 121 | if (jos != null) { |
drchase@6134 | 122 | try { |
drchase@6134 | 123 | if (verbose) { |
drchase@6134 | 124 | System.out.println("ByteClassLoader: closing " + jar_name); |
drchase@6134 | 125 | } |
drchase@6134 | 126 | jos.close(); |
drchase@6134 | 127 | } catch (IOException ex) { |
drchase@6134 | 128 | } |
drchase@6134 | 129 | } |
drchase@5800 | 130 | } |
drchase@5800 | 131 | } |