Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

ByteBuffer: shared arrays, capacity, and reads without moving the position

Wrap an existing byte array without copying it, distinguish capacity from limit, and read a byte at a chosen index without moving the buffer’s position.

Version: Java SE 21

Syntax

import java.nio.ByteBuffer;

ByteBuffer buffer = ByteBuffer.wrap(array);
ByteBuffer window = ByteBuffer.wrap(array, offset, length);
int capacity = buffer.capacity();
byte atIndex = buffer.get(index);
byte next = buffer.get();

The static ByteBuffer.wrap method in java.nio.ByteBuffer creates a buffer backed by an existing byte[] without copying its contents. Writes to the array are visible through the buffer, and writes through the buffer change the same array. With ByteBuffer.wrap(array), the initial position is 0, and the limit and capacity are both array.length.

The overload ByteBuffer.wrap(array, offset, length) starts at position offset with limit offset + length, but capacity is still array.length. It requires 0 <= offset <= array.length and 0 <= length <= array.length - offset; otherwise it throws IndexOutOfBoundsException. Index 0 still refers to array[0], not array[offset].

ByteBuffer.capacity refers to the capacity() method inherited from java.nio.Buffer. It returns the buffer's fixed capacity in bytes, not the number of bytes left for relative reads. For that, remaining() returns limit() - position(). The invariant is 0 <= position() <= limit() <= capacity().

ByteBuffer.get(int) reads one byte at a zero-based index without changing the position. The index can be below the current position, but it must satisfy 0 <= index < limit(); otherwise the method throws IndexOutOfBoundsException. In contrast, relative get() reads at the current position and advances it by one; it throws BufferUnderflowException if the position equals the limit.

Examples

import java.nio.ByteBuffer;

public class Main {
    public static void main(String[] args) {
        byte[] data = {10, 20, 30, 40};
        ByteBuffer buffer = ByteBuffer.wrap(data, 1, 2);
        System.out.println("capacity=" + buffer.capacity());
        System.out.println("limit=" + buffer.limit());

        data[0] = 11;
        System.out.println("get(0)=" + buffer.get(0));
        System.out.println("position=" + buffer.position());

        System.out.println("get()=" + buffer.get());
        System.out.println("position=" + buffer.position());

        buffer.put(2, (byte) 33);
        System.out.println("data[2]=" + data[2]);
    }
}

Result

capacity=4
limit=3
get(0)=11
position=1
get()=20
position=2
data[2]=33

With position `1` and limit `3`, `get(0)` sees the `11` written to the array and leaves the position unchanged. The next `get()` returns `20` and advances the position to `2`. Writing through the buffer changes `data[2]` to `33`. A call to `get(3)` would throw because `3` equals the limit, even though it is less than the capacity of `4`.

Related lessons

  • Java · Lesson 60 · Intermediate · Version: 2

Sources