Java Object Deserialize deserialize(byte[] objectData)

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

Description

Deserializes a single Object from an array of bytes.

License

Open Source License

Parameter

Parameter Description
objectData the serialized object, must not be null

Exception

Parameter Description
IllegalArgumentException if <code>objectData</code> is <code>null</code>
RuntimeException (runtime) if the serialization fails

Return

the deserialized object

Declaration

public static Object deserialize(byte[] objectData) 

Method Source Code

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

import java.io.ByteArrayInputStream;

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

public class Main {
    /**/*from   www .  j  a v  a 2s . c  om*/
     * <p>Deserializes a single <code>Object</code> from an array of bytes.</p>
     *
     * @param objectData  the serialized object, must not be null
     * @return the deserialized object
     * @throws IllegalArgumentException if <code>objectData</code> is <code>null</code>
     * @throws RuntimeException (runtime) if the serialization fails
     */
    public static Object deserialize(byte[] objectData) {
        if (objectData == null)
            throw new IllegalArgumentException(
                    "The byte[] must not be null");
        ByteArrayInputStream bais = new ByteArrayInputStream(objectData);
        return deserialize(bais);
    }

    /**
     * <p>Deserializes an <code>Object</code> from the specified stream.</p>
     *
     * <p>The stream will be closed once the object is written. This
     * avoids the need for a finally clause, and maybe also exception
     * handling, in the application code.</p>
     *
     * <p>The stream passed in is not buffered internally within this method.
     * This is the responsibility of your application if desired.</p>
     *
     * @param inputStream  the serialized object input stream, must not be null
     * @return the deserialized object
     * @throws IllegalArgumentException if <code>inputStream</code> is <code>null</code>
     * @throws RuntimeException (runtime) if the serialization fails
     */
    public static Object deserialize(InputStream inputStream) {
        if (inputStream == null)
            throw new IllegalArgumentException(
                    "The InputStream must not be null");
        ObjectInputStream in = null;
        try {
            // stream closed in the finally
            in = new ObjectInputStream(inputStream);
            return in.readObject();

        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException ex) {
                // ignore close exception
            }
        }
    }
}

Related

  1. deserialize(byte[] in)
  2. deserialize(byte[] in)
  3. Deserialize(byte[] objectBytes)
  4. deserialize(byte[] objectData)
  5. deserialize(byte[] objectData)
  6. deserialize(byte[] objectData)
  7. deserialize(byte[] serial)
  8. deserialize(byte[] serial)
  9. deserialize(byte[] serialized)