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