Basic Usage - Creating a ByteBuffer

suggest change

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()

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


ByteBuffer:
* Basic Usage - Creating a ByteBuffer

Table Of Contents
8 Arrays
10 Maps
11 Strings
25 JAXB
29 Enums
32 Audio
41 Scanner
46 ByteBuffer
63 Logging
75 Lists
78 Sets
89 JAX-WS
96 XJC
98 Process
106 Modules
114 Applets
122 JNDI
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
167 Enum Map
175 Hashtable
177 SortedMap