Object serialization from Byte Array - Java java.io

Java examples for java.io:Serializable

Description

Object serialization from Byte Array

Demo Code


import java.io.*;

public class Main{
    public static void main(String[] argv) throws Exception{
        byte[] bytes = new byte[]{34,35,36,37,37,37,67,68,69};
        System.out.println(fromByteArray(bytes));
    }/*w w  w  .ja  va  2  s .c om*/
    public static <T> T fromByteArray(byte[] bytes) {
        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            ObjectInputStream oos = new ObjectInputStream(bais);
            return (T) oos.readObject();
        } catch (IOException e) {
            LogHelper.warn("IOException during fromBytesArray");
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            LogHelper.warn("ClassNotFoundException fromBytesArray");
            e.printStackTrace();
        }
        return null;
    }
}

Related Tutorials