package io.netty.channel;

import g.a.a.a.a;
import io.netty.util.AbstractReferenceCounted;
import io.netty.util.IllegalReferenceCountException;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public class DefaultFileRegion extends AbstractReferenceCounted implements FileRegion {
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) DefaultFileRegion.class);
    private final long count;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final File f5793f;
    private FileChannel file;
    private final long position;
    private long transferred;

    public DefaultFileRegion(File file, long j2, long j3) {
        Objects.requireNonNull(file, "f");
        if (j2 < 0) {
            throw new IllegalArgumentException(a.q("position must be >= 0 but was ", j2));
        }
        if (j3 < 0) {
            throw new IllegalArgumentException(a.q("count must be >= 0 but was ", j3));
        }
        this.position = j2;
        this.count = j3;
        this.f5793f = file;
    }

    public DefaultFileRegion(FileChannel fileChannel, long j2, long j3) {
        Objects.requireNonNull(fileChannel, "file");
        if (j2 < 0) {
            throw new IllegalArgumentException(a.q("position must be >= 0 but was ", j2));
        }
        if (j3 < 0) {
            throw new IllegalArgumentException(a.q("count must be >= 0 but was ", j3));
        }
        this.file = fileChannel;
        this.position = j2;
        this.count = j3;
        this.f5793f = null;
    }

    @Override // io.netty.channel.FileRegion
    public long count() {
        return this.count;
    }

    @Override // io.netty.util.AbstractReferenceCounted
    public void deallocate() {
        FileChannel fileChannel = this.file;
        if (fileChannel == null) {
            return;
        }
        this.file = null;
        try {
            fileChannel.close();
        } catch (IOException e2) {
            if (logger.isWarnEnabled()) {
                logger.warn("Failed to close a file.", (Throwable) e2);
            }
        }
    }

    public boolean isOpen() {
        return this.file != null;
    }

    public void open() {
        if (isOpen() || refCnt() <= 0) {
            return;
        }
        this.file = new RandomAccessFile(this.f5793f, "r").getChannel();
    }

    @Override // io.netty.channel.FileRegion
    public long position() {
        return this.position;
    }

    @Override // io.netty.util.AbstractReferenceCounted, io.netty.util.ReferenceCounted
    public FileRegion retain() {
        super.retain();
        return this;
    }

    @Override // io.netty.util.AbstractReferenceCounted, io.netty.util.ReferenceCounted
    public FileRegion retain(int i2) {
        super.retain(i2);
        return this;
    }

    @Override // io.netty.util.AbstractReferenceCounted, io.netty.util.ReferenceCounted
    public FileRegion touch() {
        return this;
    }

    @Override // io.netty.util.ReferenceCounted
    public FileRegion touch(Object obj) {
        return this;
    }

    @Override // io.netty.channel.FileRegion
    public long transferTo(WritableByteChannel writableByteChannel, long j2) throws IOException {
        long j3 = this.count - j2;
        if (j3 < 0 || j2 < 0) {
            throw new IllegalArgumentException("position out of range: " + j2 + " (expected: 0 - " + (this.count - 1) + ')');
        }
        if (j3 == 0) {
            return 0L;
        }
        if (refCnt() == 0) {
            throw new IllegalReferenceCountException(0);
        }
        open();
        long jTransferTo = this.file.transferTo(this.position + j2, j3, writableByteChannel);
        if (jTransferTo > 0) {
            this.transferred += jTransferTo;
        }
        return jTransferTo;
    }

    @Override // io.netty.channel.FileRegion
    @Deprecated
    public long transfered() {
        return this.transferred;
    }

    @Override // io.netty.channel.FileRegion
    public long transferred() {
        return this.transferred;
    }
}
