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;
import java.nio.ByteOrder;

/* JADX INFO: loaded from: classes.dex */
class FsInfoStructure {
    private static int FREE_COUNT_OFF = 488;
    static int INVALID_VALUE = -1;
    private static int LEAD_SIGNATURE = 1096897106;
    private static int LEAD_SIGNATURE_OFF = 0;
    private static int NEXT_FREE_OFFSET = 492;
    private static int STRUCT_SIGNATURE = 1631679090;
    private static int STRUCT_SIGNATURE_OFF = 484;
    private static final String TAG = "FsInfoStructure";
    private static int TRAIL_SIGNATURE = -1437270016;
    private static int TRAIL_SIGNATURE_OFF = 508;
    private BlockDeviceDriver blockDevice;
    private ByteBuffer buffer;
    private int offset;

    private FsInfoStructure(BlockDeviceDriver blockDeviceDriver, int i) throws IOException {
        this.blockDevice = blockDeviceDriver;
        this.offset = i;
        ByteBuffer byteBufferAllocate = ByteBuffer.allocate(512);
        this.buffer = byteBufferAllocate;
        byteBufferAllocate.order(ByteOrder.LITTLE_ENDIAN);
        blockDeviceDriver.read(i, this.buffer);
        this.buffer.clear();
        if (this.buffer.getInt(LEAD_SIGNATURE_OFF) != LEAD_SIGNATURE || this.buffer.getInt(STRUCT_SIGNATURE_OFF) != STRUCT_SIGNATURE || this.buffer.getInt(TRAIL_SIGNATURE_OFF) != TRAIL_SIGNATURE) {
            throw new IOException("invalid fs info structure!");
        }
    }

    static FsInfoStructure read(BlockDeviceDriver blockDeviceDriver, int i) throws IOException {
        return new FsInfoStructure(blockDeviceDriver, i);
    }

    void setFreeClusterCount(long j) {
        this.buffer.putInt(FREE_COUNT_OFF, (int) j);
    }

    long getFreeClusterCount() {
        return this.buffer.getInt(FREE_COUNT_OFF);
    }

    void setLastAllocatedClusterHint(long j) {
        this.buffer.putInt(NEXT_FREE_OFFSET, (int) j);
    }

    long getLastAllocatedClusterHint() {
        return this.buffer.getInt(NEXT_FREE_OFFSET);
    }

    void decreaseClusterCount(long j) {
        long freeClusterCount = getFreeClusterCount();
        if (freeClusterCount != INVALID_VALUE) {
            setFreeClusterCount(freeClusterCount - j);
        }
    }

    void write() throws IOException {
        Log.d(TAG, "writing to device");
        this.blockDevice.write(this.offset, this.buffer);
        this.buffer.clear();
    }
}
