Java Reference
In-Depth Information
Reading from and Writing to a Buffer
There are two ways to read data from a buffer:
Using absolute position
Using relative position
In an absolute position read, you specify the index in the buffer from which you want to read the data. The
position of the buffer is unchanged after an absolute position read.
In a relative position read, you specify how many data elements you want to read. The current position of the
buffer determines which data elements will be read. In a relative position read, the read starts at the current position
of the buffer and it is incremented by one after reading each data element.
The get() method is used to read data from a buffer. The get() method is overloaded. It has four versions. Just
replace the data type byte with another data type for other primitive type buffers in the following methods:
get(int index) : It returns the data at the given index. For example, get(2) will return the
data at index 2 from the buffer. It is an absolute way of reading data from a buffer because
you provide the absolute position of the element from which you want to read the data. This
method does not change the current position of the buffer.
get() : It returns the data from the current position in the buffer and increases the position
by 1. For example, if position is set at index 2, calling the get() method will return the value
at index 2 from the buffer and set the position to 3. It is a relative way of reading data from a
buffer because you read the data relative to the current position.
get(byte[] destination, int offset, int length) : It is used to read data from a buffer
in bulk. It reads length number of bytes from the current position of the buffer and puts
them in the specified destination array starting at the specified offset . If it cannot read the
length number of bytes from the buffer, it throws a BufferUnderflowException . If there is no
exception, it increases the current position by length . It is a relative read from a buffer.
get(byte[] destination) : It fills the specified destination array by reading data from
the current position of the buffer and incrementing the current position by one each
time it reads a data element. If there is not enough data to fill the array, it will throw a
BufferUnderflowException . It is a relative way of reading data from a buffer. This method call
is the same as calling get(byte[] destination, 0, destination.length) .
Writing data to a buffer is the opposite of reading data from it. The put() method is used to write data to a buffer.
The put() method has five versions: one for absolute position write and four for relative position write. The absolute
version of the put() method does not affect the position of the buffer. The relative versions of the put() method write
the data and advance the position of the buffer by one for each written element. Different buffer classes have different
versions of the put() method; however, there are five versions that are common among all types of buffers. The
following are the five versions of the put() method for ByteBuffer . Just replace the data type byte with another data
type for other primitive type buffers in the following methods.
put(int index, byte b) : It writes the specified b data at the specified index . The call to this
method does not change the current position of the buffer.
put(byte b) : It is a relative put() method that writes the specified byte at the current position
of the buffer and increments the position by 1.
put(byte[] source, int offset, int length) : It reads the length number of bytes from
the source array starting at offset and writes them to the buffer starting at the current
position. It throws a BufferOverflowException if there is not enough room in the buffer to
write all bytes. The position of the buffer is incremented by length .
 
Search WWH ::




Custom Search