Converts a byte array to a class. - Java java.lang

Java examples for java.lang:byte Array Convert

Description

Converts a byte array to a class.

Demo Code


import java.io.*;

public class Main{
    public static void main(String[] argv) throws Exception{
        byte[] bytesToConvert = new byte[]{34,35,36,37,37,37,67,68,69};
        System.out.println(byteArrayToClass(bytesToConvert));
    }/*from  w  ww .  j  a v  a  2s. c om*/
    private static final ByteManipulationUtils instance = new ByteManipulationUtils();
    /**
     * Converts a byte array to a class.
     * @param bytesToConvert
     * @return
     */
    public static final <X> Class<? extends X> byteArrayToClass(
            byte[] bytesToConvert) {
        return instance.byteArrayToClassPrivate(bytesToConvert);
    }
    /**
     * Converts a byte array to a class. Is private due to non-staticness.
     * @param bytesToConvert
     * @return
     */
    private final <X> Class<? extends X> byteArrayToClassPrivate(
            byte[] bytesToConvert) {
        Class<? extends X> tempClass = null;
        DummyClassLoader loader = new DummyClassLoader(
                ByteManipulationUtils.class.getClassLoader());
        tempClass = (Class<? extends X>) loader.dummyDefineClass(null,
                bytesToConvert, 0, bytesToConvert.length);
        return tempClass;
    }
}

Related Tutorials