package io.netty.handler.codec;

import g.a.a.a.a;
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;

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

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

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

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

    private void releaseCurrentMessage() {
        O o2 = this.currentMessage;
        if (o2 != null) {
            o2.release();
            this.currentMessage = null;
            this.handlingOversizedMessage = false;
        }
    }

    private static void validateMaxContentLength(int i2) {
        if (i2 < 0) {
            throw new IllegalArgumentException(a.n("maxContentLength: ", i2, " (expected: >= 0)"));
        }
    }

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

    public void aggregate(O o2, C c2) {
    }

    public abstract O beginAggregation(S s2, ByteBuf byteBuf);

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelInactive(ChannelHandlerContext channelHandlerContext) {
        try {
            super.channelInactive(channelHandlerContext);
        } finally {
            releaseCurrentMessage();
        }
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelReadComplete(ChannelHandlerContext channelHandlerContext) {
        if (this.currentMessage != null && !channelHandlerContext.channel().config().isAutoRead()) {
            channelHandlerContext.read();
        }
        channelHandlerContext.fireChannelReadComplete();
    }

    public abstract boolean closeAfterContinueResponse(Object obj);

    public 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:55:0x0105  */
    @Override // io.netty.handler.codec.MessageToMessageDecoder
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void decode(final io.netty.channel.ChannelHandlerContext r6, I r7, java.util.List<java.lang.Object> r8) {
        /*
            Method dump skipped, instruction units count: 286
            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");
    }

    public void finishAggregation(O o2) {
    }

    public void handleOversizedMessage(ChannelHandlerContext channelHandlerContext, S s2) {
        StringBuilder sbF = a.F("content length exceeded ");
        sbF.append(maxContentLength());
        sbF.append(" bytes.");
        channelHandlerContext.fireExceptionCaught((Throwable) new TooLongFrameException(sbF.toString()));
    }

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

    @Override // io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler
    public void handlerRemoved(ChannelHandlerContext channelHandlerContext) {
        try {
            super.handlerRemoved(channelHandlerContext);
        } finally {
            releaseCurrentMessage();
        }
    }

    public abstract boolean ignoreContentAfterContinueResponse(Object obj);

    public abstract boolean isAggregated(I i2);

    public abstract boolean isContentLengthInvalid(S s2, int i2);

    public abstract boolean isContentMessage(I i2);

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

    public abstract boolean isLastContentMessage(C c2);

    public abstract boolean isStartMessage(I i2);

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

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

    public abstract Object newContinueResponse(S s2, int i2, ChannelPipeline channelPipeline);

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