FileChannelpublic abstract class FileChannel extends AbstractInterruptibleChannel implements ByteChannel, GatheringByteChannel, ScatteringByteChannelAn abstract channel type for interaction with a platform file.
A {@code FileChannel} defines the methods for reading, writing, memory
mapping, and manipulating the logical state of a platform file. This type
does not have a method for opening files, since this behavior has been
delegated to the {@link java.io.FileInputStream},
{@link java.io.FileOutputStream} and {@link java.io.RandomAccessFile} types.
FileChannels created from a {@code FileInputStream} or a
{@code RandomAccessFile} created in mode "r", are read-only. FileChannels
created from a {@code FileOutputStream} are write-only. FileChannels created
from a {@code RandomAccessFile} created in mode "rw" are read/write.
FileChannels created from a {@code RandomAccessFile} that was opened in
append-mode will also be in append-mode -- meaning that each write will be
proceeded by a seek to the end of file.
FileChannels have a virtual pointer into the file which is referred to as a
file position. The position can be manipulated by moving it
within the file, and the current position can be queried.
FileChannels also have an associated size. The size of the file
is the number of bytes that it currently contains. The size can be
manipulated by adding more bytes to the end of the file (which increases the
size) or truncating the file (which decreases the size). The current size can
also be queried.
FileChannels have operations beyond the simple read, write, and close. They
can also:
- request that cached data be forced onto the disk,
- lock ranges of bytes associated with the file,
- transfer data directly to another channel in a manner that has the
potential to be optimized by the platform,
- memory-mapping files into NIO buffers to provide efficient manipulation
of file data,
- read and write to the file at absolute byte offsets in a fashion that
does not modify the current position.
FileChannels are thread-safe. Only one operation involving manipulation of
the file position may be executed at the same time. Subsequent calls to such
operations will block, and one of those blocked will be freed to continue
when the first operation has completed. There is no ordered queue or fairness
applied to the blocked threads.
It is undefined whether operations that do not manipulate the file position
will also block when there are any other operations in-flight.
The logical view of the underlying file is consistent across all FileChannels
and I/O streams opened on the same file by the same virtual machine process.
Therefore, modifications performed via a channel will be visible to the
stream and vice versa; this includes modifications to the file position,
content, size, etc.
|
Constructors Summary |
---|
protected FileChannel()Protected default constructor.
super();
|
Methods Summary |
---|
public abstract void | force(boolean metadata)Requests that all updates to this channel are committed to the storage
device.
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.
| public final java.nio.channels.FileLock | lock()Obtains an exclusive lock on this file.
This is a convenience method for acquiring a maximum length lock on a
file. It is equivalent to:
{@code fileChannel.lock(0L, Long.MAX_VALUE, false);}
return lock(0L, Long.MAX_VALUE, false);
| public abstract java.nio.channels.FileLock | lock(long position, long size, boolean shared)Obtains a lock on a specified region of the file.
This is the blocking version of lock acquisition, see also the
tryLock() methods.
Attempts to acquire an overlapping lock region will fail. The attempt
will fail if the overlapping lock has already been obtained, or if
another thread is currently waiting to acquire the overlapping lock.
If the request is not for an overlapping lock, the thread calling this
method will block until the lock is obtained (likely by no contention or
another process releasing a lock), or until this thread is interrupted or
the channel is closed.
If the lock is obtained successfully then the {@link FileLock} object
returned represents the lock for subsequent operations on the locked
region.
If the thread is interrupted while waiting for the lock, the thread is
set to the interrupted state and throws a
{@link FileLockInterruptionException}. If this channel is closed while
the thread is waiting to obtain the lock then the thread throws a
{@link AsynchronousCloseException}.
There is no requirement for the position and size to be within the
current start and length of the file.
Some platforms do not support shared locks, and if a request is made for
a shared lock on such a platform, this method will attempt to acquire an
exclusive lock instead. It is undefined whether the lock obtained is
advisory or mandatory.
| public abstract java.nio.MappedByteBuffer | map(java.nio.channels.FileChannel$MapMode mode, long position, long size)Maps the file into memory. There can be three modes: read-only,
read/write and private. After mapping, changes made to memory or the file
channel do not affect the other storage place.
Note: mapping a file into memory is usually expensive.
| public abstract long | position()Returns the current value of the file position pointer.
| public abstract java.nio.channels.FileChannel | position(long offset)Sets the file position pointer to a new value.
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.
| public final long | read(java.nio.ByteBuffer[] buffers)Reads bytes from this file channel and stores them in the specified array
of buffers. This method attempts to read as many bytes as can be stored
in the buffer array from this channel and returns the number of bytes
actually read. It also increases the file position by the number of bytes
read.
If a read operation is in progress, subsequent threads will block until
the read is completed and will then contend for the ability to read.
Calling this method is equivalent to calling
{@code read(buffers, 0, buffers.length);}
return read(buffers, 0, buffers.length);
| public abstract long | read(java.nio.ByteBuffer[] buffers, int start, int number)Reads bytes from this file channel and stores them in a subset of the
specified array of buffers. The subset is defined by {@code start} and
{@code number}, indicating the first buffer and the number of buffers to
use. This method attempts to read as many bytes as can be stored in the
buffer subset from this channel and returns the number of bytes actually
read. It also increases the file position by the number of bytes read.
If a read operation is in progress, subsequent threads will block until
the read is completed and will then contend for the ability to read.
| public abstract int | read(java.nio.ByteBuffer buffer)Reads bytes from this file channel into the given buffer.
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.
| public abstract int | read(java.nio.ByteBuffer buffer, long position)Reads bytes from this file channel into the given buffer starting from
the specified file position.
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 {@code position} is beyond the current end of file, then no bytes are
read.
Note that the file position is unmodified by this method.
| public abstract long | size()Returns the size of the file underlying this channel in bytes.
| public abstract long | transferFrom(java.nio.channels.ReadableByteChannel src, long position, long count)Reads up to {@code count} bytes from {@code src} and stores them in this
channel's file starting at {@code position}. No bytes are transferred if
{@code position} is larger than the size of this channel's file. Less
than {@code 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 {@code count} bytes immediately available in its output
buffer.
Note that this channel's position is not modified.
| public abstract long | transferTo(long position, long count, java.nio.channels.WritableByteChannel target)Reads up to {@code count} bytes from this channel's file starting at
{@code position} and writes them to {@code target}. No bytes are
transferred if {@code position} is larger than the size of this channel's
file. Less than {@code 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 {@code count} bytes free in its input
buffer.
Note that this channel's position is not modified.
| public abstract java.nio.channels.FileChannel | truncate(long size)Truncates the file underlying this channel to a given size. Any bytes
beyond the given size are removed from the file. If there are no bytes
beyond the given size then the file contents are unmodified.
If the file position is currently greater than the given size, then it is
set to the new size.
| public final java.nio.channels.FileLock | tryLock()Attempts to acquire an exclusive lock on this file without blocking.
This is a convenience method for attempting to acquire a maximum length
lock on the file. It is equivalent to:
{@code fileChannel.tryLock(0L, Long.MAX_VALUE, false);}
The method returns {@code null} if the acquisition would result in an
overlapped lock with another OS process.
return tryLock(0L, Long.MAX_VALUE, false);
| public abstract java.nio.channels.FileLock | tryLock(long position, long size, boolean shared)Attempts to acquire an exclusive lock on this file without blocking. The
method returns {@code null} if the acquisition would result in an
overlapped lock with another OS process.
It is possible to acquire a lock for any region even if it's completely
outside of the file's size. The size of the lock is fixed. If the file
grows outside of the lock that region of the file won't be locked by this
lock.
| public abstract int | write(java.nio.ByteBuffer src)Writes bytes from the given byte buffer to this file channel.
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.
| public abstract int | write(java.nio.ByteBuffer buffer, long position)Writes bytes from the given buffer to this file channel starting at the
given file position.
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.
| public final long | write(java.nio.ByteBuffer[] buffers)Writes bytes from all the given byte buffers to this file channel.
The bytes are written starting at the current file position, and after
the bytes are written (up to the remaining number of bytes in all the
buffers), the file position is increased by the number of bytes actually
written.
Calling this method is equivalent to calling
{@code write(buffers, 0, buffers.length);}
return write(buffers, 0, buffers.length);
| public abstract long | write(java.nio.ByteBuffer[] buffers, int offset, int length)Writes bytes from a subset of the specified array of buffers into this
file channel. The subset is defined by {@code offset} and {@code length},
indicating the first buffer and the number of buffers to use.
If a write operation is in progress, subsequent threads will block until
the write is completed and then contend for the ability to write.
|
|