How to create a ByteBuffer in Java

There are many ways to create a ByteBuffer:

  • Create a ByteBuffer using an existing array
byte[] bytes = new byte[BUFFER_SIZE];
ByteBuffer buffer = ByteBuffer.wrap(bytes);

  • Create a non-direct ByteBuffer with a specific byte capacity
ByteBuffer buffer = ByteBuffer.allocate(10);

  • Create a direct ByteBuffer with a specific byte capacity
ByteBuffer buffer = ByteBuffer.allocateDirect(10);

Please upvote, if you find it useful.

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now