public class IOCipherFileChannel
extends java.nio.channels.spi.AbstractInterruptibleChannel
implements java.nio.channels.ByteChannel
Constructor and Description |
---|
IOCipherFileChannel(java.lang.Object stream,
FileDescriptor fd,
int mode)
Create a new file channel implementation class that wraps the given fd
and operates in the specified mode.
|
Modifier and Type | Method and Description |
---|---|
void |
force(boolean metadata)
Requests that all updates to this channel are committed to the storage
device.
|
protected void |
implCloseChannel()
Implements the channel closing behavior.
|
long |
lseek(long offset,
int whence)
IOCipher version of POSIX lseek, since the underlying FUSE layer does not
track the position in open files for us, we do it inside of this class.
|
long |
position()
Returns the current value of the file position pointer.
|
IOCipherFileChannel |
position(long newPosition)
Sets the file position pointer to a new value.
|
int |
read(java.nio.ByteBuffer buffer)
Reads bytes from this file channel into the given buffer.
|
int |
read(java.nio.ByteBuffer buffer,
long position)
Reads bytes from this file channel into the given buffer starting from
the specified file position.
|
long |
size()
Returns the size of the file underlying this channel in bytes.
|
long |
transferFrom(java.nio.channels.ReadableByteChannel src,
long position,
long count)
Reads up to
count bytes from src and stores them in this
channel's file starting at position . |
long |
transferTo(long position,
long count,
java.nio.channels.WritableByteChannel target)
Reads up to
count bytes from this channel's file starting at
position and writes them to target . |
IOCipherFileChannel |
truncate(long size)
Truncates the file underlying this channel to a given size.
|
int |
write(java.nio.ByteBuffer src)
Writes bytes from the given byte buffer to this file channel.
|
int |
write(java.nio.ByteBuffer buffer,
long position)
Writes bytes from the given buffer to this file channel starting at the
given file position.
|
begin, close, end, isOpen
public IOCipherFileChannel(java.lang.Object stream, FileDescriptor fd, int mode)
protected void implCloseChannel() throws java.io.IOException
Closes the channel with a guarantee that the channel is not currently
closed through another invocation of close()
and that the method
is thread-safe.
Any outstanding threads blocked on I/O operations on this channel must be
released with either a normal return code, or by throwing an
AsynchronousCloseException
.
implCloseChannel
in class java.nio.channels.spi.AbstractInterruptibleChannel
java.io.IOException
- if a problem occurs while closing the channel.public void force(boolean metadata) throws java.io.IOException
When this method returns, all modifications made to the platform file underlying this channel have been committed if the file resides on a local storage device. If the file is not hosted locally, for example on a networked file system, then applications cannot be certain that the modifications have been committed.
There are no assurances given that changes made to the file using methods defined elsewhere will be committed. For example, changes made via a mapped byte buffer may not be committed.
The metadata
parameter indicates whether the update should
include the file's metadata such as last modification time, last access
time, etc. Note that passing true
may invoke an underlying
write to the operating system (if the platform is maintaining metadata
such as last access time), even if the channel is opened read-only.
metadata
- true
if the file metadata should be flushed in
addition to the file content, false
otherwise.java.nio.channels.ClosedChannelException
- if this channel is already closed.java.io.IOException
- if another I/O error occurs.public long lseek(long offset, int whence) throws java.io.IOException
FileDescriptor
, so you cannot specify one as
an argument.offset
- the new position to seek to.whence
- changes the pointer repositioning behavior:
SEEK_SET
then file pointer is set to offsetSEEK_CUR
then file pointer is set to current position +
offsetSEEK_END
then file pointer is set to file size +
offsetjava.nio.channels.ClosedChannelException
- if this channel is already closed.java.io.IOException
public long position() throws java.io.IOException
java.nio.channels.ClosedChannelException
- if this channel is closed.java.io.IOException
public IOCipherFileChannel position(long newPosition) throws java.io.IOException
The argument is the number of bytes counted from the start of the file. The position cannot be set to a value that is negative. The new position can be set beyond the current file size. If set beyond the current file size, attempts to read will return end of file. Write operations will succeed but they will fill the bytes between the current end of file and the new position with the required number of (unspecified) byte values.
newPosition
- the new file position, in bytes.java.lang.IllegalArgumentException
- if the new position is negative.java.nio.channels.ClosedChannelException
- if this channel is closed.java.io.IOException
public int read(java.nio.ByteBuffer buffer) throws java.io.IOException
The maximum number of bytes that will be read is the remaining number of bytes in the buffer when the method is invoked. The bytes will be copied into the buffer starting at the buffer's current position.
The call may block if other threads are also attempting to read from this channel.
Upon completion, the buffer's position is set to the end of the bytes that have been read. The buffer's limit is not changed.
read
in interface java.nio.channels.ReadableByteChannel
buffer
- the byte buffer to receive the bytes.java.nio.channels.AsynchronousCloseException
- if another thread closes the channel
during the read.java.nio.channels.ClosedByInterruptException
- if another thread interrupts the
calling thread during the read.java.nio.channels.ClosedChannelException
- if this channel is closed.java.io.IOException
- if another I/O error occurs, details are in the
message.java.nio.channels.NonReadableChannelException
- if the channel has not been opened in
a mode that permits reading.public int read(java.nio.ByteBuffer buffer, long position) throws java.io.IOException
The bytes are read starting at the given file position (up to the remaining number of bytes in the buffer). The number of bytes actually read is returned.
If position
is beyond the current end of file, then no bytes are
read.
Note that the file position is unmodified by this method.
buffer
- the buffer to receive the bytes.position
- the (non-negative) position at which to read the bytes.java.nio.channels.AsynchronousCloseException
- if this channel is closed by another
thread while this method is executing.java.nio.channels.ClosedByInterruptException
- if another thread interrupts the
calling thread while this operation is in progress. The
calling thread will have the interrupt state set, and the
channel will be closed.java.nio.channels.ClosedChannelException
- if this channel is closed.java.lang.IllegalArgumentException
- if position
is less than 0.java.io.IOException
- if another I/O error occurs.java.nio.channels.NonReadableChannelException
- if the channel has not been opened in
a mode that permits reading.public long size() throws java.io.IOException
java.nio.channels.ClosedChannelException
- if this channel is closed.java.io.IOException
- if an I/O error occurs while getting the size of the
file.public int write(java.nio.ByteBuffer src) throws java.io.IOException
The bytes are written starting at the current file position, and after some number of bytes are written (up to the remaining number of bytes in the buffer) the file position is increased by the number of bytes actually written.
write
in interface java.nio.channels.WritableByteChannel
src
- the byte buffer containing the bytes to be written.java.nio.channels.NonWritableChannelException
- if the channel was not opened for
writing.java.nio.channels.ClosedChannelException
- if the channel was already closed.java.nio.channels.AsynchronousCloseException
- if another thread closes the channel
during the write.java.nio.channels.ClosedByInterruptException
- if another thread interrupts the
calling thread while this operation is in progress. The
interrupt state of the calling thread is set and the channel
is closed.java.io.IOException
- if another I/O error occurs, details are in the
message.WritableByteChannel.write(java.nio.ByteBuffer)
public long transferFrom(java.nio.channels.ReadableByteChannel src, long position, long count) throws java.io.IOException
count
bytes from src
and stores them in this
channel's file starting at position
. No bytes are transferred if
position
is larger than the size of this channel's file. Less
than count
bytes are transferred if there are less bytes
remaining in the source channel or if the source channel is non-blocking
and has less than count
bytes immediately available in its output
buffer.
Note that this channel's position is not modified.
src
- the source channel to read bytes from.position
- the non-negative start position.count
- the non-negative number of bytes to transfer.java.lang.IllegalArgumentException
- if the parameters are invalid.java.nio.channels.NonReadableChannelException
- if the source channel is not
readable.java.nio.channels.NonWritableChannelException
- if this channel is not writable.java.nio.channels.ClosedChannelException
- if either channel has already been closed.java.nio.channels.AsynchronousCloseException
- if either channel is closed by other
threads during this operation.java.nio.channels.ClosedByInterruptException
- if the thread is interrupted during
this operation.java.io.IOException
- if any I/O error occurs.public long transferTo(long position, long count, java.nio.channels.WritableByteChannel target) throws java.io.IOException
count
bytes from this channel's file starting at
position
and writes them to target
. No bytes are
transferred if position
is larger than the size of this channel's
file. Less than count
bytes are transferred if there less bytes
available from this channel's file or if the target channel is
non-blocking and has less than count
bytes free in its input
buffer.
Note that this channel's position is not modified.
position
- the non-negative position to begin.count
- the non-negative number of bytes to transfer.target
- the target channel to write to.java.lang.IllegalArgumentException
- if the parameters are invalid.java.nio.channels.NonReadableChannelException
- if this channel is not readable.java.nio.channels.NonWritableChannelException
- if the target channel is not
writable.java.nio.channels.ClosedChannelException
- if either channel has already been closed.java.nio.channels.AsynchronousCloseException
- if either channel is closed by other
threads during this operation.java.nio.channels.ClosedByInterruptException
- if the thread is interrupted during
this operation.java.io.IOException
- if any I/O error occurs.public IOCipherFileChannel truncate(long size) throws java.io.IOException
If the file position is currently greater than the given size, then it is set to the new size.
size
- the maximum size of the underlying file.java.lang.IllegalArgumentException
- if the requested size is negative.java.nio.channels.ClosedChannelException
- if this channel is closed.java.nio.channels.NonWritableChannelException
- if the channel cannot be written to.java.io.IOException
- if another I/O error occurs.public int write(java.nio.ByteBuffer buffer, long position) throws java.io.IOException
The bytes are written starting at the given file position (up to the remaining number of bytes in the buffer). The number of bytes actually written is returned.
If the position is beyond the current end of file, then the file is first extended up to the given position by the required number of unspecified byte values.
Note that the file position is not modified by this method.
buffer
- the buffer containing the bytes to be written.position
- the (non-negative) position at which to write the bytes.java.lang.IllegalArgumentException
- if position
is less than 0.java.nio.channels.ClosedChannelException
- if this channel is closed.java.nio.channels.NonWritableChannelException
- if the channel was not opened in
write-mode.java.nio.channels.AsynchronousCloseException
- if this channel is closed by another
thread while this method is executing.java.nio.channels.ClosedByInterruptException
- if another thread interrupts the
calling thread while this operation is in progress. The
interrupt state of the calling thread is set and the channel
is closed.java.io.IOException
- if another I/O error occurs.