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.
java.nio.file.Files#newByteChannel
| Modifier and Type | Method and Description |
|---|---|
| public long | Returns: This channel's position, a non-negative integer counting the number of bytes from the beginning of the entity to the current positionReturns this channel's position. |
| public SeekableByteChannel | Returns: This channelThe new position, a non-negative integer counting
the number of bytes from the beginning of the entity newPosition)Sets this channel's position. |
| public int | read(ByteBuffer dst)
Redeclares java. Reads a sequence of bytes from this channel into the given buffer. |
| public long | Returns: The current size, measured in bytesReturns the current size of entity to which this channel is connected. |
| public SeekableByteChannel | Returns: This channelThe new size, a non-negative byte count size)Truncates the entity, to which this channel is connected, to the given size. |
| public int | write(ByteBuffer src)
Redeclares java. Writes a sequence of bytes to this channel from the given buffer. |
| position | back to summary |
|---|---|
| public long position() throws IOException Returns this channel's position.
| |
| position | back to summary |
|---|---|
| public SeekableByteChannel position(long newPosition) throws IOException 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
| |
| read | back to summary |
|---|---|
| public int read(ByteBuffer dst) throws IOException Redeclares java. 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
| |
| size | back to summary |
|---|---|
| public long size() throws IOException Returns the current size of entity to which this channel is connected.
| |
| truncate | back to summary |
|---|---|
| public SeekableByteChannel truncate(long size) throws IOException 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
| |
| write | back to summary |
|---|---|
| public int write(ByteBuffer src) throws IOException Redeclares java. 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
| |