Creating a ByteBuffer

A ByteBuffer is a fixed-capacity buffer that holds byte values. This example demonstrates a number of ways to create a ByteBuffer.

See also Getting Bytes from a ByteBuffer and Putting Bytes into a ByteBuffer.

// Create a ByteBuffer using a byte array byte[] bytes = new byte[10]; ByteBuffer buf = ByteBuffer.wrap(bytes); // Create a non-direct ByteBuffer with a 10 byte capacity // The underlying storage is a byte array. buf = ByteBuffer.allocate(10); // Create a direct (memory-mapped) ByteBuffer with a 10 byte capacity. buf = ByteBuffer.allocateDirect(10); // To create a ByteBuffer for a memory-mapped file, // see Creating a Memory-Mapped File

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.