源码分析kryo对象序列化实现原理

kryo对象序列化入口为Kryo的writeClassAndObject。

Kryo#writeClassAndObject

 1public void writeClassAndObject (Output output, Object object) {
 2    if (output == null) throw new IllegalArgumentException("output cannot be null.");
 3    beginObject();                                             // @1
 4    try {
 5        if (object == null) {
 6            writeClass(output, null);              // @2
 7            return;
 8        }
 9        Registration registration = writeClass(output, object.getClass());   // @3
10        if (references && writeReferenceOrNull(output, object, false)) {    // @4
11            registration.getSerializer().setGenerics(this, null);
12            return;
13        }
14        if (TRACE || (DEBUG && depth == 1)) log("Write", object);
15        registration.getSerializer().write(this, output, object);  // @5
16    } finally {
17        if (--depth == 0 && autoReset) reset();       // @6
18    }
19}

代码@1:开始序列化, 将dept自增,表示当前深度,因为在序列化一个对象时,该方法有可能会被递归调用,每递归调用增加1,一次调用结束后在finally字句中自减。

代码@2:如果对象为空,则调用writeClass(DefaultSerializers ClassSerializer),序列化为空。

代码@3:如果对象不为空,首先序列化对象所属的Class实例,从这里可以看出,Kryo在序列化时

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值