|
Préférences
Moteurs de recherche
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
JavaTM 2 Platform Std. Ed. v1.4.2
java.nio.channels
|
Nested Class Summary | |
static class |
FileChannel.MapMode
A typesafe enumeration for file-mapping modes. |
Constructor Summary | |
protected |
FileChannel()
Initializes a new instance of this class. |
Method Summary | |
abstract void |
force(boolean metaData)
Forces any updates to this channel's file to be written to the storage device that contains it. |
FileLock |
lock()
Acquires an exclusive lock on this channel's file. |
abstract FileLock |
lock(long position,
long size,
boolean shared)
Acquires a lock on the given region of this channel's file. |
abstract MappedByteBuffer |
map(FileChannel.MapMode mode,
long position,
long size)
Maps a region of this channel's file directly into memory. |
abstract long |
position()
Returns this channel's file position. |
abstract FileChannel |
position(long newPosition)
Sets this channel's file position. |
abstract int |
read(ByteBuffer dst)
Reads a sequence of bytes from this channel into the given buffer. |
long |
read(ByteBuffer[] dsts)
Reads a sequence of bytes from this channel into the given buffers. |
abstract long |
read(ByteBuffer[] dsts,
int offset,
int length)
Reads a sequence of bytes from this channel into a subsequence of the given buffers. |
abstract int |
read(ByteBuffer dst,
long position)
Reads a sequence of bytes from this channel into the given buffer, starting at the given file position. |
abstract long |
size()
Returns the current size of this channel's file. |
abstract long |
transferFrom(ReadableByteChannel src,
long position,
long count)
Transfers bytes into this channel's file from the given readable byte channel. |
abstract long |
transferTo(long position,
long count,
WritableByteChannel target)
Transfers bytes from this channel's file to the given writable byte channel. |
abstract FileChannel |
truncate(long size)
Truncates this channel's file to the given size. |
FileLock |
tryLock()
Attempts to acquire an exclusive lock on this channel's file. |
abstract FileLock |
tryLock(long position,
long size,
boolean shared)
Attempts to acquire a lock on the given region of this channel's file. |
abstract int |
write(ByteBuffer src)
Writes a sequence of bytes to this channel from the given buffer. |
long |
write(ByteBuffer[] srcs)
Writes a sequence of bytes to this channel from the given buffers. |
abstract long |
write(ByteBuffer[] srcs,
int offset,
int length)
Writes a sequence of bytes to this channel from a subsequence of the given buffers. |
abstract int |
write(ByteBuffer src,
long position)
Writes a sequence of bytes to this channel from the given buffer, starting at the given file position. |
Methods inherited from class java.nio.channels.spi.AbstractInterruptibleChannel |
begin, close, end, implCloseChannel, isOpen |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface java.nio.channels.Channel |
close, isOpen |
Constructor Detail |
protected FileChannel()
Method Detail |
public abstract int read(ByteBuffer dst) throws IOException
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 ReadableByteChannel
interface.
read
in interface ReadableByteChannel
dst
- The buffer into which bytes are to be transferred
IOException
- If some other I/O error occurspublic abstract long read(ByteBuffer[] dsts, int offset, int length) throws IOException
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.
read
in interface ScatteringByteChannel
dsts
- The buffers into which bytes are to be transferredoffset
- 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.lengthlength
- The maximum number of buffers to be accessed; must be
non-negative and no larger than
dsts.length - offset
IOException
- If some other I/O error occurspublic final long read(ByteBuffer[] dsts) throws IOException
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.
read
in interface ScatteringByteChannel
dsts
- The buffers into which bytes are to be transferred
IOException
- If some other I/O error occurspublic abstract int write(ByteBuffer src) throws IOException
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 accomodate 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 WritableByteChannel
interface.
write
in interface WritableByteChannel
src
- The buffer from which bytes are to be retrieved
IOException
- If some other I/O error occurspublic abstract long write(ByteBuffer[] srcs, int offset, int length) throws IOException
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 accomodate 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.
write
in interface GatheringByteChannel
srcs
- The buffers from which bytes are to be retrievedoffset
- 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.lengthlength
- The maximum number of buffers to be accessed; must be
non-negative and no larger than
srcs.length - offset
IOException
- If some other I/O error occurspublic final long write(ByteBuffer[] srcs) throws IOException
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 accomodate 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.
write
in interface GatheringByteChannel
srcs
- The buffers from which bytes are to be retrieved
IOException
- If some other I/O error occurspublic abstract long position() throws IOException
ClosedChannelException
- If this channel is closed
IOException
- If some other I/O error occurspublic abstract FileChannel position(long newPosition) throws IOException
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 accomodate the new bytes; the values of any bytes between the previous end-of-file and the newly-written bytes are unspecified.
newPosition
- The new position, a non-negative integer counting
the number of bytes from the beginning of the file
ClosedChannelException
- If this channel is closed
IllegalArgumentException
- If the new position is negative
IOException
- If some other I/O error occurspublic abstract long size() throws IOException
ClosedChannelException
- If this channel is closed
IOException
- If some other I/O error occurspublic abstract FileChannel truncate(long size) throws IOException
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.
size
- The new size, a non-negative byte count
NonWritableChannelException
- If this channel was not opened for writing
ClosedChannelException
- If this channel is closed
IllegalArgumentException
- If the new size is negative
IOException
- If some other I/O error occurspublic abstract void force(boolean metaData) throws IOException
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. 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.
metaData
- 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
ClosedChannelException
- If this channel is closed
IOException
- If some other I/O error occurspublic abstract long transferTo(long position, long count, WritableByteChannel target) throws IOException
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 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.
position
- The position within the file at which the transfer is to begin;
must be non-negativecount
- The maximum number of bytes to be transferred; must be
non-negativetarget
- The target channel
IllegalArgumentException
- If the preconditions on the parameters do not hold
NonReadableChannelException
- If this channel was not opened for reading
NonWritableChannelException
- If the target channel was not opened for writing
ClosedChannelException
- If either this channel or the target channel is closed
AsynchronousCloseException
- If another thread closes either channel
while the transfer is in progress
ClosedByInterruptException
- If another thread interrupts the current thread while the
transfer is in progress, thereby closing both channels and
setting the current thread's interrupt status
IOException
- If some other I/O error occurspublic abstract long transferFrom(ReadableByteChannel src, long position, long count) throws IOException
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.
This method does not modify this channel's position. If the given position is greater than the file's current size then no bytes are transferred. 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.
src
- The source channelposition
- The position within the file at which the transfer is to begin;
must be non-negativecount
- The maximum number of bytes to be transferred; must be
non-negative
IllegalArgumentException
- If the preconditions on the parameters do not hold
NonReadableChannelException
- If the source channel was not opened for reading
NonWritableChannelException
- If this channel was not opened for writing
ClosedChannelException
- If either this channel or the source channel is closed
AsynchronousCloseException
- If another thread closes either channel
while the transfer is in progress
ClosedByInterruptException
- If another thread interrupts the current thread while the
transfer is in progress, thereby closing both channels and
setting the current thread's interrupt status
IOException
- If some other I/O error occurspublic abstract int read(ByteBuffer dst, long position) throws IOException
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 the file's current size then no bytes are read.
dst
- The buffer into which bytes are to be transferredposition
- The file position at which the transfer is to begin;
must be non-negative
IllegalArgumentException
- If the position is negative
NonReadableChannelException
- If this channel was not opened for reading
ClosedChannelException
- If this channel is closed
AsynchronousCloseException
- If another thread closes this channel
while the read operation is in progress
ClosedByInterruptException
- If another thread interrupts the current thread
while the read operation is in progress, thereby
closing the channel and setting the current thread's
interrupt status
IOException
- If some other I/O error occurspublic abstract int write(ByteBuffer src, long position) throws IOException
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 the file's current size then the file will be
grown to accomodate the new bytes; the values of any bytes between the
previous end-of-file and the newly-written bytes are unspecified.
src
- The buffer from which bytes are to be transferredposition
- The file position at which the transfer is to begin;
must be non-negative
IllegalArgumentException
- If the position is negative
NonWritableChannelException
- If this channel was not opened for writing
ClosedChannelException
- If this channel is closed
AsynchronousCloseException
- If another thread closes this channel
while the write operation is in progress
ClosedByInterruptException
- If another thread interrupts the current thread
while the write operation is in progress, thereby
closing the channel and setting the current thread's
interrupt status
IOException
- If some other I/O error occurspublic abstract MappedByteBuffer map(FileChannel.MapMode mode, long position, long size) throws IOException
A region of a file may be mapped into memory in one of three modes:
Read-only: Any attempt to modify the resulting buffer
will cause a 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
)
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.
mode
- One of the constants READ_ONLY
, READ_WRITE
, or PRIVATE
defined in the FileChannel.MapMode
class, according to
whether the file is to be mapped read-only, read/write, or
privately (copy-on-write), respectivelyposition
- The position within the file at which the mapped region
is to start; must be non-negativesize
- The size of the region to be mapped; must be non-negative and
no greater than Integer.MAX_VALUE
NonReadableChannelException
- If the mode is READ_ONLY
but
this channel was not opened for reading
NonWritableChannelException
- If the mode is READ_WRITE
or
PRIVATE
but this channel was not opened
for both reading and writing
IllegalArgumentException
- If the preconditions on the parameters do not hold
IOException
- If some other I/O error occursFileChannel.MapMode
,
MappedByteBuffer
public abstract FileLock lock(long position, long size, boolean shared) throws IOException
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
.
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.
position
- The position at which the locked region is to start; must be
non-negativesize
- The size of the locked region; must be non-negative, and the sum
position + size must be non-negativeshared
- 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)
IllegalArgumentException
- If the preconditions on the parameters do not hold
ClosedChannelException
- If this channel is closed
AsynchronousCloseException
- If another thread closes this channel while the invoking
thread is blocked in this method
FileLockInterruptionException
- If the invoking thread is interrupted while blocked in this
method
OverlappingFileLockException
- If a lock that overlaps the requested region is already held by
this Java virtual machine, or if another thread is already
blocked in this method and is attempting to lock an overlapping
region
NonReadableChannelException
- If shared is true this channel was not
opened for reading
NonWritableChannelException
- If shared is false but this channel was not
opened for writing
IOException
- If some other I/O error occurslock()
,
tryLock()
,
tryLock(long,long,boolean)
public final FileLock lock() throws IOException
An invocation of this method of the form fc.lock() behaves in exactly the same way as the invocation
fc.lock
(0L, Long.MAX_VALUE, false)
ClosedChannelException
- If this channel is closed
AsynchronousCloseException
- If another thread closes this channel while the invoking
thread is blocked in this method
FileLockInterruptionException
- If the invoking thread is interrupted while blocked in this
method
OverlappingFileLockException
- If a lock that overlaps the requested region is already held by
this Java virtual machine, or if another thread is already
blocked in this method and is attempting to lock an overlapping
region of the same file
NonReadableChannelException
- If shared is true this channel was not
opened for reading
NonWritableChannelException
- If shared is false but this channel was not
opened for writing
IOException
- If some other I/O error occurslock(long,long,boolean)
,
tryLock()
,
tryLock(long,long,boolean)
public abstract FileLock tryLock(long position, long size, boolean shared) throws IOException
This method does not block. An invocation of this 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
.
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.
position
- The position at which the locked region is to start; must be
non-negativesize
- The size of the locked region; must be non-negative, and the sum
position + size must be non-negativeshared
- true to request a shared lock,
false to request an exclusive lock
IllegalArgumentException
- If the preconditions on the parameters do not hold
ClosedChannelException
- If this channel is closed
OverlappingFileLockException
- If a lock that overlaps the requested region is already held by
this Java virtual machine, or if another thread is already
blocked in this method and is attempting to lock an overlapping
region of the same file
IOException
- If some other I/O error occurslock()
,
lock(long,long,boolean)
,
tryLock()
public final FileLock tryLock() throws IOException
An invocation of this method of the form fc.tryLock() behaves in exactly the same way as the invocation
fc.tryLock
(0L, Long.MAX_VALUE, false)
ClosedChannelException
- If this channel is closed
OverlappingFileLockException
- If a lock that overlaps the requested region is already held by
this Java virtual machine, or if another thread is already
blocked in this method and is attempting to lock an overlapping
region
IOException
- If some other I/O error occurslock()
,
lock(long,long,boolean)
,
tryLock(long,long,boolean)