繁体   English   中英

将字节数组加载到内存类加载器中

[英]Load a Byte Array into a Memory Class Loader

我想知道如何将字节数组加载到内存 URLClassLoader中? 字节数组是jar文件的解密字节(如下所示)!

大多数内存类加载器都使用ClassLoader而不是URLClassLoader! 我需要它使用URLClassLoader。

    byte[] fileB = Util.crypt.getFileBytes(inputFile);
    byte[] dec;
    dec = Util.crypt.decrypt(fileB, "16LENGTHLONGKEYX".getBytes());
    //Load bytes into memory and load a class here?

谢谢!

您是否看过ClassLoader javadocs中的NetworkClassLoader示例:

http://docs.oracle.com/javase/6/docs/api/index.html?java/lang/ClassLoader.html

以此为基础,您只需要实现loadClassData方法,该方法将从解密的jar字节中提取所需的资源。 您可以使用JarInputStream(new ByteArrayInputStream(dec))包装解密的字节,然后遍历jar条目,直到找到您感兴趣的资源/类,然后返回jar条目的字节数组。

我将在这里发布我过去做过的实现:

// main
String className = "tests.compiler.DynamicCompilationHelloWorldEtc";
//...
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 
File classesDir = new File(tempDir);
CustomClassLoader ccl = new CustomClassLoader(classLoader, classesDir);         
if (ccl != null) {
    Class clazz = ccl.loadClass(className);
///...
}

我的自定义ClassLoader:

package tests.classloader;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;

public class CustomClassLoader extends ClassLoader {

    private File classesDir;

    public CustomClassLoader(ClassLoader parent, File classesDir) {
       super(parent);      

       this.classesDir = classesDir;
    }

    public Class findClass(String name) {
       byte[] data = loadDataFromAny(name);
       return defineClass(name, data, 0, data.length);
    }

    private byte[] loadDataFromAny(String name) {

        name = name.replace('.', '/');
        name = name + ".class";

        byte[] ret = null;

        try {
            File f = new File(classesDir.getAbsolutePath(), name);
            FileInputStream fis = new FileInputStream(f);

            ByteBuffer bb = ByteBuffer.allocate(4*1024); 
            byte[] buf = new byte[1024];
            int readedBytes = -1; 

            while ((readedBytes = fis.read(buf)) != -1) {
                bb.put(buf, 0, readedBytes);
            }

            ret = bb.array();           
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return ret;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM