SeekableByteChannel (Java Platform SE 8 ) (original) (raw)
- All Superinterfaces:
AutoCloseable, ByteChannel, Channel, Closeable, ReadableByteChannel, WritableByteChannel
All Known Implementing Classes:
FileChannel
public interface SeekableByteChannel
extends ByteChannel
A byte channel that maintains a current position and allows the position to be changed.
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 andmodified. 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.
Since:
1.7
See Also:
Files.newByteChannel(java.nio.file.Path, java.util.Set, java.nio.file.attribute.FileAttribute...)
Method Summary
All Methods Instance Methods Abstract Methods
Modifier and Type Method Description long position() Returns this channel's position. SeekableByteChannel position(long newPosition) Sets this channel's position. int read(ByteBuffer dst) 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(long size) Truncates the entity, to which this channel is connected, to the given size. int write(ByteBuffer src) Writes a sequence of bytes to this channel from the given buffer. * ### Methods inherited from interface java.nio.channels.[Channel](../../../java/nio/channels/Channel.html "interface in java.nio.channels") `[close](../../../java/nio/channels/Channel.html#close--), [isOpen](../../../java/nio/channels/Channel.html#isOpen--)`
Method Detail
* #### read int read([ByteBuffer](../../../java/nio/ByteBuffer.html "class in java.nio") dst) throws [IOException](../../../java/io/IOException.html "class in java.io") Reads a sequence of bytes from this channel into the given buffer. 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](../../../java/nio/channels/ReadableByteChannel.html "interface in java.nio.channels") interface. Specified by: `[read](../../../java/nio/channels/ReadableByteChannel.html#read-java.nio.ByteBuffer-)` in interface `[ReadableByteChannel](../../../java/nio/channels/ReadableByteChannel.html "interface in java.nio.channels")` Parameters: `dst` \- The buffer into which bytes are to be transferred Returns: The number of bytes read, possibly zero, or \-1 if the channel has reached end-of-stream Throws: `[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels")` \- If this channel is closed `[AsynchronousCloseException](../../../java/nio/channels/AsynchronousCloseException.html "class in java.nio.channels")` \- If another thread closes this channel while the read operation is in progress `[ClosedByInterruptException](../../../java/nio/channels/ClosedByInterruptException.html "class in java.nio.channels")` \- 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](../../../java/io/IOException.html "class in java.io")` \- If some other I/O error occurs * #### write int write([ByteBuffer](../../../java/nio/ByteBuffer.html "class in java.nio") src) throws [IOException](../../../java/io/IOException.html "class in java.io") Writes a sequence of bytes to this channel from the given buffer. 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](../../../java/nio/file/StandardOpenOption.html#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](../../../java/nio/channels/WritableByteChannel.html "interface in java.nio.channels") interface. Specified by: `[write](../../../java/nio/channels/WritableByteChannel.html#write-java.nio.ByteBuffer-)` in interface `[WritableByteChannel](../../../java/nio/channels/WritableByteChannel.html "interface in java.nio.channels")` Parameters: `src` \- The buffer from which bytes are to be retrieved Returns: The number of bytes written, possibly zero Throws: `[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels")` \- If this channel is closed `[AsynchronousCloseException](../../../java/nio/channels/AsynchronousCloseException.html "class in java.nio.channels")` \- If another thread closes this channel while the write operation is in progress `[ClosedByInterruptException](../../../java/nio/channels/ClosedByInterruptException.html "class in java.nio.channels")` \- 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](../../../java/io/IOException.html "class in java.io")` \- If some other I/O error occurs * #### position long position() throws [IOException](../../../java/io/IOException.html "class in java.io") Returns this channel's position. Returns: This channel's position, a non-negative integer counting the number of bytes from the beginning of the entity to the current position Throws: `[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels")` \- If this channel is closed `[IOException](../../../java/io/IOException.html "class in java.io")` \- If some other I/O error occurs * #### position [SeekableByteChannel](../../../java/nio/channels/SeekableByteChannel.html "interface in java.nio.channels") position(long newPosition) throws [IOException](../../../java/io/IOException.html "class in java.io") Sets this channel's position. 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](../../../java/nio/file/StandardOpenOption.html#APPEND) option. When opened for append, the position is first advanced to the end before writing. Parameters: `newPosition` \- The new position, a non-negative integer counting the number of bytes from the beginning of the entity Returns: This channel Throws: `[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels")` \- If this channel is closed `[IllegalArgumentException](../../../java/lang/IllegalArgumentException.html "class in java.lang")` \- If the new position is negative `[IOException](../../../java/io/IOException.html "class in java.io")` \- If some other I/O error occurs * #### size long size() throws [IOException](../../../java/io/IOException.html "class in java.io") Returns the current size of entity to which this channel is connected. Returns: The current size, measured in bytes Throws: `[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels")` \- If this channel is closed `[IOException](../../../java/io/IOException.html "class in java.io")` \- If some other I/O error occurs * #### truncate [SeekableByteChannel](../../../java/nio/channels/SeekableByteChannel.html "interface in java.nio.channels") truncate(long size) throws [IOException](../../../java/io/IOException.html "class in java.io") Truncates the entity, to which this channel is connected, to the given size. 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](../../../java/nio/file/StandardOpenOption.html#APPEND) option. Parameters: `size` \- The new size, a non-negative byte count Returns: This channel Throws: `[NonWritableChannelException](../../../java/nio/channels/NonWritableChannelException.html "class in java.nio.channels")` \- If this channel was not opened for writing `[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels")` \- If this channel is closed `[IllegalArgumentException](../../../java/lang/IllegalArgumentException.html "class in java.lang")` \- If the new size is negative `[IOException](../../../java/io/IOException.html "class in java.io")` \- If some other I/O error occurs
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2025, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.