Convert ByteBuffer to Byte Array in Java
Java applications often work with binary data while performing file operations, network communication, serialization, and NIO (New I/O) programming. Two commonly used data structures for handling binary data are byte[] and ByteBuffer. A byte[] is the traditional Java array used to store raw bytes, while ByteBuffer provides a more flexible and efficient way to manipulate binary data, especially when working with Java NIO channels. In this article, we’ll learn how to convert between ByteBuffer and byte[], understand the differences between them, and explore practical examples with detailed explanations and outputs.
1. Overview
The ByteBuffer class belongs to the java.nio package and represents a buffer of bytes. Unlike a simple byte array, a ByteBuffer maintains additional metadata such as:
- Capacity – Total number of bytes the buffer can hold.
- Position – Current location for reading or writing.
- Limit – Boundary beyond which data cannot be read or written.
There are many scenarios where we need to convert between these two forms:
- Reading data from files into a byte array
- Sending binary data through network sockets
- Interacting with APIs that accept only byte arrays
- Working with Java NIO channels and buffers
2. Code Example
The following example demonstrates converting a byte array to a ByteBuffer, reading data from the ByteBuffer, converting the ByteBuffer back to a byte array, and verifying that the original and converted arrays are identical.
// ByteBufferConversionExample.java
import java.nio.ByteBuffer;
import java.util.Arrays;
public class ByteBufferConversionExample {
public static void main(String[] args) {
// Original byte array
byte[] originalArray = {10, 20, 30, 40, 50};
System.out.println("Original Byte Array:");
System.out.println(Arrays.toString(originalArray));
// Convert byte array to ByteBuffer
ByteBuffer buffer = ByteBuffer.wrap(originalArray);
System.out.println("\nByteBuffer Information:");
System.out.println("Capacity : " + buffer.capacity());
System.out.println("Position : " + buffer.position());
System.out.println("Limit : " + buffer.limit());
// Convert ByteBuffer back to byte array
byte[] convertedArray = new byte[buffer.remaining()];
buffer.get(convertedArray);
System.out.println("\nConverted Byte Array:");
System.out.println(Arrays.toString(convertedArray));
// Verify both arrays contain the same data
boolean equal = Arrays.equals(originalArray, convertedArray);
System.out.println("\nArrays Equal: " + equal);
// Current position after reading
System.out.println("Buffer Position After Read: "
+ buffer.position());
}
}
2.1 Code Explanation
In this example, we first create a byte array named originalArray containing sample byte values and print its contents using Arrays.toString(). Next, we convert the byte array into a ByteBuffer using ByteBuffer.wrap(), which creates a buffer backed by the existing array without copying the data. We then display the buffer’s capacity, position, and limit to understand its internal state. To convert the ByteBuffer back into a byte array, we create a new array sized according to buffer.remaining() and use buffer.get() to copy all remaining bytes from the buffer into the new array. Afterward, we print the converted array and use Arrays.equals() to verify that the original and converted arrays contain identical data. Finally, we print the buffer’s current position, which has advanced to the end of the buffer after the read operation, demonstrating how reading data from a ByteBuffer updates its position automatically.
2.2 Code Output
Original Byte Array: [10, 20, 30, 40, 50] ByteBuffer Information: Capacity : 5 Position : 0 Limit : 5 Converted Byte Array: [10, 20, 30, 40, 50] Arrays Equal: true Buffer Position After Read: 5
The output first prints the original byte array showing the values [10, 20, 30, 40, 50] to confirm the initial data, then displays the ByteBuffer information where the Capacity is 5 indicating total storage size, the Position is 0 showing that reading has not yet started, and the Limit is 5 indicating the maximum readable range; next, it prints the converted byte array which again shows [10, 20, 30, 40, 50] confirming that all bytes were successfully copied from the ByteBuffer, followed by Arrays Equal: true which verifies that both the original and converted arrays contain identical values, and finally Buffer Position After Read: 5 indicates that the buffer’s position has moved to the end after reading all elements, confirming that the entire ByteBuffer content was consumed during the get() operation.
3. Conclusion
In conclusion, both byte[] and ByteBuffer play an important role in Java binary data handling, and understanding how to convert between them is essential for working efficiently with file I/O, networking, and NIO-based systems. While byte[] offers simplicity, ByteBuffer provides greater control and performance through its position, limit, and capacity management. Choosing the right approach depends on the use case, but mastering both ensures more flexible and robust handling of binary data in real-world Java applications.

