package io.netty.handler.codec;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.socket.ChannelInputShutdownEvent;
import io.netty.util.internal.StringUtil;
import java.util.List;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public abstract class ByteToMessageDecoder extends ChannelInboundHandlerAdapter {
    private static final byte STATE_CALLING_CHILD_DECODE = 1;
    private static final byte STATE_HANDLER_REMOVED_PENDING = 2;
    private static final byte STATE_INIT = 0;
    public ByteBuf cumulation;
    private boolean decodeWasNull;
    private boolean first;
    private int numReads;
    private boolean singleDecode;
    public static final Cumulator MERGE_CUMULATOR = new Cumulator() { // from class: io.netty.handler.codec.ByteToMessageDecoder.1
        @Override // io.netty.handler.codec.ByteToMessageDecoder.Cumulator
        public ByteBuf cumulate(ByteBufAllocator byteBufAllocator, ByteBuf byteBuf, ByteBuf byteBuf2) {
            if (byteBuf.writerIndex() > byteBuf.maxCapacity() - byteBuf2.readableBytes() || byteBuf.refCnt() > 1 || byteBuf.isReadOnly()) {
                byteBuf = ByteToMessageDecoder.expandCumulation(byteBufAllocator, byteBuf, byteBuf2.readableBytes());
            }
            byteBuf.writeBytes(byteBuf2);
            byteBuf2.release();
            return byteBuf;
        }
    };
    public static final Cumulator COMPOSITE_CUMULATOR = new Cumulator() { // from class: io.netty.handler.codec.ByteToMessageDecoder.2
        @Override // io.netty.handler.codec.ByteToMessageDecoder.Cumulator
        public ByteBuf cumulate(ByteBufAllocator byteBufAllocator, ByteBuf byteBuf, ByteBuf byteBuf2) {
            CompositeByteBuf compositeByteBufCompositeBuffer;
            if (byteBuf.refCnt() > 1) {
                ByteBuf byteBufExpandCumulation = ByteToMessageDecoder.expandCumulation(byteBufAllocator, byteBuf, byteBuf2.readableBytes());
                byteBufExpandCumulation.writeBytes(byteBuf2);
                byteBuf2.release();
                return byteBufExpandCumulation;
            }
            if (byteBuf instanceof CompositeByteBuf) {
                compositeByteBufCompositeBuffer = (CompositeByteBuf) byteBuf;
            } else {
                compositeByteBufCompositeBuffer = byteBufAllocator.compositeBuffer(Integer.MAX_VALUE);
                compositeByteBufCompositeBuffer.addComponent(true, byteBuf);
            }
            compositeByteBufCompositeBuffer.addComponent(true, byteBuf2);
            return compositeByteBufCompositeBuffer;
        }
    };
    private Cumulator cumulator = MERGE_CUMULATOR;
    private byte decodeState = 0;
    private int discardAfterReads = 16;

    public interface Cumulator {
        ByteBuf cumulate(ByteBufAllocator byteBufAllocator, ByteBuf byteBuf, ByteBuf byteBuf2);
    }

    public ByteToMessageDecoder() {
        ensureNotSharable();
    }

    private void channelInputClosed(ChannelHandlerContext channelHandlerContext, boolean z2) {
        CodecOutputList codecOutputListNewInstance = CodecOutputList.newInstance();
        try {
            try {
                channelInputClosed(channelHandlerContext, codecOutputListNewInstance);
                try {
                    ByteBuf byteBuf = this.cumulation;
                    if (byteBuf != null) {
                        byteBuf.release();
                        this.cumulation = null;
                    }
                    int size = codecOutputListNewInstance.size();
                    fireChannelRead(channelHandlerContext, codecOutputListNewInstance, size);
                    if (size > 0) {
                        channelHandlerContext.fireChannelReadComplete();
                    }
                    if (z2) {
                        channelHandlerContext.fireChannelInactive();
                    }
                } finally {
                }
            } catch (DecoderException e2) {
                throw e2;
            } catch (Exception e3) {
                throw new DecoderException(e3);
            }
        } catch (Throwable th) {
            try {
                ByteBuf byteBuf2 = this.cumulation;
                if (byteBuf2 != null) {
                    byteBuf2.release();
                    this.cumulation = null;
                }
                int size2 = codecOutputListNewInstance.size();
                fireChannelRead(channelHandlerContext, codecOutputListNewInstance, size2);
                if (size2 > 0) {
                    channelHandlerContext.fireChannelReadComplete();
                }
                if (z2) {
                    channelHandlerContext.fireChannelInactive();
                }
                throw th;
            } finally {
            }
        }
    }

    public static ByteBuf expandCumulation(ByteBufAllocator byteBufAllocator, ByteBuf byteBuf, int i2) {
        ByteBuf byteBufBuffer = byteBufAllocator.buffer(byteBuf.readableBytes() + i2);
        byteBufBuffer.writeBytes(byteBuf);
        byteBuf.release();
        return byteBufBuffer;
    }

    public static void fireChannelRead(ChannelHandlerContext channelHandlerContext, CodecOutputList codecOutputList, int i2) {
        for (int i3 = 0; i3 < i2; i3++) {
            channelHandlerContext.fireChannelRead(codecOutputList.getUnsafe(i3));
        }
    }

    public static void fireChannelRead(ChannelHandlerContext channelHandlerContext, List<Object> list, int i2) {
        if (list instanceof CodecOutputList) {
            fireChannelRead(channelHandlerContext, (CodecOutputList) list, i2);
            return;
        }
        for (int i3 = 0; i3 < i2; i3++) {
            channelHandlerContext.fireChannelRead(list.get(i3));
        }
    }

    public int actualReadableBytes() {
        return internalBuffer().readableBytes();
    }

    public void callDecode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {
        while (byteBuf.isReadable()) {
            try {
                int size = list.size();
                if (size > 0) {
                    fireChannelRead(channelHandlerContext, list, size);
                    list.clear();
                    if (channelHandlerContext.isRemoved()) {
                        return;
                    } else {
                        size = 0;
                    }
                }
                int i2 = byteBuf.readableBytes();
                decodeRemovalReentryProtection(channelHandlerContext, byteBuf, list);
                if (channelHandlerContext.isRemoved()) {
                    return;
                }
                if (size == list.size()) {
                    if (i2 == byteBuf.readableBytes()) {
                        return;
                    }
                } else {
                    if (i2 == byteBuf.readableBytes()) {
                        throw new DecoderException(StringUtil.simpleClassName(getClass()) + ".decode() did not read anything but decoded a message.");
                    }
                    if (isSingleDecode()) {
                        return;
                    }
                }
            } catch (DecoderException e2) {
                throw e2;
            } catch (Exception e3) {
                throw new DecoderException(e3);
            }
        }
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelInactive(ChannelHandlerContext channelHandlerContext) {
        channelInputClosed(channelHandlerContext, true);
    }

    public void channelInputClosed(ChannelHandlerContext channelHandlerContext, List<Object> list) {
        ByteBuf byteBuf;
        ByteBuf byteBuf2 = this.cumulation;
        if (byteBuf2 != null) {
            callDecode(channelHandlerContext, byteBuf2, list);
            byteBuf = this.cumulation;
        } else {
            byteBuf = Unpooled.EMPTY_BUFFER;
        }
        decodeLast(channelHandlerContext, byteBuf, list);
    }

    /* JADX WARN: Finally extract failed */
    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelRead(ChannelHandlerContext channelHandlerContext, Object obj) {
        if (!(obj instanceof ByteBuf)) {
            channelHandlerContext.fireChannelRead(obj);
            return;
        }
        CodecOutputList codecOutputListNewInstance = CodecOutputList.newInstance();
        try {
            try {
                try {
                    ByteBuf byteBufCumulate = (ByteBuf) obj;
                    boolean z2 = this.cumulation == null;
                    this.first = z2;
                    if (!z2) {
                        byteBufCumulate = this.cumulator.cumulate(channelHandlerContext.alloc(), this.cumulation, byteBufCumulate);
                    }
                    this.cumulation = byteBufCumulate;
                    callDecode(channelHandlerContext, this.cumulation, codecOutputListNewInstance);
                    ByteBuf byteBuf = this.cumulation;
                    if (byteBuf == null || byteBuf.isReadable()) {
                        int i2 = this.numReads + 1;
                        this.numReads = i2;
                        if (i2 >= this.discardAfterReads) {
                            this.numReads = 0;
                            discardSomeReadBytes();
                        }
                    } else {
                        this.numReads = 0;
                        this.cumulation.release();
                        this.cumulation = null;
                    }
                    int size = codecOutputListNewInstance.size();
                    this.decodeWasNull = !codecOutputListNewInstance.insertSinceRecycled();
                    fireChannelRead(channelHandlerContext, codecOutputListNewInstance, size);
                    codecOutputListNewInstance.recycle();
                } catch (DecoderException e2) {
                    throw e2;
                }
            } catch (Exception e3) {
                throw new DecoderException(e3);
            }
        } catch (Throwable th) {
            ByteBuf byteBuf2 = this.cumulation;
            if (byteBuf2 == null || byteBuf2.isReadable()) {
                int i3 = this.numReads + 1;
                this.numReads = i3;
                if (i3 >= this.discardAfterReads) {
                    this.numReads = 0;
                    discardSomeReadBytes();
                }
            } else {
                this.numReads = 0;
                this.cumulation.release();
                this.cumulation = null;
            }
            int size2 = codecOutputListNewInstance.size();
            this.decodeWasNull = !codecOutputListNewInstance.insertSinceRecycled();
            fireChannelRead(channelHandlerContext, codecOutputListNewInstance, size2);
            codecOutputListNewInstance.recycle();
            throw th;
        }
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelReadComplete(ChannelHandlerContext channelHandlerContext) {
        this.numReads = 0;
        discardSomeReadBytes();
        if (this.decodeWasNull) {
            this.decodeWasNull = false;
            if (!channelHandlerContext.channel().config().isAutoRead()) {
                channelHandlerContext.read();
            }
        }
        channelHandlerContext.fireChannelReadComplete();
    }

    public abstract void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list);

    public void decodeLast(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {
        if (byteBuf.isReadable()) {
            decodeRemovalReentryProtection(channelHandlerContext, byteBuf, list);
        }
    }

    public final void decodeRemovalReentryProtection(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {
        this.decodeState = (byte) 1;
        try {
            decode(channelHandlerContext, byteBuf, list);
        } finally {
            b = this.decodeState != 2 ? (byte) 0 : (byte) 1;
            this.decodeState = (byte) 0;
            if (b != 0) {
                handlerRemoved(channelHandlerContext);
            }
        }
    }

    public final void discardSomeReadBytes() {
        ByteBuf byteBuf = this.cumulation;
        if (byteBuf == null || this.first || byteBuf.refCnt() != 1) {
            return;
        }
        this.cumulation.discardSomeReadBytes();
    }

    @Override // io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler
    public final void handlerRemoved(ChannelHandlerContext channelHandlerContext) {
        if (this.decodeState == 1) {
            this.decodeState = (byte) 2;
            return;
        }
        ByteBuf byteBuf = this.cumulation;
        if (byteBuf != null) {
            this.cumulation = null;
            int i2 = byteBuf.readableBytes();
            if (i2 > 0) {
                ByteBuf bytes = byteBuf.readBytes(i2);
                byteBuf.release();
                channelHandlerContext.fireChannelRead((Object) bytes);
            } else {
                byteBuf.release();
            }
            this.numReads = 0;
            channelHandlerContext.fireChannelReadComplete();
        }
        handlerRemoved0(channelHandlerContext);
    }

    public void handlerRemoved0(ChannelHandlerContext channelHandlerContext) {
    }

    public ByteBuf internalBuffer() {
        ByteBuf byteBuf = this.cumulation;
        return byteBuf != null ? byteBuf : Unpooled.EMPTY_BUFFER;
    }

    public boolean isSingleDecode() {
        return this.singleDecode;
    }

    public void setCumulator(Cumulator cumulator) {
        Objects.requireNonNull(cumulator, "cumulator");
        this.cumulator = cumulator;
    }

    public void setDiscardAfterReads(int i2) {
        if (i2 <= 0) {
            throw new IllegalArgumentException("discardAfterReads must be > 0");
        }
        this.discardAfterReads = i2;
    }

    public void setSingleDecode(boolean z2) {
        this.singleDecode = z2;
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void userEventTriggered(ChannelHandlerContext channelHandlerContext, Object obj) {
        if (obj instanceof ChannelInputShutdownEvent) {
            channelInputClosed(channelHandlerContext, false);
        }
        super.userEventTriggered(channelHandlerContext, obj);
    }
}
