首页

基于apache的commons-lang源码包SerializationUtils序列化工具类实现对象克隆、序列化及反序列化等操作

标签:SerializationUtils,序列化工具类,commons-lang,apache     发布时间:2018-02-09   

一、前言

基于apache的commons-lang(3-3.2)源码包中的org.apache.commons.lang3.SerializationUtils序列化工具类对对象克隆、序列化对象为二进制字节数组、输入流反序列化等操作。

二、源码说明

package org.apache.commons.lang3;@b@@b@import java.io.ByteArrayInputStream;@b@import java.io.ByteArrayOutputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.ObjectInputStream;@b@import java.io.ObjectOutputStream;@b@import java.io.ObjectStreamClass;@b@import java.io.OutputStream;@b@import java.io.Serializable;@b@import java.util.HashMap;@b@import java.util.Map;@b@@b@public class SerializationUtils@b@{@b@  public static <T extends Serializable> T clone(T object)@b@  {@b@    if (object == null)@b@      return null;@b@@b@    byte[] objectData = serialize(object);@b@    ByteArrayInputStream bais = new ByteArrayInputStream(objectData);@b@@b@    ClassLoaderAwareObjectInputStream in = null;@b@    try@b@    {@b@      in = new ClassLoaderAwareObjectInputStream(bais, object.getClass().getClassLoader());@b@@b@      Serializable readObject = (Serializable)in.readObject();@b@      Serializable localSerializable1 = readObject;@b@@b@      return localSerializable1;@b@    }@b@    catch (ClassNotFoundException ex)@b@    {@b@    }@b@    catch (IOException ex)@b@    {@b@    }@b@    finally@b@    {@b@      try@b@      {@b@        if (in != null)@b@          in.close();@b@      }@b@      catch (IOException ex) {@b@        throw new SerializationException("IOException on closing cloned object data InputStream.", ex);@b@      }@b@    }@b@  }@b@@b@  public static void serialize(Serializable obj, OutputStream outputStream)@b@  {@b@    if (outputStream == null)@b@      throw new IllegalArgumentException("The OutputStream must not be null");@b@@b@    ObjectOutputStream out = null;@b@    try@b@    {@b@      out = new ObjectOutputStream(outputStream);@b@      out.writeObject(obj);@b@    }@b@    catch (IOException ex) {@b@    }@b@    finally {@b@      try {@b@        if (out != null)@b@          out.close();@b@      }@b@      catch (IOException ex)@b@      {@b@      }@b@    }@b@  }@b@@b@  public static byte[] serialize(Serializable obj)@b@  {@b@    ByteArrayOutputStream baos = new ByteArrayOutputStream(512);@b@    serialize(obj, baos);@b@    return baos.toByteArray();@b@  }@b@@b@  public static <T> T deserialize(InputStream inputStream)@b@  {@b@    if (inputStream == null)@b@      throw new IllegalArgumentException("The InputStream must not be null");@b@@b@    ObjectInputStream in = null;@b@    try@b@    {@b@      in = new ObjectInputStream(inputStream);@b@@b@      Object obj = in.readObject();@b@      Object localObject1 = obj;@b@@b@      return localObject1;@b@    }@b@    catch (ClassCastException ex)@b@    {@b@    }@b@    catch (ClassNotFoundException ex)@b@    {@b@    }@b@    catch (IOException ex)@b@    {@b@    }@b@    finally@b@    {@b@      try@b@      {@b@        if (in != null)@b@          in.close();@b@      }@b@      catch (IOException ex)@b@      {@b@      }@b@    }@b@  }@b@@b@  public static <T> T deserialize(byte[] objectData)@b@  {@b@    if (objectData == null)@b@      throw new IllegalArgumentException("The byte[] must not be null");@b@@b@    return deserialize(new ByteArrayInputStream(objectData));@b@  }@b@@b@  static class ClassLoaderAwareObjectInputStream extends ObjectInputStream@b@  {@b@    private static final Map<String, Class<?>> primitiveTypes = new HashMap();@b@    private final ClassLoader classLoader;@b@@b@    public ClassLoaderAwareObjectInputStream(InputStream in, ClassLoader classLoader)@b@      throws IOException@b@    {@b@      super(in);@b@      this.classLoader = classLoader;@b@@b@      primitiveTypes.put("byte", Byte.TYPE);@b@      primitiveTypes.put("short", Short.TYPE);@b@      primitiveTypes.put("int", Integer.TYPE);@b@      primitiveTypes.put("long", Long.TYPE);@b@      primitiveTypes.put("float", Float.TYPE);@b@      primitiveTypes.put("double", Double.TYPE);@b@      primitiveTypes.put("boolean", Boolean.TYPE);@b@      primitiveTypes.put("char", Character.TYPE);@b@      primitiveTypes.put("void", Void.TYPE);@b@    }@b@@b@    protected Class<?> resolveClass(ObjectStreamClass desc)@b@      throws IOException, ClassNotFoundException@b@    {@b@      String name = desc.getName();@b@      try {@b@        return Class.forName(name, false, this.classLoader);@b@      } catch (ClassNotFoundException ex) {@b@        try {@b@          return Class.forName(name, false, Thread.currentThread().getContextClassLoader());@b@        } catch (ClassNotFoundException cnfe) {@b@          Class cls = (Class)primitiveTypes.get(name);@b@          if (cls != null)@b@            return cls;@b@@b@          throw cnfe;@b@        }@b@      }@b@    }@b@  }@b@}