package io.netty.channel;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.RecvByteBufAllocator;
import java.util.AbstractMap;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public class DefaultMaxBytesRecvByteBufAllocator implements MaxBytesRecvByteBufAllocator {
    private volatile int maxBytesPerIndividualRead;
    private volatile int maxBytesPerRead;

    private final class HandleImpl implements RecvByteBufAllocator.Handle {
        private int attemptBytesRead;
        private int bytesToRead;
        private int individualReadMax;
        private int lastBytesRead;

        private HandleImpl() {
        }

        @Override // io.netty.channel.RecvByteBufAllocator.Handle
        public ByteBuf allocate(ByteBufAllocator byteBufAllocator) {
            return byteBufAllocator.ioBuffer(guess());
        }

        @Override // io.netty.channel.RecvByteBufAllocator.Handle
        public void attemptedBytesRead(int i2) {
            this.attemptBytesRead = i2;
        }

        @Override // io.netty.channel.RecvByteBufAllocator.Handle
        public boolean continueReading() {
            return this.bytesToRead > 0 && this.attemptBytesRead == this.lastBytesRead;
        }

        @Override // io.netty.channel.RecvByteBufAllocator.Handle
        public int guess() {
            return Math.min(this.individualReadMax, this.bytesToRead);
        }

        @Override // io.netty.channel.RecvByteBufAllocator.Handle
        public void incMessagesRead(int i2) {
        }

        @Override // io.netty.channel.RecvByteBufAllocator.Handle
        public void lastBytesRead(int i2) {
            this.lastBytesRead = i2;
            this.bytesToRead -= i2;
        }

        @Override // io.netty.channel.RecvByteBufAllocator.Handle
        public void readComplete() {
        }

        @Override // io.netty.channel.RecvByteBufAllocator.Handle
        public void reset(ChannelConfig channelConfig) {
            this.bytesToRead = DefaultMaxBytesRecvByteBufAllocator.this.maxBytesPerRead();
            this.individualReadMax = DefaultMaxBytesRecvByteBufAllocator.this.maxBytesPerIndividualRead();
        }

        @Override // io.netty.channel.RecvByteBufAllocator.Handle
        public int attemptedBytesRead() {
            return this.attemptBytesRead;
        }

        @Override // io.netty.channel.RecvByteBufAllocator.Handle
        public int lastBytesRead() {
            return this.lastBytesRead;
        }
    }

    public DefaultMaxBytesRecvByteBufAllocator() {
        this(65536, 65536);
    }

    private void checkMaxBytesPerReadPair(int i2, int i3) {
        if (i2 <= 0) {
            throw new IllegalArgumentException("maxBytesPerRead: " + i2 + " (expected: > 0)");
        }
        if (i3 <= 0) {
            throw new IllegalArgumentException("maxBytesPerIndividualRead: " + i3 + " (expected: > 0)");
        }
        if (i2 >= i3) {
            return;
        }
        throw new IllegalArgumentException("maxBytesPerRead cannot be less than maxBytesPerIndividualRead (" + i3 + "): " + i2);
    }

    @Override // io.netty.channel.RecvByteBufAllocator
    public RecvByteBufAllocator.Handle newHandle() {
        return new HandleImpl();
    }

    public DefaultMaxBytesRecvByteBufAllocator(int i2, int i3) {
        checkMaxBytesPerReadPair(i2, i3);
        this.maxBytesPerRead = i2;
        this.maxBytesPerIndividualRead = i3;
    }

    @Override // io.netty.channel.MaxBytesRecvByteBufAllocator
    public int maxBytesPerIndividualRead() {
        return this.maxBytesPerIndividualRead;
    }

    @Override // io.netty.channel.MaxBytesRecvByteBufAllocator
    public int maxBytesPerRead() {
        return this.maxBytesPerRead;
    }

    @Override // io.netty.channel.MaxBytesRecvByteBufAllocator
    public synchronized Map.Entry<Integer, Integer> maxBytesPerReadPair() {
        return new AbstractMap.SimpleEntry(Integer.valueOf(this.maxBytesPerRead), Integer.valueOf(this.maxBytesPerIndividualRead));
    }

    @Override // io.netty.channel.MaxBytesRecvByteBufAllocator
    public DefaultMaxBytesRecvByteBufAllocator maxBytesPerIndividualRead(int i2) {
        if (i2 > 0) {
            synchronized (this) {
                int iMaxBytesPerRead = maxBytesPerRead();
                if (i2 <= iMaxBytesPerRead) {
                    this.maxBytesPerIndividualRead = i2;
                } else {
                    throw new IllegalArgumentException("maxBytesPerIndividualRead cannot be greater than maxBytesPerRead (" + iMaxBytesPerRead + "): " + i2);
                }
            }
            return this;
        }
        throw new IllegalArgumentException("maxBytesPerIndividualRead: " + i2 + " (expected: > 0)");
    }

    @Override // io.netty.channel.MaxBytesRecvByteBufAllocator
    public DefaultMaxBytesRecvByteBufAllocator maxBytesPerRead(int i2) {
        if (i2 > 0) {
            synchronized (this) {
                int iMaxBytesPerIndividualRead = maxBytesPerIndividualRead();
                if (i2 >= iMaxBytesPerIndividualRead) {
                    this.maxBytesPerRead = i2;
                } else {
                    throw new IllegalArgumentException("maxBytesPerRead cannot be less than maxBytesPerIndividualRead (" + iMaxBytesPerIndividualRead + "): " + i2);
                }
            }
            return this;
        }
        throw new IllegalArgumentException("maxBytesPerRead: " + i2 + " (expected: > 0)");
    }

    @Override // io.netty.channel.MaxBytesRecvByteBufAllocator
    public DefaultMaxBytesRecvByteBufAllocator maxBytesPerReadPair(int i2, int i3) {
        checkMaxBytesPerReadPair(i2, i3);
        synchronized (this) {
            this.maxBytesPerRead = i2;
            this.maxBytesPerIndividualRead = i3;
        }
        return this;
    }
}
