Java : Buffer con ejemplos

Buffer (Java SE 22 & JDK 22) en Java con ejemplos.
Encontrará muestras de código para la mayoría de los métodos Buffer.

Nota :


Summary

Un contenedor para datos de un tipo primitivo específico. (Traducción automática)

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 ()

Devuelve la matriz que respalda este búfer (operación opcional). (Traducción automática)

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 ()

Devuelve el desplazamiento dentro de la matriz de respaldo de este búfer del primer elemento del búfer (operación opcional). (Traducción automática)

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 ()

Devuelve la capacidad de este buffer. (Traducción automática)

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 ()

Borra este buffer. (Traducción automática)

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 ()

Crea un nuevo buffer que comparte el contenido de este buffer. (Traducción automática)

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 ()

Voltea este buffer. (Traducción automática)

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 ()

Indica si este búfer está respaldado o no por una matriz accesible. (Traducción automática)

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 ()

Indica si hay elementos entre la posición actual y el límite. (Traducción automática)

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 ()

Indica si este búfer es directo o no. (Traducción automática)

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 ()

Indica si este búfer es de solo lectura o no. (Traducción automática)

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 ()

Devuelve el límite de este buffer. (Traducción automática)

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)

Establece el límite de este búfer. (Traducción automática)

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 ()

Establece la marca de este búfer en su posición. (Traducción automática)

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 ()

Devuelve la posición de este buffer. (Traducción automática)

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)

Establece la posición de este búfer. (Traducción automática)

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 ()

Devuelve el número de elementos entre la posición actual y el límite. (Traducción automática)

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 ()

Restablece la posición de este búfer a la posición marcada previamente. (Traducción automática)

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 ()

Rebobina este búfer. (Traducción automática)

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 ()

Crea un nuevo buffer cuyo contenido es una subsecuencia compartida del contenido de este buffer. (Traducción automática)

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)

Crea un nuevo buffer cuyo contenido es una subsecuencia compartida del contenido de este buffer. (Traducción automática)

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

  • Ejemplos de API
    • Buffer
To top of page