package io.netty.channel.unix;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelOutboundBuffer;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.PlatformDependent;
import java.nio.ByteBuffer;

/* JADX INFO: loaded from: classes.dex */
public final class IovArray implements ChannelOutboundBuffer.MessageProcessor {
    public static final /* synthetic */ boolean $assertionsDisabled = false;
    private static final int ADDRESS_SIZE;
    private static final int CAPACITY;
    private static final int IOV_SIZE;
    private int count;
    private long maxBytes = Limits.SSIZE_MAX;
    private final long memoryAddress = PlatformDependent.allocateMemory(CAPACITY);
    private long size;

    static {
        int iAddressSize = PlatformDependent.addressSize();
        ADDRESS_SIZE = iAddressSize;
        int i2 = iAddressSize * 2;
        IOV_SIZE = i2;
        CAPACITY = Limits.IOV_MAX * i2;
    }

    private boolean add(long j2, int i2, int i3) {
        long jMemoryAddress = memoryAddress(this.count);
        int i4 = ADDRESS_SIZE;
        long j3 = ((long) i4) + jMemoryAddress;
        long j4 = i3;
        long j5 = this.maxBytes - j4;
        long j6 = this.size;
        if (j5 < j6 && this.count > 0) {
            return false;
        }
        this.size = j6 + j4;
        this.count++;
        if (i4 == 8) {
            PlatformDependent.putLong(jMemoryAddress, j2 + ((long) i2));
            PlatformDependent.putLong(j3, j4);
        } else {
            PlatformDependent.putInt(jMemoryAddress, ((int) j2) + i2);
            PlatformDependent.putInt(j3, i3);
        }
        return true;
    }

    public boolean add(ByteBuf byteBuf) {
        if (this.count == Limits.IOV_MAX) {
            return false;
        }
        if (byteBuf.hasMemoryAddress() && byteBuf.nioBufferCount() == 1) {
            int i2 = byteBuf.readableBytes();
            return i2 == 0 || add(byteBuf.memoryAddress(), byteBuf.readerIndex(), i2);
        }
        for (ByteBuffer byteBuffer : byteBuf.nioBuffers()) {
            int iRemaining = byteBuffer.remaining();
            if (iRemaining != 0 && (!add(PlatformDependent.directBufferAddress(byteBuffer), byteBuffer.position(), iRemaining) || this.count == Limits.IOV_MAX)) {
                return false;
            }
        }
        return true;
    }

    public void clear() {
        this.count = 0;
        this.size = 0L;
    }

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

    public long maxBytes() {
        return this.maxBytes;
    }

    public void maxBytes(long j2) {
        this.maxBytes = Math.min(Limits.SSIZE_MAX, ObjectUtil.checkPositive(j2, "maxBytes"));
    }

    public long memoryAddress(int i2) {
        return this.memoryAddress + ((long) (IOV_SIZE * i2));
    }

    @Override // io.netty.channel.ChannelOutboundBuffer.MessageProcessor
    public boolean processMessage(Object obj) {
        return (obj instanceof ByteBuf) && add((ByteBuf) obj);
    }

    public void release() {
        PlatformDependent.freeMemory(this.memoryAddress);
    }

    public long size() {
        return this.size;
    }
}
