Package com.github.mjdev.libaums.fs
Interface UsbFile
-
- All Superinterfaces:
java.lang.AutoCloseable,java.io.Closeable
- All Known Implementing Classes:
AbstractUsbFile,FatDirectory,FatFile
public interface UsbFile extends java.io.CloseableThis class represents either an file or an directory. This can be determined byisDirectory(). Not all methods make sense for both cases. For example if representing a file methods likecreateDirectory(String)orcreateFile(String)do not make sense and will throw an exception. On the other hand methods likeread(long, ByteBuffer)orwrite(long, ByteBuffer)do not make sense for directories and will throw an exception!
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.Stringseparator
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voidclose()Closes and flushes the file.longcreatedAt()Returns the time this directory or file was created.UsbFilecreateDirectory(java.lang.String name)This methods creates a new directory with the given name and returns it.UsbFilecreateFile(java.lang.String name)This methods creates a new file with the given name and returns it.voiddelete()Deletes this file or directory from the parent directory.voidflush()Forces a write.java.lang.StringgetAbsolutePath()Absolute path of a file or directory.longgetLength()Returns the file length or throws an exception if called on a directory.java.lang.StringgetName()Actual file or directory name or '/' for root directory.UsbFilegetParent()Returns the parent directory for the file or directory or null if this is the root directory.booleanisDirectory()booleanisRoot()longlastAccessed()Returns the time this directory or file was last accessed.longlastModified()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.voidmoveTo(UsbFile destination)This methods moves THIS item to the destination directory.voidread(long offset, java.nio.ByteBuffer destination)Reads from a file or throws an exception if called on a directory.UsbFilesearch(java.lang.String path)Tries to search a corresponding entry associated with the path parameter.voidsetLength(long newLength)Sets the new file length.voidsetName(java.lang.String newName)Set a new name for this file or directory.voidwrite(long offset, java.nio.ByteBuffer source)Writes to a file or throws an exception if called on a directory.
-
-
-
Field Detail
-
separator
static final java.lang.String separator
- See Also:
- Constant Field Values
-
-
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.IOExceptionSet 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.IOExceptionLists 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.IOExceptionSets 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.IOExceptionReads 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.IOExceptionWrites 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.IOExceptionForces 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.IOExceptionCloses 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:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.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.IOExceptionDeletes 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.
-
-