Java : ByteBuffer con ejemplos

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

Nota :


Summary

Un buffer de bytes. (Traducción automática)

Class diagram

final var buffer = ByteBuffer.allocate(5);

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

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

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

buffer.put((byte) 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.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

final ByteBuffer alignedSlice (int unitSize)

Crea un nuevo búfer de bytes cuyo contenido es una subsecuencia compartida y alineada del contenido de este búfer. (Traducción automática)

Please see also : alignmentOffset(int index, int unitSize)

void test(int unitSize) {
    final byte[] array = {10, 20, 30, 40, 50, 60, 70, 80};
    final var buffer = ByteBuffer.wrap(array);

    System.out.println("unitSize = " + unitSize);
    for (int i = 0; i < 8; i++) {
        System.out.println("------");

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

        final var sliced = buffer.alignedSlice(unitSize);
        final var dst = new byte[sliced.remaining()];

        sliced.get(dst);
        System.out.println(Arrays.toString(dst));
    }
}
test(1);

// Result
// ↓
//unitSize = 1
//------
//position = 0
//[10, 20, 30, 40, 50, 60, 70, 80]
//------
//position = 1
//[20, 30, 40, 50, 60, 70, 80]
//------
//position = 2
//[30, 40, 50, 60, 70, 80]
//------
//position = 3
//[40, 50, 60, 70, 80]
//------
//position = 4
//[50, 60, 70, 80]
//------
//position = 5
//[60, 70, 80]
//------
//position = 6
//[70, 80]
//------
//position = 7
//[80]

test(2);

// Result
// ↓
//unitSize = 2
//------
//position = 0
//[10, 20, 30, 40, 50, 60, 70, 80]
//------
//position = 1
//[30, 40, 50, 60, 70, 80]
//------
//position = 2
//[30, 40, 50, 60, 70, 80]
//------
//position = 3
//[50, 60, 70, 80]
//------
//position = 4
//[50, 60, 70, 80]
//------
//position = 5
//[70, 80]
//------
//position = 6
//[70, 80]
//------
//position = 7
//[]

test(4);

// Result
// ↓
//unitSize = 4
//------
//position = 0
//[10, 20, 30, 40, 50, 60, 70, 80]
//------
//position = 1
//[50, 60, 70, 80]
//------
//position = 2
//[50, 60, 70, 80]
//------
//position = 3
//[50, 60, 70, 80]
//------
//position = 4
//[50, 60, 70, 80]
//------
//position = 5
//[]
//------
//position = 6
//[]
//------
//position = 7
//[]

final int alignmentOffset (int index, int unitSize)

Devuelve la dirección de memoria, apuntando al byte en el índice dado, módulo el tamaño de unidad dado. (Traducción automática)

Please see also : alignedSlice(int unitSize)

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

System.out.println("--- unitSize = 1 ---");
for (int i = 0; i < array.length; i++) {
    final var ret = buffer.alignmentOffset(i, 1);
    System.out.println("index = " + i + " : offset = " + ret);
}

System.out.println();
System.out.println("--- unitSize = 2 ---");
for (int i = 0; i < array.length; i++) {
    final var ret = buffer.alignmentOffset(i, 2);
    System.out.println("index = " + i + " : offset = " + ret);
}

System.out.println();
System.out.println("--- unitSize = 4 ---");
for (int i = 0; i < array.length; i++) {
    final var ret = buffer.alignmentOffset(i, 4);
    System.out.println("index = " + i + " : offset = " + ret);
}

// Result
// ↓
//--- unitSize = 1 ---
//index = 0 : offset = 0
//index = 1 : offset = 0
//index = 2 : offset = 0
//index = 3 : offset = 0
//index = 4 : offset = 0
//index = 5 : offset = 0
//index = 6 : offset = 0
//index = 7 : offset = 0
//
//--- unitSize = 2 ---
//index = 0 : offset = 0
//index = 1 : offset = 1
//index = 2 : offset = 0
//index = 3 : offset = 1
//index = 4 : offset = 0
//index = 5 : offset = 1
//index = 6 : offset = 0
//index = 7 : offset = 1
//
//--- unitSize = 4 ---
//index = 0 : offset = 0
//index = 1 : offset = 1
//index = 2 : offset = 2
//index = 3 : offset = 3
//index = 4 : offset = 0
//index = 5 : offset = 1
//index = 6 : offset = 2
//index = 7 : offset = 3

static ByteBuffer allocate (int capacity)

Asigna un nuevo búfer de bytes. (Traducción automática)

Please see also : allocateDirect(int capacity)

final var buffer = ByteBuffer.allocate(5);

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

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

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [0, 0, 0, 0, 0]
}

static ByteBuffer allocateDirect (int capacity)

Asigna un nuevo búfer de bytes directo. (Traducción automática)

Please see also : allocate(int capacity)

final var buffer = ByteBuffer.allocateDirect(5);

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

System.out.println(buffer.isDirect()); // true
System.out.println(buffer.hasArray()); // false

final byte[] array ()

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

final var buffer = ByteBuffer.allocate(5)
        .put((byte) 10)
        .put((byte) 20)
        .put((byte) 30);

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

final 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[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);

final var sliced = buffer.slice(2, 3);

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
}

abstract CharBuffer asCharBuffer ()

Crea una vista de este búfer de bytes como un búfer de caracteres. (Traducción automática)

final var array = new byte[6];

final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer.capacity()); // 6
System.out.println(buffer.limit()); // 6
System.out.println(buffer.position()); // 0

final var charBuffer = buffer.asCharBuffer();
System.out.println(charBuffer.capacity()); // 3
System.out.println(charBuffer.limit()); // 3
System.out.println(charBuffer.position()); // 0

System.out.println(Arrays.toString(array)); // [0, 0, 0, 0, 0, 0]

