package io.netty.handler.stream;

import com.cvte.vman.instance.InvokeCmd;
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 org.snmp4j.util.SnmpConfigurator;

/* 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) throws IOException {
        this(file, InvokeCmd.CMD_TOUCH_CTRL_START);
    }

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

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

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

    @Override // io.netty.handler.stream.ChunkedInput
    public boolean isEndOfInput() throws Exception {
        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;
    }

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

    public ChunkedFile(File file, int i2) throws IOException {
        this(new RandomAccessFile(file, SnmpConfigurator.O_RETRIES), i2);
    }

    public ChunkedFile(RandomAccessFile randomAccessFile) throws IOException {
        this(randomAccessFile, InvokeCmd.CMD_TOUCH_CTRL_START);
    }

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

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

    /* JADX WARN: Can't rename method to resolve collision */
    @Override // io.netty.handler.stream.ChunkedInput
    public ByteBuf readChunk(ByteBufAllocator byteBufAllocator) throws Exception {
        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;
        }
    }

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