Package org.h2.store
Class FileStore
- java.lang.Object
-
- org.h2.store.FileStore
-
- Direct Known Subclasses:
SecureFileStore
public class FileStore extends java.lang.ObjectThis class is an abstraction of a random access file. Each file contains a magic header, and reading / writing is done in blocks. See alsoSecureFileStore
-
-
Field Summary
Fields Modifier and Type Field Description private static booleanASSERTprivate java.lang.ref.Reference<?>autoDeleteReferenceprivate booleancheckedWritingprivate java.nio.channels.FileChannelfileprivate longfileLengthprivate longfilePosprivate DataHandlerhandlerThe callback object is responsible to check access rights, and free up disk space if required.private static java.lang.StringHEADERThe magic file header.static intHEADER_LENGTHThe size of the file header in bytes.private java.nio.channels.FileLocklockprivate java.lang.Stringmodeprotected java.lang.StringnameThe file name.
-
Constructor Summary
Constructors Modifier Constructor Description protectedFileStore(DataHandler handler, java.lang.String name, java.lang.String mode)Create a new file using the given settings.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidautoDelete()Automatically delete the file once it is no longer in use.private voidcheckPowerOff()private voidcheckWritingAllowed()voidclose()Close the file.voidcloseAndDeleteSilently()Close the file (ignoring exceptions) and delete the file.voidcloseFile()Close the file.private voidcloseFileSilently()Just close the file, without setting the reference to null.voidcloseSilently()Close the file without throwing any exceptions.protected byte[]generateSalt()Generate the random salt bytes if required.longgetFilePointer()Get the current location of the file pointer.voidinit()Initialize the file.protected voidinitKey(byte[] salt)Initialize the key using the given salt.longlength()Get the file size in bytes.static FileStoreopen(DataHandler handler, java.lang.String name, java.lang.String mode)Open a non encrypted file store with the given settings.static FileStoreopen(DataHandler handler, java.lang.String name, java.lang.String mode, java.lang.String cipher, byte[] key)Open an encrypted file store with the given settings.static FileStoreopen(DataHandler handler, java.lang.String name, java.lang.String mode, java.lang.String cipher, byte[] key, int keyIterations)Open an encrypted file store with the given settings.voidopenFile()Re-open the file.voidreadFully(byte[] b, int off, int len)Read a number of bytes.voidreadFullyDirect(byte[] b, int off, int len)Read a number of bytes without decrypting.voidreleaseLock()Release the file lock.voidseek(long pos)Go to the specified file location.voidsetCheckedWriting(boolean value)voidsetLength(long newLength)Set the length of the file.voidstopAutoDelete()No longer automatically delete the file once it is no longer in use.voidsync()Call fsync.private static voidtrace(java.lang.String method, java.lang.String fileName, java.lang.Object o)booleantryLock()Try to lock the file.voidwrite(byte[] b, int off, int len)Write a number of bytes.protected voidwriteDirect(byte[] b, int off, int len)Write a number of bytes without encrypting.
-
-
-
Field Detail
-
HEADER_LENGTH
public static final int HEADER_LENGTH
The size of the file header in bytes.- See Also:
- Constant Field Values
-
HEADER
private static final java.lang.String HEADER
The magic file header.
-
ASSERT
private static final boolean ASSERT
-
name
protected java.lang.String name
The file name.
-
handler
private final DataHandler handler
The callback object is responsible to check access rights, and free up disk space if required.
-
file
private java.nio.channels.FileChannel file
-
filePos
private long filePos
-
fileLength
private long fileLength
-
autoDeleteReference
private java.lang.ref.Reference<?> autoDeleteReference
-
checkedWriting
private boolean checkedWriting
-
mode
private final java.lang.String mode
-
lock
private java.nio.channels.FileLock lock
-
-
Constructor Detail
-
FileStore
protected FileStore(DataHandler handler, java.lang.String name, java.lang.String mode)
Create a new file using the given settings.- Parameters:
handler- the callback objectname- the file namemode- the access mode ("r", "rw", "rws", "rwd")
-
-
Method Detail
-
open
public static FileStore open(DataHandler handler, java.lang.String name, java.lang.String mode)
Open a non encrypted file store with the given settings.- Parameters:
handler- the data handlername- the file namemode- the access mode (r, rw, rws, rwd)- Returns:
- the created object
-
open
public static FileStore open(DataHandler handler, java.lang.String name, java.lang.String mode, java.lang.String cipher, byte[] key)
Open an encrypted file store with the given settings.- Parameters:
handler- the data handlername- the file namemode- the access mode (r, rw, rws, rwd)cipher- the name of the cipher algorithmkey- the encryption key- Returns:
- the created object
-
open
public static FileStore open(DataHandler handler, java.lang.String name, java.lang.String mode, java.lang.String cipher, byte[] key, int keyIterations)
Open an encrypted file store with the given settings.- Parameters:
handler- the data handlername- the file namemode- the access mode (r, rw, rws, rwd)cipher- the name of the cipher algorithmkey- the encryption keykeyIterations- 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)
-
checkWritingAllowed
private void checkWritingAllowed()
-
checkPowerOff
private void checkPowerOff()
-
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 bufferoff- the offsetlen- 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 bufferoff- the offsetlen- 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 bufferoff- the offsetlen- 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 bufferoff- the offsetlen- 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 java.io.IOExceptionClose the file. The file may later be re-opened using openFile.- Throws:
java.io.IOException- on failure
-
closeFileSilently
private void closeFileSilently()
Just close the file, without setting the reference to null. This method is called when writing failed. The reference is not set to null so that there are no NullPointerExceptions later on.
-
openFile
public void openFile() throws java.io.IOExceptionRe-open the file. The file pointer will be reset to the previous location.- Throws:
java.io.IOException- on failure
-
trace
private static void trace(java.lang.String method, java.lang.String fileName, java.lang.Object o)
-
tryLock
public boolean tryLock()
Try to lock the file.- Returns:
- true if successful
-
releaseLock
public void releaseLock()
Release the file lock.
-
-