charBuffer.put('a');
System.out.println(Arrays.toString(array)); // [0, 97, 0, 0, 0, 0]

charBuffer.put('b');
System.out.println(Arrays.toString(array)); // [0, 97, 0, 98, 0, 0]

charBuffer.put('c');
System.out.println(Arrays.toString(array)); // [0, 97, 0, 98, 0, 99]

abstract DoubleBuffer asDoubleBuffer ()

Crea una vista de este búfer de bytes como un búfer doble. (Traducción automática)

final var array = new byte[16];

final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer.capacity()); // 16
System.out.println(buffer.limit()); // 16
System.out.println(buffer.position()); // 0

final var doubleBuffer = buffer.asDoubleBuffer();
System.out.println(doubleBuffer.capacity()); // 2
System.out.println(doubleBuffer.limit()); // 2
System.out.println(doubleBuffer.position()); // 0

// [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(array));

doubleBuffer.put(123.45);

// [64, 94, -36, -52, -52, -52, -52, -51, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(array));

doubleBuffer.put(-0.789);

// [64, 94, -36, -52, -52, -52, -52, -51, -65, -23, 63, 124, -19, -111, 104, 115]
System.out.println(Arrays.toString(array));

abstract FloatBuffer asFloatBuffer ()

Crea una vista de este búfer de bytes como un búfer flotante. (Traducción automática)

final var array = new byte[8];

final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer.capacity()); // 8
System.out.println(buffer.limit()); // 8
System.out.println(buffer.position()); // 0

final var floatBuffer = buffer.asFloatBuffer();
System.out.println(floatBuffer.capacity()); // 2
System.out.println(floatBuffer.limit()); // 2
System.out.println(floatBuffer.position()); // 0

// [0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(array));

floatBuffer.put(123.45f);

// [66, -10, -26, 102, 0, 0, 0, 0]
System.out.println(Arrays.toString(array));

floatBuffer.put(-0.789f);

// [66, -10, -26, 102, -65, 73, -5, -25]
System.out.println(Arrays.toString(array));

abstract IntBuffer asIntBuffer ()

Crea una vista de este búfer de bytes como un búfer int. (Traducción automática)

final var array = new byte[8];

final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer.capacity()); // 8
System.out.println(buffer.limit()); // 8
System.out.println(buffer.position()); // 0

final var intBuffer = buffer.asIntBuffer();
System.out.println(intBuffer.capacity()); // 2
System.out.println(intBuffer.limit()); // 2
System.out.println(intBuffer.position()); // 0

// [0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(array));

intBuffer.put(123456789);

// [7, 91, -51, 21, 0, 0, 0, 0]
System.out.println(Arrays.toString(array));

intBuffer.put(-1);

// [7, 91, -51, 21, -1, -1, -1, -1]
System.out.println(Arrays.toString(array));

abstract LongBuffer asLongBuffer ()

Crea una vista de este búfer de bytes como un búfer largo. (Traducción automática)

final var array = new byte[16];

final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer.capacity()); // 16
System.out.println(buffer.limit()); // 16
System.out.println(buffer.position()); // 0

final var longBuffer = buffer.asLongBuffer();
System.out.println(longBuffer.capacity()); // 2
System.out.println(longBuffer.limit()); // 2
System.out.println(longBuffer.position()); // 0

// [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(array));

longBuffer.put(1234567890123456789L);

// [17, 34, 16, -12, 125, -23, -127, 21, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(array));

longBuffer.put(-1L);

// [17, 34, 16, -12, 125, -23, -127, 21, -1, -1, -1, -1, -1, -1, -1, -1]
System.out.println(Arrays.toString(array));

abstract ByteBuffer asReadOnlyBuffer ()

Crea un nuevo búfer de bytes de solo lectura que comparte el contenido de este búfer. (Traducción automática)

final var buffer = ByteBuffer.allocate(3);
buffer.put((byte) 10);

final var readOnlyBuffer = buffer.asReadOnlyBuffer();

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=1 lim=3 cap=3]
System.out.println(readOnlyBuffer); // java.nio.HeapByteBufferR[pos=1 lim=3 cap=3]

readOnlyBuffer.clear();
System.out.println(readOnlyBuffer.get()); // 10
System.out.println(readOnlyBuffer.get()); // 0
System.out.println(readOnlyBuffer.get()); // 0

buffer.put((byte) 20);

readOnlyBuffer.clear();
System.out.println(readOnlyBuffer.get()); // 10
System.out.println(readOnlyBuffer.get()); // 20
System.out.println(readOnlyBuffer.get()); // 0

try {
    var _ = readOnlyBuffer.put((byte) 30);
} catch (ReadOnlyBufferException e) {
    System.out.println("ReadOnlyBufferException!");
}

// Result
// ↓
//ReadOnlyBufferException!

abstract ShortBuffer asShortBuffer ()

Crea una vista de este búfer de bytes como un búfer corto. (Traducción automática)

final var array = new byte[4];

final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer.capacity()); // 4
System.out.println(buffer.limit()); // 4
System.out.println(buffer.position()); // 0

final var shortBuffer = buffer.asShortBuffer();
System.out.println(shortBuffer.capacity()); // 2
System.out.println(shortBuffer.limit()); // 2
System.out.println(shortBuffer.position()); // 0

// [0, 0, 0, 0]
System.out.println(Arrays.toString(array));

shortBuffer.put((short) 1234);

// [4, -46, 0, 0]
System.out.println(Arrays.toString(array));

shortBuffer.put((short) -1);

// [4, -46, -1, -1]
System.out.println(Arrays.toString(array));

ByteBuffer clear ()

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

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

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

Compacta este buffer (operación opcional). (Traducción automática)

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

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

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

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

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

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

System.out.println(Arrays.toString(array)); // [50, 60, 30, 40, 50, 60]

int compareTo (ByteBuffer that)

