Top Description Constructors Methods
org.h2.store.fs

public abstract Class FileBase

extends FileChannel
Class Inheritance
Known Direct Subclasses
org.h2.store.fs.FileBaseDefault, org.h2.store.fs.rec.FileRec, org.h2.store.fs.retry.FileRetryOnInterrupt, org.h2.store.fs.zip.FileZip, org.h2.mvstore.cache.FilePathCache.FileCache
Imports
java.io.IOException, java.nio.ByteBuffer, .MappedByteBuffer, java.nio.channels.FileChannel, .FileLock, .ReadableByteChannel, .WritableByteChannel

The base class for file implementations.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
force(boolean
If true then this method is required to force changes to both the file's content and metadata to be written to storage; otherwise, it need only force content changes to be written
metaData
)

Implements abstract java.nio.channels.FileChannel.force.

Forces any updates to this channel's file to be written to the storage device that contains it.

protected void
public FileLock
lock(long
The position at which the locked region is to start; must be non-negative
position
,
long
The size of the locked region; must be non-negative, and the sum position + size must be non-negative. A value of zero means to lock all bytes from the specified starting position to the end of the file, regardless of whether the file is subsequently extended or truncated
size
,
boolean
true to request a shared lock, in which case this channel must be open for reading (and possibly writing); false to request an exclusive lock, in which case this channel must be open for writing (and possibly reading)
shared
)

Implements abstract java.nio.channels.FileChannel.lock.

Acquires a lock on the given region of this channel's file.

public MappedByteBuffer
map(FileChannel.MapMode
One of the constants READ_ONLY, READ_WRITE, or PRIVATE defined in the MapMode class, according to whether the file is to be mapped read-only, read/write, or privately (copy-on-write), respectively, or an implementation specific map mode
mode
,
long
The position within the file at which the mapped region is to start; must be non-negative
position
,
long
The size of the region to be mapped; must be non-negative and no greater than java.lang.Integer#MAX_VALUE
size
)

Implements abstract java.nio.channels.FileChannel.map.

Maps a region of this channel's file directly into memory.

public synchronized int
read(ByteBuffer
The buffer into which bytes are to be transferred
dst
,
long
The file position at which the transfer is to begin; must be non-negative
position
)

Implements abstract java.nio.channels.FileChannel.read.

Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.

public long
read(ByteBuffer[]
The buffers into which bytes are to be transferred
dsts
,
int
The offset within the buffer array of the first buffer into which bytes are to be transferred; must be non-negative and no larger than dsts.length
offset
,
int
The maximum number of buffers to be accessed; must be non-negative and no larger than dsts.length - offset
length
)

Implements abstract java.nio.channels.FileChannel.read.

Implements java.nio.channels.ScatteringByteChannel.read.

Reads a sequence of bytes from this channel into a subsequence of the given buffers.

public long
transferFrom(ReadableByteChannel
The source channel
src
,
long
The file position at which the transfer is to begin; must be non-negative
position
,
long
The maximum number of bytes to be transferred; must be non-negative
count
)

Implements abstract java.nio.channels.FileChannel.transferFrom.

Transfers bytes into this channel's file from the given readable byte channel.

public long
transferTo(long
The position within the file at which the transfer is to begin; must be non-negative
position
,
long
The maximum number of bytes to be transferred; must be non-negative
count
,
WritableByteChannel
The target channel
target
)

Implements abstract java.nio.channels.FileChannel.transferTo.

Transfers bytes from this channel's file to the given writable byte channel.

public FileLock
tryLock(long
The position at which the locked region is to start; must be non-negative
position
,
long
The size of the locked region; must be non-negative, and the sum position + size must be non-negative. A value of zero means to lock all bytes from the specified starting position to the end of the file, regardless of whether the file is subsequently extended or truncated
size
,
boolean
true to request a shared lock, false to request an exclusive lock
shared
)

Implements abstract java.nio.channels.FileChannel.tryLock.

Attempts to acquire a lock on the given region of this channel's file.

