In this article we will look into Java NIO ByteBuffer’s operations.
Allocate
We call allocate()
on ByteBuffer
to create a buffer of bytes. Internally, a byte array is created with the position set to 0.
Put
When we put a new element into the buffer, the element is added to the buffer array element at index defined by position
. The position
is incremented with each put()
.
Get
In order to read the elements in buffer call ByteBuffer.get()
, it will return the element at index define by position
. After each get()
, the position gets incremented.
Compact
The bytes between the buffer’s current position and its limit, if any, are copied to the beginning of the buffer.
If we want to read the remaining bytes, we will have to call flip()
and then read the individual bytes.
Flip
As we call flip()
, limit will be set to the current position and the position will be moved to the first index (zero).
Java NIO ByteBuffer Example
JavaNIOBufferExample:
package com.javarticles.nio; import java.nio.ByteBuffer; public class JavaNIOByteBufferExample { public static void main(String[] args) { ByteBuffer buffer = ByteBuffer.allocate(7); System.out.println("Buffer limit: " + buffer.limit() + ", capacity: " + buffer.capacity()); put(buffer, (byte) 1); put(buffer, (byte) 3); put(buffer, (byte) 5); put(buffer, (byte) 7); put(buffer, (byte) 9); put(buffer, (byte) 11); put(buffer, (byte) 13); System.out.println("Flip the buffer"); buffer.flip(); for (int i = 0; i < buffer.limit() -2; i++) { get(buffer); } System.out.println("Position " + buffer.position() + ", remaining " + buffer.remaining() + ". Compact the buffer"); buffer.compact(); System.out.println("After compact, position " + buffer.position() + ", remaining " + buffer.remaining() + ", limit " + buffer.limit()); System.out.println("Read elements. Start"); for (int i = 0; i < buffer.limit(); i++) { System.out.println("Get[" + i + "]=>" + buffer.get(i)); } System.out.println("End"); flip(buffer); for (int i = 0; i <= buffer.remaining(); i++) { get(buffer); } } private static void put(ByteBuffer buffer, byte b) { System.out.println("Put[" + buffer.position() +"]=" + b); buffer.put(b); System.out.println("Position " + buffer.position() + ", remaining " + buffer.remaining()); } private static void get(ByteBuffer buffer) { System.out.println("Get[" + buffer.position() + "]=>" + buffer.get() + ". Remaining " + buffer.remaining()); } private static void flip(ByteBuffer buffer) { buffer.flip(); System.out.println("Flip. Position " + buffer.position() + ", remaining " + buffer.remaining() + ", limit " + buffer.limit()); } }
Output:
Buffer limit: 7, capacity: 7 Put[0]=1 Position 1, remaining 6 Put[1]=3 Position 2, remaining 5 Put[2]=5 Position 3, remaining 4 Put[3]=7 Position 4, remaining 3 Put[4]=9 Position 5, remaining 2 Put[5]=11 Position 6, remaining 1 Put[6]=13 Position 7, remaining 0 Flip the buffer Get[0]=>1. Remaining 6 Get[1]=>3. Remaining 5 Get[2]=>5. Remaining 4 Get[3]=>7. Remaining 3 Get[4]=>9. Remaining 2 Position 5, remaining 2. Compact the buffer After compact, position 2, remaining 5, limit 7 Read elements. Start Get[0]=>11 Get[1]=>13 Get[2]=>5 Get[3]=>7 Get[4]=>9 Get[5]=>11 Get[6]=>13 End Flip. Position 0, remaining 2, limit 2 Get[0]=>11. Remaining 1 Get[1]=>13. Remaining 0
Download the source code
This was an example about java NIO ByteBuffer.