Compara este buffer con otro. (Traducción automática)

final var buffer1 = ByteBuffer.allocate(3)
        .put((byte) 10)
        .put((byte) 20)
        .put((byte) 30);

final var buffer2 = ByteBuffer.allocate(3)
        .put((byte) 10)
        .put((byte) 20)
        .put((byte) 30);

buffer1.clear();
buffer2.clear();

System.out.println(buffer1.compareTo(buffer2)); // 0
final var buffer1 = ByteBuffer.allocate(3)
        .put((byte) 10)
        .put((byte) 20)
        .put((byte) 30);

final var buffer2 = ByteBuffer.allocate(3)
        .put((byte) 5)
        .put((byte) 50)
        .put((byte) 500);

final var buffer3 = ByteBuffer.allocate(3)
        .put((byte) 30)
        .put((byte) 20)
        .put((byte) 10);

buffer1.clear();
buffer2.clear();
buffer3.clear();

System.out.println(buffer1.compareTo(buffer2)); // 5
System.out.println(buffer1.compareTo(buffer3)); // -20

abstract ByteBuffer duplicate ()

Crea un nuevo búfer de bytes que comparte el contenido de este búfer. (Traducción automática)

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

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

final var duplicated = buffer.duplicate();

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

boolean equals (Object ob)

Indica si este búfer es igual o no a otro objeto. (Traducción automática)

final var buffer1 = ByteBuffer.allocate(3)
        .put((byte) 10)
        .put((byte) 20)
        .put((byte) 30);

final var buffer2 = ByteBuffer.allocate(3)
        .put((byte) 10)
        .put((byte) 20)
        .put((byte) 30);

buffer1.clear();
buffer2.clear();

System.out.println(buffer1.equals(buffer2)); // true
final var buffer1 = ByteBuffer.allocate(3)
        .put((byte) 10)
        .put((byte) 20)
        .put((byte) 30);

final var buffer2 = ByteBuffer.allocate(3)
        .put((byte) 5)
        .put((byte) 50)
        .put((byte) 500);

final var buffer3 = ByteBuffer.allocate(3)
        .put((byte) 30)
        .put((byte) 20)
        .put((byte) 10);

buffer1.clear();
buffer2.clear();
buffer3.clear();

System.out.println(buffer1.equals(buffer2)); // false
System.out.println(buffer1.equals(buffer3)); // false

ByteBuffer 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.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 byte get ()

Método de obtención relativa. (Traducción automática)

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

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 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(buffer.position()); // 5

ByteBuffer get (byte[] dst)

Método de obtención masiva relativa. (Traducción automática)

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

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

{
    final var dst = new byte[3];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0]

    final var ret = buffer.get(dst);
    System.out.println(ret); // java.nio.HeapByteBuffer[pos=3 lim=7 cap=7]

    System.out.println(Arrays.toString(dst)); // [10, 20, 30]
    System.out.println(buffer.position()); // 3
}
{
    final var dst = new byte[3];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0]

    final var ret = buffer.get(dst);
    System.out.println(ret); // java.nio.HeapByteBuffer[pos=6 lim=7 cap=7]

    System.out.println(Arrays.toString(dst)); // [40, 50, 60]
    System.out.println(buffer.position()); // 6
}

try {
    final var dst = new byte[3];
    var _ = buffer.get(dst);
} catch (BufferUnderflowException e) {
    System.out.println("BufferUnderflowException!");
}

// Result
// ↓
//BufferUnderflowException!

ByteBuffer get (byte[] dst, int offset, int length)

Método de obtención masiva relativa. (Traducción automática)

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

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

{
    final var dst = new byte[5];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0, 0, 0]

    final var ret = buffer.get(dst, 0, 3);
    System.out.println(ret); // java.nio.HeapByteBuffer[pos=3 lim=8 cap=8]

    System.out.println(Arrays.toString(dst)); // [10, 20, 30, 0, 0]
    System.out.println(buffer.position()); // 3
}
{
    final var dst = new byte[5];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0, 0, 0]

    final var ret = buffer.get(dst, 1, 3);
    System.out.println(ret); // java.nio.HeapByteBuffer[pos=6 lim=8 cap=8]

    System.out.println(Arrays.toString(dst)); // [0, 40, 50, 60, 0]
    System.out.println(buffer.position()); // 6
}
{
    final var dst = new byte[5];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0, 0, 0]

    final var ret = buffer.get(dst, 3, 2);
    System.out.println(ret); // java.nio.HeapByteBuffer[pos=8 lim=8 cap=8]

    System.out.println(Arrays.toString(dst)); // [0, 0, 0, 70, 80]
    System.out.println(buffer.position()); // 8
}

abstract byte get (int index)

Método de obtención absoluta. (Traducción automática)

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

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

System.out.println(buffer.get(0)); // 10
System.out.println(buffer.get(1)); // 20
System.out.println(buffer.get(2)); // 30
System.out.println(buffer.get(3)); // 40
System.out.println(buffer.get(4)); // 50

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

ByteBuffer get (int index, byte[] dst)

Método de obtención masiva absoluta. (Traducción automática)

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

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

{
    final var dst = new byte[3];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0]

    final var ret = buffer.get(0, dst);
    System.out.println(ret); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]

    System.out.println(Arrays.toString(dst)); // [10, 20, 30]
    System.out.println(buffer.position()); // 0
}
{
    final var dst = new byte[3];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0]

    final var ret = buffer.get(1, dst);
    System.out.println(ret); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]

    System.out.println(Arrays.toString(dst)); // [20, 30, 40]
    System.out.println(buffer.position()); // 0
}
{
    final var dst = new byte[3];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0]

    final var ret = buffer.get(2, dst);
    System.out.println(ret); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]

    System.out.println(Arrays.toString(dst)); // [30, 40, 50]
    System.out.println(buffer.position()); // 0
}

