| Modifier and Type | Field and Description |
|---|---|
| private final FileChannel |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | force(boolean
If metaData)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
writtenImplements abstract java. Forces any updates to this channel's file to be written to the storage device that contains it. |
| protected void | implCloseChannel()
Implements abstract java. Closes this channel. |
| 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
size, boolean 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 truncatedtrue 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)Implements abstract java. Acquires a lock on the given region of this channel's file. |
| public MappedByteBuffer | map(FileChannel.
One of the constants mode,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 modeThe 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 size)java.Implements abstract java. Maps a region of this channel's file directly into memory. |
| public long | position()
Implements abstract java. Implements java. Returns this channel's file position. |
| public FileChannel | position(long
The new position, a non-negative integer counting
the number of bytes from the beginning of the file newPosition)Implements abstract java. Implements java. Sets this channel's file position. |
| public int | read(ByteBuffer
The buffer into which bytes are to be transferred dst)Implements abstract java. Implements java. Reads a sequence of bytes from this channel into the given buffer. |
| 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 offset, int dsts.lengthThe maximum number of buffers to be accessed; must be
non-negative and no larger than
length)dsts.length - offsetImplements abstract java. Implements java. Reads a sequence of bytes from this channel into a subsequence of the given buffers. |
| public 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. Reads a sequence of bytes from this channel into the given buffer, starting at the given file position. |
| public long | size()
Implements abstract java. Implements java. Returns the current size of this channel's file. |
| 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. 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. Transfers bytes from this channel's file to the given writable byte channel. |
| public FileChannel | truncate(long
The new size, a non-negative byte count size)Implements abstract java. Implements java. Truncates this channel's file to the given size. |
| 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
size, boolean 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 truncatedtrue to request a shared lock,
false to request an exclusive lockImplements abstract java. Attempts to acquire a lock on the given region of this channel's file. |
| public int | write(ByteBuffer
The buffer from which bytes are to be retrieved src)Implements abstract java. Implements java. Writes a sequence of bytes to this channel from the given buffer. |
| 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 offset, int srcs.lengthThe maximum number of buffers to be accessed; must be
non-negative and no larger than
length)srcs.length - offsetImplements abstract java. Implements java. Writes a sequence of bytes to this channel from a subsequence of the given buffers. |
| public 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. Writes a sequence of bytes to this channel from the given buffer, starting at the given file position. |
| delegate | back to summary |
|---|---|
| private final FileChannel delegate | |
| AppendModeChannel | back to summary |
|---|---|
| public AppendModeChannel(FileChannel delegate) | |
| force | back to summary |
|---|---|
| public void force(boolean metaData) throws IOException 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
| |
| implCloseChannel | back to summary |
|---|---|
| protected void implCloseChannel() throws IOException 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 position, long size, boolean shared) throws IOException 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 interrupt 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
| |
| position | back to summary |
|---|---|
| public long position() throws IOException Implements abstract java. Implements java. Doc from java. Returns this channel's file position.
| |
| position | back to summary |
|---|---|
| public FileChannel position(long newPosition) throws IOException 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) throws IOException 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 long read(ByteBuffer[] dsts, int offset, int length) throws IOException 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
| |
| read | back to summary |
|---|---|
| public int read(ByteBuffer dst, long position) throws IOException 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
| |
| size | back to summary |
|---|---|
| public long size() throws IOException 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 position, 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(long position, long count, WritableByteChannel target) 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.
| |
| truncate | back to summary |
|---|---|
| public FileChannel truncate(long size) throws IOException 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 position, long size, boolean shared) throws IOException 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) throws IOException 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 long write(ByteBuffer[] srcs, int offset, int length) throws IOException 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
| |
| write | back to summary |
|---|---|
| public int write(ByteBuffer src, long position) throws IOException 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.
| |