package io.netty.handler.codec;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty.buffer.CompositeByteBuf;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.util.ReferenceCountUtil;

/* JADX INFO: loaded from: classes.dex */
public abstract class MessageAggregator<I, S, C extends ByteBufHolder, O extends ByteBufHolder> extends MessageToMessageDecoder<I> {
    private static final int DEFAULT_MAX_COMPOSITEBUFFER_COMPONENTS = 1024;
    private ChannelFutureListener continueResponseWriteListener;
    private ChannelHandlerContext ctx;
    private O currentMessage;
    private boolean handlingOversizedMessage;
    private final int maxContentLength;
    private int maxCumulationBufferComponents;

    protected MessageAggregator(int i2) {
        this.maxCumulationBufferComponents = 1024;
        validateMaxContentLength(i2);
        this.maxContentLength = i2;
    }

    private static void appendPartialContent(CompositeByteBuf compositeByteBuf, ByteBuf byteBuf) {
        if (byteBuf.isReadable()) {
            byteBuf.retain();
            compositeByteBuf.addComponent(true, byteBuf);
        }
    }

    private void invokeHandleOversizedMessage(ChannelHandlerContext channelHandlerContext, S s) throws Exception {
        this.handlingOversizedMessage = true;
        this.currentMessage = null;
        try {
            handleOversizedMessage(channelHandlerContext, s);
        } finally {
            ReferenceCountUtil.release(s);
        }
    }

    private static void validateMaxContentLength(int i2) {
        if (i2 >= 0) {
            return;
        }
        throw new IllegalArgumentException("maxContentLength: " + i2 + " (expected: >= 0)");
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Override // io.netty.handler.codec.MessageToMessageDecoder
    public boolean acceptInboundMessage(Object obj) throws Exception {
        if (super.acceptInboundMessage(obj)) {
            return (isContentMessage(obj) || isStartMessage(obj)) && !isAggregated(obj);
        }
        return false;
    }

    protected void aggregate(O o, C c2) throws Exception {
    }

    protected abstract O beginAggregation(S s, ByteBuf byteBuf) throws Exception;

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelInactive(ChannelHandlerContext channelHandlerContext) throws Exception {
        O o = this.currentMessage;
        if (o != null) {
            o.release();
            this.currentMessage = null;
        }
        super.channelInactive(channelHandlerContext);
    }

    protected abstract boolean closeAfterContinueResponse(Object obj) throws Exception;

    protected final ChannelHandlerContext ctx() {
        ChannelHandlerContext channelHandlerContext = this.ctx;
        if (channelHandlerContext != null) {
            return channelHandlerContext;
        }
        throw new IllegalStateException("not added to a pipeline yet");
    }

    /* JADX WARN: Removed duplicated region for block: B:31:0x007f  */
    @Override // io.netty.handler.codec.MessageToMessageDecoder
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    protected void decode(final io.netty.channel.ChannelHandlerContext r9, I r10, java.util.List<java.lang.Object> r11) throws java.lang.Exception {
        /*
            Method dump skipped, instruction units count: 294
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: io.netty.handler.codec.MessageAggregator.decode(io.netty.channel.ChannelHandlerContext, java.lang.Object, java.util.List):void");
    }

    protected void finishAggregation(O o) throws Exception {
    }

    protected void handleOversizedMessage(ChannelHandlerContext channelHandlerContext, S s) throws Exception {
        channelHandlerContext.fireExceptionCaught((Throwable) new TooLongFrameException("content length exceeded " + maxContentLength() + " bytes."));
    }

    @Override // io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler
    public void handlerAdded(ChannelHandlerContext channelHandlerContext) throws Exception {
        this.ctx = channelHandlerContext;
    }

    @Override // io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler
    public void handlerRemoved(ChannelHandlerContext channelHandlerContext) throws Exception {
        super.handlerRemoved(channelHandlerContext);
        O o = this.currentMessage;
        if (o != null) {
            o.release();
            this.currentMessage = null;
        }
    }

    protected abstract boolean ignoreContentAfterContinueResponse(Object obj) throws Exception;

    protected abstract boolean isAggregated(I i2) throws Exception;

    protected abstract boolean isContentLengthInvalid(S s, int i2) throws Exception;

    protected abstract boolean isContentMessage(I i2) throws Exception;

    public final boolean isHandlingOversizedMessage() {
        return this.handlingOversizedMessage;
    }

    protected abstract boolean isLastContentMessage(C c2) throws Exception;

    protected abstract boolean isStartMessage(I i2) throws Exception;

    public final int maxContentLength() {
        return this.maxContentLength;
    }

    public final int maxCumulationBufferComponents() {
        return this.maxCumulationBufferComponents;
    }

    protected abstract Object newContinueResponse(S s, int i2, ChannelPipeline channelPipeline) throws Exception;

    public final void setMaxCumulationBufferComponents(int i2) {
        if (i2 >= 2) {
            if (this.ctx != null) {
                throw new IllegalStateException("decoder properties cannot be changed once the decoder is added to a pipeline.");
            }
            this.maxCumulationBufferComponents = i2;
        } else {
            throw new IllegalArgumentException("maxCumulationBufferComponents: " + i2 + " (expected: >= 2)");
        }
    }

    protected MessageAggregator(int i2, Class<? extends I> cls) {
        super(cls);
        this.maxCumulationBufferComponents = 1024;
        validateMaxContentLength(i2);
        this.maxContentLength = i2;
    }
}
