Java : Buffer with Examples

Buffer (Java SE 22 & JDK 22) in Java with Examples.
You will find code samples for most of the Buffer methods.


Summary

A container for data of a specific primitive type.

Class diagram

final var buffer = IntBuffer.allocate(5);

System.out.println(buffer instanceof Buffer); // true

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.limit()); // 5
System.out.println(buffer.position()); // 0

buffer.put(10);
System.out.println(buffer.position()); // 1

buffer.put(20);
System.out.println(buffer.position()); // 2

buffer.put(30);
System.out.println(buffer.position()); // 3

if (buffer.hasArray()) {
    final var ret = buffer.array();
    System.out.println(Arrays.toString(ret)); // [10, 20, 30, 0, 0]
}
final byte[] array = {10, 20, 30};
final var buffer = ByteBuffer.wrap(array);

System.out.println(buffer instanceof Buffer); // true

System.out.println(buffer.capacity()); // 3
System.out.println(buffer.limit()); // 3
System.out.println(buffer.position()); // 0

System.out.println(buffer.get()); // 10
System.out.println(buffer.position()); // 1

System.out.println(buffer.get()); // 20
System.out.println(buffer.position()); // 2

System.out.println(buffer.get()); // 30
System.out.println(buffer.position()); // 3

Methods

abstract Object array ()

Returns the array that backs this buffer (optional operation).

final Buffer buffer = IntBuffer.allocate(5)
        .put(10)
        .put(20)
        .put(30);

if (buffer.hasArray()) {
    if (buffer.array() instanceof int[] array) {
        System.out.println(Arrays.toString(array)); // [10, 20, 30, 0, 0]
    }
}

abstract int arrayOffset ()

Returns the offset within this buffer's backing array of the first element of the buffer (optional operation).

final byte[] bytes = {10, 20, 30, 40, 50};

final var buffer = ByteBuffer.wrap(bytes);
System.out.println(buffer instanceof Buffer); // true

final var sliced = buffer.slice(2, 3);
System.out.println(sliced instanceof Buffer); // true

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
if (buffer.hasArray()) {
    System.out.println(buffer.arrayOffset()); // 0
    System.out.println(buffer.get()); // 10
    System.out.println(buffer.get()); // 20
    System.out.println(buffer.get()); // 30
    System.out.println(buffer.get()); // 40
    System.out.println(buffer.get()); // 50
}

System.out.println(sliced); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=3]
if (sliced.hasArray()) {
    System.out.println(sliced.arrayOffset()); // 2
    System.out.println(sliced.get()); // 30
    System.out.println(sliced.get()); // 40
    System.out.println(sliced.get()); // 50
}

final int capacity ()

Returns this buffer's capacity.

final Buffer buffer = ByteBuffer.allocate(100);
System.out.println(buffer.capacity()); // 100
final byte[] bytes = {10, 20, 30, 40, 50};
final Buffer buffer = ByteBuffer.wrap(bytes);

System.out.println(buffer.capacity()); // 5

Buffer clear ()

Clears this buffer.

Please see also : rewind()

final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true

buffer.position(2);
buffer.limit(4);

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=2 lim=4 cap=5]

System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.hasRemaining()); // false

System.out.println(buffer.clear()); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
System.out.println(buffer.hasRemaining()); // false

abstract Buffer duplicate ()

Creates a new buffer that shares this buffer's content.

final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true

buffer.position(1);
buffer.limit(4);

final var duplicated = buffer.duplicate();
System.out.println(duplicated instanceof Buffer); // true

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=1 lim=4 cap=5]
System.out.println(duplicated); // java.nio.HeapByteBuffer[pos=1 lim=4 cap=5]

System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40

System.out.println(duplicated.get()); // 20
System.out.println(duplicated.get()); // 30
System.out.println(duplicated.get()); // 40

Buffer flip ()

Flips this buffer.

final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=3 lim=5 cap=5]
System.out.println(buffer.flip()); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=5]

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30

abstract boolean hasArray ()

