Java Object Deserialize deserialize(byte[] bytes, Class clazz)

Here you can find the source of deserialize(byte[] bytes, Class clazz)

Description

Deserialize a byte array into an object.

License

Open Source License

Parameter

Parameter Description
bytes Byte array to be deserialized.
clazz Class type the object should be deserialized into.

Return

Object deserialized from the byte array.

Declaration

public static <T extends Serializable> T deserialize(byte[] bytes, Class<T> clazz)
        throws IOException, ClassNotFoundException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayInputStream;

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

import java.io.Serializable;

public class Main {
    /**//from  w w w.  j  ava  2 s. c o m
     * Deserialize a byte array into an object. The object has to implement {@link Serializable} interface.
     *
     * @param bytes Byte array to be deserialized.
     * @param clazz Class type the object should be deserialized into.
     * @return Object deserialized from the byte array.
     */
    public static <T extends Serializable> T deserialize(byte[] bytes, Class<T> clazz)
            throws IOException, ClassNotFoundException {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        Object object = objectInputStream.readObject();

        return clazz.cast(object);
    }
}

Related

  1. deserialize(@Nonnull byte[] byteArray)
  2. deserialize(@Nullable final byte[] binaryInput)
  3. deserialize(byte[] bytes, boolean zipped)
  4. deserialize(byte[] bytes, Class clazz)
  5. deserialize(byte[] bytes, Class klass)
  6. deserialize(byte[] bytes, final Class asClass)
  7. deserialize(byte[] bytes, List customClassLoaders)