Java Byte Array to Object deserialize(byte[] byteArray)

Here you can find the source of deserialize(byte[] byteArray)

Description

deserialize

License

Apache License

Declaration

public static Serializable deserialize(byte[] byteArray) throws IOException, ClassNotFoundException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayInputStream;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;

import java.io.Serializable;

public class Main {
    public static Serializable deserialize(byte[] byteArray) throws IOException, ClassNotFoundException {
        return deserialize(byteArray, Thread.currentThread().getContextClassLoader());
    }//  ww  w  .j  a v a2  s.  com

    public static Serializable deserialize(byte[] byteArray, ClassLoader classLoader)
            throws IOException, ClassNotFoundException {
        ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(classLoader);

        ByteArrayInputStream byteArrayIn = new ByteArrayInputStream(byteArray);
        ObjectInput objectIn = null;
        Serializable result;
        try {
            objectIn = new ObjectInputStream(byteArrayIn);
            result = (Serializable) objectIn.readObject();
        } finally {
            try {
                byteArrayIn.close();
            } catch (IOException ex) {
                // ignore close exception
            }
            try {
                if (objectIn != null) {
                    objectIn.close();
                }
            } catch (IOException ex) {
                // ignore close exception
            }
            Thread.currentThread().setContextClassLoader(originalClassLoader);
        }
        return result;
    }
}

Related

  1. deserialize(byte[] b)
  2. deserialize(byte[] blob)
  3. deserialize(byte[] buf)
  4. deserialize(byte[] buf)
  5. deserialize(byte[] buffer, int count)
  6. deserialize(byte[] byteArray)
  7. deserialize(byte[] bytes)
  8. deserialize(byte[] bytes)
  9. deserialize(byte[] bytes)