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

import com.github.mjdev.libaums.driver.BlockDeviceDriver;
import com.github.mjdev.libaums.fs.UsbFile;
import java.io.IOException;
import java.nio.ByteBuffer;

/* JADX INFO: loaded from: classes.dex */
public class FatFile implements UsbFile {
    private BlockDeviceDriver blockDevice;
    private Fat32BootSector bootSector;
    private ClusterChain chain;
    private FatLfnDirectoryEntry entry;
    private FAT fat;
    private FatDirectory parent;

    @Override // com.github.mjdev.libaums.fs.UsbFile
    public boolean isDirectory() {
        return false;
    }

    @Override // com.github.mjdev.libaums.fs.UsbFile
    public boolean isRoot() {
        return false;
    }

    private FatFile(BlockDeviceDriver blockDeviceDriver, FAT fat, Fat32BootSector fat32BootSector, FatLfnDirectoryEntry fatLfnDirectoryEntry, FatDirectory fatDirectory) {
        this.blockDevice = blockDeviceDriver;
        this.fat = fat;
        this.bootSector = fat32BootSector;
        this.entry = fatLfnDirectoryEntry;
        this.parent = fatDirectory;
    }

    public static FatFile create(FatLfnDirectoryEntry fatLfnDirectoryEntry, BlockDeviceDriver blockDeviceDriver, FAT fat, Fat32BootSector fat32BootSector, FatDirectory fatDirectory) throws IOException {
        return new FatFile(blockDeviceDriver, fat, fat32BootSector, fatLfnDirectoryEntry, fatDirectory);
    }

    private void initChain() throws IOException {
        if (this.chain == null) {
            this.chain = new ClusterChain(this.entry.getStartCluster(), this.blockDevice, this.fat, this.bootSector);
        }
    }

    @Override // com.github.mjdev.libaums.fs.UsbFile
    public UsbFile search(String str) {
        throw new UnsupportedOperationException("This is a file!");
    }

    @Override // com.github.mjdev.libaums.fs.UsbFile
    public String getName() {
        return this.entry.getName();
    }

    @Override // com.github.mjdev.libaums.fs.UsbFile
    public void setName(String str) throws IOException {
        this.parent.renameEntry(this.entry, str);
    }

    @Override // com.github.mjdev.libaums.fs.UsbFile
    public long createdAt() {
        return this.entry.getActualEntry().getCreatedDateTime();
    }

    @Override // com.github.mjdev.libaums.fs.UsbFile
    public long lastModified() {
        return this.entry.getActualEntry().getLastModifiedDateTime();
    }

    @Override // com.github.mjdev.libaums.fs.UsbFile
    public long lastAccessed() {
        return this.entry.getActualEntry().getLastAccessedDateTime();
    }

    @Override // com.github.mjdev.libaums.fs.UsbFile
    public UsbFile getParent() {
        return this.parent;
    }

    @Override // com.github.mjdev.libaums.fs.UsbFile
    public String[] list() {
        throw new UnsupportedOperationException("This is a file!");
    }

    @Override // com.github.mjdev.libaums.fs.UsbFile
    public UsbFile[] listFiles() throws IOException {
        throw new UnsupportedOperationException("This is a file!");
    }

    @Override // com.github.mjdev.libaums.fs.UsbFile
    public long getLength() {
        return this.entry.getFileSize();
    }

    @Override // com.github.mjdev.libaums.fs.UsbFile
    public void setLength(long j) throws IOException {
        initChain();
        this.chain.setLength(j);
        this.entry.setFileSize(j);
    }

    @Override // com.github.mjdev.libaums.fs.UsbFile
    public void read(long j, ByteBuffer byteBuffer) throws IOException {
        initChain();
        this.entry.setLastAccessedTimeToNow();
        this.chain.read(j, byteBuffer);
    }

    @Override // com.github.mjdev.libaums.fs.UsbFile
    public void write(long j, ByteBuffer byteBuffer) throws IOException {
        initChain();
        long jRemaining = ((long) byteBuffer.remaining()) + j;
        if (jRemaining > getLength()) {
            setLength(jRemaining);
        }
        this.entry.setLastModifiedTimeToNow();
        this.chain.write(j, byteBuffer);
    }

    @Override // com.github.mjdev.libaums.fs.UsbFile
    public void flush() throws IOException {
        this.parent.write();
    }

    @Override // com.github.mjdev.libaums.fs.UsbFile, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        flush();
    }

    @Override // com.github.mjdev.libaums.fs.UsbFile
    public UsbFile createDirectory(String str) throws IOException {
        throw new UnsupportedOperationException("This is a file!");
    }

    @Override // com.github.mjdev.libaums.fs.UsbFile
    public UsbFile createFile(String str) throws IOException {
        throw new UnsupportedOperationException("This is a file!");
    }

    @Override // com.github.mjdev.libaums.fs.UsbFile
    public void moveTo(UsbFile usbFile) throws IOException {
        this.parent.move(this.entry, usbFile);
        this.parent = (FatDirectory) usbFile;
    }

    @Override // com.github.mjdev.libaums.fs.UsbFile
    public void delete() throws IOException {
        initChain();
        this.parent.removeEntry(this.entry);
        this.parent.write();
        this.chain.setLength(0L);
    }
}
