Package org.h2.store

Class FileStore

java.lang.Object
org.h2.store.FileStore
Direct Known Subclasses:
SecureFileStore

public class FileStore extends Object
This class is an abstraction of a random access file. Each file contains a magic header, and reading / writing is done in blocks. See also SecureFileStore
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The size of the file header in bytes.
    protected String
    The file name.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    FileStore(DataHandler handler, String name, String mode)
    Create a new file using the given settings.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Automatically delete the file once it is no longer in use.
    void
    Close the file.
    void
    Close the file (ignoring exceptions) and delete the file.
    void
    Close the file.
    void
    Close the file without throwing any exceptions.
    protected byte[]
    Generate the random salt bytes if required.
    long
    Get the current location of the file pointer.
    void
    Initialize the file.
    protected void
    initKey(byte[] salt)
    Initialize the key using the given salt.
    long
    Get the file size in bytes.
    static FileStore
    open(DataHandler handler, String name, String mode)
    Open a non encrypted file store with the given settings.
    static FileStore
    open(DataHandler handler, String name, String mode, String cipher, byte[] key)
    Open an encrypted file store with the given settings.
    static FileStore
    open(DataHandler handler, String name, String mode, String cipher, byte[] key, int keyIterations)
    Open an encrypted file store with the given settings.
    void
    Re-open the file.
    void
    readFully(byte[] b, int off, int len)
    Read a number of bytes.
    void
    readFullyDirect(byte[] b, int off, int len)
    Read a number of bytes without decrypting.
    void
    Release the file lock.
    void
    seek(long pos)
    Go to the specified file location.
    void
    setCheckedWriting(boolean value)
     
    void
    setLength(long newLength)
    Set the length of the file.
    void
    No longer automatically delete the file once it is no longer in use.
    void
    Call fsync.
    boolean
    Try to lock the file.
    void
    write(byte[] b, int off, int len)
    Write a number of bytes.
    protected void
    writeDirect(byte[] b, int off, int len)
    Write a number of bytes without encrypting.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • HEADER_LENGTH

      public static final int HEADER_LENGTH
      The size of the file header in bytes.
      See Also:
    • name

      protected String name
      The file name.
  • Constructor Details

    • FileStore

      protected FileStore(DataHandler handler, String name, String mode)
      Create a new file using the given settings.
      Parameters:
      handler - the callback object
      name - the file name
      mode - the access mode ("r", "rw", "rws", "rwd")
  • Method Details

    • open

      public static FileStore open(DataHandler handler, String name, String mode)
      Open a non encrypted file store with the given settings.
      Parameters:
      handler - the data handler
      name - the file name
      mode - the access mode (r, rw, rws, rwd)
      Returns:
      the created object
    • open

      public static FileStore open(DataHandler handler, String name, String mode, String cipher, byte[] key)
      Open an encrypted file store with the given settings.
      Parameters:
      handler - the data handler
      name - the file name
      mode - the access mode (r, rw, rws, rwd)
      cipher - the name of the cipher algorithm
      key - the encryption key
      Returns:
      the created object
    • open

      public static FileStore open(DataHandler handler, String name, String mode, String cipher, byte[] key, int keyIterations)
      Open an encrypted file store with the given settings.
      Parameters:
      handler - the data handler
      name - the file name
      mode - the access mode (r, rw, rws, rwd)
      cipher - the name of the cipher algorithm
      key - the encryption key
      keyIterations - the number of iterations the key should be hashed
      Returns:
      the created object
    • generateSalt

      protected byte[] generateSalt()
      Generate the random salt bytes if required.
      Returns:
      the random salt or the magic
    • initKey

      protected void initKey(byte[] salt)
      Initialize the key using the given salt.
      Parameters:
      salt - the salt
    • setCheckedWriting

      public void setCheckedWriting(boolean value)
    • init

      public void init()
      Initialize the file. This method will write or check the file header if required.
    • close

      public void close()
      Close the file.
    • closeSilently

      public void closeSilently()
      Close the file without throwing any exceptions. Exceptions are simply ignored.
    • closeAndDeleteSilently

      public void closeAndDeleteSilently()
      Close the file (ignoring exceptions) and delete the file.
    • readFullyDirect

      public void readFullyDirect(byte[] b, int off, int len)
      Read a number of bytes without decrypting.
      Parameters:
      b - the target buffer
      off - the offset
      len - the number of bytes to read
    • readFully

      public void readFully(byte[] b, int off, int len)
      Read a number of bytes.
      Parameters:
      b - the target buffer
      off - the offset
      len - the number of bytes to read
    • seek

      public void seek(long pos)
      Go to the specified file location.
      Parameters:
      pos - the location
    • writeDirect

      protected void writeDirect(byte[] b, int off, int len)
      Write a number of bytes without encrypting.
      Parameters:
      b - the source buffer
      off - the offset
      len - the number of bytes to write
    • write

      public void write(byte[] b, int off, int len)
      Write a number of bytes.
      Parameters:
      b - the source buffer
      off - the offset
      len - the number of bytes to write
    • setLength

      public void setLength(long newLength)
      Set the length of the file. This will expand or shrink the file.
      Parameters:
      newLength - the new file size
    • length

      public long length()
      Get the file size in bytes.
      Returns:
      the file size
    • getFilePointer

      public long getFilePointer()
      Get the current location of the file pointer.
      Returns:
      the location
    • sync

      public void sync()
      Call fsync. Depending on the operating system and hardware, this may or may not in fact write the changes.
    • autoDelete

      public void autoDelete()
      Automatically delete the file once it is no longer in use.
    • stopAutoDelete

      public void stopAutoDelete()
      No longer automatically delete the file once it is no longer in use.
    • closeFile

      public void closeFile() throws IOException
      Close the file. The file may later be re-opened using openFile.
      Throws:
      IOException - on failure
    • openFile

      public void openFile() throws IOException
      Re-open the file. The file pointer will be reset to the previous location.
      Throws:
      IOException - on failure
    • tryLock

      public boolean tryLock()
      Try to lock the file.
      Returns:
      true if successful
    • releaseLock

      public void releaseLock()
      Release the file lock.