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.FileInputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

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

    public ChunkedNioFile(File file) throws IOException {
        this(new FileInputStream(file).getChannel());
    }

    @Override // io.netty.handler.stream.ChunkedInput
    public void close() throws Exception {
        this.in.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.in.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 ChunkedNioFile(File file, int i2) throws IOException {
        this(new FileInputStream(file).getChannel(), i2);
    }

    public ChunkedNioFile(FileChannel fileChannel) throws IOException {
        this(fileChannel, 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 ChunkedNioFile(FileChannel fileChannel, int i2) throws IOException {
        this(fileChannel, 0L, fileChannel.size(), 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 byteBufBuffer = byteBufAllocator.buffer(iMin);
        int i2 = 0;
        do {
            try {
                int iWriteBytes = byteBufBuffer.writeBytes(this.in, iMin - i2);
                if (iWriteBytes < 0) {
                    break;
                }
                i2 += iWriteBytes;
            } catch (Throwable th) {
                byteBufBuffer.release();
                throw th;
            }
        } while (i2 != iMin);
        this.offset += (long) i2;
        return byteBufBuffer;
    }

    public ChunkedNioFile(FileChannel fileChannel, long j2, long j3, int i2) throws IOException {
        if (fileChannel == null) {
            throw new NullPointerException("in");
        }
        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) {
            if (j2 != 0) {
                fileChannel.position(j2);
            }
            this.in = fileChannel;
            this.chunkSize = i2;
            this.startOffset = j2;
            this.offset = j2;
            this.endOffset = j2 + j3;
            return;
        }
        throw new IllegalArgumentException("chunkSize: " + i2 + " (expected: a positive integer)");
    }
}
