序列化

简介:

序列化
  序列化主要在zookeeper.jute包中,其中涉及的主要接口如下
    · InputArchive
    · OutputArchive
    · Index
    · Record
2.1 InputArchive
  其是所有反序列化器都需要实现的接口,其方法如下 
InputArchive的类结构如下   

  1. BinaryInputArchive 
  2. CsvInputArchive 
  3. XmlInputArchive
    2.2 OutputArchive
      其是所有序列化器都需要实现此接口,其方法如下。  
    OutputArchive的类结构如下   
  4. BinaryOutputArchive 
  5. CsvOutputArchive 
  6. XmlOutputArchive
    2.3 Index
      其用于迭代反序列化器的迭代器。  
    Index的类结构如下
      
    1.BinaryIndex 
  7. CsxIndex 
    3.XmlIndex
    2.4 Record
      所有用于网络传输或者本地存储的类型都实现该接口,其方法如下 
     所有的实现类都需要实现seriallize和deserialize方法。
    三、示例
      下面通过一个示例来理解OutputArchive和InputArchive的搭配使用。 
    Java
    运行代码
    复制代码
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    package com.leesf.zookeeper_samples;

     OutputStream outputStream = new FileOutputStream(new File(path));
     BinaryOutputArchive binaryOutputArchive = BinaryOutputArchive.getArchive(outputStream);
    
     binaryOutputArchive.writeBool(true, "boolean");
     byte[] bytes = "leesf".getBytes();
     binaryOutputArchive.writeBuffer(bytes, "buffer");
     binaryOutputArchive.writeDouble(13.14, "double");
     binaryOutputArchive.writeFloat(5.20f, "float");
     binaryOutputArchive.writeInt(520, "int");
     Person person = new Person(25, "leesf");
     binaryOutputArchive.writeRecord(person, "leesf");
     TreeMap<String, Integer> map = new TreeMap<String, Integer>();
     map.put("leesf", 25);
     map.put("dyd", 25);
     Set<String> keys = map.keySet();
     binaryOutputArchive.startMap(map, "map");
     int i = 0;
     for (String key: keys) {
         String tag = i + "";
         binaryOutputArchive.writeString(key, tag);
         binaryOutputArchive.writeInt(map.get(key), tag);
         i++;
     }
    
     binaryOutputArchive.endMap(map, "map");
    
    // read operation
    InputStream inputStream = new FileInputStream(new File(path));
    BinaryInputArchive binaryInputArchive = BinaryInputArchive.getArchive(inputStream);

    System.out.println(binaryInputArchive.readBool("boolean"));
    System.out.println(new String(binaryInputArchive.readBuffer("buffer")));
    System.out.println(binaryInputArchive.readDouble("double"));
    System.out.println(binaryInputArchive.readFloat("float"));
    System.out.println(binaryInputArchive.readInt("int"));
    Person person2 = new Person();
    binaryInputArchive.readRecord(person2, "leesf");
    System.out.println(person2);       

    Index index = binaryInputArchive.startMap("map");
    int j = 0;
    while (!index.done()) {
        String tag = j + "";
        System.out.println("key = " + binaryInputArchive.readString(tag) 
            + ", value = " + binaryInputArchive.readInt(tag));
        index.incr();
        j++;
    }
}

static class Person implements Record {
    private int age;
    private String name;

    public Person() {

    }

    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }
    public void serialize(OutputArchive archive, String tag) throws IOException {
        archive.startRecord(this, tag);
        archive.writeInt(age, "age");
        archive.writeString(name, "name");
        archive.endRecord(this, tag);
    }
    public void deserialize(InputArchive archive, String tag) throws IOException {
        archive.startRecord(tag);
        age = archive.readInt("age");
        name = archive.readString("name");
        archive.endRecord(tag);            
    }    

    public String toString() {
        return "age = " + age + ", name = " + name;
    }
}

}
输出结果
true
leesf
13.14
5.2
520
age = 25, name = leesf
key = dyd, value = 25
key = leesf, value = 25
四、总结
这里只需要知道
序列化涉及的类存放在:org.zookeeper.jute包下
常用的类有:
InputArchive
OutputArchive
Index
Record

相关文章
|
4月前
|
人工智能 前端开发 算法
大厂CIO独家分享:AI如何重塑开发者未来十年
在 AI 时代,若你还在紧盯代码量、执着于全栈工程师的招聘,或者仅凭技术贡献率来评判价值,执着于业务提效的比例而忽略产研价值,你很可能已经被所谓的“常识”困住了脚步。
2865 90
大厂CIO独家分享:AI如何重塑开发者未来十年
|
3月前
|
人工智能 Java 网络安全
|
3月前
|
消息中间件 人工智能 NoSQL
RocketMQ:A2A协议实现多智能体优化
Agentic AI 时代已至,在智能客服、代码生成、流程自动化等场景中,多智能体(Multi-Agent)协作正从构想走向落地。然而,当多个 Agent 需要像一个团队那样高效协作时,脆弱的通信机制可能因网络抖动或服务宕机,就让整个系统瞬间瘫痪,导致昂贵的计算任务失败、会话状态丢失。如何为这些聪明的“数字员工”们构建一个真正可靠、高效的通信基座? 本文将为您介绍 Apache RocketMQ 全新推出的轻量级通信模型 LiteTopic,如何在 AI 应用场景中有效简化系统架构、提升稳定性与可靠性,并结合 A2A(Agent-to-Agent)协议与阿里巴巴AgentScope 框架的生产
|
3月前
|
运维 Devops 开发工具
生产环境缺陷管理
在一个大型团队中,bug协同管理是一件复杂的事情,发布经理要追版本bug,运维同学要评估bug影响范围,开发同学要在多个开发分支同时修复同一个bug,很容易出现bug漏提交、漏确认等生产安全问题。 本团队也出现过一起不同分支漏提交bugfix导致的一起P1故障(最高等级),该bug在生产环境进行hotfix时,漏掉了少量集群导致该二次故障。举个相似的例子,某品牌汽车发现潜在安全隐患进行召回,但却遗漏了某个小地区,偏偏在遗漏的地区,发生了安全事故导致有人员伤亡。 我们基于go-git开发实现了通用化的git-poison,通过分布式源码管理bug追溯、查询,可复制性高,适用于所有git仓库,与分
|
3月前
|
Java 测试技术 Linux
生产环境发布管理
在一个大型团队中,生产发布是一件复杂的事情,从dev(前后端联调)-->test(测试集成&压力测试)-->pre(灰度测试)-->prod(生产环境)的多环境推进,以及生产环境的热更新、回滚等问题一直在困扰着各个公司,今天我将基于公司的自动化部署平台为大家讲解下我们是如何做到多环境部署。
|
3月前
|
敏捷开发 Java 测试技术
|
3月前
|
安全 前端开发 数据安全/隐私保护
|
3月前
|
数据安全/隐私保护

热门文章

最新文章