Java Object Deserialize deserialize(@Nonnull byte[] byteArray)

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

Description

Deserialize a byte array of an object.

License

Open Source License

Parameter

Parameter Description
byteArray The byte array serialized from an object.

Exception

Parameter Description
ClassNotFoundException On failure finding target class.

Return

An object from which the byte array is serialized.

Declaration

public @Nonnull static <T> T deserialize(@Nonnull byte[] byteArray) throws IOException, ClassNotFoundException 

Method Source Code


//package com.java2s;
/*/*from w w w. j  a  va2 s.c  om*/
 * This file is part of LaS-VPE Platform.
 *
 * LaS-VPE Platform is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LaS-VPE Platform is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LaS-VPE Platform.  If not, see <http://www.gnu.org/licenses/>.
 */

import javax.annotation.Nonnull;
import java.io.*;

public class Main {
    /**
     * Deserialize a byte array of an object.
     *
     * @param byteArray The byte array serialized from an object.
     * @return An object from which the byte array is serialized.
     * @throws ClassNotFoundException On failure finding target class.
     */
    public @Nonnull static <T> T deserialize(@Nonnull byte[] byteArray) throws IOException, ClassNotFoundException {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
        ObjectInput objectInput = null;
        try {
            objectInput = new ObjectInputStream(byteArrayInputStream);
            //noinspection unchecked
            return (T) objectInput.readObject();
        } finally {
            try {
                if (objectInput != null) {
                    objectInput.close();
                }
            } catch (IOException e) {
                // ignore close exception
            }
            try {
                byteArrayInputStream.close();
            } catch (IOException e) {
                // ignore close exception
            }
        }
    }
}

Related

  1. deserialize(@Nullable final byte[] binaryInput)
  2. deserialize(byte[] bytes, boolean zipped)
  3. deserialize(byte[] bytes, Class clazz)
  4. deserialize(byte[] bytes, Class clazz)