JAVA序列化和反序列化

package com.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.common.framework.exception.BussinessException;

/**
 * 
* @ClassName: SerializeUtils
* @Description: 序列化和反序列化工具类
* @author: 
* @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: 
	* @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: 
    * @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();  
            }  
            
  
        }  
    }  

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值