There’s two ways to create a ByteBuffer, where one can be subdivided again.

If you have an already existing byte[], you can “wrap” it into a ByteBuffer to simplify processing:

byte[] reqBuffer = new byte[BUFFER_SIZE];
int readBytes = socketInputStream.read(reqBuffer);
final ByteBuffer reqBufferWrapper = ByteBuffer.wrap(reqBuffer);

This would be a possibility for code that handles low-level networking interactions


If you do not have an already existing byte[], you can create a ByteBuffer over an array that’s specifically allocated for the buffer like this:

final ByteBuffer respBuffer = ByteBuffer.allocate(RESPONSE_BUFFER_SIZE);
putResponseData(respBuffer);
socketOutputStream.write(respBuffer.array());

If the code-path is extremely performance critical and you need direct system memory access, the ByteBuffer can even allocate direct buffers using #allocateDirect()