package io.netty.handler.stream;

import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelHandlerContext;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public class ChunkedFile implements ChunkedInput<ByteBuf> {
    private final int chunkSize;
    private final long endOffset;
    private final RandomAccessFile file;
    private long offset;
    private final long startOffset;

    public ChunkedFile(File file) {
        this(file, 8192);
    }

    public ChunkedFile(File file, int i2) {
        this(new RandomAccessFile(file, "r"), i2);
    }

    public ChunkedFile(RandomAccessFile randomAccessFile) {
        this(randomAccessFile, 8192);
    }

    public ChunkedFile(RandomAccessFile randomAccessFile, int i2) {
        this(randomAccessFile, 0L, randomAccessFile.length(), i2);
    }

    public ChunkedFile(RandomAccessFile randomAccessFile, long j2, long j3, int i2) throws IOException {
        Objects.requireNonNull(randomAccessFile, "file");
        if (j2 < 0) {
            throw new IllegalArgumentException(a.r("offset: ", j2, " (expected: 0 or greater)"));
        }
        if (j3 < 0) {
            throw new IllegalArgumentException(a.r("length: ", j3, " (expected: 0 or greater)"));
        }
        if (i2 <= 0) {
            throw new IllegalArgumentException(a.n("chunkSize: ", i2, " (expected: a positive integer)"));
        }
        this.file = randomAccessFile;
        this.startOffset = j2;
        this.offset = j2;
        this.endOffset = j3 + j2;
        this.chunkSize = i2;
        randomAccessFile.seek(j2);
    }

    @Override // io.netty.handler.stream.ChunkedInput
    public void close() throws IOException {
        this.file.close();
    }

    public long currentOffset() {
        return this.offset;
    }

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

    @Override // io.netty.handler.stream.ChunkedInput
    public boolean isEndOfInput() {
        return this.offset >= this.endOffset || !this.file.getChannel().isOpen();
    }

    @Override // io.netty.handler.stream.ChunkedInput
    public long length() {
        return this.endOffset - this.startOffset;
    }

    @Override // io.netty.handler.stream.ChunkedInput
    public long progress() {
        return this.offset - this.startOffset;
    }

    /* JADX WARN: Can't rename method to resolve collision */
    @Override // io.netty.handler.stream.ChunkedInput
    public ByteBuf readChunk(ByteBufAllocator byteBufAllocator) {
        long j2 = this.offset;
        long j3 = this.endOffset;
        if (j2 >= j3) {
            return null;
        }
        int iMin = (int) Math.min(this.chunkSize, j3 - j2);
        ByteBuf byteBufHeapBuffer = byteBufAllocator.heapBuffer(iMin);
        try {
            this.file.readFully(byteBufHeapBuffer.array(), byteBufHeapBuffer.arrayOffset(), iMin);
            byteBufHeapBuffer.writerIndex(iMin);
            this.offset = j2 + ((long) iMin);
            return byteBufHeapBuffer;
        } catch (Throwable th) {
            byteBufHeapBuffer.release();
            throw th;
        }
    }

    /* JADX WARN: Can't rename method to resolve collision */
    @Override // io.netty.handler.stream.ChunkedInput
    @Deprecated
    public ByteBuf readChunk(ChannelHandlerContext channelHandlerContext) {
        return readChunk(channelHandlerContext.alloc());
    }

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