-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVarIntSerializer.java
52 lines (44 loc) · 1.82 KB
/
VarIntSerializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
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
package org.machinemc.paklet.serialization;
import org.machinemc.paklet.DataVisitor;
/**
* Serializer for variable-length integers.
* <p>
* Variable-length format such that smaller numbers use fewer bytes.
* The 7 least significant bits are used to encode the value and the most significant bit indicates whether there's another
* byte after it for the next part of the number. The least significant group is written first,
* followed by each of the more significant groups; thus,
* VarInts are effectively little endian (however, groups are 7 bits, not 8).
* <p>
* VarInts are never longer than 5 bytes, and VarLongs are never longer than 10 bytes.
* Within these limits, unnecessarily long encodings (e.g. 81 00 to encode 1) are allowed.
*/
@Supports({Integer.class, int.class})
public class VarIntSerializer implements Serializer<Integer> {
private static final int SEGMENT_BITS = 0x7F;
private static final int CONTINUE_BIT = 0x80;
@Override
public void serialize(SerializerContext context, DataVisitor visitor, Integer value) {
while (true) {
if ((value & ~SEGMENT_BITS) == 0) {
visitor.writeByte(value.byteValue());
return;
}
visitor.writeByte((byte) ((value & SEGMENT_BITS) | CONTINUE_BIT));
value >>>= 7;
}
}
@Override
public Integer deserialize(SerializerContext context, DataVisitor visitor) {
int value = 0;
int position = 0;
byte currentByte;
while (true) {
currentByte = visitor.readByte();
value |= (currentByte & SEGMENT_BITS) << position;
if ((currentByte & CONTINUE_BIT) == 0) break;
position += 7;
if (position >= 32) throw new RuntimeException("VarInt is too big");
}
return value;
}
}