newChannel() methods to construct an instance of this class.
In order to use this class with a Database, you must use the DatabaseBuilder to open/create the
Database instance, passing an instance of this class to the DatabaseBuilder#withChannel method.
Implementation Note
this class is optimized for use with Database. Therefore not all methods may be
implemented and individual read/write operations are only supported within page boundaries.
| Modifier and Type | Class and Description |
|---|---|
| private static class | MemFileChannel.
Subclass of MemFileChannel which is read-only. |
| Modifier and Type | Field and Description |
|---|---|
| private static final int | |
| private byte[][] | data
chunks containing the file data. |
| private static final byte[][] | |
| private static final int | |
| private long | position
current read/write position |
| public static final String | RO_CHANNEL_MODE
read-only channel access mode |
| public static final String | RW_CHANNEL_MODE
read/write channel access mode |
| private long | size
current amount of actual data in the file |
| Access | Constructor and Description |
|---|---|
| private | |
| private |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| public void | force(boolean metaData)
Implements abstract java. Forces any updates to this channel's file to be written to the storage device that contains it. |
| private static int | |
| private static int | |
| private static int | |
| protected void | implCloseChannel()
Implements abstract java. Closes this channel. |
| public FileLock | lock(long pos, long len, boolean shared)
Implements abstract java. Acquires a lock on the given region of this channel's file. |
| public MappedByteBuffer | map(FileChannel.
Implements abstract java. Maps a region of this channel's file directly into memory. |
| public static MemFileChannel | |
| public static MemFileChannel | newChannel(File file)
Creates a new read/write MemFileChannel containing the contents of the given File. |
| public static MemFileChannel | newChannel(File file, String mode)
Creates a new MemFileChannel containing the contents of the given File with the given mode (for mode details see
|
| public static MemFileChannel | newChannel(Path file, OpenOption... opts)
Creates a new MemFileChannel containing the contents of the given Path with the given mode (for mode details see
|
| public static MemFileChannel | newChannel(Path file)
Creates a new read/write MemFileChannel containing the contents of the given Path. |
| public static MemFileChannel | newChannel(InputStream in)
Creates a new read/write MemFileChannel containing the contents of the given InputStream. |
| public static MemFileChannel | newChannel(InputStream in, String mode)
Creates a new MemFileChannel containing the contents of the given InputStream with the given mode (for mode
details see |
| public static MemFileChannel | newChannel(ReadableByteChannel in)
Creates a new read/write MemFileChannel containing the contents of the given ReadableByteChannel. |
| public static MemFileChannel | newChannel(ReadableByteChannel in, String mode)
Creates a new MemFileChannel containing the contents of the given ReadableByteChannel with the given mode (for
mode details see |
| public long | position()
Implements abstract java. Implements java. Returns this channel's file position. |
| public FileChannel | position(long newPosition)
Implements abstract java. Implements java. Sets this channel's file position. |
| public int | read(ByteBuffer dst)
Implements abstract java. Implements java. Reads a sequence of bytes from this channel into the given buffer. |
| public int | read(ByteBuffer dst, long readPos)
Implements abstract java. Reads a sequence of bytes from this channel into the given buffer, starting at the given file position. |
| public long | read(ByteBuffer[] dsts, int offset, int length)
Implements abstract java. Implements java. Reads a sequence of bytes from this channel into a subsequence of the given buffers. |
| public long | size()
Implements abstract java. Implements java. Returns the current size of this channel's file. |
| public long | transferFrom(ReadableByteChannel src, long dstPos, long count)
Implements abstract java. Transfers bytes into this channel's file from the given readable byte channel. |
| public long | transferTo(WritableByteChannel dst)
Convenience method for writing the entire contents of this channel to the given destination channel. |
| public long | transferTo(long srcPos, long count, WritableByteChannel dst)
Implements abstract java. Transfers bytes from this channel's file to the given writable byte channel. |
| public long | transferTo(OutputStream dst)
Convenience method for writing the entire contents of this channel to the given destination stream. |
| public long | transferTo(long srcPos, long count, OutputStream dst)
Convenience method for writing the selected portion of this channel to the given destination stream. |
| public FileChannel | truncate(long newSize)
Implements abstract java. Implements java. Truncates this channel's file to the given size. |
| public FileLock | tryLock(long pos, long len, boolean shared)
Implements abstract java. Attempts to acquire a lock on the given region of this channel's file. |
| public int | write(ByteBuffer src)
Implements abstract java. Implements java. Writes a sequence of bytes to this channel from the given buffer. |
| public int | write(ByteBuffer src, long writePos)
Implements abstract java. Writes a sequence of bytes to this channel from the given buffer, starting at the given file position. |
| public long | write(ByteBuffer[] srcs, int offset, int length)
Implements abstract java. Implements java. Writes a sequence of bytes to this channel from a subsequence of the given buffers. |
| CHUNK_SIZE | back to summary |
|---|---|
| private static final int CHUNK_SIZE | |
| data | back to summary |
|---|---|
| private byte[][] data chunks containing the file data. the length of the chunk array is always a power of 2 and the chunks are always CHUNK_SIZE. | |
| EMPTY_DATA | back to summary |
|---|---|
| private static final byte[][] EMPTY_DATA | |
| INIT_CHUNKS | back to summary |
|---|---|
| private static final int INIT_CHUNKS | |
| position | back to summary |
|---|---|
| private long position current read/write position | |
| RO_CHANNEL_MODE | back to summary |
|---|---|
| public static final String RO_CHANNEL_MODE read-only channel access mode | |
| RW_CHANNEL_MODE | back to summary |
|---|---|
| public static final String RW_CHANNEL_MODE read/write channel access mode | |
| size | back to summary |
|---|---|
| private long size current amount of actual data in the file | |
| MemFileChannel | back to summary |
|---|---|
| private MemFileChannel() | |
| MemFileChannel | back to summary |
|---|---|
| private MemFileChannel(long position, long size, byte[][] data) | |
| ensureCapacity | back to summary |
|---|---|
| private void ensureCapacity(long newSize) | |
| force | back to summary |
|---|---|
| public void force(boolean metaData) Implements abstract java. Doc from java. 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 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
| |
| getChunkIndex | back to summary |
|---|---|
| private static int getChunkIndex(long pos) | |
| getChunkOffset | back to summary |
|---|---|
| private static int getChunkOffset(long pos) | |
| getNumChunks | back to summary |
|---|---|
| private static int getNumChunks(long size) | |
| implCloseChannel | back to summary |
|---|---|
| protected void implCloseChannel() Implements abstract java. Doc from java. Closes this channel. This method is invoked by the 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.
| |
| lock | back to summary |
|---|---|
| public FileLock lock(long pos, long len, boolean shared) Implements abstract java. Doc from java. 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 If the invoking thread is interrupted while waiting to acquire the
lock then its interrupted status will be set and a The region specified by the 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 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.
| |
| map | back to summary |
|---|---|
| public MappedByteBuffer map(FileChannel. Implements abstract java. Doc from java. Maps a region of this channel's file directly into memory. The
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 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
| |
| newChannel | back to summary |
|---|---|
| public static MemFileChannel newChannel() Creates a new read/write, empty MemFileChannel. | |
| newChannel | back to summary |
|---|---|
| public static MemFileChannel newChannel(File file) throws IOException Creates a new read/write MemFileChannel containing the contents of the given File. Note, modifications to the returned channel will not affect the original File source. | |
| newChannel | back to summary |
|---|---|
| public static MemFileChannel newChannel(File file, String mode) throws IOException Creates a new MemFileChannel containing the contents of the given File with the given mode (for mode details see
| |
| newChannel | back to summary |
|---|---|
| public static MemFileChannel newChannel(Path file, OpenOption... opts) throws IOException Creates a new MemFileChannel containing the contents of the given Path with the given mode (for mode details see
| |
| newChannel | back to summary |
|---|---|
| public static MemFileChannel newChannel(Path file) throws IOException Creates a new read/write MemFileChannel containing the contents of the given Path. Note, modifications to the returned channel will not affect the original File source. | |
| newChannel | back to summary |
|---|---|
| public static MemFileChannel newChannel(InputStream in) throws IOException Creates a new read/write MemFileChannel containing the contents of the given InputStream. | |
| newChannel | back to summary |
|---|---|
| public static MemFileChannel newChannel(InputStream in, String mode) throws IOException Creates a new MemFileChannel containing the contents of the given InputStream with the given mode (for mode
details see | |
| newChannel | back to summary |
|---|---|
| public static MemFileChannel newChannel(ReadableByteChannel in) throws IOException Creates a new read/write MemFileChannel containing the contents of the given ReadableByteChannel. | |
| newChannel | back to summary |
|---|---|
| public static MemFileChannel newChannel(ReadableByteChannel in, String mode) throws IOException Creates a new MemFileChannel containing the contents of the given ReadableByteChannel with the given mode (for
mode details see
| |
| position | back to summary |
|---|---|
| public long position() Implements abstract java. Implements java. Doc from java. Returns this channel's file position.
| |
| position | back to summary |
|---|---|
| public FileChannel position(long newPosition) Implements abstract java. Implements java. Doc from java. Sets this channel's file position. Setting the position to a value that is greater than the file's current size is legal but does not change the size of the file. A later attempt to read bytes at such a position will immediately return an end-of-file indication. A later attempt to write bytes at such a position will cause the file to 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.
| |
| read | back to summary |
|---|---|
| public int read(ByteBuffer dst) Implements abstract java. Implements java. Doc from java. Reads a sequence of bytes from this channel into the given buffer. 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
| |
| read | back to summary |
|---|---|
| public int read(ByteBuffer dst, long readPos) Implements abstract java. Doc from java. 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 | back to summary |
|---|---|
| public long read(ByteBuffer[] dsts, int offset, int length) Implements abstract java. Implements java. Doc from java. 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
| |
| size | back to summary |
|---|---|
| public long size() Implements abstract java. Implements java. Doc from java. Returns the current size of this channel's file.
| |
| transferFrom | back to summary |
|---|---|
| public long transferFrom(ReadableByteChannel src, long dstPos, long count) throws IOException Implements abstract java. Doc from java. Transfers bytes into this channel's file from the given readable byte channel. An attempt is made to read up to 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.
| |
| transferTo | back to summary |
|---|---|
| public long transferTo(WritableByteChannel dst) throws IOException Convenience method for writing the entire contents of this channel to the given destination channel. | |
| transferTo | back to summary |
|---|---|
| public long transferTo(long srcPos, long count, WritableByteChannel dst) throws IOException Implements abstract java. Doc from java. Transfers bytes from this channel's file to the given writable byte channel. An attempt is made to read up to 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.
| |
| transferTo | back to summary |
|---|---|
| public long transferTo(OutputStream dst) throws IOException Convenience method for writing the entire contents of this channel to the given destination stream. | |
| transferTo | back to summary |
|---|---|
| public long transferTo(long srcPos, long count, OutputStream dst) throws IOException Convenience method for writing the selected portion of this channel to the given destination stream. | |
| truncate | back to summary |
|---|---|
| public FileChannel truncate(long newSize) Implements abstract java. Implements java. Doc from java. Truncates this channel's file to the given size. If the given size is less than the file's current size then the file is truncated, discarding any bytes beyond the new end of the file. If the given size is greater than or equal to the file's current size then the file is not modified. In either case, if this channel's file position is greater than the given size then it is set to that size.
| |
| tryLock | back to summary |
|---|---|
| public FileLock tryLock(long pos, long len, boolean shared) Implements abstract java. Doc from java. 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
The region specified by the 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 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.
| |
| write | back to summary |
|---|---|
| public int write(ByteBuffer src) Implements abstract java. Implements java. Doc from java. Writes a sequence of bytes to this channel from the given buffer. 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 by the
| |
| write | back to summary |
|---|---|
| public int write(ByteBuffer src, long writePos) Implements abstract java. Doc from java. 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 If the file is open in append mode, then the effect of invoking this method is unspecified.
| |
| write | back to summary |
|---|---|
| public long write(ByteBuffer[] srcs, int offset, int length) Implements abstract java. Implements java. Doc from java. 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
| |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public long | transferFrom(ReadableByteChannel src, long position, long count)
Overrides io. Transfers bytes into this channel's file from the given readable byte channel. |
| public FileChannel | truncate(long newSize)
Overrides io. Implements java. Truncates this channel's file to the given size. |
| public int | write(ByteBuffer src, long position)
Overrides io. Writes a sequence of bytes to this channel from the given buffer, starting at the given file position. |
| ReadOnlyChannel | back to summary |
|---|---|
| private ReadOnlyChannel(MemFileChannel channel) | |
| transferFrom | back to summary |
|---|---|
| public long transferFrom(ReadableByteChannel src, long position, long count) Overrides io. Doc from java. Transfers bytes into this channel's file from the given readable byte channel. An attempt is made to read up to 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.
| |
| truncate | back to summary |
|---|---|
| public FileChannel truncate(long newSize) Overrides io. Implements java. Doc from java. Truncates this channel's file to the given size. If the given size is less than the file's current size then the file is truncated, discarding any bytes beyond the new end of the file. If the given size is greater than or equal to the file's current size then the file is not modified. In either case, if this channel's file position is greater than the given size then it is set to that size.
| |
| write | back to summary |
|---|---|
| public int write(ByteBuffer src, long position) Overrides io. Doc from java. 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 If the file is open in append mode, then the effect of invoking this method is unspecified.
| |