Here you can find the source of deserializeJdk(byte[] bytes)
Parameter | Description |
---|---|
T | Type of result object. |
bytes | Object bytes to deserialize. |
Parameter | Description |
---|---|
IOException | If deserialization failed. |
ClassNotFoundException | If deserialization failed. |
@SuppressWarnings({ "unchecked" }) public static <T extends Serializable> T deserializeJdk(byte[] bytes) throws IOException, ClassNotFoundException
//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(); } }