package com.github.mjdev.libaums.partition;

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

/* JADX INFO: loaded from: classes.dex */
public class Partition implements BlockDeviceDriver {
    private static final String TAG = "Partition";
    private BlockDeviceDriver blockDevice;
    private int blockSize;
    private FileSystem fileSystem;
    private int logicalBlockAddress;

    @Override // com.github.mjdev.libaums.driver.BlockDeviceDriver
    public void init() {
    }

    private Partition() {
    }

    public static Partition createPartition(PartitionTableEntry partitionTableEntry, BlockDeviceDriver blockDeviceDriver) throws IOException {
        byte partitionType = partitionTableEntry.getPartitionType();
        if (partitionType == 11 || partitionType == 12) {
            Partition partition = new Partition();
            partition.logicalBlockAddress = partitionTableEntry.getLogicalBlockAddress();
            partition.blockDevice = blockDeviceDriver;
            partition.blockSize = blockDeviceDriver.getBlockSize();
            partition.fileSystem = FileSystemFactory.createFileSystem(partitionTableEntry, partition);
            return partition;
        }
        Log.w(TAG, "unsupported partition type: " + ((int) partitionTableEntry.getPartitionType()));
        return null;
    }

    public FileSystem getFileSystem() {
        return this.fileSystem;
    }

    public String getVolumeLabel() {
        return this.fileSystem.getVolumeLabel();
    }

    @Override // com.github.mjdev.libaums.driver.BlockDeviceDriver
    public void read(long j, ByteBuffer byteBuffer) throws IOException {
        ByteBuffer byteBufferAllocate;
        int i = this.blockSize;
        long j2 = (j / ((long) i)) + ((long) this.logicalBlockAddress);
        if (j % ((long) i) != 0) {
            ByteBuffer byteBufferAllocate2 = ByteBuffer.allocate(i);
            this.blockDevice.read(j2, byteBufferAllocate2);
            byteBufferAllocate2.clear();
            byteBufferAllocate2.position((int) (j % ((long) this.blockSize)));
            byteBufferAllocate2.limit(Math.min(byteBuffer.remaining(), byteBufferAllocate2.capacity()));
            byteBuffer.put(byteBufferAllocate2);
            j2++;
        }
        if (byteBuffer.remaining() > 0) {
            int iRemaining = byteBuffer.remaining();
            int i2 = this.blockSize;
            if (iRemaining % i2 != 0) {
                int iRemaining2 = (i2 - (byteBuffer.remaining() % this.blockSize)) + byteBuffer.remaining();
                byteBufferAllocate = ByteBuffer.allocate(iRemaining2);
                byteBufferAllocate.limit(iRemaining2);
            } else {
                byteBufferAllocate = byteBuffer;
            }
            this.blockDevice.read(j2, byteBufferAllocate);
            if (byteBuffer.remaining() % this.blockSize != 0) {
                System.arraycopy(byteBufferAllocate.array(), 0, byteBuffer.array(), byteBuffer.position(), byteBuffer.remaining());
            }
        }
    }

    @Override // com.github.mjdev.libaums.driver.BlockDeviceDriver
    public void write(long j, ByteBuffer byteBuffer) throws IOException {
        int i = this.blockSize;
        long j2 = (j / ((long) i)) + ((long) this.logicalBlockAddress);
        if (j % ((long) i) != 0) {
            ByteBuffer byteBufferAllocate = ByteBuffer.allocate(i);
            this.blockDevice.read(j2, byteBufferAllocate);
            byteBufferAllocate.clear();
            byteBufferAllocate.position((int) (j % ((long) this.blockSize)));
            int iMin = Math.min(byteBufferAllocate.remaining(), byteBuffer.remaining());
            byteBufferAllocate.put(byteBuffer.array(), byteBuffer.position(), iMin);
            byteBuffer.position(byteBuffer.position() + iMin);
            byteBufferAllocate.clear();
            this.blockDevice.write(j2, byteBufferAllocate);
            j2++;
        }
        if (byteBuffer.remaining() > 0) {
            int iRemaining = byteBuffer.remaining();
            int i2 = this.blockSize;
            if (iRemaining % i2 != 0) {
                int iRemaining2 = (i2 - (byteBuffer.remaining() % this.blockSize)) + byteBuffer.remaining();
                ByteBuffer byteBufferAllocate2 = ByteBuffer.allocate(iRemaining2);
                byteBufferAllocate2.limit(iRemaining2);
                System.arraycopy(byteBuffer.array(), byteBuffer.position(), byteBufferAllocate2.array(), 0, byteBuffer.remaining());
                byteBuffer = byteBufferAllocate2;
            }
            this.blockDevice.write(j2, byteBuffer);
        }
    }

    @Override // com.github.mjdev.libaums.driver.BlockDeviceDriver
    public int getBlockSize() {
        return this.blockDevice.getBlockSize();
    }
}
