Class VirtualFile

java.lang.Object
org.apache.derby.impl.io.vfmem.VirtualFile
All Implemented Interfaces:
StorageFile

public class VirtualFile extends 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.

  • Field Details

    • path

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

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

    • VirtualFile

      public VirtualFile(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 Details

    • list

      public 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.
    • getPath

      public String getPath()
      Returns the path of this file.
      Specified by:
      getPath in interface StorageFile
      Returns:
      The path of this file.
      See Also:
    • getCanonicalPath

      public 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 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 OutputStream getOutputStream() throws 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:
      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 OutputStream getOutputStream(boolean append) throws 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:
      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 InputStream getInputStream() throws FileNotFoundException
      Returns an input stream for the file denoted.
      Specified by:
      getInputStream in interface StorageFile
      Returns:
      An InputStream instance.
      Throws:
      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
    • releaseExclusiveFileLock

      public void releaseExclusiveFileLock()
      Description copied from interface: StorageFile
      Release the resource associated with an earlier acquired exclusive lock releaseExclusiveFileLock() may delete the file
      Specified by:
      releaseExclusiveFileLock in interface StorageFile
      See Also:
    • getRandomAccessFile

      public StorageRandomAccessFile getRandomAccessFile(String mode) throws 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:
      IllegalArgumentException - if the specificed mode is invalid
      FileNotFoundException - if the file denoted is a directory,
      See Also:
    • toString

      public String toString()
      Returns a textual representation of this file.
      Overrides:
      toString in class 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