package com.github.mjdev.libaums.fs.fat32;

import android.util.Log;
import com.github.mjdev.libaums.driver.BlockDeviceDriver;
import java.io.IOException;
import java.nio.ByteBuffer;

/* JADX INFO: loaded from: classes.dex */
public class ClusterChain {
    private static final String TAG = "ClusterChain";
    private BlockDeviceDriver blockDevice;
    private Long[] chain;
    private long clusterSize;
    private long dataAreaOffset;
    private FAT fat;

    ClusterChain(long j, BlockDeviceDriver blockDeviceDriver, FAT fat, Fat32BootSector fat32BootSector) throws IOException {
        String str = TAG;
        Log.d(str, "Init a cluster chain, reading from FAT");
        this.fat = fat;
        this.blockDevice = blockDeviceDriver;
        this.chain = fat.getChain(j);
        this.clusterSize = fat32BootSector.getBytesPerCluster();
        this.dataAreaOffset = fat32BootSector.getDataAreaOffset();
        Log.d(str, "Finished init of a cluster chain");
    }

    void read(long j, ByteBuffer byteBuffer) throws IOException {
        int iRemaining = byteBuffer.remaining();
        long j2 = this.clusterSize;
        int i = (int) (j / j2);
        if (j % j2 != 0) {
            int i2 = (int) (j % j2);
            int iMin = Math.min(iRemaining, (int) (j2 - ((long) i2)));
            byteBuffer.limit(byteBuffer.position() + iMin);
            this.blockDevice.read(getFileSystemOffset(this.chain[i].longValue(), i2), byteBuffer);
            i++;
            iRemaining -= iMin;
        }
        while (iRemaining > 0) {
            int iMin2 = (int) Math.min(this.clusterSize, iRemaining);
            byteBuffer.limit(byteBuffer.position() + iMin2);
            this.blockDevice.read(getFileSystemOffset(this.chain[i].longValue(), 0), byteBuffer);
            i++;
            iRemaining -= iMin2;
        }
    }

    void write(long j, ByteBuffer byteBuffer) throws IOException {
        int iRemaining = byteBuffer.remaining();
        long j2 = this.clusterSize;
        int i = (int) (j / j2);
        if (j % j2 != 0) {
            int i2 = (int) (j % j2);
            int iMin = Math.min(iRemaining, (int) (j2 - ((long) i2)));
            byteBuffer.limit(byteBuffer.position() + iMin);
            this.blockDevice.write(getFileSystemOffset(this.chain[i].longValue(), i2), byteBuffer);
            i++;
            iRemaining -= iMin;
        }
        while (iRemaining > 0) {
            int iMin2 = (int) Math.min(this.clusterSize, iRemaining);
            byteBuffer.limit(byteBuffer.position() + iMin2);
            this.blockDevice.write(getFileSystemOffset(this.chain[i].longValue(), 0), byteBuffer);
            i++;
            iRemaining -= iMin2;
        }
    }

    private long getFileSystemOffset(long j, int i) {
        return this.dataAreaOffset + ((long) i) + ((j - 2) * this.clusterSize);
    }

    void setClusters(int i) throws IOException {
        int clusters = getClusters();
        if (i == clusters) {
            return;
        }
        if (i > clusters) {
            Log.d(TAG, "grow chain");
            this.chain = this.fat.alloc(this.chain, i - clusters);
        } else {
            Log.d(TAG, "shrink chain");
            this.chain = this.fat.free(this.chain, clusters - i);
        }
    }

    int getClusters() {
        return this.chain.length;
    }

    void setLength(long j) throws IOException {
        long j2 = this.clusterSize;
        setClusters((int) (((j + j2) - 1) / j2));
    }

    long getLength() {
        return ((long) this.chain.length) * this.clusterSize;
    }
}
