AutoCloseable
, ByteChannel
, Channel
, Closeable
, ReadableByteChannel
, WritableByteChannel
FileChannel
public interface SeekableByteChannel extends ByteChannel
A seekable byte channel is connected to an entity, typically a file, that contains a variable-length sequence of bytes that can be read and written. The current position can be queried
and modified
. The channel also provides access to the current size of the entity to which the channel is connected. The size increases when bytes are written beyond its current size; the size decreases when it is truncated
.
The position
and truncate
methods which do not otherwise have a value to return are specified to return the channel upon which they are invoked. This allows method invocations to be chained. Implementations of this interface should specialize the return type so that method invocations on the implementation class can be chained.
Modifier and Type | Method | Description |
---|---|---|
long |
position() |
Returns this channel's position. |
SeekableByteChannel |
position |
Sets this channel's position. |
int |
read |
Reads a sequence of bytes from this channel into the given buffer. |
long |
size() |
Returns the current size of entity to which this channel is connected. |
SeekableByteChannel |
truncate |
Truncates the entity, to which this channel is connected, to the given size. |
int |
write |
Writes a sequence of bytes to this channel from the given buffer. |
int read(ByteBuffer dst) throws IOException
Bytes are read starting at this channel's current position, and then the 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-1
if the channel has reached end-of-streamClosedChannelException
- If this channel is closedAsynchronousCloseException
- If another thread closes this channel while the read operation is in progressClosedByInterruptException
- 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 statusNonReadableChannelException
- If this channel was not opened for readingIOException
- If some other I/O error occursint write(ByteBuffer src) throws IOException
Bytes are written starting at this channel's current position, unless the channel is connected to an entity such as a file that is opened with the APPEND
option, in which case the position is first advanced to the end. The entity to which the channel is connected is grown, if necessary, to accommodate the written bytes, and then the 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 retrievedClosedChannelException
- If this channel is closedAsynchronousCloseException
- If another thread closes this channel while the write operation is in progressClosedByInterruptException
- 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 statusNonWritableChannelException
- If this channel was not opened for writingIOException
- If some other I/O error occurslong position() throws IOException
ClosedChannelException
- If this channel is closedIOException
- If some other I/O error occursSeekableByteChannel position(long newPosition) throws IOException
Setting the position to a value that is greater than the current size is legal but does not change the size of the entity. 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 entity to grow to accommodate the new bytes; the values of any bytes between the previous end-of-file and the newly-written bytes are unspecified.
Setting the channel's position is not recommended when connected to an entity, typically a file, that is opened with the APPEND
option. When opened for append, the position is first advanced to the end before writing.
newPosition
- The new position, a non-negative integer counting the number of bytes from the beginning of the entityClosedChannelException
- If this channel is closedIllegalArgumentException
- If the new position is negativeIOException
- If some other I/O error occurslong size() throws IOException
ClosedChannelException
- If this channel is closedIOException
- If some other I/O error occursSeekableByteChannel truncate(long size) throws IOException
If the given size is less than the current size then the entity is truncated, discarding any bytes beyond the new end. If the given size is greater than or equal to the current size then the entity is not modified. In either case, if the current position is greater than the given size then it is set to that size.
An implementation of this interface may prohibit truncation when connected to an entity, typically a file, opened with the APPEND
option.
size
- The new size, a non-negative byte countNonWritableChannelException
- If this channel was not opened for writingClosedChannelException
- If this channel is closedIllegalArgumentException
- If the new size is negativeIOException
- If some other I/O error occurs
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/channels/SeekableByteChannel.html