package com.github.mjdev.libaums.fs;

import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

/* JADX INFO: loaded from: classes.dex */
public class UsbFileInputStream extends InputStream {
    private static final String TAG = "UsbFileInputStream";
    private int currentByteOffset = 0;
    private UsbFile file;

    public UsbFileInputStream(UsbFile usbFile) {
        if (usbFile.isDirectory()) {
            throw new RuntimeException("UsbFileInputStream cannot be created on directory!");
        }
        this.file = usbFile;
    }

    @Override // java.io.InputStream
    public int available() throws IOException {
        Log.d(TAG, "available");
        return 0;
    }

    @Override // java.io.InputStream
    public int read() throws IOException {
        if (this.currentByteOffset >= this.file.getLength()) {
            return -1;
        }
        ByteBuffer byteBufferAllocate = ByteBuffer.allocate(512);
        byteBufferAllocate.limit(1);
        this.file.read(this.currentByteOffset, byteBufferAllocate);
        this.currentByteOffset++;
        byteBufferAllocate.flip();
        return byteBufferAllocate.get();
    }

    @Override // java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        super.close();
    }

    @Override // java.io.InputStream
    public int read(byte[] bArr) throws IOException {
        if (this.currentByteOffset >= this.file.getLength()) {
            return -1;
        }
        long jMin = Math.min(bArr.length, this.file.getLength() - ((long) this.currentByteOffset));
        ByteBuffer byteBufferWrap = ByteBuffer.wrap(bArr);
        int i = (int) jMin;
        byteBufferWrap.limit(i);
        this.file.read(this.currentByteOffset, byteBufferWrap);
        this.currentByteOffset = (int) (((long) this.currentByteOffset) + jMin);
        return i;
    }

    @Override // java.io.InputStream
    public int read(byte[] bArr, int i, int i2) throws IOException {
        if (this.currentByteOffset >= this.file.getLength()) {
            return -1;
        }
        long jMin = Math.min(i2, this.file.getLength() - ((long) this.currentByteOffset));
        ByteBuffer byteBufferWrap = ByteBuffer.wrap(bArr);
        byteBufferWrap.position(i);
        int i3 = (int) jMin;
        byteBufferWrap.limit(i + i3);
        this.file.read(this.currentByteOffset, byteBufferWrap);
        this.currentByteOffset = (int) (((long) this.currentByteOffset) + jMin);
        return i3;
    }

    @Override // java.io.InputStream
    public long skip(long j) throws IOException {
        long jMin = Math.min(j, this.file.getLength() - ((long) this.currentByteOffset));
        this.currentByteOffset = (int) (((long) this.currentByteOffset) + jMin);
        return jMin;
    }
}
