整数序列化

org.apache.hadoop.io.WritableUtils

 

 1     public static void writeVLong(DataOutput stream, long i) throws IOException 
 2     {
 3         // 直接写数据
 4         if (i >= -112 && i <= 127) {
 5             stream.writeByte((byte) i);
 6             return;
 7         }
 8 
 9         int len = -112;
10         
11         // 当操作数据为负数
12         if (i < 0) {
13             i ^= -1L;   // i ^ 111111111... 相当于(-i - 1)
14             len = -120; 
15         }
16         
17         // 循环取最高位长度
18         long tmp = i;
19         while (tmp != 0) {
20             tmp = tmp >> 8;
21             len--;
22         }
23         
24         // 写入长度标识
25         stream.writeByte((byte) len);
26         
27         // 计算需写入的字节长度
28         len = (len < -120) ? -(len + 120) : -(len + 112);
29         
30         for (int idx = len; idx != 0; idx--) {
31             int shiftbits = (idx - 1) * 8;  // 屏蔽长度
32             long mask = 0xFFL << shiftbits; // 255 << ((idx -1) * 8)
33             stream.writeByte((byte) ((i & mask) >> shiftbits));
34         }
35     }

 

 1     // 判断是否为负值
 2     public static boolean isNegativeVInt(byte value) 
 3     {
 4         return value < -120 || (value >= -112 && value < 0);
 5     }
 6 
 7     // 解析写入时的长度标识
 8     public static int decodeVIntSize(byte value) 
 9     {
10         if (value >= -112) return 1;
11         else if (value < -120) return -119 - value;
12         return -111 - value;
13     }
14     
15 
16     public static long readVLong(DataInput stream) throws IOException 
17     {
18         byte firstByte = stream.readByte();
19         int len = decodeVIntSize(firstByte);
20         
21         if (len == 1) {
22             return firstByte;
23         }
24         
25         long i = 0;
26         for (int idx = 0; idx < len - 1; idx++) {
27             byte b = stream.readByte();
28             i = i << 8; // 00000000 每次向左移8位
29             i = i | (b & 0xFF); // i | (b & 11111111)
30         }
31         
32         // 如果为负数则^-1L
33         return (isNegativeVInt(firstByte) ? (i ^ -1L) : i);
34     }

转载于:https://www.cnblogs.com/rilley/archive/2012/08/16/2642541.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用 Protocol Buffers(简称为 Protobuf)进行整数序列化时,你需要定义一个 Protobuf 消息类型,并在其中包含一个整数字段。下面是一个示例: ```protobuf syntax = "proto3"; message IntegerMessage { int32 value = 1; } ``` 在上面的示例中,我们定义了一个名为 `IntegerMessage` 的消息类型,它包含一个名为 `value` 的整数字段。 接下来,你需要使用 Protobuf 编译器将这个 `.proto` 文件编译成你所使用的编程语言的代码。编译后的代码将提供用于序列化和反序列化消息的方法。 以下是使用 Python 进行整数序列化的示例代码: ```python import IntegerMessage_pb2 # 创建一个 IntegerMessage 实例并设置 value 值 message = IntegerMessage_pb2.IntegerMessage() message.value = 42 # 序列化消息为字节流 serialized_data = message.SerializeToString() # 将字节流保存到文件中 with open('serialized_data.bin', 'wb') as f: f.write(serialized_data) ``` 在上面的示例中,我们先导入生成的 Protobuf 代码。然后,我们创建一个 `IntegerMessage` 实例并设置 `value` 值为 42。接下来,我们调用 `SerializeToString()` 方法将消息序列化为字节流,并将其保存到文件中。 当你需要反序列化时,可以使用以下示例代码: ```python import IntegerMessage_pb2 # 从文件中读取序列化的数据 with open('serialized_data.bin', 'rb') as f: serialized_data = f.read() # 反序列化字节流为 IntegerMessage 实例 message = IntegerMessage_pb2.IntegerMessage() message.ParseFromString(serialized_data) # 访问 value 字段的值 value = message.value print(value) # 输出:42 ``` 在上面的示例中,我们从文件中读取序列化的数据,并使用 `ParseFromString()` 方法将其反序列化为 `IntegerMessage` 实例。你可以通过访问 `value` 字段来获取整数值。 这是一个简单的示例,你可以根据你的具体需求和编程语言来调整代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值