public synchronized int
write(ByteBuffer
The buffer from which bytes are to be transferred
src
,
long
The file position at which the transfer is to begin; must be non-negative
position
)

Implements abstract java.nio.channels.FileChannel.write.

Writes a sequence of bytes to this channel from the given buffer, starting at the given file position.

public long
write(ByteBuffer[]
The buffers from which bytes are to be retrieved
srcs
,
int
The offset within the buffer array of the first buffer from which bytes are to be retrieved; must be non-negative and no larger than srcs.length
offset
,
int
The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset
length
)

Implements abstract java.nio.channels.FileChannel.write.

Implements java.nio.channels.GatheringByteChannel.write.

Writes a sequence of bytes to this channel from a subsequence of the given buffers.

Inherited from java.nio.channels.FileChannel:
lockmapopenopenpositionpositionreadreadsizetruncatetryLockwritewrite

Constructor Detail

FileBaseback to summary
public FileBase()

Method Detail

forceback to summary
public void force(boolean metaData) throws IOException

Implements abstract java.nio.channels.FileChannel.force.

Doc from java.nio.channels.FileChannel.force.

Forces any updates to this channel's file to be written to the storage device that contains it.

If this channel's file resides on a local storage device then when this method returns it is guaranteed that all changes made to the file since this channel was created, or since this method was last invoked, will have been written to that device. This is useful for ensuring that critical information is not lost in the event of a system crash.

If the file does not reside on a local device then no such guarantee is made.

The metaData parameter can be used to limit the number of I/O operations that this method is required to perform. Passing false for this parameter indicates that only updates to the file's content need be written to storage; passing true indicates that updates to both the file's content and metadata must be written, which generally requires at least one more I/O operation. Whether this parameter actually has any effect is dependent upon the underlying operating system and is therefore unspecified.

Invoking this method may cause an I/O operation to occur even if the channel was only opened for reading. Some operating systems, for example, maintain a last-access time as part of a file's metadata, and this time is updated whenever the file is read. Whether or not this is actually done is system-dependent and is therefore unspecified.

This method is only guaranteed to force changes that were made to this channel's file via the methods defined in this class, or the methods defined by java.io.FileOutputStream or java.io.RandomAccessFile when the channel was obtained with the getChannel method. It may or may not force changes that were made by modifying the content of a mapped byte buffer obtained by invoking the map method. Invoking the force method of the mapped byte buffer will force changes made to the buffer's content to be written.

Parameters
metaData:boolean

If true then this method is required to force changes to both the file's content and metadata to be written to storage; otherwise, it need only force content changes to be written

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
implCloseChannelback to summary
protected void implCloseChannel() throws IOException

Implements abstract java.nio.channels.spi.AbstractInterruptibleChannel.implCloseChannel.

Doc from java.nio.channels.spi.AbstractInterruptibleChannel.implCloseChannel.

Closes this channel.

This method is invoked by the close method in order to perform the actual work of closing the channel. This method is only invoked if the channel has not yet been closed, and it is never invoked more than once.

An implementation of this method must arrange for any other thread that is blocked in an I/O operation upon this channel to return immediately, either by throwing an exception or by returning normally.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs while closing the channel
lockback to summary
public FileLock lock(long position, long size, boolean shared) throws IOException

Implements abstract java.nio.channels.FileChannel.lock.

Doc from java.nio.channels.FileChannel.lock.

Acquires a lock on the given region of this channel's file.

An invocation of this method will block until the region can be locked, this channel is closed, or the invoking thread is interrupted, whichever comes first.

If this channel is closed by another thread during an invocation of this method then an AsynchronousCloseException will be thrown.

If the invoking thread is interrupted while waiting to acquire the lock then its interrupt status will be set and a FileLockInterruptionException will be thrown. If the invoker's interrupt status is set when this method is invoked then that exception will be thrown immediately; the thread's interrupt status will not be changed.

