ReadableByteChannel
Stay organized with collections
Save and categorize content based on your preferences.
Known Indirect Subclasses
|
A channel that can read bytes.
Only one read operation upon a readable channel may be in progress at
any given time. If one thread initiates a read operation upon a channel
then any other thread that attempts to initiate another read operation will
block until the first operation is complete. Whether or not other kinds of
I/O operations may proceed concurrently with a read operation depends upon
the type of the channel.
Public Method Summary
abstract
int
|
read( ByteBuffer dst)
Reads a sequence of bytes from this channel into the given buffer.
|
Inherited Method Summary
From interface
java.io.Closeable
abstract
void
|
close()
Closes this stream and releases any system resources associated
with it.
|
Public Methods
public
abstract
int
read
(ByteBuffer dst)
Reads a sequence of bytes from this channel into the given buffer.
An attempt is made to read up to r bytes from the channel,
where r is the number of bytes remaining in the buffer, that is,
dst.remaining(), at the moment this method is invoked.
Suppose that a byte sequence of length n is read, where
0 <= n <= r.
This byte sequence will be transferred into the buffer so that the first
byte in the sequence is at index p and the last byte is at index
p + n - 1,
where p is the buffer's position at the moment this method is
invoked. Upon return the buffer's position will be equal to
p + n; its limit will not have changed.
A read operation might not fill the buffer, and in fact it might not
read any bytes at all. Whether or not it does so depends upon the
nature and state of the channel. A socket channel in non-blocking mode,
for example, cannot read any more bytes than are immediately available
from the socket's input buffer; similarly, a file channel cannot read
any more bytes than remain in the file. It is guaranteed, however, that
if a channel is in blocking mode and there is at least one byte
remaining in the buffer then this method will block until at least one
byte is read.
This method may be invoked at any time. If another thread has
already initiated a read operation upon this channel, however, then an
invocation of this method will block until the first operation is
complete.
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
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003e\u003ccode\u003eReadableByteChannel\u003c/code\u003e is an interface that defines a channel for reading bytes.\u003c/p\u003e\n"],["\u003cp\u003eIt provides a single \u003ccode\u003eread()\u003c/code\u003e method to transfer bytes from the channel into a \u003ccode\u003eByteBuffer\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eOnly one read operation can be in progress at a time, and concurrent behavior with other I/O operations depends on the channel type.\u003c/p\u003e\n"],["\u003cp\u003eIf a channel is in blocking mode and there is space in the buffer, the \u003ccode\u003eread()\u003c/code\u003e method will block until at least one byte is read or end-of-stream is reached.\u003c/p\u003e\n"],["\u003cp\u003eIt inherits methods from \u003ccode\u003eChannel\u003c/code\u003e, \u003ccode\u003eCloseable\u003c/code\u003e, and \u003ccode\u003eAutoCloseable\u003c/code\u003e interfaces for managing the channel's state and resources.\u003c/p\u003e\n"]]],[],null,["# ReadableByteChannel\n\npublic interface **ReadableByteChannel** implements [Channel](../../../../reference/java/nio/channels/Channel.html) \n\n|---|---|---|\n| Known Indirect Subclasses [ByteChannel](../../../../reference/java/nio/channels/ByteChannel.html), [DatagramChannel](../../../../reference/java/nio/channels/DatagramChannel.html), [FileChannel](../../../../reference/java/nio/channels/FileChannel.html), [Pipe.SourceChannel](../../../../reference/java/nio/channels/Pipe.SourceChannel.html), [ScatteringByteChannel](../../../../reference/java/nio/channels/ScatteringByteChannel.html), [SeekableByteChannel](../../../../reference/java/nio/channels/SeekableByteChannel.html), [SocketChannel](../../../../reference/java/nio/channels/SocketChannel.html) |---------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------| | [ByteChannel](../../../../reference/java/nio/channels/ByteChannel.html) | A channel that can read and write bytes. | | [DatagramChannel](../../../../reference/java/nio/channels/DatagramChannel.html) | A selectable channel for datagram-oriented sockets. | | [FileChannel](../../../../reference/java/nio/channels/FileChannel.html) | A channel for reading, writing, mapping, and manipulating a file. | | [Pipe.SourceChannel](../../../../reference/java/nio/channels/Pipe.SourceChannel.html) | A channel representing the readable end of a [Pipe](../../../../reference/java/nio/channels/Pipe.html). | | [ScatteringByteChannel](../../../../reference/java/nio/channels/ScatteringByteChannel.html) | A channel that can read bytes into a sequence of buffers. | | [SeekableByteChannel](../../../../reference/java/nio/channels/SeekableByteChannel.html) | A byte channel that maintains a current *position* and allows the position to be changed. | | [SocketChannel](../../../../reference/java/nio/channels/SocketChannel.html) | A selectable channel for stream-oriented connecting sockets. | |||\n\nA channel that can read bytes.\n\nOnly one read operation upon a readable channel may be in progress at\nany given time. If one thread initiates a read operation upon a channel\nthen any other thread that attempts to initiate another read operation will\nblock until the first operation is complete. Whether or not other kinds of\nI/O operations may proceed concurrently with a read operation depends upon\nthe type of the channel.\n\n\u003cbr /\u003e\n\n### Public Method Summary\n\n|--------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract int | [read](../../../../reference/java/nio/channels/ReadableByteChannel.html#read(java.nio.ByteBuffer))([ByteBuffer](../../../../reference/java/nio/ByteBuffer.html) dst) Reads a sequence of bytes from this channel into the given buffer. |\n\n### Inherited Method Summary\n\nFrom interface [java.nio.channels.Channel](../../../../reference/java/nio/channels/Channel.html) \n\n|------------------|----------------------------------------------------------------------------------------------------------------------|\n| abstract void | [close](../../../../reference/java/nio/channels/Channel.html#close())() Closes this channel. |\n| abstract boolean | [isOpen](../../../../reference/java/nio/channels/Channel.html#isOpen())() Tells whether or not this channel is open. |\n\nFrom interface [java.io.Closeable](../../../../reference/java/io/Closeable.html) \n\n|---------------|------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract void | [close](../../../../reference/java/io/Closeable.html#close())() Closes this stream and releases any system resources associated with it. |\n\nFrom interface [java.lang.AutoCloseable](../../../../reference/java/lang/AutoCloseable.html) \n\n|---------------|-------------------------------------------------------------------------------------------------------------------------------------|\n| abstract void | [close](../../../../reference/java/lang/AutoCloseable.html#close())() Closes this resource, relinquishing any underlying resources. |\n\nPublic Methods\n--------------\n\n#### public abstract int\n**read**\n([ByteBuffer](../../../../reference/java/nio/ByteBuffer.html) dst)\n\nReads a sequence of bytes from this channel into the given buffer.\n\nAn attempt is made to read up to *r* bytes from the channel,\nwhere *r* is the number of bytes remaining in the buffer, that is,\ndst.remaining(), at the moment this method is invoked.\n\nSuppose that a byte sequence of length *n* is read, where\n0 \\\u003c= *n* \\\u003c= *r* .\nThis byte sequence will be transferred into the buffer so that the first\nbyte in the sequence is at index *p* and the last byte is at index\n*p* + *n* - 1,\nwhere *p* is the buffer's position at the moment this method is\ninvoked. Upon return the buffer's position will be equal to\n*p* + *n*; its limit will not have changed.\n\nA read operation might not fill the buffer, and in fact it might not\nread any bytes at all. Whether or not it does so depends upon the\nnature and state of the channel. A socket channel in non-blocking mode,\nfor example, cannot read any more bytes than are immediately available\nfrom the socket's input buffer; similarly, a file channel cannot read\nany more bytes than remain in the file. It is guaranteed, however, that\nif a channel is in blocking mode and there is at least one byte\nremaining in the buffer then this method will block until at least one\nbyte is read.\n\nThis method may be invoked at any time. If another thread has\nalready initiated a read operation upon this channel, however, then an\ninvocation of this method will block until the first operation is\ncomplete.\n\n\u003cbr /\u003e\n\n##### Parameters\n\n| dst | The buffer into which bytes are to be transferred |\n|-----|---------------------------------------------------|\n\n##### Returns\n\n- The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream \n\n##### Throws\n\n| [NonReadableChannelException](../../../../reference/java/nio/channels/NonReadableChannelException.html) | If this channel was not opened for reading |\n| [ClosedChannelException](../../../../reference/java/nio/channels/ClosedChannelException.html) | If this channel is closed |\n| [AsynchronousCloseException](../../../../reference/java/nio/channels/AsynchronousCloseException.html) | If another thread closes this channel while the read operation is in progress |\n| [ClosedByInterruptException](../../../../reference/java/nio/channels/ClosedByInterruptException.html) | 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 |\n| [IOException](../../../../reference/java/io/IOException.html) | If some other I/O error occurs |\n|---------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|"]]