Interface UsbFile

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String separator  
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      void close()
      Closes and flushes the file.
      long createdAt()
      Returns the time this directory or file was created.
      UsbFile createDirectory​(java.lang.String name)
      This methods creates a new directory with the given name and returns it.
      UsbFile 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.
      java.lang.String getAbsolutePath()
      Absolute path of a file or directory.
      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.
      UsbFile search​(java.lang.String path)
      Tries to search a corresponding entry associated with the path parameter.
      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.
    • Method Detail

      • search

        @Nullable
        UsbFile search​(@NonNull
                       java.lang.String path)
                throws java.io.IOException
        Tries to search a corresponding entry associated with the path parameter. Path separator is '/'. Parameter path must not start with an '/' (except if querying from root directory). Path is treated relative to current UsbFile.
        Parameters:
        path - The path to the resource to search.
        Returns:
        UsbFile directory or file if found, null otherwise.
        Throws:
        java.io.IOException
      • isDirectory

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

        java.lang.String getName()
        Actual file or directory name or '/' for root directory.
        Returns:
        The name of the file or directory.
      • getAbsolutePath

        java.lang.String getAbsolutePath()
        Absolute path of a file or directory.
        Returns:
        Absolute path seperated with '/' and beginning with an '/'
      • setName

        void setName​(java.lang.String newName)
              throws java.io.IOException
        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

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

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

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

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

        java.lang.String[] list()
                         throws java.io.IOException
        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

        UsbFile[] listFiles()
                     throws java.io.IOException
        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
      • getLength

        long getLength()
        Returns the file length or throws an exception if called on a directory.
        Returns:
        File length in bytes.
      • setLength

        void setLength​(long newLength)
                throws java.io.IOException
        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.
        Throws:
        java.io.IOException - If requesting the needed space fails.
      • read

        void read​(long offset,
                  java.nio.ByteBuffer destination)
           throws java.io.IOException
        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

        void write​(long offset,
                   java.nio.ByteBuffer source)
            throws java.io.IOException
        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

        void flush()
            throws java.io.IOException
        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

        void close()
            throws java.io.IOException
        Closes and flushes the file. It is essential to close a file after making changes to it! Throws an exception if called on directories.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException - If closing fails.
      • createDirectory

        UsbFile createDirectory​(java.lang.String name)
                         throws java.io.IOException
        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.
      • createFile

        UsbFile createFile​(java.lang.String name)
                    throws java.io.IOException
        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.
      • moveTo

        void moveTo​(UsbFile destination)
             throws java.io.IOException
        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

        void delete()
             throws java.io.IOException
        Deletes this file or directory from the parent directory.
        Throws:
        java.io.IOException - If operation fails due to write errors.
      • isRoot

        boolean isRoot()
        Returns:
        True if the current directory is the root directory, false if not or a file.