Top Description Fields Constructors Methods
org.h2.store

public Class FileStore

extends Object
Class Inheritance
Known Direct Subclasses
org.h2.security.SecureFileStore
Imports
java.io.IOException, java.lang.ref.Reference, java.nio.ByteBuffer, java.nio.channels.FileChannel, java.nio.charset.StandardCharsets, java.util.Arrays, org.h2.api.ErrorCode, org.h2.engine.Constants, .SysProperties, org.h2.message.DbException, org.h2.security.SecureFileStore, org.h2.store.fs.FileUtils

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

Modifier and TypeField and Description
private static final boolean
private Reference<?>
private boolean
private FileChannel
private long
private long
private final DataHandler
handler

The callback object is responsible to check access rights, and free up disk space if required.

private static final String
HEADER

The magic file header.

public static final int
HEADER_LENGTH

The size of the file header in bytes.

private FileLock
private final String
protected String
name

The file name.

Constructor Summary

AccessConstructor and Description
protected
FileStore(DataHandler
the callback object
handler
,
String
the file name
name
,
String
the access mode ("r", "rw", "rws", "rwd")
mode
)

Create a new file using the given settings.

Method Summary

Modifier and TypeMethod and Description
public void
autoDelete()

Automatically delete the file once it is no longer in use.

private void
private void
public void
close()

Close the file.

public void
closeAndDeleteSilently()

Close the file (ignoring exceptions) and delete the file.

public void
closeFile()

Close the file.

private void
closeFileSilently()

Just close the file, without setting the reference to null.

public void
closeSilently()

Close the file without throwing any exceptions.

protected byte[]

Returns:

the random salt or the magic
generateSalt
()

Generate the random salt bytes if required.

public long

Returns:

the location
getFilePointer
()

Get the current location of the file pointer.

public void
init()

Initialize the file.

protected void
initKey(byte[]
the salt
salt
)

Initialize the key using the given salt.

public long

Returns:

the file size
length
()

Get the file size in bytes.

public static FileStore

Returns:

the created object
open
(DataHandler
the data handler
handler
,
String
the file name
name
,
String
the access mode (r, rw, rws, rwd)
mode
)

Open a non encrypted file store with the given settings.

public static FileStore

Returns:

the created object
open
(DataHandler
the data handler
handler
,
String
the file name
name
,
String
the access mode (r, rw, rws, rwd)
mode
,
String
the name of the cipher algorithm
cipher
,
byte[]
the encryption key
key
)

Open an encrypted file store with the given settings.

public static FileStore

Returns:

the created object
open
(DataHandler
the data handler
handler
,
String
the file name
name
,
String
the access mode (r, rw, rws, rwd)
mode
,
String
the name of the cipher algorithm
cipher
,
byte[]
the encryption key
key
,
int
the number of iterations the key should be hashed
keyIterations
)

Open an encrypted file store with the given settings.

public void
openFile()

Re-open the file.

public void
readFully(byte[]
the target buffer
b
,
int
the offset
off
,
int
the number of bytes to read
len
)

Read a number of bytes.

public void
readFullyDirect(byte[]
the target buffer
b
,
int
the offset
off
,
int
the number of bytes to read
len
)

Read a number of bytes without decrypting.

public synchronized void
releaseLock()

Release the file lock.

public void
seek(long
the location
pos
)

Go to the specified file location.

public void
setCheckedWriting(boolean value)

public void
setLength(long
the new file size
newLength
)

Set the length of the file.

public void
stopAutoDelete()

No longer automatically delete the file once it is no longer in use.

public void
sync()

Call fsync.

private static void
trace(String method, String fileName, Object o)

public synchronized boolean

Returns:

true if successful
tryLock
()

Try to lock the file.

public void
write(byte[]
the source buffer
b
,
int
the offset
off
,
int
the number of bytes to write
len
)

Write a number of bytes.

protected void
writeDirect(byte[]
the source buffer
b
,
int
the offset
off
,
int
the number of bytes to write
len
)

Write a number of bytes without encrypting.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ASSERTback to summary
private static final boolean ASSERT
autoDeleteReferenceback to summary
private Reference<?> autoDeleteReference
checkedWritingback to summary
private boolean checkedWriting
fileback to summary
private FileChannel file
fileLengthback to summary
private long fileLength
filePosback to summary
private long filePos
handlerback to summary
private final DataHandler handler

The callback object is responsible to check access rights, and free up disk space if required.