ByteBuffer get (int index, byte[] dst, int offset, int length)

Método de obtención masiva absoluta. (Traducción automática)

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

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

{
    final var dst = new byte[5];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0, 0, 0]

    final var ret = buffer.get(0, dst, 0, 3);
    System.out.println(ret); // java.nio.HeapByteBuffer[pos=0 lim=6 cap=6]

    System.out.println(Arrays.toString(dst)); // [10, 20, 30, 0, 0]
    System.out.println(buffer.position()); // 0
}
{
    final var dst = new byte[5];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0, 0, 0]

    final var ret = buffer.get(2, dst, 1, 3);
    System.out.println(ret); // java.nio.HeapByteBuffer[pos=0 lim=6 cap=6]

    System.out.println(Arrays.toString(dst)); // [0, 30, 40, 50, 0]
    System.out.println(buffer.position()); // 0
}
{
    final var dst = new byte[5];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0, 0, 0]

    final var ret = buffer.get(4, dst, 3, 2);
    System.out.println(ret); // java.nio.HeapByteBuffer[pos=0 lim=6 cap=6]

    System.out.println(Arrays.toString(dst)); // [0, 0, 0, 50, 60]
    System.out.println(buffer.position()); // 0
}

abstract char getChar ()

Método de obtención relativa para leer un valor de carácter. (Traducción automática)

final var array = new byte[6];
final var buffer = ByteBuffer.wrap(array)
        .putChar('a')
        .putChar('b')
        .putChar('c')
        .clear();

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

// [0, 97, 0, 98, 0, 99]
System.out.println(Arrays.toString(array));

System.out.println(buffer.getChar()); // a
System.out.println(buffer.position()); // 2

System.out.println(buffer.getChar()); // b
System.out.println(buffer.position()); // 4

System.out.println(buffer.getChar()); // c
System.out.println(buffer.position()); // 6

abstract char getChar (int index)

Método de obtención absoluta para leer un valor de carácter. (Traducción automática)

final var array = new byte[6];
final var buffer = ByteBuffer.wrap(array)
        .putChar('a')
        .putChar('b')
        .putChar('c')
        .clear();

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

// [0, 97, 0, 98, 0, 99]
System.out.println(Arrays.toString(array));

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

System.out.println(buffer.getChar(2)); // b
System.out.println(buffer.position()); // 0

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

abstract double getDouble ()

Método de obtención relativa para leer un valor doble. (Traducción automática)

final var array = new byte[16];
final var buffer = ByteBuffer.wrap(array)
        .putDouble(123.45)
        .putDouble(-0.789)
        .clear();

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

// [64, 94, -36, -52, -52, -52, -52, -51, -65, -23, 63, 124, -19, -111, 104, 115]
System.out.println(Arrays.toString(array));

System.out.println(buffer.getDouble()); // 123.45
System.out.println(buffer.position()); // 8

System.out.println(buffer.getDouble()); // -0.789
System.out.println(buffer.position()); // 16

abstract double getDouble (int index)

Método de obtención absoluta para leer un valor doble. (Traducción automática)

final var array = new byte[16];
final var buffer = ByteBuffer.wrap(array)
        .putDouble(123.45)
        .putDouble(-0.789)
        .clear();

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

// [64, 94, -36, -52, -52, -52, -52, -51, -65, -23, 63, 124, -19, -111, 104, 115]
System.out.println(Arrays.toString(array));

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

System.out.println(buffer.getDouble(8)); // -0.789
System.out.println(buffer.position()); // 0

abstract float getFloat ()

Método de obtención relativa para leer un valor flotante. (Traducción automática)

final var array = new byte[8];
final var buffer = ByteBuffer.wrap(array)
        .putFloat(123.45f)
        .putFloat(-0.789f)
        .clear();

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

// [66, -10, -26, 102, -65, 73, -5, -25]
System.out.println(Arrays.toString(array));

System.out.println(buffer.getFloat()); // 123.45
System.out.println(buffer.position()); // 4

System.out.println(buffer.getFloat()); // -0.789
System.out.println(buffer.position()); // 8

abstract float getFloat (int index)

Método de obtención absoluta para leer un valor flotante. (Traducción automática)

final var array = new byte[8];
final var buffer = ByteBuffer.wrap(array)
        .putFloat(123.45f)
        .putFloat(-0.789f)
        .clear();

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

// [66, -10, -26, 102, -65, 73, -5, -25]
System.out.println(Arrays.toString(array));

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

System.out.println(buffer.getFloat(4)); // -0.789
System.out.println(buffer.position()); // 0

abstract int getInt ()

Método de obtención relativa para leer un valor int. (Traducción automática)

final var array = new byte[8];
final var buffer = ByteBuffer.wrap(array)
        .putInt(12345)
        .putInt(-789)
        .clear();

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

// [0, 0, 48, 57, -1, -1, -4, -21]
System.out.println(Arrays.toString(array));

System.out.println(buffer.getInt()); // 12345
System.out.println(buffer.position()); // 4

System.out.println(buffer.getInt()); // -789
System.out.println(buffer.position()); // 8

abstract int getInt (int index)

Método de obtención absoluta para leer un valor int. (Traducción automática)

final var array = new byte[8];
final var buffer = ByteBuffer.wrap(array)
        .putInt(12345)
        .putInt(-789)
        .clear();

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

// [0, 0, 48, 57, -1, -1, -4, -21]
System.out.println(Arrays.toString(array));

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

System.out.println(buffer.getInt(4)); // -789
System.out.println(buffer.position()); // 0

abstract long getLong ()

Método de obtención relativa para leer un valor largo. (Traducción automática)

final var array = new byte[16];
final var buffer = ByteBuffer.wrap(array)
        .putLong(12345L)
        .putLong(-789L)
        .clear();

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

