VarInt .java

net.minecraft.network.VarInt (class, 51 lines): members: MAX_VARINT_SIZE, CONTINUATION_BIT_MASK, DATA_BITS_PER_BYTE, getByteSize, hasContinuationBit, read.

net.minecraft.networkclass51 linescommon1.0 KB

API Surface (4)

int getByteSize(final int value)
boolean hasContinuationBit(final byte in)
int read(final ByteBuf input)
ByteBuf write(final ByteBuf output, int value)

Fields & Constants (4)

MAX_VARINT_SIZE
DATA_BITS_MASK
CONTINUATION_BIT_MASK
DATA_BITS_PER_BYTE

String literals (1)

"VarInt too big"

Full Source

VarInt.java51 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.network;
2
3
import io.netty.buffer.ByteBuf;
4
5
public class VarInt {
6
	public static final int MAX_VARINT_SIZE = 5;
7
	private static final int DATA_BITS_MASK = 127;
8
	private static final int CONTINUATION_BIT_MASK = 128;
9
	private static final int DATA_BITS_PER_BYTE = 7;
10
11
	public static int getByteSize(final int value) {
12
		for (int i = 1; i < 5; i++) {
13
			if ((value & -1 << i * 7) == 0) {
14
				return i;
15
			}
16
		}
17
18
		return 5;
19
	}
20
21
	public static boolean hasContinuationBit(final byte in) {
22
		return (in & 128) == 128;
23
	}
24
25
	public static int read(final ByteBuf input) {
26
		int out = 0;
27
		int bytes = 0;
28
29
		byte in;
30
		do {
31
			in = input.readByte();
32
			out |= (in & 127) << bytes++ * 7;
33
			if (bytes > 5) {
34
				throw new RuntimeException("VarInt too big");
35
			}
36
		} while (hasContinuationBit(in));
37
38
		return out;
39
	}
40
41
	public static ByteBuf write(final ByteBuf output, int value) {
42
		while ((value & -128) != 0) {
43
			output.writeByte(value & 127 | 128);
44
			value >>>= 7;
45
		}
46
47
		output.writeByte(value);
48
		return output;
49
	}
50
}
51

Decompiled Minecraft 26.3 source of VarInt (net/minecraft/network/VarInt.java) with Mojang official mappings, stored verbatim on the site.