VarLong .java

net.minecraft.network.VarLong (class): members: MAX_VARLONG_SIZE, CONTINUATION_BIT_MASK, DATA_BITS_PER_BYTE, getByteSize, hasContinuationBit, read, write.

net.minecraft.networkclass51 linescommon1.0 KB

API Surface (4)

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

Fields & Constants (4)

MAX_VARLONG_SIZE
DATA_BITS_MASK
CONTINUATION_BIT_MASK
DATA_BITS_PER_BYTE

String literals (1)

"VarLong too big"

Full Source

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

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