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

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

Description

Deserializes an object.

License

LGPL

Parameter

Parameter Description
bytes The bytes or null

Exception

Parameter Description
IOException In case of a reading or deserialization error
ClassNotFoundException In case an unknown class has been serialized

Return

The object or null

Declaration

public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException 

Method Source Code

//package com.java2s;
/**//from ww w. ja  va 2  s . co  m
 * Copyright 2009-2016 Three Crickets LLC.
 * <p>
 * The contents of this file are subject to the terms of the LGPL version 3.0:
 * http://www.gnu.org/copyleft/lesser.html
 * <p>
 * Alternatively, you can obtain a royalty free commercial license with less
 * limitations, transferable or non-transferable, directly from Three Crickets
 * at http://threecrickets.com/
 */

import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

public class Main {
    /**
     * Deserializes an object.
     * 
     * @param bytes
     *        The bytes or null
     * @return The object or null
     * @throws IOException
     *         In case of a reading or deserialization error
     * @throws ClassNotFoundException
     *         In case an unknown class has been serialized
     */
    public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
        if ((bytes == null) || (bytes.length == 0))
            return null;

        ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
        try {
            ObjectInputStream stream = new ObjectInputStream(byteStream);
            try {
                return stream.readObject();
            } finally {
                stream.close();
            }
        } finally {
            byteStream.close();
        }
    }
}

Related

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