CompressionEncoder .java

net.minecraft.network.CompressionEncoder (class, 51 lines): members: getThreshold, encodeBuf, deflater, setThreshold, threshold, encode. imports: ByteBuf.

net.minecraft.networkclass51 linescommonextends MessageToByteEncoder<ByteBuf>1.4 KB

API Surface (3)

void encode(final ChannelHandlerContext ctx, final ByteBuf uncompressed, final ByteBuf out)
int getThreshold()
void setThreshold(final int threshold)

Fields & Constants (3)

encodeBuf
deflater
threshold

String literals (2)

"Packet too big (is "
", should be less than 8388608)"

Code Graph: Dependencies (1)

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

Full Source

CompressionEncoder.java51 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.network;
2
3
import io.netty.buffer.ByteBuf;
4
import io.netty.channel.ChannelHandlerContext;
5
import io.netty.handler.codec.MessageToByteEncoder;
6
import java.util.zip.Deflater;
7
8
public class CompressionEncoder extends MessageToByteEncoder<ByteBuf> {
9
	private final byte[] encodeBuf = new byte[8192];
10
	private final Deflater deflater;
11
	private int threshold;
12
13
	public CompressionEncoder(final int threshold) {
14
		this.threshold = threshold;
15
		this.deflater = new Deflater();
16
	}
17
18
	protected void encode(final ChannelHandlerContext ctx, final ByteBuf uncompressed, final ByteBuf out) {
19
		int uncompressedLength = uncompressed.readableBytes();
20
		if (uncompressedLength > 8388608) {
21
			throw new IllegalArgumentException("Packet too big (is " + uncompressedLength + ", should be less than 8388608)");
22
		}
23
24
		if (uncompressedLength < this.threshold) {
25
			VarInt.write(out, 0);
26
			out.writeBytes(uncompressed);
27
		} else {
28
			byte[] input = new byte[uncompressedLength];
29
			uncompressed.readBytes(input);
30
			VarInt.write(out, input.length);
31
			this.deflater.setInput(input, 0, uncompressedLength);
32
			this.deflater.finish();
33
34
			while (!this.deflater.finished()) {
35
				int written = this.deflater.deflate(this.encodeBuf);
36
				out.writeBytes(this.encodeBuf, 0, written);
37
			}
38
39
			this.deflater.reset();
40
		}
41
	}
42
43
	public int getThreshold() {
44
		return this.threshold;
45
	}
46
47
	public void setThreshold(final int threshold) {
48
		this.threshold = threshold;
49
	}
50
}
51

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