序列化工具-hessian-序列化和反序列化用法

一、环境依赖:

<dependency>
  <groupId>com.caucho</groupId>
  <artifactId>hessian</artifactId>
  <version>4.0.37</version>
</dependency>

 

二、序列化&反序列化:

public interface ISerializer {
     <T> T deserialize(byte[] data);
     <T> byte[] serialize(T obj);
}

package com.jason.seria.hessian;

import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.jason.seria.IBaseSerializer;
import com.jason.seria.ISerializer;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class HessianSerializer implements ISerializer {
    //序列化
    @Override
    public <T> byte[] serialize(T obj) {
        byte[] result = null;
        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
        Hessian2Output  hessian2Output=new Hessian2Output(byteArrayOutputStream);
        try {
            System.out.println(obj);
            hessian2Output.startMessage();
            hessian2Output.writeObject(obj);
            hessian2Output.flush();
            hessian2Output.completeMessage();
            result=byteArrayOutputStream.toByteArray();
            System.out.println("result="+result.length);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(null!=hessian2Output)
                {
                    hessian2Output.close();
                    byteArrayOutputStream.close();
                }
                return   result;
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return new byte[0];
    }
    //反序列化
    @Override
    public <T> T deserialize(byte[] data) {
        ByteArrayInputStream byteInputStream=new ByteArrayInputStream(data);
        Hessian2Input   hessian2Input=new Hessian2Input(byteInputStream);
        T  object=null;
        try {
            hessian2Input.startMessage();
            object=(T)hessian2Input.readObject();
            hessian2Input.completeMessage();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(null !=hessian2Input){
                    hessian2Input.close();
                    byteInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return object;
        }

    }
}

//测试方法:
public class TestMain {
    public static void main(String[] args) {
        Student stu=new Student();
        stu.setName("jason");
        stu.setAge(18);
        ISerializer serializer=new HessianSerializer();
        byte[] serialize = serializer.serialize(stu);
        System.out.println("-----------:"+new String(serialize));
        System.out.println((Student)serializer.deserialize(serialize));
    }
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

石头城程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值