// [0, 0, 0, 0, 0, 0, 48, 57, -1, -1, -1, -1, -1, -1, -4, -21]
System.out.println(Arrays.toString(array));

System.out.println(buffer.getLong()); // 12345
System.out.println(buffer.position()); // 8

System.out.println(buffer.getLong()); // -789
System.out.println(buffer.position()); // 16

abstract long getLong (int index)

Método de obtención absoluta para leer un valor largo. (Traducción automática)

final var array = new byte[16];
final var buffer = ByteBuffer.wrap(array)
        .putLong(12345L)
        .putLong(-789L)
        .clear();

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

// [0, 0, 0, 0, 0, 0, 48, 57, -1, -1, -1, -1, -1, -1, -4, -21]
System.out.println(Arrays.toString(array));

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

System.out.println(buffer.getLong(8)); // -789
System.out.println(buffer.position()); // 0

abstract short getShort ()

Método de obtención relativa para leer un valor corto. (Traducción automática)

final var array = new byte[4];
final var buffer = ByteBuffer.wrap(array)
        .putShort((short) 12345)
        .putShort((short) -789)
        .clear();

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

// [48, 57, -4, -21]
System.out.println(Arrays.toString(array));

System.out.println(buffer.getShort()); // 12345
System.out.println(buffer.position()); // 2

System.out.println(buffer.getShort()); // -789
System.out.println(buffer.position()); // 4

abstract short getShort (int index)

Método de obtención absoluta para leer un valor corto. (Traducción automática)

final var array = new byte[4];
final var buffer = ByteBuffer.wrap(array)
        .putShort((short) 12345)
        .putShort((short) -789)
        .clear();

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

// [48, 57, -4, -21]
System.out.println(Arrays.toString(array));

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

System.out.println(buffer.getShort(2)); // -789
System.out.println(buffer.position()); // 0

final boolean hasArray ()

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

final var buffer = ByteBuffer.allocate(5)
        .put((byte) 10)
        .put((byte) 20)
        .put((byte) 30);

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.allocate(5)
        .put((byte) 10)
        .put((byte) 20)
        .put((byte) 30)
        .asReadOnlyBuffer();

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

// Result
// ↓
//UnsupportedOperationException!

int hashCode ()

Devuelve el código hash actual de este búfer. (Traducción automática)

final var buffer = ByteBuffer.allocate(0);
System.out.println(buffer.hashCode()); // 1
final var buffer = ByteBuffer.allocate(3)
        .put((byte) 10)
        .put((byte) 20)
        .put((byte) 30)
        .clear();

System.out.println(buffer.hashCode()); // 59251
final var buffer = ByteBuffer.allocate(3)
        .put((byte) -10)
        .put((byte) -20)
        .put((byte) -30)
        .clear();

System.out.println(buffer.hashCode()); // 331

abstract boolean isDirect ()

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

final var buffer = ByteBuffer.allocate(5);
System.out.println(buffer.isDirect()); // false
final byte[] array = {10, 20, 30};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer.isDirect()); // false
final var buffer = ByteBuffer.allocateDirect(5);
System.out.println(buffer.isDirect()); // true

ByteBuffer limit (int newLimit)

Establece el límite 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); // 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.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!

ByteBuffer 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.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

int mismatch (ByteBuffer that)

Encuentra y devuelve el índice relativo de la primera falta de coincidencia entre este búfer y un búfer determinado. (Traducción automática)

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

{
    final byte[] a = {10};
    final var that = ByteBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // 1
}
{
    final byte[] a = {10, 20};
    final var that = ByteBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // 2
}
{
    final byte[] a = {10, 20, 30};
    final var that = ByteBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // 3
}
{
    final byte[] a = {10, 20, 30, 40};
    final var that = ByteBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // -1
}
{
    final byte[] a = {10, 20, 30, 40, 50};
    final var that = ByteBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // 4
}
{
    final byte[] a = {10, 20, 99};
    final var that = ByteBuffer.wrap(a); // 2

    System.out.println(buffer.mismatch(that));
}
{
    final byte[] a = {-90, -80};
    final var that = ByteBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // 0
}

final ByteOrder order ()

Recupera el orden de bytes de este búfer. (Traducción automática)

final var array = new byte[4];
final var buffer = ByteBuffer.wrap(array)
        .putInt(1234567890)
        .clear();

System.out.println(buffer.order()); // BIG_ENDIAN

System.out.println(Arrays.toString(array)); // [73, -106, 2, -46]
System.out.println(buffer.getInt(0)); // 1234567890

final var ret = buffer.order(ByteOrder.LITTLE_ENDIAN);
System.out.println(ret); // java.nio.HeapByteBuffer[pos=0 lim=4 cap=4]
System.out.println(buffer.order()); // LITTLE_ENDIAN

System.out.println(Arrays.toString(array)); // [73, -106, 2, -46]
System.out.println(buffer.getInt(0)); // -771582391

final ByteBuffer order (ByteOrder bo)

Modifica el orden de bytes de este búfer. (Traducción automática)

final var array = new byte[4];
final var buffer = ByteBuffer.wrap(array)
        .putInt(1234567890)
        .clear();

System.out.println(buffer.order()); // BIG_ENDIAN

System.out.println(Arrays.toString(array)); // [73, -106, 2, -46]
System.out.println(buffer.getInt(0)); // 1234567890

final var ret = buffer.order(ByteOrder.LITTLE_ENDIAN);
System.out.println(ret); // java.nio.HeapByteBuffer[pos=0 lim=4 cap=4]
System.out.println(buffer.order()); // LITTLE_ENDIAN

System.out.println(Arrays.toString(array)); // [73, -106, 2, -46]
System.out.println(buffer.getInt(0)); // -771582391

ByteBuffer 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.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

abstract ByteBuffer put (byte b)

Método de colocación relativa (operación opcional). (Traducción automática)

