package io.netty.handler.codec;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.Signal;
import io.netty.util.internal.StringUtil;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public abstract class ReplayingDecoder<S> extends ByteToMessageDecoder {
    public static final Signal REPLAY = Signal.valueOf(ReplayingDecoder.class, "REPLAY");
    private int checkpoint;
    private final ReplayingDecoderByteBuf replayable;
    private S state;

    public ReplayingDecoder() {
        this(null);
    }

    public ReplayingDecoder(S s2) {
        this.replayable = new ReplayingDecoderByteBuf();
        this.checkpoint = -1;
        this.state = s2;
    }

    @Override // io.netty.handler.codec.ByteToMessageDecoder
    public void callDecode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {
        int i2;
        this.replayable.setCumulation(byteBuf);
        while (byteBuf.isReadable()) {
            try {
                int i3 = byteBuf.readerIndex();
                this.checkpoint = i3;
                int size = list.size();
                if (size > 0) {
                    ByteToMessageDecoder.fireChannelRead(channelHandlerContext, list, size);
                    list.clear();
                    if (channelHandlerContext.isRemoved()) {
                        return;
                    } else {
                        size = 0;
                    }
                }
                S s2 = this.state;
                int i4 = byteBuf.readableBytes();
                try {
                    decodeRemovalReentryProtection(channelHandlerContext, this.replayable, list);
                    if (channelHandlerContext.isRemoved()) {
                        return;
                    }
                    if (size != list.size()) {
                        if (i3 == byteBuf.readerIndex() && s2 == this.state) {
                            throw new DecoderException(StringUtil.simpleClassName(getClass()) + ".decode() method must consume the inbound data or change its state if it decoded something.");
                        }
                        if (isSingleDecode()) {
                            return;
                        }
                    } else if (i4 == byteBuf.readableBytes() && s2 == this.state) {
                        throw new DecoderException(StringUtil.simpleClassName(getClass()) + ".decode() must consume the inbound data or change its state if it did not decode anything.");
                    }
                } catch (Signal e2) {
                    e2.expect(REPLAY);
                    if (!channelHandlerContext.isRemoved() && (i2 = this.checkpoint) >= 0) {
                        byteBuf.readerIndex(i2);
                        return;
                    }
                    return;
                }
            } catch (DecoderException e3) {
                throw e3;
            } catch (Exception e4) {
                throw new DecoderException(e4);
            }
        }
    }

    @Override // io.netty.handler.codec.ByteToMessageDecoder
    public final void channelInputClosed(ChannelHandlerContext channelHandlerContext, List<Object> list) {
        ReplayingDecoderByteBuf replayingDecoderByteBuf;
        try {
            this.replayable.terminate();
            if (this.cumulation != null) {
                callDecode(channelHandlerContext, internalBuffer(), list);
                replayingDecoderByteBuf = this.replayable;
            } else {
                this.replayable.setCumulation(Unpooled.EMPTY_BUFFER);
                replayingDecoderByteBuf = this.replayable;
            }
            decodeLast(channelHandlerContext, replayingDecoderByteBuf, list);
        } catch (Signal e2) {
            e2.expect(REPLAY);
        }
    }

    public void checkpoint() {
        this.checkpoint = internalBuffer().readerIndex();
    }

    public void checkpoint(S s2) {
        checkpoint();
        state(s2);
    }

    public S state() {
        return this.state;
    }

    public S state(S s2) {
        S s3 = this.state;
        this.state = s2;
        return s3;
    }
}
