package io.netty.channel.unix;

import g.a.a.a.a;
import io.netty.channel.unix.Errors;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.ThrowableUtil;
import java.io.File;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import kotlinx.serialization.json.internal.AbstractJsonLexerKt;

/* JADX INFO: loaded from: classes.dex */
public class FileDescriptor {
    private static final Errors.NativeIoException READ_ADDRESS_CONNECTION_RESET_EXCEPTION;
    private static final Errors.NativeIoException READ_CONNECTION_RESET_EXCEPTION;
    private static final int STATE_ALL_MASK = 7;
    private static final int STATE_CLOSED_MASK = 1;
    private static final int STATE_INPUT_SHUTDOWN_MASK = 2;
    private static final int STATE_OUTPUT_SHUTDOWN_MASK = 4;
    private static final Errors.NativeIoException WRITEV_ADDRESSES_CONNECTION_RESET_EXCEPTION;
    private static final Errors.NativeIoException WRITEV_CONNECTION_RESET_EXCEPTION;
    private static final Errors.NativeIoException WRITE_ADDRESS_CONNECTION_RESET_EXCEPTION;
    private static final Errors.NativeIoException WRITE_CONNECTION_RESET_EXCEPTION;
    private static final AtomicIntegerFieldUpdater<FileDescriptor> stateUpdater;
    public final int fd;
    public volatile int state;
    private static final ClosedChannelException WRITE_CLOSED_CHANNEL_EXCEPTION = (ClosedChannelException) a.N(FileDescriptor.class, "write(..)");
    private static final ClosedChannelException WRITE_ADDRESS_CLOSED_CHANNEL_EXCEPTION = (ClosedChannelException) a.N(FileDescriptor.class, "writeAddress(..)");
    private static final ClosedChannelException WRITEV_CLOSED_CHANNEL_EXCEPTION = (ClosedChannelException) a.N(FileDescriptor.class, "writev(..)");
    private static final ClosedChannelException WRITEV_ADDRESSES_CLOSED_CHANNEL_EXCEPTION = (ClosedChannelException) a.N(FileDescriptor.class, "writevAddresses(..)");
    private static final ClosedChannelException READ_CLOSED_CHANNEL_EXCEPTION = (ClosedChannelException) a.N(FileDescriptor.class, "read(..)");
    private static final ClosedChannelException READ_ADDRESS_CLOSED_CHANNEL_EXCEPTION = (ClosedChannelException) a.N(FileDescriptor.class, "readAddress(..)");

    static {
        int i2 = Errors.ERRNO_EPIPE_NEGATIVE;
        WRITE_CONNECTION_RESET_EXCEPTION = (Errors.NativeIoException) ThrowableUtil.unknownStackTrace(Errors.newConnectionResetException("syscall:write", i2), FileDescriptor.class, "write(..)");
        WRITE_ADDRESS_CONNECTION_RESET_EXCEPTION = (Errors.NativeIoException) ThrowableUtil.unknownStackTrace(Errors.newConnectionResetException("syscall:write", i2), FileDescriptor.class, "writeAddress(..)");
        WRITEV_CONNECTION_RESET_EXCEPTION = (Errors.NativeIoException) ThrowableUtil.unknownStackTrace(Errors.newConnectionResetException("syscall:writev", i2), FileDescriptor.class, "writev(..)");
        WRITEV_ADDRESSES_CONNECTION_RESET_EXCEPTION = (Errors.NativeIoException) ThrowableUtil.unknownStackTrace(Errors.newConnectionResetException("syscall:writev", i2), FileDescriptor.class, "writeAddresses(..)");
        int i3 = Errors.ERRNO_ECONNRESET_NEGATIVE;
        READ_CONNECTION_RESET_EXCEPTION = (Errors.NativeIoException) ThrowableUtil.unknownStackTrace(Errors.newConnectionResetException("syscall:read", i3), FileDescriptor.class, "read(..)");
        READ_ADDRESS_CONNECTION_RESET_EXCEPTION = (Errors.NativeIoException) ThrowableUtil.unknownStackTrace(Errors.newConnectionResetException("syscall:read", i3), FileDescriptor.class, "readAddress(..)");
        stateUpdater = AtomicIntegerFieldUpdater.newUpdater(FileDescriptor.class, "state");
    }

    public FileDescriptor(int i2) {
        if (i2 < 0) {
            throw new IllegalArgumentException("fd must be >= 0");
        }
        this.fd = i2;
    }

    private static native int close(int i2);

    public static FileDescriptor from(File file) {
        return from(((File) ObjectUtil.checkNotNull(file, "file")).getPath());
    }

    public static FileDescriptor from(String str) throws Errors.NativeIoException {
        ObjectUtil.checkNotNull(str, "path");
        int iOpen = open(str);
        if (iOpen >= 0) {
            return new FileDescriptor(iOpen);
        }
        throw Errors.newIOException("open", iOpen);
    }

