package io.netty.handler.codec;

import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.internal.ObjectUtil;
import java.nio.ByteOrder;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
@ChannelHandler.Sharable
public class LengthFieldPrepender extends MessageToMessageEncoder<ByteBuf> {
    private final ByteOrder byteOrder;
    private final int lengthAdjustment;
    private final int lengthFieldLength;
    private final boolean lengthIncludesLengthFieldLength;

    public LengthFieldPrepender(int i2) {
        this(i2, false);
    }

    public LengthFieldPrepender(int i2, int i3) {
        this(i2, i3, false);
    }

    public LengthFieldPrepender(int i2, int i3, boolean z2) {
        this(ByteOrder.BIG_ENDIAN, i2, i3, z2);
    }

    public LengthFieldPrepender(int i2, boolean z2) {
        this(i2, 0, z2);
    }

    public LengthFieldPrepender(ByteOrder byteOrder, int i2, int i3, boolean z2) {
        if (i2 != 1 && i2 != 2 && i2 != 3 && i2 != 4 && i2 != 8) {
            throw new IllegalArgumentException(a.l("lengthFieldLength must be either 1, 2, 3, 4, or 8: ", i2));
        }
        ObjectUtil.checkNotNull(byteOrder, "byteOrder");
        this.byteOrder = byteOrder;
        this.lengthFieldLength = i2;
        this.lengthIncludesLengthFieldLength = z2;
        this.lengthAdjustment = i3;
    }

    /* JADX INFO: renamed from: encode, reason: avoid collision after fix types in other method */
    public void encode2(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {
        ByteBuf byteBufWriteByte;
        int i2 = byteBuf.readableBytes() + this.lengthAdjustment;
        if (this.lengthIncludesLengthFieldLength) {
            i2 += this.lengthFieldLength;
        }
        if (i2 < 0) {
            throw new IllegalArgumentException(a.n("Adjusted frame length (", i2, ") is less than zero"));
        }
        int i3 = this.lengthFieldLength;
        if (i3 != 1) {
            if (i3 != 2) {
                if (i3 != 3) {
                    if (i3 == 4) {
                        byteBufWriteByte = channelHandlerContext.alloc().buffer(4).order(this.byteOrder).writeInt(i2);
                    } else {
                        if (i3 != 8) {
                            throw new Error("should not reach here");
                        }
                        byteBufWriteByte = channelHandlerContext.alloc().buffer(8).order(this.byteOrder).writeLong(i2);
                    }
                } else {
                    if (i2 >= 16777216) {
                        throw new IllegalArgumentException(a.l("length does not fit into a medium integer: ", i2));
                    }
                    byteBufWriteByte = channelHandlerContext.alloc().buffer(3).order(this.byteOrder).writeMedium(i2);
                }
            } else {
                if (i2 >= 65536) {
                    throw new IllegalArgumentException(a.l("length does not fit into a short integer: ", i2));
                }
                byteBufWriteByte = channelHandlerContext.alloc().buffer(2).order(this.byteOrder).writeShort((short) i2);
            }
        } else {
            if (i2 >= 256) {
                throw new IllegalArgumentException(a.l("length does not fit into a byte: ", i2));
            }
            byteBufWriteByte = channelHandlerContext.alloc().buffer(1).order(this.byteOrder).writeByte((byte) i2);
        }
        list.add(byteBufWriteByte);
        list.add(byteBuf.retain());
    }

    @Override // io.netty.handler.codec.MessageToMessageEncoder
    public /* bridge */ /* synthetic */ void encode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) {
        encode2(channelHandlerContext, byteBuf, (List<Object>) list);
    }
}
