systemclassloader.java~4~

来自「Java源码」· JAVA~4~ 代码 · 共 79 行

JAVA~4~
79
字号
package AM.vm_impl.util;


/**
 * Created by IntelliJ IDEA. User: yellowicq Date: 2004-4-27 Time: 13:16:40
 * To change this template use File | Settings | File Templates.
 */


import java.io.*;

public class SystemClassLoader
    extends ClassLoader {

    // basePath gives the path to which this class
    // loader appends "/<typename>.class" to get the
    // full path name of the class file to load
    private String basePath;

    public SystemClassLoader(String basePath) {

        this.basePath = basePath;
    }

    public SystemClassLoader(ClassLoader parent, String basePath) {

        super(parent);
        this.basePath = basePath;
    }

    protected Class findClass(String className)
        throws ClassNotFoundException {

        byte classData[];

        // Try to load it from the basePath directory.
        classData = getTypeFromBasePath(className);
        if (classData == null) {
            throw new ClassNotFoundException();
        }

        // Parse it
        return defineClass(className, classData, 0,
            classData.length);
    }

    private byte[] getTypeFromBasePath(String typeName) {

        FileInputStream fis;
        String fileName = basePath + File.separatorChar
            + typeName.replace('.', File.separatorChar)
            + ".class";

        try {
            fis = new FileInputStream(fileName);
        }
        catch (FileNotFoundException e) {
            return null;
        }

        BufferedInputStream bis = new BufferedInputStream(fis);

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        try {
            int c = bis.read();
            while (c != -1) {
                out.write(c);
                c = bis.read();
            }
        }
        catch (IOException e) {
            return null;
        }

        return out.toByteArray();
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?