package io.netty.handler.codec.json;

import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.CorruptedFrameException;
import io.netty.handler.codec.TooLongFrameException;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public class JsonObjectDecoder extends ByteToMessageDecoder {
    private static final int ST_CORRUPTED = -1;
    private static final int ST_DECODING_ARRAY_STREAM = 2;
    private static final int ST_DECODING_NORMAL = 1;
    private static final int ST_INIT = 0;
    private int idx;
    private boolean insideString;
    private int lastReaderIndex;
    private final int maxObjectLength;
    private int openBraces;
    private int state;
    private final boolean streamArrayElements;

    public JsonObjectDecoder() {
        this(1048576);
    }

    public JsonObjectDecoder(int i2) {
        this(i2, false);
    }

    public JsonObjectDecoder(int i2, boolean z2) {
        if (i2 < 1) {
            throw new IllegalArgumentException("maxObjectLength must be a positive int");
        }
        this.maxObjectLength = i2;
        this.streamArrayElements = z2;
    }

    public JsonObjectDecoder(boolean z2) {
        this(1048576, z2);
    }

    private void decodeByte(byte b2, ByteBuf byteBuf, int i2) {
        int i3;
        if ((b2 == 123 || b2 == 91) && !this.insideString) {
            i3 = this.openBraces + 1;
        } else {
            if ((b2 != 125 && b2 != 93) || this.insideString) {
                if (b2 == 34) {
                    if (!this.insideString) {
                        this.insideString = true;
                        return;
                    }
                    int i4 = 0;
                    for (int i5 = i2 - 1; i5 >= 0 && byteBuf.getByte(i5) == 92; i5--) {
                        i4++;
                    }
                    if (i4 % 2 == 0) {
                        this.insideString = false;
                        return;
                    }
                    return;
                }
                return;
            }
            i3 = this.openBraces - 1;
        }
        this.openBraces = i3;
    }

    private void initDecoding(byte b2) {
        this.openBraces = 1;
        if (b2 == 91 && this.streamArrayElements) {
            this.state = 2;
        } else {
            this.state = 1;
        }
    }

    private void reset() {
        this.insideString = false;
        this.state = 0;
        this.openBraces = 0;
    }

    @Override // io.netty.handler.codec.ByteToMessageDecoder
    public void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {
        int i2;
        if (this.state == -1) {
            byteBuf.skipBytes(byteBuf.readableBytes());
            return;
        }
        if (this.idx > byteBuf.readerIndex() && this.lastReaderIndex != byteBuf.readerIndex()) {
            this.idx = byteBuf.readerIndex();
            if (this.state == 2) {
                this.insideString = false;
                this.openBraces = 1;
            }
        }
        int i3 = this.idx;
        int iWriterIndex = byteBuf.writerIndex();
        if (iWriterIndex > this.maxObjectLength) {
            byteBuf.skipBytes(byteBuf.readableBytes());
            reset();
            StringBuilder sbF = a.F("object length exceeds ");
            sbF.append(this.maxObjectLength);
            sbF.append(": ");
            sbF.append(iWriterIndex);
            sbF.append(" bytes discarded");
            throw new TooLongFrameException(sbF.toString());
        }
        while (i3 < iWriterIndex) {
            byte b2 = byteBuf.getByte(i3);
            int i4 = this.state;
            if (i4 == 1) {
                decodeByte(b2, byteBuf, i3);
                if (this.openBraces == 0) {
                    int i5 = i3 + 1;
                    ByteBuf byteBufExtractObject = extractObject(channelHandlerContext, byteBuf, byteBuf.readerIndex(), i5 - byteBuf.readerIndex());
                    if (byteBufExtractObject != null) {
                        list.add(byteBufExtractObject);
                    }
                    byteBuf.readerIndex(i5);
                    reset();
                }
            } else if (i4 == 2) {
                decodeByte(b2, byteBuf, i3);
                if (!this.insideString && (((i2 = this.openBraces) == 1 && b2 == 44) || (i2 == 0 && b2 == 93))) {
                    for (int i6 = byteBuf.readerIndex(); Character.isWhitespace(byteBuf.getByte(i6)); i6++) {
                        byteBuf.skipBytes(1);
                    }
                    int i7 = i3 - 1;
                    while (i7 >= byteBuf.readerIndex() && Character.isWhitespace(byteBuf.getByte(i7))) {
                        i7--;
                    }
                    ByteBuf byteBufExtractObject2 = extractObject(channelHandlerContext, byteBuf, byteBuf.readerIndex(), (i7 + 1) - byteBuf.readerIndex());
                    if (byteBufExtractObject2 != null) {
                        list.add(byteBufExtractObject2);
                    }
                    byteBuf.readerIndex(i3 + 1);
                    if (b2 == 93) {
                        reset();
                    }
                }
            } else {
                if (b2 == 123 || b2 == 91) {
                    initDecoding(b2);
                    if (this.state == 2) {
                    }
                } else if (!Character.isWhitespace(b2)) {
                    this.state = -1;
                    StringBuilder sbG = a.G("invalid JSON received at byte position ", i3, ": ");
                    sbG.append(ByteBufUtil.hexDump(byteBuf));
                    throw new CorruptedFrameException(sbG.toString());
                }
                byteBuf.skipBytes(1);
            }
            i3++;
        }
        if (byteBuf.readableBytes() == 0) {
            this.idx = 0;
        } else {
            this.idx = i3;
        }
        this.lastReaderIndex = byteBuf.readerIndex();
    }

    public ByteBuf extractObject(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, int i2, int i3) {
        return byteBuf.retainedSlice(i2, i3);
    }
}
