Class VirtualFile

  • All Implemented Interfaces:
    StorageFile

    public class VirtualFile
    extends java.lang.Object
    implements StorageFile
    Represents a file in the virtual file system.

    A virtual file is not created until one of the following methods are invoked:

    • createNewFile
    • getOutputStream
    • getRandomAccessFile
    • mkdir
    • mkdirs

    When a method that requires access to the file data or to know if the file exists or not, the associated data store is consulted.

    • Constructor Summary

      Constructors 
      Constructor Description
      VirtualFile​(java.lang.String path, DataStore dbData)
      Creates a new virtual file handle.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean canWrite()
      Tells if this file can be written to.
      boolean createNewFile()
      Creates the the file denoted by this virtual file object.
      boolean delete()
      Deletes this file, of if exists.
      boolean deleteAll()
      Deletes the path denoted by this file and all its contents, including sub directories.
      boolean exists()
      Tells if this file exists.
      java.lang.String getCanonicalPath()
      Converts this StorageFile into a canonical pathname string.
      private DataStoreEntry getEntry()
      Returns the data store entry denoted by this file, if it exists.
      int getExclusiveFileLock()
      Get an exclusive lock with this name.
      java.io.InputStream getInputStream()
      Returns an input stream for the file denoted.
      java.lang.String getName()  
      java.io.OutputStream getOutputStream()
      Obtains an output stream for the file denoted.
      java.io.OutputStream getOutputStream​(boolean append)
      Obtains an output stream for the file denoted.
      StorageFile getParentDir()
      Get the name of the parent directory if this name includes a parent.
      java.lang.String getPath()
      Returns the path of this file.
      StorageRandomAccessFile getRandomAccessFile​(java.lang.String mode)
      Creates a random access file that can be used to read and write from/into the file.
      boolean isDirectory()
      Tells if this file is a directory.
      long length()
      Returns the length of the file.
      void limitAccessToOwner()
      Use when creating a new file.
      java.lang.String[] list()
      Returns the contents of the directory denoted by this file, including any sub directories and their contents.
      boolean mkdir()
      Creates the directory denoted by this virtual file if it doesn't exist.
      boolean mkdirs()
      Creates the directory and any parent directories denoted by this virtual file.
      void releaseExclusiveFileLock()
      Release the resource associated with an earlier acquired exclusive lock releaseExclusiveFileLock() may delete the file
      boolean renameTo​(StorageFile newName)
      Renames the file denoted by this handle.
      boolean setReadOnly()
      Make the named file or directory read-only.
      java.lang.String toString()
      Returns a textual representation of this file.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • path

        private final java.lang.String path
        The path of this virtual file.
      • dStore

        private final DataStore dStore
        The data store this virtual file belongs to.
    • Constructor Detail

      • VirtualFile

        public VirtualFile​(java.lang.String path,
                           DataStore dbData)
        Creates a new virtual file handle.
        Parameters:
        path - the path of this virtual file
        dbData - the store this handle belongs to
    • Method Detail

      • list

        public java.lang.String[] list()
        Returns the contents of the directory denoted by this file, including any sub directories and their contents.
        Specified by:
        list in interface StorageFile
        Returns:
        A list of all files and directories, or null if this file doesn't denote a directory or doesn't exist.
      • canWrite

        public boolean canWrite()
        Tells if this file can be written to.
        Specified by:
        canWrite in interface StorageFile
        Returns:
        true if this file exists and can be written to, false otherwise.
      • exists

        public boolean exists()
        Tells if this file exists.
        Specified by:
        exists in interface StorageFile
        Returns:
        true if this file exists, false otherwise.
      • isDirectory

        public boolean isDirectory()
        Tells if this file is a directory.

        Note that false is returned if this path doesn't exist.

        Specified by:
        isDirectory in interface StorageFile
        Returns:
        true if this file represents an existing directoy, false otherwise.
      • delete

        public boolean delete()
        Deletes this file, of if exists.
        Specified by:
        delete in interface StorageFile
        Returns:
        true if this file exists and is successfully deleted, false otherwise.
      • deleteAll

        public boolean deleteAll()
        Deletes the path denoted by this file and all its contents, including sub directories.
        Specified by:
        deleteAll in interface StorageFile
        Returns:
        true if this file and all contents are successfully deleted, false otherwise.
      • getCanonicalPath

        public java.lang.String getCanonicalPath()
        Description copied from interface: StorageFile
        Converts this StorageFile into a canonical pathname string. The form of the canonical path is system dependent.
        Specified by:
        getCanonicalPath in interface StorageFile
        Returns:
        The pathname as a string.
      • getName

        public java.lang.String getName()
        Specified by:
        getName in interface StorageFile
        Returns:
        The last segment in the path name, "" if the path name sequence is empty.
      • createNewFile

        public boolean createNewFile()
        Creates the the file denoted by this virtual file object.
        Specified by:
        createNewFile in interface StorageFile
        Returns:
        true if the file was successfully created, false otherwise
      • renameTo

        public boolean renameTo​(StorageFile newName)
        Renames the file denoted by this handle.
        Specified by:
        renameTo in interface StorageFile
        Parameters:
        newName - the new name
        Returns:
        true if the fail was renamed, false otherwise
      • mkdir

        public boolean mkdir()
        Creates the directory denoted by this virtual file if it doesn't exist.

        For the directory to be created, it cannot exist already (either as a file or a directory), and any parent directories must exist.

        Specified by:
        mkdir in interface StorageFile
        Returns:
        true if the directory was created, false otherwise.
      • mkdirs

        public boolean mkdirs()
        Creates the directory and any parent directories denoted by this virtual file.

        For the directory to be created, it cannot exist already (either as a file or a directory), and all the parent elements most denote either existing directories or non-existing paths.

        Specified by:
        mkdirs in interface StorageFile
        Returns:
        true if the directory was created, false otherwise.
      • length

        public long length()
        Returns the length of the file.

        If the file doesn't exists, or is a directory, 0 is returned.

        Returns:
        The length of the existing file, or 0 if the path denotes a directory or a non-existing file.
      • getParentDir

        public StorageFile getParentDir()
        Description copied from interface: StorageFile
        Get the name of the parent directory if this name includes a parent.
        Specified by:
        getParentDir in interface StorageFile
        Returns:
        An StorageFile denoting the parent directory of this StorageFile, if it has a parent, null if it does not have a parent.
      • setReadOnly

        public boolean setReadOnly()
        Description copied from interface: StorageFile
        Make the named file or directory read-only. This interface does not specify whether this also makes the file undeletable.
        Specified by:
        setReadOnly in interface StorageFile
        Returns:
        true if the named file or directory was made read-only, or it already was read-only; false if not.
      • getOutputStream

        public java.io.OutputStream getOutputStream()
                                             throws java.io.FileNotFoundException
        Obtains an output stream for the file denoted.

        If the file already exists, it will be truncated.

        Specified by:
        getOutputStream in interface StorageFile
        Returns:
        An OutputStream-instance.
        Throws:
        java.io.FileNotFoundException - if the denoted path is a directory, the file is read-only, the file cannot be created or any other reason why an OutputStream cannot be created for the file
      • getOutputStream

        public java.io.OutputStream getOutputStream​(boolean append)
                                             throws java.io.FileNotFoundException
        Obtains an output stream for the file denoted.
        Specified by:
        getOutputStream in interface StorageFile
        Parameters:
        append - tells if the file should be appended or truncated
        Returns:
        An OutputStream-instance.
        Throws:
        java.io.FileNotFoundException - if the denoted path is a directory, the file is read-only, the file cannot be created or any other reason why an OutputStream cannot be created for the file
      • getInputStream

        public java.io.InputStream getInputStream()
                                           throws java.io.FileNotFoundException
        Returns an input stream for the file denoted.
        Specified by:
        getInputStream in interface StorageFile
        Returns:
        An InputStream instance.
        Throws:
        java.io.FileNotFoundException - if the file doesn't exists or it is a directory
      • getExclusiveFileLock

        public int getExclusiveFileLock()
                                 throws StandardException
        Description copied from interface: StorageFile
        Get an exclusive lock with this name. This is used to ensure that two or more JVMs do not open the same database at the same time. ny StorageFile that has getExclusiveFileLock() called on it is not intended to be read from or written to. It's sole purpose is to provide a locked entity to avoid multiple instances of Derby accessing the same database. getExclusiveFileLock() may delete or overwrite any existing file.
        Specified by:
        getExclusiveFileLock in interface StorageFile
        Returns:
        EXCLUSIVE_FILE_LOCK_NOT_AVAILABLE if the lock cannot be acquired because it is already held.
        EXCLUSIVE_FILE_LOCK if the lock was successfully acquired.
        NO_FILE_LOCK_SUPPORT if the system does not support exclusive locks.
        Throws:
        StandardException
      • getRandomAccessFile

        public StorageRandomAccessFile getRandomAccessFile​(java.lang.String mode)
                                                    throws java.io.FileNotFoundException
        Creates a random access file that can be used to read and write from/into the file.
        Specified by:
        getRandomAccessFile in interface StorageFile
        Parameters:
        mode - file mode, one of "r", "rw", "rws" or "rwd" (lower-case letters are required)
        Returns:
        A StorageRandomAccessFile-instance.
        Throws:
        java.lang.IllegalArgumentException - if the specificed mode is invalid
        java.io.FileNotFoundException - if the file denoted is a directory,
        See Also:
        java.io.RandomAccessFile
      • toString

        public java.lang.String toString()
        Returns a textual representation of this file.
        Overrides:
        toString in class java.lang.Object
        Returns:
        Textual representation.
      • getEntry

        private DataStoreEntry getEntry()
        Returns the data store entry denoted by this file, if it exists.
        Returns:
        The assoiciated DataStoreEntry if it exists, null if it doesn't exist.
      • limitAccessToOwner

        public void limitAccessToOwner()
        Description copied from interface: StorageFile
        Use when creating a new file. By default, a file created in an underlying file system, if applicable, will have read and write access for the file owner unless the property derby.useDefaultFilePermissions is set to true.
        Specified by:
        limitAccessToOwner in interface StorageFile