package io.netty.handler.codec;

import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.ByteProcessor;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public class LineBasedFrameDecoder extends ByteToMessageDecoder {
    private int discardedBytes;
    private boolean discarding;
    private final boolean failFast;
    private final int maxLength;
    private int offset;
    private final boolean stripDelimiter;

    public LineBasedFrameDecoder(int i2) {
        this(i2, true, false);
    }

    public LineBasedFrameDecoder(int i2, boolean z2, boolean z3) {
        this.maxLength = i2;
        this.failFast = z3;
        this.stripDelimiter = z2;
    }

    private void fail(ChannelHandlerContext channelHandlerContext, int i2) {
        fail(channelHandlerContext, String.valueOf(i2));
    }

    private void fail(ChannelHandlerContext channelHandlerContext, String str) {
        channelHandlerContext.fireExceptionCaught((Throwable) new TooLongFrameException(a.w(a.K("frame length (", str, ") exceeds the allowed maximum ("), this.maxLength, ')')));
    }

    private int findEndOfLine(ByteBuf byteBuf) {
        int i2 = byteBuf.readableBytes();
        int i3 = byteBuf.readerIndex();
        int i4 = this.offset;
        int iForEachByte = byteBuf.forEachByte(i3 + i4, i2 - i4, ByteProcessor.FIND_LF);
        if (iForEachByte >= 0) {
            this.offset = 0;
            return (iForEachByte <= 0 || byteBuf.getByte(iForEachByte + (-1)) != 13) ? iForEachByte : iForEachByte - 1;
        }
        this.offset = i2;
        return iForEachByte;
    }

    public Object decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) {
        int iFindEndOfLine = findEndOfLine(byteBuf);
        if (this.discarding) {
            if (iFindEndOfLine >= 0) {
                int i2 = (this.discardedBytes + iFindEndOfLine) - byteBuf.readerIndex();
                byteBuf.readerIndex(iFindEndOfLine + (byteBuf.getByte(iFindEndOfLine) != 13 ? 1 : 2));
                this.discardedBytes = 0;
                this.discarding = false;
                if (!this.failFast) {
                    fail(channelHandlerContext, i2);
                }
            } else {
                this.discardedBytes = byteBuf.readableBytes() + this.discardedBytes;
                byteBuf.readerIndex(byteBuf.writerIndex());
            }
            return null;
        }
        if (iFindEndOfLine >= 0) {
            int i3 = iFindEndOfLine - byteBuf.readerIndex();
            int i4 = byteBuf.getByte(iFindEndOfLine) != 13 ? 1 : 2;
            if (i3 > this.maxLength) {
                byteBuf.readerIndex(iFindEndOfLine + i4);
                fail(channelHandlerContext, i3);
                return null;
            }
            if (!this.stripDelimiter) {
                return byteBuf.readRetainedSlice(i3 + i4);
            }
            ByteBuf retainedSlice = byteBuf.readRetainedSlice(i3);
            byteBuf.skipBytes(i4);
            return retainedSlice;
        }
        int i5 = byteBuf.readableBytes();
        if (i5 > this.maxLength) {
            this.discardedBytes = i5;
            byteBuf.readerIndex(byteBuf.writerIndex());
            this.discarding = true;
            this.offset = 0;
            if (this.failFast) {
                StringBuilder sbF = a.F("over ");
                sbF.append(this.discardedBytes);
                fail(channelHandlerContext, sbF.toString());
            }
        }
        return null;
    }

    @Override // io.netty.handler.codec.ByteToMessageDecoder
    public final void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {
        Object objDecode = decode(channelHandlerContext, byteBuf);
        if (objDecode != null) {
            list.add(objDecode);
        }
    }
}