    public static int inputShutdown(int i2) {
        return i2 | 2;
    }

    public static boolean isClosed(int i2) {
        return (i2 & 1) != 0;
    }

    public static boolean isInputShutdown(int i2) {
        return (i2 & 2) != 0;
    }

    public static boolean isOutputShutdown(int i2) {
        return (i2 & 4) != 0;
    }

    private static native long newPipe();

    private static native int open(String str);

    public static int outputShutdown(int i2) {
        return i2 | 4;
    }

    public static FileDescriptor[] pipe() throws Errors.NativeIoException {
        long jNewPipe = newPipe();
        if (jNewPipe >= 0) {
            return new FileDescriptor[]{new FileDescriptor((int) (jNewPipe >>> 32)), new FileDescriptor((int) jNewPipe)};
        }
        throw Errors.newIOException("newPipe", (int) jNewPipe);
    }

    private static native int read(int i2, ByteBuffer byteBuffer, int i3, int i4);

    private static native int readAddress(int i2, long j2, int i3, int i4);

    private static native int write(int i2, ByteBuffer byteBuffer, int i3, int i4);

    private static native int writeAddress(int i2, long j2, int i3, int i4);

    private static native long writev(int i2, ByteBuffer[] byteBufferArr, int i3, int i4, long j2);

    private static native long writevAddresses(int i2, long j2, int i3);

    public final boolean casState(int i2, int i3) {
        return stateUpdater.compareAndSet(this, i2, i3);
    }

    public void close() throws Errors.NativeIoException {
        int i2;
        do {
            i2 = this.state;
            if (isClosed(i2)) {
                return;
            }
        } while (!casState(i2, i2 | 7));
        int iClose = close(this.fd);
        if (iClose < 0) {
            throw Errors.newIOException("close", iClose);
        }
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        return (obj instanceof FileDescriptor) && this.fd == ((FileDescriptor) obj).fd;
    }

    public int hashCode() {
        return this.fd;
    }

    public final int intValue() {
        return this.fd;
    }

    public boolean isOpen() {
        return !isClosed(this.state);
    }

    public final int read(ByteBuffer byteBuffer, int i2, int i3) {
        int i4 = read(this.fd, byteBuffer, i2, i3);
        if (i4 > 0) {
            return i4;
        }
        if (i4 == 0) {
            return -1;
        }
        return Errors.ioResult("read", i4, READ_CONNECTION_RESET_EXCEPTION, READ_CLOSED_CHANNEL_EXCEPTION);
    }

    public final int readAddress(long j2, int i2, int i3) {
        int address = readAddress(this.fd, j2, i2, i3);
        if (address > 0) {
            return address;
        }
        if (address == 0) {
            return -1;
        }
        return Errors.ioResult("readAddress", address, READ_ADDRESS_CONNECTION_RESET_EXCEPTION, READ_ADDRESS_CLOSED_CHANNEL_EXCEPTION);
    }

    public String toString() {
        return a.w(a.F("FileDescriptor{fd="), this.fd, AbstractJsonLexerKt.END_OBJ);
    }

    public final int write(ByteBuffer byteBuffer, int i2, int i3) {
        int iWrite = write(this.fd, byteBuffer, i2, i3);
        return iWrite >= 0 ? iWrite : Errors.ioResult("write", iWrite, WRITE_CONNECTION_RESET_EXCEPTION, WRITE_CLOSED_CHANNEL_EXCEPTION);
    }

    public final int writeAddress(long j2, int i2, int i3) {
        int iWriteAddress = writeAddress(this.fd, j2, i2, i3);
        return iWriteAddress >= 0 ? iWriteAddress : Errors.ioResult("writeAddress", iWriteAddress, WRITE_ADDRESS_CONNECTION_RESET_EXCEPTION, WRITE_ADDRESS_CLOSED_CHANNEL_EXCEPTION);
    }

    public final long writev(ByteBuffer[] byteBufferArr, int i2, int i3, long j2) {
        long jWritev = writev(this.fd, byteBufferArr, i2, Math.min(Limits.IOV_MAX, i3), j2);
        return jWritev >= 0 ? jWritev : Errors.ioResult("writev", (int) jWritev, WRITEV_CONNECTION_RESET_EXCEPTION, WRITEV_CLOSED_CHANNEL_EXCEPTION);
    }

    public final long writevAddresses(long j2, int i2) {
        long jWritevAddresses = writevAddresses(this.fd, j2, i2);
        return jWritevAddresses >= 0 ? jWritevAddresses : Errors.ioResult("writevAddresses", (int) jWritevAddresses, WRITEV_ADDRESSES_CONNECTION_RESET_EXCEPTION, WRITEV_ADDRESSES_CLOSED_CHANNEL_EXCEPTION);
    }
}