Tells whether or not this buffer is backed by an accessible array.

final var buffer = IntBuffer.allocate(5)
        .put(10)
        .put(20)
        .put(30);
System.out.println(buffer instanceof Buffer); // true

final var ret = buffer.hasArray();
System.out.println(ret); // true

if (ret) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [10, 20, 30, 0, 0]
}
final var buffer = ByteBuffer.allocateDirect(5);
System.out.println(buffer instanceof Buffer); // true

System.out.println(buffer.hasArray()); // false
try {
    var _ = buffer.array();
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

final boolean hasRemaining ()

Tells whether there are any elements between the current position and the limit.

final byte[] array = {10, 20, 30, 40};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true

buffer.limit(3);

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // true
System.out.println(buffer.remaining()); // 3

System.out.println(buffer.get()); // 10

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=1 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // true
System.out.println(buffer.remaining()); // 2

System.out.println(buffer.get()); // 20

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=2 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // true
System.out.println(buffer.remaining()); // 1

System.out.println(buffer.get()); // 30

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=3 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // false
System.out.println(buffer.remaining()); // 0

abstract boolean isDirect ()

Tells whether or not this buffer is direct.

final Buffer buffer = ByteBuffer.allocate(5);
System.out.println(buffer.isDirect()); // false
final Buffer buffer = ByteBuffer.allocateDirect(5);
System.out.println(buffer.isDirect()); // true

abstract boolean isReadOnly ()

Tells whether or not this buffer is read-only.

final var buffer = IntBuffer.allocate(5);
System.out.println(buffer instanceof Buffer); // true

System.out.println(buffer.isReadOnly()); // false

buffer.put(10);
buffer.put(20);
buffer.put(30);

System.out.println(buffer); // java.nio.HeapIntBuffer[pos=3 lim=5 cap=5]
final var buffer = IntBuffer.allocate(5).asReadOnlyBuffer();
System.out.println(buffer instanceof Buffer); // true

System.out.println(buffer.isReadOnly()); // true

try {
    buffer.put(10);
} catch (ReadOnlyBufferException e) {
    System.out.println("ReadOnlyBufferException!");
}

// Result
// ↓
//ReadOnlyBufferException!

final int limit ()

Returns this buffer's limit.

final byte[] array = {10, 20, 30, 40, 50};
final Buffer buffer = ByteBuffer.wrap(array);

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.limit()); // 5

System.out.println(buffer.limit(3)); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=5]
System.out.println(buffer.limit()); // 3

System.out.println(buffer.clear()); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.limit()); // 5
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true

System.out.println(buffer.limit(3)); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=5]

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30

try {
    var _ = buffer.get();
} catch (BufferUnderflowException e) {
    System.out.println("BufferUnderflowException!");
}

// Result
// ↓
//BufferUnderflowException!

Buffer limit (int newLimit)

Sets this buffer's limit.

final byte[] array = {10, 20, 30, 40, 50};
final Buffer buffer = ByteBuffer.wrap(array);

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.limit()); // 5

System.out.println(buffer.limit(3)); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=5]
System.out.println(buffer.limit()); // 3

System.out.println(buffer.clear()); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.limit()); // 5
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true

System.out.println(buffer.limit(3)); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=5]

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30

try {
    var _ = buffer.get();
} catch (BufferUnderflowException e) {
    System.out.println("BufferUnderflowException!");
}

// Result
// ↓
//BufferUnderflowException!

Buffer mark ()

Sets this buffer's mark at its position.

final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20

System.out.println(buffer.position()); // 2
System.out.println(buffer.mark()); // java.nio.HeapByteBuffer[pos=2 lim=5 cap=5]

System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50

System.out.println(buffer.position()); // 5
System.out.println(buffer.reset()); // java.nio.HeapByteBuffer[pos=2 lim=5 cap=5]

System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50

final int position ()

Returns this buffer's position.

final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true

System.out.println(buffer.position()); // 0

System.out.println(buffer.get()); // 10
System.out.println(buffer.position()); // 1

