package io.netty.handler.codec.http.websocketx;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import io.netty.handler.codec.TooLongFrameException;
import io.netty.handler.codec.memcache.binary.DefaultBinaryMemcacheRequest;
import java.util.List;
import kotlinx.serialization.json.internal.AbstractJsonLexerKt;

/* JADX INFO: loaded from: classes.dex */
public class WebSocket00FrameDecoder extends ReplayingDecoder<Void> implements WebSocketFrameDecoder {
    public static final int DEFAULT_MAX_FRAME_SIZE = 16384;
    private final long maxFrameSize;
    private boolean receivedClosingHandshake;

    public WebSocket00FrameDecoder() {
        this(16384);
    }

    public WebSocket00FrameDecoder(int i2) {
        this.maxFrameSize = i2;
    }

    private WebSocketFrame decodeBinaryFrame(ChannelHandlerContext channelHandlerContext, byte b2, ByteBuf byteBuf) {
        byte b3;
        int i2 = 0;
        long j2 = 0;
        do {
            b3 = byteBuf.readByte();
            j2 = (j2 << 7) | ((long) (b3 & AbstractJsonLexerKt.TC_INVALID));
            if (j2 > this.maxFrameSize) {
                throw new TooLongFrameException();
            }
            i2++;
            if (i2 > 8) {
                throw new TooLongFrameException();
            }
        } while ((b3 & DefaultBinaryMemcacheRequest.REQUEST_MAGIC_BYTE) == 128);
        if (b2 != -1 || j2 != 0) {
            return new BinaryWebSocketFrame(ByteBufUtil.readBytes(channelHandlerContext.alloc(), byteBuf, (int) j2));
        }
        this.receivedClosingHandshake = true;
        return new CloseWebSocketFrame();
    }

    private WebSocketFrame decodeTextFrame(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) {
        int i2 = byteBuf.readerIndex();
        int iActualReadableBytes = actualReadableBytes();
        int iIndexOf = byteBuf.indexOf(i2, i2 + iActualReadableBytes, (byte) -1);
        if (iIndexOf == -1) {
            if (iActualReadableBytes <= this.maxFrameSize) {
                return null;
            }
            throw new TooLongFrameException();
        }
        int i3 = iIndexOf - i2;
        if (i3 > this.maxFrameSize) {
            throw new TooLongFrameException();
        }
        ByteBuf bytes = ByteBufUtil.readBytes(channelHandlerContext.alloc(), byteBuf, i3);
        byteBuf.skipBytes(1);
        if (bytes.indexOf(bytes.readerIndex(), bytes.writerIndex(), (byte) -1) < 0) {
            return new TextWebSocketFrame(bytes);
        }
        bytes.release();
        throw new IllegalArgumentException("a text frame should not contain 0xFF.");
    }

    @Override // io.netty.handler.codec.ByteToMessageDecoder
    public void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {
        if (this.receivedClosingHandshake) {
            byteBuf.skipBytes(actualReadableBytes());
            return;
        }
        byte b2 = byteBuf.readByte();
        WebSocketFrame webSocketFrameDecodeBinaryFrame = (b2 & DefaultBinaryMemcacheRequest.REQUEST_MAGIC_BYTE) == 128 ? decodeBinaryFrame(channelHandlerContext, b2, byteBuf) : decodeTextFrame(channelHandlerContext, byteBuf);
        if (webSocketFrameDecodeBinaryFrame != null) {
            list.add(webSocketFrameDecodeBinaryFrame);
        }
    }
}
