From fe0d31b0f06cfbb3a9f1a55c3067919b8ccc7d93 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sat, 29 Aug 2020 19:11:23 -0400 Subject: [PATCH] Update comments in MinecraftCompressEncoder to be more accurate. --- .../protocol/netty/MinecraftCompressEncoder.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressEncoder.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressEncoder.java index 0c3bcc0f2..a3e36f4cc 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressEncoder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressEncoder.java @@ -38,9 +38,14 @@ public class MinecraftCompressEncoder extends MessageToByteEncoder { @Override protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect) throws Exception { - // Follow the advice of https://github.com/ebiggers/libdeflate/blob/master/libdeflate.h#L103 - // here for compression. The maximum buffer size if the data compresses well (which is almost - // always the case) is one less the input buffer. + // We allocate bytes to be compressed plus 1 byte. This covers two cases: + // + // - Compression + // According to https://github.com/ebiggers/libdeflate/blob/master/libdeflate.h#L103, + // if the data compresses well (and we do not have some pathological case) then the maximum + // size the compressed size will ever be is the input size minus one. + // - Uncompressed + // This is fairly obvious - we will then have one more than the uncompressed size. int initialBufferSize = msg.readableBytes() + 1; return MoreByteBufUtils.preferredBuffer(ctx.alloc(), compressor, initialBufferSize); }