原文链接:https://blog.csdn.net/yaomingyang/article/details/79322327
序列化工具类详解
package org.apache.commons.lang3;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class SerializationUtils
{
public SerializationUtils() {}
//克隆一个序列化对象
public static <T extends Serializable> T clone(T object)
{
}
//执行串行往返也就是执行序列化或者反序列化
public static <T extends Serializable> T roundtrip(T msg)
{
return (Serializable)deserialize(serialize(msg));
}
//讲一个对象序列化并且输出到输出流中
public static void serialize(Serializable obj, OutputStream outputStream)
{
Validate.isTrue(outputStream != null, "The OutputStream must not be null", new Object[0]);
try { ObjectOutputStream out = new ObjectOutputStream(outputStream);Throwable localThrowable2 = null;
try { out.writeObject(obj);
}
catch (Throwable localThrowable1)
{
localThrowable2 = localThrowable1;throw localThrowable1;
} finally {
if (out != null) if (localThrowable2 != null) try { out.close(); } catch (Throwable x2) { localThrowable2.addSuppressed(x2); } else out.close();
} } catch (IOException ex) { throw new SerializationException(ex);
}
}
//将一个对象序列化并返回字节数组
public static byte[] serialize(Serializable obj)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
serialize(obj, baos);
return baos.toByteArray();
}
//讲一个输入流反序列化成一个对象
public static <T> T deserialize(InputStream inputStream)
{
}
//将一个字节数组反序列化成一个对象
public static <T> T deserialize(byte[] objectData)
{
Validate.isTrue(objectData != null, "The byte[] must not be null", new Object[0]);
return deserialize(new ByteArrayInputStream(objectData));
}
static class ClassLoaderAwareObjectInputStream
extends ObjectInputStream
{
private static final Map<String, Class<?>> primitiveTypes = new HashMap();
private final ClassLoader classLoader;
static {
primitiveTypes.put("byte", Byte.TYPE);
primitiveTypes.put("short", Short.TYPE);
primitiveTypes.put("int", Integer.TYPE);
primitiveTypes.put("long", Long.TYPE);
primitiveTypes.put("float", Float.TYPE);
primitiveTypes.put("double", Double.TYPE);
primitiveTypes.put("boolean", Boolean.TYPE);
primitiveTypes.put("char", Character.TYPE);
primitiveTypes.put("void", Void.TYPE);
}
ClassLoaderAwareObjectInputStream(InputStream in, ClassLoader classLoader)
throws IOException
{
super();
this.classLoader = classLoader;
}
protected Class<?> resolveClass(ObjectStreamClass desc)
throws IOException, ClassNotFoundException
{
String name = desc.getName();
try {
return Class.forName(name, false, this.classLoader);
} catch (ClassNotFoundException ex) {
try {
return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException cnfe) {
Class<?> cls = (Class)primitiveTypes.get(name);
if (cls != null) {
return cls;
}
throw cnfe;
}
}
}
}
}