The region specified by the position and size parameters need not be contained within, or even overlap, the actual underlying file. Lock regions are fixed in size; if a locked region initially contains the end of the file and the file grows beyond the region then the new portion of the file will not be covered by the lock. If a file is expected to grow in size and a lock on the entire file is required then a region starting at zero, and no smaller than the expected maximum size of the file, should be locked. The zero-argument lock() method simply locks a region of size Long#MAX_VALUE. If the position is non-negative and the size is zero, then a lock of size Long.MAX_VALUE - position is returned.

Some operating systems do not support shared locks, in which case a request for a shared lock is automatically converted into a request for an exclusive lock. Whether the newly-acquired lock is shared or exclusive may be tested by invoking the resulting lock object's isShared method.

File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine.

Parameters
position:long

The position at which the locked region is to start; must be non-negative

size:long

The size of the locked region; must be non-negative, and the sum position + size must be non-negative. A value of zero means to lock all bytes from the specified starting position to the end of the file, regardless of whether the file is subsequently extended or truncated

shared:boolean

true to request a shared lock, in which case this channel must be open for reading (and possibly writing); false to request an exclusive lock, in which case this channel must be open for writing (and possibly reading)

Returns:FileLock

A lock object representing the newly-acquired lock

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
mapback to summary
public MappedByteBuffer map(FileChannel.MapMode mode, long position, long size) throws IOException

Implements abstract java.nio.channels.FileChannel.map.

Doc from java.nio.channels.FileChannel.map.

Maps a region of this channel's file directly into memory.

The mode parameter specifies how the region of the file is mapped and may be one of the following modes:

  • Read-only: Any attempt to modify the resulting buffer will cause a java.nio.ReadOnlyBufferException to be thrown. (MapMode.READ_ONLY)

  • Read/write: Changes made to the resulting buffer will eventually be propagated to the file; they may or may not be made visible to other programs that have mapped the same file. (MapMode.READ_WRITE)

  • Private: Changes made to the resulting buffer will not be propagated to the file and will not be visible to other programs that have mapped the same file; instead, they will cause private copies of the modified portions of the buffer to be created. (MapMode.PRIVATE)

An implementation may support additional map modes.

For a read-only mapping, this channel must have been opened for reading; for a read/write or private mapping, this channel must have been opened for both reading and writing.

The mapped byte buffer returned by this method will have a position of zero and a limit and capacity of size; its mark will be undefined. The buffer and the mapping that it represents will remain valid until the buffer itself is garbage-collected.

A mapping, once established, is not dependent upon the file channel that was used to create it. Closing the channel, in particular, has no effect upon the validity of the mapping.

Many of the details of memory-mapped files are inherently dependent upon the underlying operating system and are therefore unspecified. The behavior of this method when the requested region is not completely contained within this channel's file is unspecified. Whether changes made to the content or size of the underlying file, by this program or another, are propagated to the buffer is unspecified. The rate at which changes to the buffer are propagated to the file is unspecified.

For most operating systems, mapping a file into memory is more expensive than reading or writing a few tens of kilobytes of data via the usual read and write methods. From the standpoint of performance it is generally only worth mapping relatively large files into memory.

Parameters
mode:FileChannel.MapMode

One of the constants READ_ONLY, READ_WRITE, or PRIVATE defined in the MapMode class, according to whether the file is to be mapped read-only, read/write, or privately (copy-on-write), respectively, or an implementation specific map mode

position:long

The position within the file at which the mapped region is to start; must be non-negative

size:long

The size of the region to be mapped; must be non-negative and no greater than java.lang.Integer#MAX_VALUE

Returns:MappedByteBuffer

The mapped byte buffer

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
readback to summary
public synchronized int read(ByteBuffer dst, long position) throws IOException

Implements abstract java.nio.channels.FileChannel.read.

Doc from java.nio.channels.FileChannel.read.

Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.

This method works in the same manner as the read(ByteBuffer) method, except that bytes are read starting at the given file position rather than at the channel's current position. This method does not modify this channel's position. If the given position is greater than or equal to the file's current size then no bytes are read.

Parameters
dst:ByteBuffer

The buffer into which bytes are to be transferred

