package io.netty.channel;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import io.netty.util.internal.ObjectUtil;
import java.util.ArrayDeque;

/* JADX INFO: loaded from: classes.dex */
public final class CoalescingBufferQueue {
    static final /* synthetic */ boolean $assertionsDisabled = false;
    private final ArrayDeque<Object> bufAndListenerPairs;
    private final Channel channel;
    private int readableBytes;

    public CoalescingBufferQueue(Channel channel) {
        this(channel, 4);
    }

    private ByteBuf compose(ByteBuf byteBuf, ByteBuf byteBuf2) {
        if (byteBuf == null) {
            return byteBuf2;
        }
        if (byteBuf instanceof CompositeByteBuf) {
            CompositeByteBuf compositeByteBuf = (CompositeByteBuf) byteBuf;
            compositeByteBuf.addComponent(true, byteBuf2);
            return compositeByteBuf;
        }
        CompositeByteBuf compositeByteBufCompositeBuffer = this.channel.alloc().compositeBuffer(this.bufAndListenerPairs.size() + 2);
        compositeByteBufCompositeBuffer.addComponent(true, byteBuf);
        compositeByteBufCompositeBuffer.addComponent(true, byteBuf2);
        return compositeByteBufCompositeBuffer;
    }

    private void releaseAndCompleteAll(ChannelFuture channelFuture) {
        this.readableBytes = 0;
        Throwable th = null;
        while (true) {
            Object objPoll = this.bufAndListenerPairs.poll();
            if (objPoll == null) {
                break;
            }
            try {
                if (objPoll instanceof ByteBuf) {
                    ReferenceCountUtil.safeRelease(objPoll);
                } else {
                    ((ChannelFutureListener) objPoll).operationComplete(channelFuture);
                }
            } catch (Throwable th2) {
                th = th2;
            }
        }
        if (th != null) {
            throw new IllegalStateException(th);
        }
    }

    public void add(ByteBuf byteBuf) {
        add(byteBuf, (ChannelFutureListener) null);
    }

    public void copyTo(CoalescingBufferQueue coalescingBufferQueue) {
        coalescingBufferQueue.bufAndListenerPairs.addAll(this.bufAndListenerPairs);
        coalescingBufferQueue.readableBytes += this.readableBytes;
    }

    public boolean isEmpty() {
        return this.bufAndListenerPairs.isEmpty();
    }

    public int readableBytes() {
        return this.readableBytes;
    }

    public void releaseAndFailAll(Throwable th) {
        releaseAndCompleteAll(this.channel.newFailedFuture(th));
    }

    public ByteBuf remove(int i, ChannelPromise channelPromise) {
        if (i < 0) {
            throw new IllegalArgumentException("bytes (expected >= 0): " + i);
        }
        ObjectUtil.checkNotNull(channelPromise, "aggregatePromise");
        if (this.bufAndListenerPairs.isEmpty()) {
            return Unpooled.EMPTY_BUFFER;
        }
        int iMin = Math.min(i, this.readableBytes);
        ByteBuf byteBufCompose = null;
        int i2 = iMin;
        while (true) {
            Object objPoll = this.bufAndListenerPairs.poll();
            if (objPoll == null) {
                break;
            }
            if (objPoll instanceof ChannelFutureListener) {
                channelPromise.addListener((GenericFutureListener<? extends Future<? super Void>>) objPoll);
            } else {
                ByteBuf byteBuf = (ByteBuf) objPoll;
                if (byteBuf.readableBytes() > i2) {
                    this.bufAndListenerPairs.addFirst(byteBuf);
                    if (i2 > 0) {
                        byteBufCompose = compose(byteBufCompose, byteBuf.readRetainedSlice(i2));
                        i2 = 0;
                    }
                } else {
                    byteBufCompose = compose(byteBufCompose, byteBuf);
                    i2 -= byteBuf.readableBytes();
                }
            }
        }
        this.readableBytes -= iMin - i2;
        return byteBufCompose;
    }

    public CoalescingBufferQueue(Channel channel, int i) {
        this.channel = (Channel) ObjectUtil.checkNotNull(channel, "channel");
        this.bufAndListenerPairs = new ArrayDeque<>(i);
    }

    public void add(ByteBuf byteBuf, ChannelPromise channelPromise) {
        ObjectUtil.checkNotNull(channelPromise, "promise");
        add(byteBuf, channelPromise.isVoid() ? null : new ChannelPromiseNotifier(channelPromise));
    }

    public void add(ByteBuf byteBuf, ChannelFutureListener channelFutureListener) {
        ObjectUtil.checkNotNull(byteBuf, "buf");
        if (this.readableBytes <= Integer.MAX_VALUE - byteBuf.readableBytes()) {
            this.bufAndListenerPairs.add(byteBuf);
            if (channelFutureListener != null) {
                this.bufAndListenerPairs.add(channelFutureListener);
            }
            this.readableBytes += byteBuf.readableBytes();
            return;
        }
        throw new IllegalStateException("buffer queue length overflow: " + this.readableBytes + " + " + byteBuf.readableBytes());
    }
}
