CompressionEncoder.java

net.minecraft.network.CompressionEncoder

信息

  • 全限定名:net.minecraft.network.CompressionEncoder
  • 类型:public class
  • 包:net.minecraft.network
  • 源码路径:src/main/java/net/minecraft/network/CompressionEncoder.java
  • 起始行号:L8
  • 继承:MessageToByteEncoder
  • 职责:

    TODO

字段/常量

  • encodeBuf

    • 类型: byte[]
    • 修饰符: private final
    • 源码定位: L9
    • 说明:

      TODO

  • deflater

    • 类型: Deflater
    • 修饰符: private final
    • 源码定位: L10
    • 说明:

      TODO

  • threshold

    • 类型: int
    • 修饰符: private
    • 源码定位: L11
    • 说明:

      TODO

内部类/嵌套类型

构造器

public CompressionEncoder(int threshold) @ L13

  • 构造器名:CompressionEncoder
  • 源码定位:L13
  • 修饰符:public

参数:

  • threshold: int

说明:

TODO

方法

下面的方法块按源码顺序生成。

protected void encode(ChannelHandlerContext ctx, ByteBuf uncompressed, ByteBuf out) @ L18

  • 方法名:encode
  • 源码定位:L18
  • 返回类型:void
  • 修饰符:protected

参数:

  • ctx: ChannelHandlerContext
  • uncompressed: ByteBuf
  • out: ByteBuf

说明:

TODO

public int getThreshold() @ L43

  • 方法名:getThreshold
  • 源码定位:L43
  • 返回类型:int
  • 修饰符:public

参数:

说明:

TODO

public void setThreshold(int threshold) @ L47

  • 方法名:setThreshold
  • 源码定位:L47
  • 返回类型:void
  • 修饰符:public

参数:

  • threshold: int

说明:

TODO

代码

public class CompressionEncoder extends MessageToByteEncoder<ByteBuf> {
    private final byte[] encodeBuf = new byte[8192];
    private final Deflater deflater;
    private int threshold;
 
    public CompressionEncoder(int threshold) {
        this.threshold = threshold;
        this.deflater = new Deflater();
    }
 
    protected void encode(ChannelHandlerContext ctx, ByteBuf uncompressed, ByteBuf out) {
        int uncompressedLength = uncompressed.readableBytes();
        if (uncompressedLength > 8388608) {
            throw new IllegalArgumentException("Packet too big (is " + uncompressedLength + ", should be less than 8388608)");
        } else {
            if (uncompressedLength < this.threshold) {
                VarInt.write(out, 0);
                out.writeBytes(uncompressed);
            } else {
                byte[] input = new byte[uncompressedLength];
                uncompressed.readBytes(input);
                VarInt.write(out, input.length);
                this.deflater.setInput(input, 0, uncompressedLength);
                this.deflater.finish();
 
                while (!this.deflater.finished()) {
                    int written = this.deflater.deflate(this.encodeBuf);
                    out.writeBytes(this.encodeBuf, 0, written);
                }
 
                this.deflater.reset();
            }
        }
    }
 
    public int getThreshold() {
        return this.threshold;
    }
 
    public void setThreshold(int threshold) {
        this.threshold = threshold;
    }
}

引用的其他类

  • VarInt
    • 引用位置: 方法调用
    • 关联成员: VarInt.write()