final var buffer = ByteBuffer.allocate(5);

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

System.out.println(buffer.put((byte) 10)); // java.nio.HeapByteBuffer[pos=1 lim=5 cap=5]
System.out.println(buffer.put((byte) 20)); // java.nio.HeapByteBuffer[pos=2 lim=5 cap=5]
System.out.println(buffer.put((byte) 30)); // java.nio.HeapByteBuffer[pos=3 lim=5 cap=5]
System.out.println(buffer.put((byte) 40)); // java.nio.HeapByteBuffer[pos=4 lim=5 cap=5]
System.out.println(buffer.put((byte) 50)); // java.nio.HeapByteBuffer[pos=5 lim=5 cap=5]

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

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50]
}

final ByteBuffer put (byte[] src)

Método de colocación masiva relativa (operación opcional). (Traducción automática)

final var buffer = ByteBuffer.allocate(6);

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

{
    final byte[] src = {10};

    final var ret = buffer.put(src);
    System.out.println(ret); // java.nio.HeapByteBuffer[pos=1 lim=6 cap=6]
}
{
    final byte[] src = {20, 30};

    final var ret = buffer.put(src);
    System.out.println(ret); // java.nio.HeapByteBuffer[pos=3 lim=6 cap=6]
}
{
    final byte[] src = {40, 50, 60};

    final var ret = buffer.put(src);
    System.out.println(ret); // java.nio.HeapByteBuffer[pos=6 lim=6 cap=6]
}

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

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50, 60]
}

ByteBuffer put (byte[] src, int offset, int length)

Método de colocación masiva relativa (operación opcional). (Traducción automática)

final var array = new byte[6];
final var buffer = ByteBuffer.wrap(array);

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

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

final var ret1 = buffer.put(src, 0, 1);
System.out.println(ret1); // java.nio.HeapByteBuffer[pos=1 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 0, 0, 0, 0, 0]

final var ret2 = buffer.put(src, 1, 2);
System.out.println(ret2); // java.nio.HeapByteBuffer[pos=3 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 20, 30, 0, 0, 0]

final var ret3 = buffer.put(src, 3, 3);
System.out.println(ret3); // java.nio.HeapByteBuffer[pos=6 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50, 60]

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

abstract ByteBuffer put (int index, byte b)

Método put absoluto (operación opcional). (Traducción automática)

final var buffer = ByteBuffer.allocate(5);

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

System.out.println(buffer.put(0, (byte) 10)); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.put(1, (byte) 20)); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.put(2, (byte) 30)); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.put(3, (byte) 40)); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.put(4, (byte) 50)); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]

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

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50]
}

ByteBuffer put (int index, byte[] src)

Método de colocación masiva absoluta (operación opcional). (Traducción automática)

final var buffer = ByteBuffer.allocate(6);

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

{
    final byte[] src = {10};

    final var ret = buffer.put(0, src);
    System.out.println(ret); // java.nio.HeapByteBuffer[pos=0 lim=6 cap=6]
}
{
    final byte[] src = {20, 30};

    final var ret = buffer.put(1, src);
    System.out.println(ret); // java.nio.HeapByteBuffer[pos=0 lim=6 cap=6]
}
{
    final byte[] src = {40, 50, 60};

    final var ret = buffer.put(3, src);
    System.out.println(ret); // java.nio.HeapByteBuffer[pos=0 lim=6 cap=6]
}

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

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50, 60]
}

ByteBuffer put (int index, byte[] src, int offset, int length)

Método de colocación masiva absoluta (operación opcional). (Traducción automática)

final var array = new byte[6];
final var buffer = ByteBuffer.wrap(array);

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

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

final var ret1 = buffer.put(0, src, 0, 1);
System.out.println(ret1); // java.nio.HeapByteBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 0, 0, 0, 0, 0]

final var ret2 = buffer.put(1, src, 1, 2);
System.out.println(ret2); // java.nio.HeapByteBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 20, 30, 0, 0, 0]

final var ret3 = buffer.put(3, src, 3, 3);
System.out.println(ret3); // java.nio.HeapByteBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50, 60]

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

ByteBuffer put (int index, ByteBuffer src, int offset, int length)

Método de colocación masiva absoluta (operación opcional). (Traducción automática)

final var array = new byte[6];
final var buffer = ByteBuffer.wrap(array);

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

final byte[] srcArray = {10, 20, 30, 40, 50, 60};
final var src = ByteBuffer.wrap(srcArray);

final var ret1 = buffer.put(0, src, 0, 1);
System.out.println(ret1); // java.nio.HeapByteBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 0, 0, 0, 0, 0]

final var ret2 = buffer.put(1, src, 1, 2);
System.out.println(ret2); // java.nio.HeapByteBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 20, 30, 0, 0, 0]

final var ret3 = buffer.put(3, src, 3, 3);
System.out.println(ret3); // java.nio.HeapByteBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50, 60]

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

ByteBuffer put (ByteBuffer src)

Método de colocación masiva relativa (operación opcional). (Traducción automática)

final var buffer = ByteBuffer.allocate(5);

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

{
    final byte[] array = {10, 20};
    final var src = ByteBuffer.wrap(array);

    final var ret = buffer.put(src);
    System.out.println(ret); // java.nio.HeapByteBuffer[pos=2 lim=5 cap=5]
}
{
    final byte[] array = {30, 40, 50};
    final var src = ByteBuffer.wrap(array);

    final var ret = buffer.put(src);
    System.out.println(ret); // java.nio.HeapByteBuffer[pos=5 lim=5 cap=5]
}

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

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50]
}

abstract ByteBuffer putChar (char value)

Método put relativo para escribir un valor char (operación opcional). (Traducción automática)

final var array = new byte[6];
final var buffer = ByteBuffer.wrap(array);

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

