package net.minecraft.network;

import io.netty.buffer.Unpooled;
import io.netty.handler.codec.DecoderException;
import java.util.List;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import java.util.zip.Inflater;
import io.netty.handler.codec.ByteToMessageDecoder;

public class CompressionDecoder extends ByteToMessageDecoder {
    private final Inflater inflater;
    private int threshold;
    
    public CompressionDecoder(final int integer) {
        this.threshold = integer;
        this.inflater = new Inflater();
    }
    
    protected void decode(final ChannelHandlerContext channelHandlerContext, final ByteBuf byteBuf, final List<Object> list) throws Exception {
        if (byteBuf.readableBytes() == 0) {
            return;
        }
        final FriendlyByteBuf mg5 = new FriendlyByteBuf(byteBuf);
        final int integer6 = mg5.readVarInt();
        if (integer6 == 0) {
            list.add(mg5.readBytes(mg5.readableBytes()));
        }
        else {
            if (integer6 < this.threshold) {
                throw new DecoderException("Badly compressed packet - size of " + integer6 + " is below server threshold of " + this.threshold);
            }
            if (integer6 > 2097152) {
                throw new DecoderException("Badly compressed packet - size of " + integer6 + " is larger than protocol maximum of " + 2097152);
            }
            final byte[] arr7 = new byte[mg5.readableBytes()];
            mg5.readBytes(arr7);
            this.inflater.setInput(arr7);
            final byte[] arr8 = new byte[integer6];
            this.inflater.inflate(arr8);
            list.add(Unpooled.wrappedBuffer(arr8));
            this.inflater.reset();
        }
    }
    
    public void setThreshold(final int integer) {
        this.threshold = integer;
    }
}