System.out.println(buffer.get()); // 20
System.out.println(buffer.position()); // 2

System.out.println(buffer.get()); // 30
System.out.println(buffer.position()); // 3

System.out.println(buffer.get()); // 40
System.out.println(buffer.position()); // 4

System.out.println(buffer.get()); // 50
System.out.println(buffer.position()); // 5

System.out.println(buffer.position(3)); // java.nio.HeapByteBuffer[pos=3 lim=5 cap=5]

System.out.println(buffer.get()); // 40
System.out.println(buffer.position()); // 4

System.out.println(buffer.get()); // 50
System.out.println(buffer.position()); // 5

Buffer position (int newPosition)

Sets this buffer's position.

final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true

System.out.println(buffer.position()); // 0

System.out.println(buffer.get()); // 10
System.out.println(buffer.position()); // 1

System.out.println(buffer.get()); // 20
System.out.println(buffer.position()); // 2

System.out.println(buffer.get()); // 30
System.out.println(buffer.position()); // 3

System.out.println(buffer.get()); // 40
System.out.println(buffer.position()); // 4

System.out.println(buffer.get()); // 50
System.out.println(buffer.position()); // 5

System.out.println(buffer.position(3)); // java.nio.HeapByteBuffer[pos=3 lim=5 cap=5]

System.out.println(buffer.get()); // 40
System.out.println(buffer.position()); // 4

System.out.println(buffer.get()); // 50
System.out.println(buffer.position()); // 5

final int remaining ()

Returns the number of elements between the current position and the limit.

final byte[] array = {10, 20, 30, 40};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true

buffer.limit(3);

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // true
System.out.println(buffer.remaining()); // 3

System.out.println(buffer.get()); // 10

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=1 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // true
System.out.println(buffer.remaining()); // 2

System.out.println(buffer.get()); // 20

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=2 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // true
System.out.println(buffer.remaining()); // 1

System.out.println(buffer.get()); // 30

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=3 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // false
System.out.println(buffer.remaining()); // 0

Buffer reset ()

Resets this buffer's position to the previously-marked position.

final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20

System.out.println(buffer.position()); // 2
System.out.println(buffer.mark()); // java.nio.HeapByteBuffer[pos=2 lim=5 cap=5]

System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50

System.out.println(buffer.position()); // 5
System.out.println(buffer.reset()); // java.nio.HeapByteBuffer[pos=2 lim=5 cap=5]

System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50

Buffer rewind ()

Rewinds this buffer.

Please see also : clear()

final int[] array = {10, 20, 30, 40, 50};
final Buffer buffer = IntBuffer.wrap(array);

buffer.position(2);
buffer.limit(4);

System.out.println(buffer.position()); // 2
System.out.println(buffer.limit()); // 4

System.out.println(buffer.rewind()); // java.nio.HeapIntBuffer[pos=0 lim=4 cap=5]

System.out.println(buffer.position()); // 0
System.out.println(buffer.limit()); // 4

abstract Buffer slice ()

Creates a new buffer whose content is a shared subsequence of this buffer's content.

final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true

buffer.position(1);
buffer.limit(4);

final var sliced = buffer.slice();
System.out.println(sliced instanceof Buffer); // true

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=1 lim=4 cap=5]
System.out.println(sliced); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=3]

System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.hasRemaining()); // false

System.out.println(sliced.get()); // 20
System.out.println(sliced.get()); // 30
System.out.println(sliced.get()); // 40
System.out.println(sliced.hasRemaining()); // false

abstract Buffer slice (int index, int length)

Creates a new buffer whose content is a shared subsequence of this buffer's content.

final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true

final var sliced = buffer.slice(2, 3);
System.out.println(sliced instanceof Buffer); // true

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
System.out.println(sliced); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=3]

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
System.out.println(buffer.hasRemaining()); // false

System.out.println(sliced.get()); // 30
System.out.println(sliced.get()); // 40
System.out.println(sliced.get()); // 50
System.out.println(sliced.hasRemaining()); // false

Related posts

To top of page