final var ret1 = buffer.putChar('a');
System.out.println(ret1); // java.nio.HeapByteBuffer[pos=2 lim=6 cap=6]
System.out.println(buffer.position()); // 2

// [0, 97, 0, 0, 0, 0]
System.out.println(Arrays.toString(array));

final var ret2 = buffer.putChar('b');
System.out.println(ret2); // java.nio.HeapByteBuffer[pos=4 lim=6 cap=6]
System.out.println(buffer.position()); // 4

// [0, 97, 0, 98, 0, 0]
System.out.println(Arrays.toString(array));

final var ret3 = buffer.putChar('c');
System.out.println(ret3); // java.nio.HeapByteBuffer[pos=6 lim=6 cap=6]
System.out.println(buffer.position()); // 6

// [0, 97, 0, 98, 0, 99]
System.out.println(Arrays.toString(array));

abstract ByteBuffer putChar (int index, char value)

Método put absoluto para escribir un valor char (operación opcional). (Traducción automática)

final var array = new byte[6];
final var buffer = ByteBuffer.wrap(array);

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

final var ret1 = buffer.putChar(0, 'a');
System.out.println(ret1); // java.nio.HeapByteBuffer[pos=0 lim=6 cap=6]
System.out.println(buffer.position()); // 0

// [0, 97, 0, 0, 0, 0]
System.out.println(Arrays.toString(array));

final var ret2 = buffer.putChar(2, 'b');
System.out.println(ret2); // java.nio.HeapByteBuffer[pos=0 lim=6 cap=6]
System.out.println(buffer.position()); // 0

// [0, 97, 0, 98, 0, 0]
System.out.println(Arrays.toString(array));

final var ret3 = buffer.putChar(4, 'c');
System.out.println(ret3); // java.nio.HeapByteBuffer[pos=0 lim=6 cap=6]
System.out.println(buffer.position()); // 0

// [0, 97, 0, 98, 0, 99]
System.out.println(Arrays.toString(array));

abstract ByteBuffer putDouble (double value)

Método put relativo para escribir un valor doble (operación opcional). (Traducción automática)

final var array = new byte[16];
final var buffer = ByteBuffer.wrap(array);

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

final var ret1 = buffer.putDouble(123.45);
System.out.println(ret1); // java.nio.HeapByteBuffer[pos=8 lim=16 cap=16]
System.out.println(buffer.position()); // 8

// [64, 94, -36, -52, -52, -52, -52, -51, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(array));

final var ret2 = buffer.putDouble(-0.789);
System.out.println(ret2); // java.nio.HeapByteBuffer[pos=16 lim=16 cap=16]
System.out.println(buffer.position()); // 16

// [64, 94, -36, -52, -52, -52, -52, -51, -65, -23, 63, 124, -19, -111, 104, 115]
System.out.println(Arrays.toString(array));

abstract ByteBuffer putDouble (int index, double value)

Método put absoluto para escribir un valor doble (operación opcional). (Traducción automática)

final var array = new byte[16];
final var buffer = ByteBuffer.wrap(array);

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

final var ret1 = buffer.putDouble(0, 123.45);
System.out.println(ret1); // java.nio.HeapByteBuffer[pos=0 lim=16 cap=16]
System.out.println(buffer.position()); // 0

// [64, 94, -36, -52, -52, -52, -52, -51, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(array));

final var ret2 = buffer.putDouble(8, -0.789);
System.out.println(ret2); // java.nio.HeapByteBuffer[pos=0 lim=16 cap=16]
System.out.println(buffer.position()); // 0

// [64, 94, -36, -52, -52, -52, -52, -51, -65, -23, 63, 124, -19, -111, 104, 115]
System.out.println(Arrays.toString(array));

abstract ByteBuffer putFloat (float value)

Método put relativo para escribir un valor flotante (operación opcional). (Traducción automática)

final var array = new byte[8];
final var buffer = ByteBuffer.wrap(array);

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

final var ret1 = buffer.putFloat(123.45f);
System.out.println(ret1); // java.nio.HeapByteBuffer[pos=4 lim=8 cap=8]
System.out.println(buffer.position()); // 4

// [66, -10, -26, 102, 0, 0, 0, 0]
System.out.println(Arrays.toString(array));

final var ret2 = buffer.putFloat(-0.789f);
System.out.println(ret2); // java.nio.HeapByteBuffer[pos=8 lim=8 cap=8]
System.out.println(buffer.position()); // 8

// [66, -10, -26, 102, -65, 73, -5, -25]
System.out.println(Arrays.toString(array));

abstract ByteBuffer putFloat (int index, float value)

Método put absoluto para escribir un valor flotante (operación opcional). (Traducción automática)

final var array = new byte[8];
final var buffer = ByteBuffer.wrap(array);

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

final var ret1 = buffer.putFloat(0, 123.45f);
System.out.println(ret1); // java.nio.HeapByteBuffer[pos=0 lim=8 cap=8]
System.out.println(buffer.position()); // 0

// [66, -10, -26, 102, 0, 0, 0, 0]
System.out.println(Arrays.toString(array));

final var ret2 = buffer.putFloat(4, -0.789f);
System.out.println(ret2); // java.nio.HeapByteBuffer[pos=0 lim=8 cap=8]
System.out.println(buffer.position()); // 0

// [66, -10, -26, 102, -65, 73, -5, -25]
System.out.println(Arrays.toString(array));

abstract ByteBuffer putInt (int value)

Método put relativo para escribir un valor int (operación opcional). (Traducción automática)

final var array = new byte[8];
final var buffer = ByteBuffer.wrap(array);

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

final var ret1 = buffer.putInt(12345);
System.out.println(ret1); // java.nio.HeapByteBuffer[pos=4 lim=8 cap=8]
System.out.println(buffer.position()); // 4

// [0, 0, 48, 57, 0, 0, 0, 0]
System.out.println(Arrays.toString(array));

