package io.netty.handler.codec;

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

/* 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);
    }

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

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

    public DelimiterBasedFrameDecoder(int i2, boolean z2, boolean z3, ByteBuf... byteBufArr) {
        validateMaxFrameLength(i2);
        Objects.requireNonNull(byteBufArr, "delimiters");
        if (byteBufArr.length == 0) {
            throw new IllegalArgumentException("empty delimiters");
        }
        if (!isLineBased(byteBufArr) || isSubclass()) {
            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;
        } else {
            this.lineBasedDecoder = new LineBasedFrameDecoder(i2, z2, z3);
            this.delimiters = null;
        }
        this.maxFrameLength = i2;
        this.stripDelimiter = z2;
        this.failFast = z3;
    }

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

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

    private void fail(long j2) {
        if (j2 <= 0) {
            throw new TooLongFrameException(a.y(a.F("frame length exceeds "), this.maxFrameLength, " - discarding"));
        }
        StringBuilder sbF = a.F("frame length exceeds ");
        sbF.append(this.maxFrameLength);
        sbF.append(": ");
        sbF.append(j2);
        sbF.append(" - discarded");
        throw new TooLongFrameException(sbF.toString());
    }

    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 getClass() != DelimiterBasedFrameDecoder.class;
    }

    private static void validateDelimiter(ByteBuf byteBuf) {
        Objects.requireNonNull(byteBuf, "delimiter");
        if (!byteBuf.isReadable()) {
            throw new IllegalArgumentException("empty delimiter");
        }
    }

    private static void validateMaxFrameLength(int i2) {
        if (i2 <= 0) {
            throw new IllegalArgumentException(a.l("maxFrameLength must be a positive integer: ", i2));
        }
    }

    public Object decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) {
        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) {
            if (this.discardingTooLongFrame) {
                this.tooLongFrameLength = byteBuf.readableBytes() + this.tooLongFrameLength;
                byteBuf.skipBytes(byteBuf.readableBytes());
            } else if (byteBuf.readableBytes() > this.maxFrameLength) {
                this.tooLongFrameLength = byteBuf.readableBytes();
                byteBuf.skipBytes(byteBuf.readableBytes());
                this.discardingTooLongFrame = true;
                if (this.failFast) {
                    fail(this.tooLongFrameLength);
                }
            }
            return 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) {
            return byteBuf.readRetainedSlice(i2 + iCapacity);
        }
        ByteBuf retainedSlice = byteBuf.readRetainedSlice(i2);
        byteBuf.skipBytes(iCapacity);
        return retainedSlice;
    }

    @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);
        }
    }
}
