1.序列化

简介: casvasgas

一、前言
  在完成了前面的理论学习后,现在可以从源码角度来解析Zookeeper的细节,首先笔者想从序列化入手,因为在网络通信、数据存储中都用到了序列化,下面开始分析。

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


1. BinaryInputArchive 
2. CsvInputArchive 

3. XmlInputArchive

2.2 OutputArchive
  其是所有序列化器都需要实现此接口,其方法如下。  


OutputArchive的类结构如下
  


1. BinaryOutputArchive 
2. CsvOutputArchive 
3. XmlOutputArchive
2.3 Index
  其用于迭代反序列化器的迭代器。  
Index的类结构如下
  



1.BinaryIndex 
2. CsxIndex 
3.XmlIndex
2.4 Record    
  所有用于网络传输或者本地存储的类型都实现该接口,其方法如下 
 所有的实现类都需要实现seriallize和deserialize方法。
三、示例
  下面通过一个示例来理解OutputArchive和InputArchive的搭配使用。 

Java

运行代码复制代码

14

15

16

17

18

19

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;

import org.apache.jute.OutputArchive;

import org.apache.jute.Record;

public class ArchiveTest {

public static void main( String[] args ) throws IOException {

String path = "F:\\test.txt";

// write operation

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

若有收获,就点个赞吧


相关文章
|
7月前
|
缓存 Java 关系型数据库
|
7月前
|
canal 缓存 关系型数据库
|
7月前
|
负载均衡 算法 Java
|
7月前
|
Java 数据库 Nacos
|
7月前
|
XML Dubbo 应用服务中间件
|
7月前
|
Dubbo Java 应用服务中间件
|
7月前
|
XML Dubbo Java
|
7月前
|
Arthas Java 测试技术
|
7月前
|
Dubbo 前端开发 JavaScript