final var ret2 = buffer.putInt(-789);
System.out.println(ret2); // java.nio.HeapByteBuffer[pos=8 lim=8 cap=8]
System.out.println(buffer.position()); // 8

// [0, 0, 48, 57, -1, -1, -4, -21]
System.out.println(Arrays.toString(array));

abstract ByteBuffer putInt (int index, int value)

Método put absoluto para escribir un valor int (operación opcional). (Traducción automática)

final var array = new byte[8];
final var buffer = ByteBuffer.wrap(array);

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

final var ret1 = buffer.putInt(0, 12345);
System.out.println(ret1); // java.nio.HeapByteBuffer[pos=0 lim=8 cap=8]
System.out.println(buffer.position()); // 0

// [0, 0, 48, 57, 0, 0, 0, 0]
System.out.println(Arrays.toString(array));

final var ret2 = buffer.putInt(4, -789);
System.out.println(ret2); // java.nio.HeapByteBuffer[pos=0 lim=8 cap=8]
System.out.println(buffer.position()); // 0

// [0, 0, 48, 57, -1, -1, -4, -21]
System.out.println(Arrays.toString(array));

abstract ByteBuffer putLong (int index, long value)

Método put absoluto para escribir un valor largo (operación opcional). (Traducción automática)

final var array = new byte[16];
final var buffer = ByteBuffer.wrap(array);

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

final var ret1 = buffer.putLong(0, 12345L);
System.out.println(ret1); // java.nio.HeapByteBuffer[pos=0 lim=16 cap=16]
System.out.println(buffer.position()); // 0

// [0, 0, 0, 0, 0, 0, 48, 57, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(array));

final var ret2 = buffer.putLong(8, -789L);
System.out.println(ret2); // java.nio.HeapByteBuffer[pos=0 lim=16 cap=16]
System.out.println(buffer.position()); // 0

// [0, 0, 0, 0, 0, 0, 48, 57, -1, -1, -1, -1, -1, -1, -4, -21]
System.out.println(Arrays.toString(array));

abstract ByteBuffer putLong (long value)

Método put relativo para escribir un valor largo (operación opcional). (Traducción automática)

final var array = new byte[16];
final var buffer = ByteBuffer.wrap(array);

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

final var ret1 = buffer.putLong(12345L);
System.out.println(ret1); // java.nio.HeapByteBuffer[pos=8 lim=16 cap=16]
System.out.println(buffer.position()); // 8

// [0, 0, 0, 0, 0, 0, 48, 57, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(array));

final var ret2 = buffer.putLong(-789L);
System.out.println(ret2); // java.nio.HeapByteBuffer[pos=16 lim=16 cap=16]
System.out.println(buffer.position()); // 16

// [0, 0, 0, 0, 0, 0, 48, 57, -1, -1, -1, -1, -1, -1, -4, -21]
System.out.println(Arrays.toString(array));

abstract ByteBuffer putShort (int index, short value)

Método put absoluto para escribir un valor corto (operación opcional). (Traducción automática)

final var array = new byte[4];
final var buffer = ByteBuffer.wrap(array);

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

final var ret1 = buffer.putShort(0, (short) 12345);
System.out.println(ret1); // java.nio.HeapByteBuffer[pos=0 lim=4 cap=4]
System.out.println(buffer.position()); // 0

// [48, 57, 0, 0]
System.out.println(Arrays.toString(array));

final var ret2 = buffer.putShort(2, (short) -789);
System.out.println(ret2); // java.nio.HeapByteBuffer[pos=0 lim=4 cap=4]
System.out.println(buffer.position()); // 0

// [48, 57, -4, -21]
System.out.println(Arrays.toString(array));

abstract ByteBuffer putShort (short value)

Método put relativo para escribir un valor corto (operación opcional). (Traducción automática)

final var array = new byte[4];
final var buffer = ByteBuffer.wrap(array);

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

final var ret1 = buffer.putShort((short) 12345);
System.out.println(ret1); // java.nio.HeapByteBuffer[pos=2 lim=4 cap=4]
System.out.println(buffer.position()); // 2

// [48, 57, 0, 0]
System.out.println(Arrays.toString(array));

final var ret2 = buffer.putShort((short) -789);
System.out.println(ret2); // java.nio.HeapByteBuffer[pos=4 lim=4 cap=4]
System.out.println(buffer.position()); // 4

// [48, 57, -4, -21]
System.out.println(Arrays.toString(array));

ByteBuffer 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.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

ByteBuffer rewind ()

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

final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.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.HeapByteBuffer[pos=0 lim=4 cap=5]

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

abstract ByteBuffer slice ()

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

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

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

final var sliced = buffer.slice();

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 ByteBuffer slice (int index, int length)

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

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

final var sliced = buffer.slice(2, 3);

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

String toString ()

Devuelve una cadena que resume el estado de este búfer. (Traducción automática)

final var buffer = ByteBuffer.allocate(5)
        .limit(4)
        .position(2);

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

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

buffer.clear();

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

static ByteBuffer wrap (byte[] array)

Envuelve una matriz de bytes en un búfer. (Traducción automática)

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

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

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
}

static ByteBuffer wrap (byte[] array, int offset, int length)

Envuelve una matriz de bytes en un búfer. (Traducción automática)

final byte[] array = {10, 20, 30, 40, 50, 60, 70};
final var buffer = ByteBuffer.wrap(array, 2, 4);

System.out.println(buffer.capacity()); // 7
System.out.println(buffer.limit()); // 6
System.out.println(buffer.position()); // 2

if (buffer.hasArray()) {
    System.out.println(buffer.arrayOffset()); // 0

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

Methods declared in Buffer

capacity, hasRemaining, isReadOnly, limit, position, remaining

Consulte el siguiente enlace.


Related posts

To top of page