首页

关于spring-core包中org.springframework.util.SerializationUtils对象序列化工具类源码说明

标签:springframework,SerializationUtils,对象序列化工具类,serialize,deserialize     发布时间:2018-08-27   

一、前言

关于spring-core源码包中org.springframework.util.SerializationUtils序列化工具类,进行对象serialize序列化、反序列化操作deserialize,详情源码说明。

二、源码说明

package org.springframework.util;@b@@b@import java.io.ByteArrayInputStream;@b@import java.io.ByteArrayOutputStream;@b@import java.io.IOException;@b@import java.io.ObjectInputStream;@b@import java.io.ObjectOutputStream;@b@@b@public abstract class SerializationUtils@b@{@b@  public static byte[] serialize(Object object)@b@  {@b@    if (object == null)@b@      return null;@b@@b@    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);@b@    try {@b@      ObjectOutputStream oos = new ObjectOutputStream(baos);@b@      oos.writeObject(object);@b@      oos.flush();@b@    }@b@    catch (IOException ex) {@b@      throw new IllegalArgumentException("Failed to serialize object of type: " + object.getClass(), ex);@b@    }@b@    return baos.toByteArray();@b@  }@b@@b@  public static Object deserialize(byte[] bytes)@b@  {@b@    if (bytes == null)@b@      return null;@b@    try@b@    {@b@      ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));@b@      return ois.readObject();@b@    }@b@    catch (IOException ex) {@b@      throw new IllegalArgumentException("Failed to deserialize object", ex);@b@    }@b@    catch (ClassNotFoundException ex) {@b@      throw new IllegalStateException("Failed to deserialize object type", ex);@b@    }@b@  }@b@}