package io.netty.handler.codec.json;

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 final int maxObjectLength;
    private int openBraces;
    private int state;
    private final boolean streamArrayElements;

    public JsonObjectDecoder() {
        this(1048576);
    }

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

    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
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        if (this.state == -1) {
            byteBuf.skipBytes(byteBuf.readableBytes());
            return;
        }
        int i2 = this.idx;
        int iWriterIndex = byteBuf.writerIndex();
        if (iWriterIndex > this.maxObjectLength) {
            byteBuf.skipBytes(byteBuf.readableBytes());
            reset();
            throw new TooLongFrameException("object length exceeds " + this.maxObjectLength + ": " + iWriterIndex + " bytes discarded");
        }
        while (i2 < iWriterIndex) {
            byte b2 = byteBuf.getByte(i2);
            int i3 = this.state;
            if (i3 == 1) {
                decodeByte(b2, byteBuf, i2);
                if (this.openBraces == 0) {
                    int i4 = i2 + 1;
                    ByteBuf byteBufExtractObject = extractObject(channelHandlerContext, byteBuf, byteBuf.readerIndex(), i4 - byteBuf.readerIndex());
                    if (byteBufExtractObject != null) {
                        list.add(byteBufExtractObject);
                    }
                    byteBuf.readerIndex(i4);
                    reset();
                }
            } else if (i3 == 2) {
                decodeByte(b2, byteBuf, i2);
                if (!this.insideString && ((this.openBraces == 1 && b2 == 44) || (this.openBraces == 0 && b2 == 93))) {
                    for (int i5 = byteBuf.readerIndex(); Character.isWhitespace(byteBuf.getByte(i5)); i5++) {
                        byteBuf.skipBytes(1);
                    }
                    int i6 = i2 - 1;
                    while (i6 >= byteBuf.readerIndex() && Character.isWhitespace(byteBuf.getByte(i6))) {
                        i6--;
                    }
                    ByteBuf byteBufExtractObject2 = extractObject(channelHandlerContext, byteBuf, byteBuf.readerIndex(), (i6 + 1) - byteBuf.readerIndex());
                    if (byteBufExtractObject2 != null) {
                        list.add(byteBufExtractObject2);
                    }
                    byteBuf.readerIndex(i2 + 1);
                    if (b2 == 93) {
                        reset();
                    }
                }
            } else if (b2 == 123 || b2 == 91) {
                initDecoding(b2);
                if (this.state == 2) {
                    byteBuf.skipBytes(1);
                }
            } else {
                if (!Character.isWhitespace(b2)) {
                    this.state = -1;
                    throw new CorruptedFrameException("invalid JSON received at byte position " + i2 + ": " + ByteBufUtil.hexDump(byteBuf));
                }
                byteBuf.skipBytes(1);
            }
            i2++;
        }
        if (byteBuf.readableBytes() == 0) {
            this.idx = 0;
        } else {
            this.idx = i2;
        }
    }

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

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

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

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