Java Reference
In-Depth Information
read(ByteBuffer buf)
Tries to read buf.remaining() bytes (equivalent to
limit-position bytes) from the file into the buffer,
buf , starting at the buffer's current position. The number
of bytes read is returned as type int , or -1 if the channel
reaches the end-of-file. The buffer position will be
incremented by the number of bytes read and the buffer's
limit will be left unchanged.
read(ByteBuffer[] buffers)
Tries to read bytes into each of the buffers in the buffers
array in sequence. Bytes will be read into each buffer
starting at the point defined by that buffer's position. The
number of bytes read into each buffer is defined by the
remaining() method for that buffer. The read()
method returns the total number of bytes read as type
int , or -1 if the channel reaches the end-of-file. Each
buffer's position will be incremented by the number of
bytes read into it. Each buffer's limit will be unchanged.
read(ByteBuffer[] buffers,
int offset,
This operates in the same way as the previous method
except that bytes are read starting with the buffer
buffers[offset] , and up to and including the buffer
buffers[offset+length-1] . This method will throw
an exception of type IndexOutOfBoundsException if
offset or offset+length-1 are not valid index values
for the array buffers .
int length)
As you can see, all three read() methods read data into one or more buffers of type ByteBuffer . Since
you can only use ByteBuffer objects to receive the data read from the file, you can only read data from a
file via a channel as a series of bytes. How you interpret these bytes afterwards though is up to you.
All three methods can throw exceptions of any of the following types: :
NonReadableChannelException
Thrown if the file was not opened for reading.
ClosedChannelException
Thrown if the channel is closed.
AsynchronousCloseException
Thrown if the channel is closed by another thread
while the read operation is in progress.
ClosedByInterruptException
Thrown if another thread interrupts the current
thread while the read operation is in progress.
IOException
Thrown if some other I/O error occurs.
The first of these is a subclass of RuntimeException so you are not obliged to catch this exception. If
you don't need to identify the other exceptions individually, you can use a single catch block for
exceptions of type IOException to catch any of them.
Search WWH ::




Custom Search