JAVA序列化和反序列化

来源:互联网 发布:mac 围棋打谱 编辑:程序博客网 时间:2024/06/06 07:04
package com.deppon.vas.common.framework.shiro.session;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import org.apache.commons.lang3.StringUtils;import org.apache.shiro.codec.Base64;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.deppon.vas.common.framework.exception.BussinessException;/** * * @ClassName: SerializeUtils* @Description: 序列化和反序列化工具类* @author: 钱胜东(388972)* @date: 2017-4-13  上午11:01:58* @version V1.0 */public class SerializeUtils {/** * 日志 */private static Logger logger = LoggerFactory.getLogger(SerializeUtils.class);/** * * @Title: deserialize* @Description: 反序列化* @param: @param str* @param: @return* @return: Object* @throws* @author: 钱胜东(388972)* @date: 2017-4-20  上午10:53:37 */public static Object deserialize(String str) {  //字节输入流        ByteArrayInputStream bis = null;          //对象输入流        ObjectInputStream ois = null;          try {          if (StringUtils.isNotEmpty(str)) {        //获得base64解码后的字节输入流对象        bis = new ByteArrayInputStream(Base64.decode(str));          //对象输入流                ois = new ObjectInputStream(bis);                 if (bis == null || ois == null) {                throw new BussinessException("SerializeUtils.deserialize.exception");  }                                //读取并返回对象                return ois.readObject();  }else {return null;}                    } catch (Exception e ) {          //日志记录        logger.error("msg={}", e.toString());        //抛出异常            throw new BussinessException("deserialize session error", e);          } finally {                      try {              if (ois != null) {            //关闭对象输入流            ois.close();  }            } catch (IOException e) {                  e.printStackTrace();              }      try {       if (bis != null) {     //关闭字节输入流     bis.close();  }             } catch (IOException e) {                   e.printStackTrace();               }            }      }        /**     *     * @Title: serialize    * @Description: 序列化    * @param: @param obj    * @param: @return    * @return: String    * @throws    * @author: 钱胜东(388972)    * @date: 2017-4-20  上午10:53:51     */public static String serialize(Object obj) {  //字节数组输出流对象        ByteArrayOutputStream bos = null;          //对象输出流对象        ObjectOutputStream oos = null;          try {                  if (obj == null) {return null;}else {//字节数组输出流对象bos = new ByteArrayOutputStream();  //对象输出流对象            oos = new ObjectOutputStream(bos);              //写入Object类型对象            oos.writeObject(obj);              if (bos == null || oos == null) {            //抛出异常                throw new BussinessException("SerializeUtils.serialize.exception");  }                        //返回64位编码后的字符串            return Base64.encodeToString(bos.toByteArray()); }                     } catch (Exception e) {          //抛出异常            throw new BussinessException("serialize session error", e);          } finally {                      try {              if (oos != null) {            //关闭流            oos.close();  }            } catch (IOException e) {              //日志记录            logger.error("msg={}", e.toString());                e.printStackTrace();              }                          try {              if (bos != null) {            //关闭流            bos.close();  }            } catch (IOException e) {              //日志记录            logger.error("msg={}", e.toString());                e.printStackTrace();              }                        }      }  }

原创粉丝点击