package io.netty.handler.codec;

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

/* JADX INFO: loaded from: classes.dex */
public class DelimiterBasedFrameDecoder extends ByteToMessageDecoder {
    private final ByteBuf[] delimiters;
    private boolean discardingTooLongFrame;
    private final boolean failFast;
    private final LineBasedFrameDecoder lineBasedDecoder;
    private final int maxFrameLength;
    private final boolean stripDelimiter;
    private int tooLongFrameLength;

    public DelimiterBasedFrameDecoder(int i2, ByteBuf byteBuf) {
        this(i2, true, byteBuf);
    }

    private void fail(long j2) {
        if (j2 <= 0) {
            throw new TooLongFrameException("frame length exceeds " + this.maxFrameLength + " - discarding");
        }
        throw new TooLongFrameException("frame length exceeds " + this.maxFrameLength + ": " + j2 + " - discarded");
    }

    private static int indexOf(ByteBuf byteBuf, ByteBuf byteBuf2) {
        for (int i2 = byteBuf.readerIndex(); i2 < byteBuf.writerIndex(); i2++) {
            int i3 = 0;
            int i4 = i2;
            while (i3 < byteBuf2.capacity() && byteBuf.getByte(i4) == byteBuf2.getByte(i3)) {
                i4++;
                if (i4 == byteBuf.writerIndex() && i3 != byteBuf2.capacity() - 1) {
                    return -1;
                }
                i3++;
            }
            if (i3 == byteBuf2.capacity()) {
                return i2 - byteBuf.readerIndex();
            }
        }
        return -1;
    }

    private static boolean isLineBased(ByteBuf[] byteBufArr) {
        if (byteBufArr.length != 2) {
            return false;
        }
        ByteBuf byteBuf = byteBufArr[0];
        ByteBuf byteBuf2 = byteBufArr[1];
        if (byteBuf.capacity() < byteBuf2.capacity()) {
            byteBuf = byteBufArr[1];
            byteBuf2 = byteBufArr[0];
        }
        return byteBuf.capacity() == 2 && byteBuf2.capacity() == 1 && byteBuf.getByte(0) == 13 && byteBuf.getByte(1) == 10 && byteBuf2.getByte(0) == 10;
    }

    private boolean isSubclass() {
        return DelimiterBasedFrameDecoder.class != DelimiterBasedFrameDecoder.class;
    }

    private static void validateDelimiter(ByteBuf byteBuf) {
        if (byteBuf == null) {
            throw new NullPointerException("delimiter");
        }
        if (!byteBuf.isReadable()) {
            throw new IllegalArgumentException("empty delimiter");
        }
    }

    private static void validateMaxFrameLength(int i2) {
        if (i2 > 0) {
            return;
        }
        throw new IllegalArgumentException("maxFrameLength must be a positive integer: " + i2);
    }

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

    public DelimiterBasedFrameDecoder(int i2, boolean z, ByteBuf byteBuf) {
        this(i2, z, true, byteBuf);
    }

    public DelimiterBasedFrameDecoder(int i2, boolean z, boolean z2, ByteBuf byteBuf) {
        this(i2, z, z2, byteBuf.slice(byteBuf.readerIndex(), byteBuf.readableBytes()));
    }

    protected Object decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
        LineBasedFrameDecoder lineBasedFrameDecoder = this.lineBasedDecoder;
        if (lineBasedFrameDecoder != null) {
            return lineBasedFrameDecoder.decode(channelHandlerContext, byteBuf);
        }
        int i2 = Integer.MAX_VALUE;
        ByteBuf byteBuf2 = null;
        for (ByteBuf byteBuf3 : this.delimiters) {
            int iIndexOf = indexOf(byteBuf, byteBuf3);
            if (iIndexOf >= 0 && iIndexOf < i2) {
                byteBuf2 = byteBuf3;
                i2 = iIndexOf;
            }
        }
        if (byteBuf2 != null) {
            int iCapacity = byteBuf2.capacity();
            if (this.discardingTooLongFrame) {
                this.discardingTooLongFrame = false;
                byteBuf.skipBytes(i2 + iCapacity);
                int i3 = this.tooLongFrameLength;
                this.tooLongFrameLength = 0;
                if (!this.failFast) {
                    fail(i3);
                }
                return null;
            }
            if (i2 > this.maxFrameLength) {
                byteBuf.skipBytes(iCapacity + i2);
                fail(i2);
                return null;
            }
            if (this.stripDelimiter) {
                ByteBuf retainedSlice = byteBuf.readRetainedSlice(i2);
                byteBuf.skipBytes(iCapacity);
                return retainedSlice;
            }
            return byteBuf.readRetainedSlice(i2 + iCapacity);
        }
        if (!this.discardingTooLongFrame) {
            if (byteBuf.readableBytes() > this.maxFrameLength) {
                this.tooLongFrameLength = byteBuf.readableBytes();
                byteBuf.skipBytes(byteBuf.readableBytes());
                this.discardingTooLongFrame = true;
                if (this.failFast) {
                    fail(this.tooLongFrameLength);
                }
            }
        } else {
            this.tooLongFrameLength += byteBuf.readableBytes();
            byteBuf.skipBytes(byteBuf.readableBytes());
        }
        return null;
    }

    public DelimiterBasedFrameDecoder(int i2, ByteBuf... byteBufArr) {
        this(i2, true, byteBufArr);
    }

    public DelimiterBasedFrameDecoder(int i2, boolean z, ByteBuf... byteBufArr) {
        this(i2, z, true, byteBufArr);
    }

    public DelimiterBasedFrameDecoder(int i2, boolean z, boolean z2, ByteBuf... byteBufArr) {
        validateMaxFrameLength(i2);
        if (byteBufArr != null) {
            if (byteBufArr.length != 0) {
                if (isLineBased(byteBufArr) && !isSubclass()) {
                    this.lineBasedDecoder = new LineBasedFrameDecoder(i2, z, z2);
                    this.delimiters = null;
                } else {
                    this.delimiters = new ByteBuf[byteBufArr.length];
                    for (int i3 = 0; i3 < byteBufArr.length; i3++) {
                        ByteBuf byteBuf = byteBufArr[i3];
                        validateDelimiter(byteBuf);
                        this.delimiters[i3] = byteBuf.slice(byteBuf.readerIndex(), byteBuf.readableBytes());
                    }
                    this.lineBasedDecoder = null;
                }
                this.maxFrameLength = i2;
                this.stripDelimiter = z;
                this.failFast = z2;
                return;
            }
            throw new IllegalArgumentException("empty delimiters");
        }
        throw new NullPointerException("delimiters");
    }
}
