How to create a ByteBuffer in Java

mirahu(26)
Published in
#java
Words
40
Reading
1 min
Listen
Play
9y

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.

How to create a ByteBuffer in Java | Ecency