Java Object Deserialize deserialize(byte[] data)

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

Description

Deserializes an object previously written using an ObjectOutputStream.

License

Open Source License

Parameter

Parameter Description
data binary array.

Exception

Parameter Description
IOException if an I/O error occured.

Return

deserialized object.

Declaration

public static Object deserialize(byte[] data) throws IOException 

Method Source Code


//package com.java2s;
/*//from  ww  w  .ja v  a 2s. co  m
 * Copyright (c) 2001-2002, Marco Hunsicker. All rights reserved.
 *
 * This software is distributable under the BSD license. See the terms of the
 * BSD license in the documentation provided with this software.
 */

import java.io.BufferedInputStream;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;

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

import java.io.ObjectInputStream;

public class Main {
    /**
     * Deserializes an object previously written using an ObjectOutputStream.
     *
     * @param data binary array.
     *
     * @return deserialized object.
     *
     * @throws IOException if an I/O error occured.
     *
     * @see #serialize
     */
    public static Object deserialize(byte[] data) throws IOException {
        if (data.length == 0) {
            return null;
        }

        return deserialize(new BufferedInputStream(new ByteArrayInputStream(data)));
    }

    /**
     * Deserializes the object stored in the given stream.
     *
     * @param in an input stream.
     *
     * @return the deserialized object.
     *
     * @throws IOException if an I/O exception occured.
     */
    public static Object deserialize(InputStream in) throws IOException {
        ObjectInputStream oin = new ObjectInputStream(in);

        try {
            return oin.readObject();
        } catch (ClassNotFoundException ex) {
            /**
             * @todo once we only support JDK 1.4, add chained exception
             */
            throw new IOException(ex.getMessage());
        } finally {
            if (oin != null) {
                oin.close();
            }
        }
    }

    /**
     * Deserializes the object stored in the given file.
     *
     * @param file a file.
     *
     * @return the deserialized object.
     *
     * @throws IOException if an I/O exception occured.
     */
    public static final Object deserialize(File file) throws IOException {
        return deserialize(new BufferedInputStream(new FileInputStream(file)));
    }
}

Related

  1. deserialize(byte[] bytes, Class klass)
  2. deserialize(byte[] bytes, final Class asClass)
  3. deserialize(byte[] bytes, List customClassLoaders)
  4. deserialize(byte[] data)
  5. deserialize(byte[] data)
  6. deserialize(byte[] data)
  7. deserialize(byte[] data)
  8. deserialize(byte[] data)
  9. deserialize(byte[] data)