Java Object Serialize and Deserialize deserializeJdk(byte[] bytes)

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

Description

Deserializes passed in bytes using provided class loader.

License

Open Source License

Parameter

Parameter Description
T Type of result object.
bytes Object bytes to deserialize.

Exception

Parameter Description
IOException If deserialization failed.
ClassNotFoundException If deserialization failed.

Return

Deserialized object.

Declaration

@SuppressWarnings({ "unchecked" })
public static <T extends Serializable> T deserializeJdk(byte[] bytes)
        throws IOException, ClassNotFoundException 

Method Source Code


//package com.java2s;
/* /*  w w w .ja va2s.  co  m*/
 Copyright (C) GridGain Systems. All Rights Reserved.
     
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
    
 http://www.apache.org/licenses/LICENSE-2.0
     
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

import java.io.*;

public class Main {
    /**
     * Deserializes passed in bytes using provided class loader.
     *
     * @param <T> Type of result object.
     * @param bytes Object bytes to deserialize.
     * @return Deserialized object.
     * @throws IOException If deserialization failed.
     * @throws ClassNotFoundException If deserialization failed.
     */
    @SuppressWarnings({ "unchecked" })
    public static <T extends Serializable> T deserializeJdk(byte[] bytes)
            throws IOException, ClassNotFoundException {
        ObjectInputStream in = null;

        try {
            in = new ObjectInputStream(new ByteArrayInputStream(bytes));

            return (T) in.readObject();
        } finally {
            close(in);
        }
    }

    /**
     * Deserializes passed in bytes using provided class loader.
     *
     * @param <T> Type of result object.
     * @param bytes Object bytes to deserialize.
     * @param clsLdr Class loader.
     * @return Deserialized object.
     * @throws IOException If deserialization failed.
     * @throws ClassNotFoundException If deserialization failed.
     */
    @SuppressWarnings({ "unchecked" })
    public static <T> T deserializeJdk(byte[] bytes, final ClassLoader clsLdr)
            throws IOException, ClassNotFoundException {
        ObjectInputStream in = null;

        try {
            in = new ObjectInputStream(new ByteArrayInputStream(bytes)) {
                @Override
                protected Class<?> resolveClass(ObjectStreamClass desc) throws ClassNotFoundException {
                    return clsLdr.loadClass(desc.getName());
                }
            };

            return (T) in.readObject();
        } finally {
            close(in);
        }
    }

    /**
     * Closes resource.
     *
     * @param c Resource
     * @throws IOException If failed to close resource.
     */
    private static void close(Closeable c) throws IOException {
        if (c != null)
            c.close();
    }
}

Related

  1. deSerialize(String str)
  2. deserializeAndCheckObject(final byte[] object, final Class type)
  3. deserialized(final byte[] data)
  4. deserializeFromString(String obj)
  5. deserializeGZip(byte[] buf, T obj)
  6. deSerializeObj(byte[] array)
  7. deSerializeObject(byte[] base64SerializedObject)
  8. deserializeObject(String dir)
  9. deSerializeObject(String str, Class cls)