Class FatDirectory

  • All Implemented Interfaces:
    UsbFile, java.io.Closeable, java.lang.AutoCloseable

    public class FatDirectory
    extends AbstractUsbFile
    This class represents a directory in the FAT32 file system. It can hold other directories and files.
    • Field Summary

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes and flushes the file.
      long createdAt()
      Returns the time this directory or file was created.
      FatDirectory createDirectory​(java.lang.String name)
      This methods creates a new directory with the given name and returns it.
      FatFile createFile​(java.lang.String name)
      This methods creates a new file with the given name and returns it.
      void delete()
      Deletes this file or directory from the parent directory.
      void flush()
      Forces a write.
      long getLength()
      Returns the file length or throws an exception if called on a directory.
      java.lang.String getName()
      Actual file or directory name or '/' for root directory.
      UsbFile getParent()
      Returns the parent directory for the file or directory or null if this is the root directory.
      boolean isDirectory()  
      boolean isRoot()  
      long lastAccessed()
      Returns the time this directory or file was last accessed.
      long lastModified()
      Returns the time this directory or file was last modified.
      java.lang.String[] list()
      Lists all files in the directory.
      UsbFile[] listFiles()
      Lists all files in the directory.
      void moveTo​(UsbFile destination)
      This methods moves THIS item to the destination directory.
      void read​(long offset, java.nio.ByteBuffer destination)
      Reads from a file or throws an exception if called on a directory.
      void setLength​(long newLength)
      Sets the new file length.
      void setName​(java.lang.String newName)
      Set a new name for this file or directory.
      void write​(long offset, java.nio.ByteBuffer source)
      Writes to a file or throws an exception if called on a directory.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Method Detail

      • isRoot

        public boolean isRoot()
        Returns:
        True if this directory is the root directory.
      • createFile

        public FatFile createFile​(java.lang.String name)
                           throws java.io.IOException
        Description copied from interface: UsbFile
        This methods creates a new file with the given name and returns it.
        Parameters:
        name - The name of the new file.
        Returns:
        The newly created file.
        Throws:
        java.io.IOException - If writing to the disk fails or a item with the same name already exists.
      • createDirectory

        public FatDirectory createDirectory​(java.lang.String name)
                                     throws java.io.IOException
        Description copied from interface: UsbFile
        This methods creates a new directory with the given name and returns it.
        Parameters:
        name - The name of the new directory.
        Returns:
        The newly created directory.
        Throws:
        java.io.IOException - If writing to the disk fails or a item with the same name already exists.
      • setLength

        public void setLength​(long newLength)
        Description copied from interface: UsbFile
        Sets the new file length. This can sometimes be more efficient if all needed place for a file is allocated on the disk at once and before writing to it.

        If the space is not allocated before writing the space must be exceeded every time a new write occurs. This can sometimes be less efficient.

        Parameters:
        newLength - The file length in bytes.
      • getLength

        public long getLength()
        Description copied from interface: UsbFile
        Returns the file length or throws an exception if called on a directory.
        Returns:
        File length in bytes.
      • isDirectory

        public boolean isDirectory()
        Returns:
        True if representing a directory.
      • getName

        public java.lang.String getName()
        Description copied from interface: UsbFile
        Actual file or directory name or '/' for root directory.
        Returns:
        The name of the file or directory.
      • setName

        public void setName​(java.lang.String newName)
                     throws java.io.IOException
        Description copied from interface: UsbFile
        Set a new name for this file or directory.
        Parameters:
        newName - The new name.
        Throws:
        java.io.IOException - If new name is already assigned or writing to the file system fails.
      • createdAt

        public long createdAt()
        Description copied from interface: UsbFile
        Returns the time this directory or file was created.
        Returns:
        Time in milliseconds since January 1 00:00:00, 1970 UTC
      • lastModified

        public long lastModified()
        Description copied from interface: UsbFile
        Returns the time this directory or file was last modified.
        Returns:
        Time in milliseconds since January 1 00:00:00, 1970 UTC
      • lastAccessed

        public long lastAccessed()
        Description copied from interface: UsbFile
        Returns the time this directory or file was last accessed.
        Returns:
        Time in milliseconds since January 1 00:00:00, 1970 UTC
      • getParent

        public UsbFile getParent()
        Description copied from interface: UsbFile
        Returns the parent directory for the file or directory or null if this is the root directory.
        Returns:
        The parent directory or null.
      • list

        public java.lang.String[] list()
                                throws java.io.IOException
        Description copied from interface: UsbFile
        Lists all files in the directory. Throws an exception if called on a file.
        Returns:
        String array containing all names in the directory.
        Throws:
        java.io.IOException - If reading fails
      • listFiles

        public UsbFile[] listFiles()
                            throws java.io.IOException
        Description copied from interface: UsbFile
        Lists all files in the directory. Throws an exception if called on a file.
        Returns:
        UsbFile array containing all files or directories in the directory.
        Throws:
        java.io.IOException - If reading fails
      • read

        public void read​(long offset,
                         java.nio.ByteBuffer destination)
                  throws java.io.IOException
        Description copied from interface: UsbFile
        Reads from a file or throws an exception if called on a directory.
        Parameters:
        offset - The offset in bytes where reading in the file should be begin.
        destination - Buffer the data shall be transferred to.
        Throws:
        java.io.IOException - If reading fails.
      • write

        public void write​(long offset,
                          java.nio.ByteBuffer source)
                   throws java.io.IOException
        Description copied from interface: UsbFile
        Writes to a file or throws an exception if called on a directory.
        Parameters:
        offset - The offset in bytes where writing in the file should be begin.
        source - Buffer which contains the data which shall be transferred.
        Throws:
        java.io.IOException - If writing fails.
      • flush

        public void flush()
                   throws java.io.IOException
        Description copied from interface: UsbFile
        Forces a write. Every change to the file is then committed to the disk. Throws an exception if called on directories.
        Throws:
        java.io.IOException - If flushing fails.
      • close

        public void close()
                   throws java.io.IOException
        Description copied from interface: UsbFile
        Closes and flushes the file. It is essential to close a file after making changes to it! Throws an exception if called on directories.
        Throws:
        java.io.IOException - If closing fails.
      • moveTo

        public void moveTo​(UsbFile destination)
                    throws java.io.IOException
        Description copied from interface: UsbFile
        This methods moves THIS item to the destination directory. Make sure that the destination is a directory, otherwise an exception will be thrown. Make also sure that both items are on the same logical device (disk, partition, file system). Moving between different file systems is currently not supported. If you want to do this, you have to manually copy the content and delete the old item.
        Parameters:
        destination - The directory where this item should be moved.
        Throws:
        java.io.IOException - If writing fails, or the operation cannot be done (eg. item already exists in the destination directory)
      • delete

        public void delete()
                    throws java.io.IOException
        Description copied from interface: UsbFile
        Deletes this file or directory from the parent directory.
        Throws:
        java.io.IOException - If operation fails due to write errors.