HEADER_LENGTHback to summary
public static final int HEADER_LENGTH

The size of the file header in bytes.

lockback to summary
private FileLock lock
modeback to summary
private final String mode
nameback to summary
protected String name

The file name.

Constructor Detail

FileStoreback to summary
protected FileStore(DataHandler handler, String name, String mode)

Create a new file using the given settings.

Parameters
handler:DataHandler

the callback object

name:String

the file name

mode:String

the access mode ("r", "rw", "rws", "rwd")

Method Detail

autoDeleteback to summary
public void autoDelete()

Automatically delete the file once it is no longer in use.

checkPowerOffback to summary
private void checkPowerOff()
checkWritingAllowedback to summary
private void checkWritingAllowed()
closeback to summary
public void close()

Close the file.

closeAndDeleteSilentlyback to summary
public void closeAndDeleteSilently()

Close the file (ignoring exceptions) and delete the file.

closeFileback to summary
public void closeFile() throws IOException

Close the file. The file may later be re-opened using openFile.

Exceptions
IOException:
on failure
closeFileSilentlyback to summary
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.

closeSilentlyback to summary
public void closeSilently()

Close the file without throwing any exceptions. Exceptions are simply ignored.

generateSaltback to summary
protected byte[] generateSalt()

Generate the random salt bytes if required.

Returns:byte[]

the random salt or the magic

getFilePointerback to summary
public long getFilePointer()

Get the current location of the file pointer.

Returns:long

the location

initback to summary
public void init()

Initialize the file. This method will write or check the file header if required.

initKeyback to summary
protected void initKey(byte[] salt)

Initialize the key using the given salt.

Parameters
salt:byte[]

the salt

Annotations
@SuppressWarnings:unused
lengthback to summary
public long length()

Get the file size in bytes.

Returns:long

the file size

openback to summary
public static FileStore open(DataHandler handler, String name, String mode)

Open a non encrypted file store with the given settings.

Parameters
handler:DataHandler

the data handler

name:String

the file name

mode:String

the access mode (r, rw, rws, rwd)

Returns:FileStore

the created object

openback to summary
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:DataHandler

the data handler

name:String

the file name

mode:String

the access mode (r, rw, rws, rwd)

cipher:String

the name of the cipher algorithm

key:byte[]

the encryption key

Returns:FileStore

the created object

openback to summary
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:DataHandler

the data handler

name:String

the file name

mode:String

the access mode (r, rw, rws, rwd)

cipher:String

the name of the cipher algorithm

key:byte[]

the encryption key

keyIterations:int

the number of iterations the key should be hashed

Returns:FileStore

the created object

openFileback to summary
public void openFile() throws IOException

Re-open the file. The file pointer will be reset to the previous location.

Exceptions
IOException:
on failure
readFullyback to summary
public void readFully(byte[] b, int off, int len)

Read a number of bytes.

Parameters
b:byte[]

the target buffer

off:int

the offset

len:int

the number of bytes to read

readFullyDirectback to summary
public void readFullyDirect(byte[] b, int off, int len)

Read a number of bytes without decrypting.

Parameters
b:byte[]

the target buffer

off:int

the offset

len:int

the number of bytes to read

releaseLockback to summary
public synchronized void releaseLock()

Release the file lock.

seekback to summary
public void seek(long pos)

Go to the specified file location.

Parameters
pos:long

the location

setCheckedWritingback to summary
public void setCheckedWriting(boolean value)
setLengthback to summary
public void setLength(long newLength)

Set the length of the file. This will expand or shrink the file.

Parameters
newLength:long

the new file size

stopAutoDeleteback to summary
public void stopAutoDelete()

No longer automatically delete the file once it is no longer in use.

syncback to summary
public void sync()

Call fsync. Depending on the operating system and hardware, this may or may not in fact write the changes.

traceback to summary
private static void trace(String method, String fileName, Object o)
tryLockback to summary
public synchronized boolean tryLock()

Try to lock the file.

Returns:boolean

true if successful

writeback to summary
public void write(byte[] b, int off, int len)

Write a number of bytes.

Parameters
b:byte[]

the source buffer

off:int

the offset

len:int

the number of bytes to write

writeDirectback to summary
protected void writeDirect(byte[] b, int off, int len)

Write a number of bytes without encrypting.

Parameters
b:byte[]

the source buffer

off:int

the offset

len:int

the number of bytes to write