4

I Am trying to load a class as a byte array so I could send it over the network and execute it remotely via Reflection. This Class (Bubble in this case) is in the same package. The thing is that I can't get the resource using the getResourceAsStream(classpath) method. The .getResourceAsStream(classpath) is always returning null. I've tested this code in a Java project and worked properly. I think the problem is the resource path, does Android load a .class file?

private void doSomething() {

    Bubble b = new Bubble();

    try {
        //Try to retrieve the class byte array
        byte[] classBytes = getBytes(b.getClass());         
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
    ... 
}

private byte[] getBytes(Class c) throws IOException{

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte b[] = new byte[1024];
    String classpath = c.getCanonicalName().replace('.', File.pathSeparatorChar) + ".class";
    //classpath is now, for example, com:myproject:Bubble.class
    InputStream in = c.getClassLoader().getResourceAsStream(classpath);
    int load;
    while((load=in.read(b))>0){
        out.write(b,0,load);
    }
    byte[] _r = out.toByteArray();
    out.close();
    in.close();
    return _r;
}

1 Answers1

0

Android uses dex file format for classes, and the best way would be to not send zipped (jarred) classes.dex, which contains all classes you need.

Tapemaster
  • 487
  • 5
  • 9