position:long

The file position at which the transfer is to begin; must be non-negative

Returns:int

The number of bytes read, possibly zero, or -1 if the given position is greater than or equal to the file's current size

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
readback to summary
public long read(ByteBuffer[] dsts, int offset, int length) throws IOException

Implements abstract java.nio.channels.FileChannel.read.

Implements java.nio.channels.ScatteringByteChannel.read.

Doc from java.nio.channels.FileChannel.read.

Reads a sequence of bytes from this channel into a subsequence of the given buffers.

Bytes are read starting at this channel's current file position, and then the file position is updated with the number of bytes actually read. Otherwise this method behaves exactly as specified in the ScatteringByteChannel interface.

Parameters
dsts:ByteBuffer[]

Doc from java.nio.channels.ScatteringByteChannel.read.

The buffers into which bytes are to be transferred

offset:int

Doc from java.nio.channels.ScatteringByteChannel.read.

The offset within the buffer array of the first buffer into which bytes are to be transferred; must be non-negative and no larger than dsts.length

length:int

Doc from java.nio.channels.ScatteringByteChannel.read.

The maximum number of buffers to be accessed; must be non-negative and no larger than dsts.length - offset

Returns:long

Doc from java.nio.channels.ScatteringByteChannel.read.

The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream

Annotations
@Override
Exceptions
IOException:

Doc from java.nio.channels.ScatteringByteChannel.read.

If some other I/O error occurs

transferFromback to summary
public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException

Implements abstract java.nio.channels.FileChannel.transferFrom.

Doc from java.nio.channels.FileChannel.transferFrom.

Transfers bytes into this channel's file from the given readable byte channel.

An attempt is made to read up to count bytes from the source channel and write them to this channel's file starting at the given position. An invocation of this method may or may not transfer all of the requested bytes; whether or not it does so depends upon the natures and states of the channels. Fewer than the requested number of bytes will be transferred if the source channel has fewer than count bytes remaining, or if the source channel is non-blocking and has fewer than count bytes immediately available in its input buffer. No bytes are transferred, and zero is returned, if the source has reached end-of-stream.

This method does not modify this channel's position. If the given position is greater than or equal to the file's current size then the file will be grown to accommodate the new bytes; the values of any bytes between the previous end-of-file and the newly-written bytes are unspecified. If the source channel has a position then bytes are read starting at that position and then the position is incremented by the number of bytes read.

This method is potentially much more efficient than a simple loop that reads from the source channel and writes to this channel. Many operating systems can transfer bytes directly from the source channel into the filesystem cache without actually copying them.

Parameters
src:ReadableByteChannel

The source channel

position:long

The file position at which the transfer is to begin; must be non-negative

count:long

The maximum number of bytes to be transferred; must be non-negative

Returns:long

The number of bytes, possibly zero, that were actually transferred

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
transferToback to summary
public long transferTo(long position, long count, WritableByteChannel target) throws IOException

Implements abstract java.nio.channels.FileChannel.transferTo.

Doc from java.nio.channels.FileChannel.transferTo.

Transfers bytes from this channel's file to the given writable byte channel.

An attempt is made to read up to count bytes starting at the given position in this channel's file and write them to the target channel. An invocation of this method may or may not transfer all of the requested bytes; whether or not it does so depends upon the natures and states of the channels. Fewer than the requested number of bytes are transferred if this channel's file contains fewer than count bytes starting at the given position, or if the target channel is non-blocking and it has fewer than count bytes free in its output buffer.

This method does not modify this channel's position. If the given position is greater than or equal to the file's current size then no bytes are transferred. If the target channel has a position then bytes are written starting at that position and then the position is incremented by the number of bytes written.

This method is potentially much more efficient than a simple loop that reads from this channel and writes to the target channel. Many operating systems can transfer bytes directly from the filesystem cache to the target channel without actually copying them.

Parameters
position:long

The position within the file at which the transfer is to begin; must be non-negative

count:long

The maximum number of bytes to be transferred; must be non-negative

target:WritableByteChannel

