Varint21FrameDecoder .java

net.minecraft.network.Varint21FrameDecoder (class): members: handlerRemoved0, helperBuf, MAX_VARINT21_BYTES, monitor, decode. imports: Unpooled, ByteBuf.

net.minecraft.networkclass65 linescommonextends ByteToMessageDecoder1.7 KB

API Surface (2)

void handlerRemoved0(final ChannelHandlerContext ctx)
void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out)

Fields & Constants (3)

MAX_VARINT21_BYTES
helperBuf
monitor

String literals (2)

"length wider than 21-bit"
"Frame length cannot be zero"

Code Graph: Dependencies (2)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

Varint21FrameDecoder.java65 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.network;
2
3
import io.netty.buffer.ByteBuf;
4
import io.netty.buffer.Unpooled;
5
import io.netty.channel.ChannelHandlerContext;
6
import io.netty.handler.codec.ByteToMessageDecoder;
7
import io.netty.handler.codec.CorruptedFrameException;
8
import java.util.List;
9
import org.jspecify.annotations.Nullable;
10
11
public class Varint21FrameDecoder extends ByteToMessageDecoder {
12
	private static final int MAX_VARINT21_BYTES = 3;
13
	private final ByteBuf helperBuf = Unpooled.directBuffer(3);
14
	private final @Nullable BandwidthDebugMonitor monitor;
15
16
	public Varint21FrameDecoder(final @Nullable BandwidthDebugMonitor monitor) {
17
		this.monitor = monitor;
18
	}
19
20
	@Override
21
	protected void handlerRemoved0(final ChannelHandlerContext ctx) {
22
		this.helperBuf.release();
23
	}
24
25
	private static boolean copyVarint(final ByteBuf in, final ByteBuf out) {
26
		for (int i = 0; i < 3; i++) {
27
			if (!in.isReadable()) {
28
				return false;
29
			}
30
31
			byte b = in.readByte();
32
			out.writeByte(b);
33
			if (!VarInt.hasContinuationBit(b)) {
34
				return true;
35
			}
36
		}
37
38
		throw new CorruptedFrameException("length wider than 21-bit");
39
	}
40
41
	@Override
42
	protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) {
43
		in.markReaderIndex();
44
		this.helperBuf.clear();
45
		if (!copyVarint(in, this.helperBuf)) {
46
			in.resetReaderIndex();
47
		} else {
48
			int length = VarInt.read(this.helperBuf);
49
			if (length == 0) {
50
				throw new CorruptedFrameException("Frame length cannot be zero");
51
			}
52
53
			if (in.readableBytes() < length) {
54
				in.resetReaderIndex();
55
			} else {
56
				if (this.monitor != null) {
57
					this.monitor.onReceive(length + VarInt.getByteSize(length));
58
				}
59
60
				out.add(in.readBytes(length));
61
			}
62
		}
63
	}
64
}
65

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