The target channel

Returns:long

The number of bytes, possibly zero, that were actually transferred

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
tryLockback to summary
public FileLock tryLock(long position, long size, boolean shared) throws IOException

Implements abstract java.nio.channels.FileChannel.tryLock.

Doc from java.nio.channels.FileChannel.tryLock.

Attempts to acquire a lock on the given region of this channel's file.

This method does not block. An invocation always returns immediately, either having acquired a lock on the requested region or having failed to do so. If it fails to acquire a lock because an overlapping lock is held by another program then it returns null. If it fails to acquire a lock for any other reason then an appropriate exception is thrown.

The region specified by the position and size parameters need not be contained within, or even overlap, the actual underlying file. Lock regions are fixed in size; if a locked region initially contains the end of the file and the file grows beyond the region then the new portion of the file will not be covered by the lock. If a file is expected to grow in size and a lock on the entire file is required then a region starting at zero, and no smaller than the expected maximum size of the file, should be locked. The zero-argument tryLock() method simply locks a region of size Long#MAX_VALUE. If the position is non-negative and the size is zero, then a lock of size Long.MAX_VALUE - position is returned.

Some operating systems do not support shared locks, in which case a request for a shared lock is automatically converted into a request for an exclusive lock. Whether the newly-acquired lock is shared or exclusive may be tested by invoking the resulting lock object's isShared method.

File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine.

Parameters
position:long

The position at which the locked region is to start; must be non-negative

size:long

The size of the locked region; must be non-negative, and the sum position + size must be non-negative. A value of zero means to lock all bytes from the specified starting position to the end of the file, regardless of whether the file is subsequently extended or truncated

shared:boolean

true to request a shared lock, false to request an exclusive lock

Returns:FileLock

A lock object representing the newly-acquired lock, or null if the lock could not be acquired because another program holds an overlapping lock

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
writeback to summary
public synchronized int write(ByteBuffer src, long position) throws IOException

Implements abstract java.nio.channels.FileChannel.write.

Doc from java.nio.channels.FileChannel.write.

Writes a sequence of bytes to this channel from the given buffer, starting at the given file position.

This method works in the same manner as the write(ByteBuffer) method, except that bytes are written starting at the given file position rather than at the channel's current position. This method does not modify this channel's position. If the given position is greater than or equal to the file's current size then the file will be grown to accommodate the new bytes; the values of any bytes between the previous end-of-file and the newly-written bytes are unspecified.

If the file is open in append mode, then the effect of invoking this method is unspecified.

Parameters
src:ByteBuffer

The buffer from which bytes are to be transferred

position:long

The file position at which the transfer is to begin; must be non-negative

Returns:int

The number of bytes written, possibly zero

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
writeback to summary
public long write(ByteBuffer[] srcs, int offset, int length) throws IOException

Implements abstract java.nio.channels.FileChannel.write.

Implements java.nio.channels.GatheringByteChannel.write.

Doc from java.nio.channels.FileChannel.write.

Writes a sequence of bytes to this channel from a subsequence of the given buffers.

Bytes are written starting at this channel's current file position unless the channel is in append mode, in which case the position is first advanced to the end of the file. The file is grown, if necessary, to accommodate the written bytes, and then the file position is updated with the number of bytes actually written. Otherwise this method behaves exactly as specified in the GatheringByteChannel interface.

Parameters
srcs:ByteBuffer[]

Doc from java.nio.channels.GatheringByteChannel.write.

The buffers from which bytes are to be retrieved

offset:int

Doc from java.nio.channels.GatheringByteChannel.write.

The offset within the buffer array of the first buffer from which bytes are to be retrieved; must be non-negative and no larger than srcs.length

length:int

Doc from java.nio.channels.GatheringByteChannel.write.

The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset

Returns:long

Doc from java.nio.channels.GatheringByteChannel.write.

The number of bytes written, possibly zero

Annotations
@Override
Exceptions
IOException:

Doc from java.nio.channels.GatheringByteChannel.write.

If some other I/O error occurs