Java : CharBuffer con ejemplos

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

Nota :


Summary

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

Class diagram

final var buffer = CharBuffer.allocate(5);

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

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

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

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

if (buffer.hasArray()) {
    final var ret = buffer.array();
    System.out.println(Arrays.toString(ret)); // [a, b, c,  ,  ]
}
final char[] array = {'a', 'b', 'c'};
final var buffer = CharBuffer.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()); // a
System.out.println(buffer.position()); // 1

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

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

Methods

static CharBuffer allocate (int capacity)

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

final var buffer = CharBuffer.allocate(5);

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

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

CharBuffer append (char c)

Añade el carácter especificado a este búfer (operación opcional). (Traducción automática)

final var buffer = CharBuffer.allocate(3);

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

final var ret1 = buffer.append('a');
System.out.println(ret1.position()); // 1

final var ret2 = buffer.append('b');
System.out.println(ret2.position()); // 2

final var ret3 = buffer.append('c');
System.out.println(ret3.position()); // 3

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

CharBuffer append (CharSequence csq)

Añade la secuencia de caracteres especificada a este búfer (operación opcional). (Traducción automática)

final var buffer = CharBuffer.allocate(6);

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

final var ret1 = buffer.append("a");
System.out.println(ret1.position()); // 1

final var ret2 = buffer.append("bc");
System.out.println(ret2.position()); // 3

final var ret3 = buffer.append("XYZ");
System.out.println(ret3.position()); // 6

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [a, b, c, X, Y, Z]
}

CharBuffer append (CharSequence csq, int start, int end)

Añade una subsecuencia de la secuencia de caracteres especificada a este búfer (operación opcional). (Traducción automática)

final var array = new char[6];
final var buffer = CharBuffer.wrap(array);

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

final var csq = "abcXYZ";

final var ret1 = buffer.append(csq, 0, 1);
System.out.println(ret1.position()); // 1
System.out.println(Arrays.toString(array)); // [a,  ,  ,  ,  ,  ]

final var ret2 = buffer.append(csq, 1, 3);
System.out.println(ret2.position()); // 3
System.out.println(Arrays.toString(array)); // [a, b, c,  ,  ,  ]

final var ret3 = buffer.append(csq, 3, 6);
System.out.println(ret3.position()); // 6
System.out.println(Arrays.toString(array)); // [a, b, c, X, Y, Z]

final char[] array ()

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

final var buffer = CharBuffer.allocate(5)
        .put('a')
        .put('b')
        .put('c');

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

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 char[] array = {'a', 'b', 'c', 'd', 'e'};
final var buffer = CharBuffer.wrap(array);

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

System.out.println(buffer); // abcde
if (buffer.hasArray()) {
    System.out.println(buffer.arrayOffset()); // 0
    System.out.println(buffer.get()); // a
    System.out.println(buffer.get()); // b
    System.out.println(buffer.get()); // c
    System.out.println(buffer.get()); // d
    System.out.println(buffer.get()); // e
}

System.out.println(sliced); // cde
if (sliced.hasArray()) {
    System.out.println(sliced.arrayOffset()); // 2
    System.out.println(sliced.get()); // c
    System.out.println(sliced.get()); // d
    System.out.println(sliced.get()); // e
}

abstract CharBuffer asReadOnlyBuffer ()

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

final var buffer = CharBuffer.allocate(3);
buffer.put('a');

final var readOnlyBuffer = buffer.asReadOnlyBuffer();

System.out.println(buffer.position()); // 1
System.out.println(readOnlyBuffer.position()); // 1

readOnlyBuffer.clear();
System.out.println(readOnlyBuffer.get()); // a
System.out.println(readOnlyBuffer.get() == '\0'); // true
System.out.println(readOnlyBuffer.get() == '\0'); // true

buffer.put('b');

readOnlyBuffer.clear();
System.out.println(readOnlyBuffer.get()); // a
System.out.println(readOnlyBuffer.get()); // b
System.out.println(readOnlyBuffer.get() == '\0'); // true

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

// Result
// ↓
//ReadOnlyBufferException!

final char charAt (int index)

Lee el carácter en el índice dado en relación con la posición actual. (Traducción automática)

final var buffer = CharBuffer.wrap("abcXYZ");

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

System.out.println(buffer.charAt(0)); // a
System.out.println(buffer.charAt(1)); // b
System.out.println(buffer.charAt(2)); // c
System.out.println(buffer.charAt(3)); // X
System.out.println(buffer.charAt(4)); // Y
System.out.println(buffer.charAt(5)); // Z

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

System.out.println(buffer.charAt(0)); // X
System.out.println(buffer.charAt(1)); // Y
System.out.println(buffer.charAt(2)); // Z

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

final CharBuffer clear ()

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

final char[] array = {'a', 'b', 'c', 'd', 'e'};
final var buffer = CharBuffer.wrap(array);

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

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

System.out.println(buffer.get()); // c
System.out.println(buffer.get()); // d
System.out.println(buffer.hasRemaining()); // false

final var ret = buffer.clear();
System.out.println(ret.capacity()); // 5
System.out.println(ret.limit()); // 5
System.out.println(ret.position()); // 0

System.out.println(buffer.get()); // a
System.out.println(buffer.get()); // b
System.out.println(buffer.get()); // c
System.out.println(buffer.get()); // d
System.out.println(buffer.get()); // e
System.out.println(buffer.hasRemaining()); // false

abstract CharBuffer compact ()

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

final char[] array = {'a', 'b', 'c', 'X', 'Y', 'Z'};
final var buffer = CharBuffer.wrap(array);

buffer.position(3);

final var ret = buffer.compact();
System.out.println(ret.position()); // 3

System.out.println(Arrays.toString(array)); // [X, Y, Z, X, Y, Z]
final char[] array = {'a', 'b', 'c', 'X', 'Y', 'Z'};
final var buffer = CharBuffer.wrap(array);

buffer.position(2);

final var ret = buffer.compact();
System.out.println(ret.position()); // 4

System.out.println(Arrays.toString(array)); // [c, X, Y, Z, Y, Z]
final char[] array = {'a', 'b', 'c', 'X', 'Y', 'Z'};
final var buffer = CharBuffer.wrap(array);

buffer.position(4);

final var ret = buffer.compact();
System.out.println(ret.position()); // 2

System.out.println(Arrays.toString(array)); // [Y, Z, c, X, Y, Z]

int compareTo (CharBuffer that)

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

final var buffer1 = CharBuffer.allocate(3)
        .put('a')
        .put('b')
        .put('c');

final var buffer2 = CharBuffer.allocate(3)
        .put('a')
        .put('b')
        .put('c');

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

System.out.println(buffer1.compareTo(buffer2)); // 0
final var buffer1 = CharBuffer.allocate(3)
        .put('b')
        .put('c')
        .put('d');

final var buffer2 = CharBuffer.allocate(3)
        .put('a')
        .put('x')
        .put('y');

final var buffer3 = CharBuffer.allocate(3)
        .put('d')
        .put('c')
        .put('b');

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

System.out.println(buffer1.compareTo(buffer2)); // 1
System.out.println(buffer1.compareTo(buffer3)); // -2

abstract CharBuffer duplicate ()

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

final char[] array = {'a', 'b', 'c', 'd', 'e'};
final var buffer = CharBuffer.wrap(array);

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

final var duplicated = buffer.duplicate();

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

System.out.println(duplicated.capacity()); // 5
System.out.println(duplicated.limit()); // 4
System.out.println(duplicated.position()); // 1

System.out.println(buffer.get()); // b
System.out.println(buffer.get()); // c
System.out.println(buffer.get()); // d

System.out.println(duplicated.get()); // b
System.out.println(duplicated.get()); // c
System.out.println(duplicated.get()); // d

boolean equals (Object ob)

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

final var buffer1 = CharBuffer.allocate(3)
        .put('a')
        .put('b')
        .put('c');

final var buffer2 = CharBuffer.allocate(3)
        .put('a')
        .put('b')
        .put('c');

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

System.out.println(buffer1.equals(buffer2)); // true
final var buffer1 = CharBuffer.allocate(3)
        .put('b')
        .put('c')
        .put('d');

final var buffer2 = CharBuffer.allocate(3)
        .put('a')
        .put('x')
        .put('y');

final var buffer3 = CharBuffer.allocate(3)
        .put('d')
        .put('c')
        .put('b');

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

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

final CharBuffer flip ()

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

final char[] array = {'a', 'b', 'c', 'd', 'e'};
final var buffer = CharBuffer.wrap(array);

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

System.out.println(buffer.get()); // a
System.out.println(buffer.get()); // b
System.out.println(buffer.get()); // c

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

final var ret = buffer.flip();
System.out.println(ret.limit()); // 3
System.out.println(ret.position()); // 0

System.out.println(buffer.get()); // a
System.out.println(buffer.get()); // b
System.out.println(buffer.get()); // c

abstract char get ()

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

final char[] array = {'a', 'b', 'c'};
final var buffer = CharBuffer.wrap(array);

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

System.out.println(buffer.get()); // a
System.out.println(buffer.get()); // b
System.out.println(buffer.get()); // c

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

CharBuffer get (char[] dst)

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

final char[] array = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
final var buffer = CharBuffer.wrap(array);

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

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

    final var ret = buffer.get(dst);
    System.out.println(ret.position()); // 3

    System.out.println(Arrays.toString(dst)); // [a, b, c]
}
{
    final var dst = new char[3];
    System.out.println(Arrays.toString(dst)); // [ ,  ,  ]

    final var ret = buffer.get(dst);
    System.out.println(ret.position()); // 6

    System.out.println(Arrays.toString(dst)); // [d, e, f]
}

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

// Result
// ↓
//BufferUnderflowException!

CharBuffer get (char[] dst, int offset, int length)

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

final char[] array = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
final var buffer = CharBuffer.wrap(array);

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

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

    final var ret = buffer.get(dst, 0, 3);
    System.out.println(ret.position()); // 3

    System.out.println(Arrays.toString(dst)); // [a, b, c,  ,  ]
}
{
    final var dst = new char[5];
    System.out.println(Arrays.toString(dst)); // [ ,  ,  ,  ,  ]

    final var ret = buffer.get(dst, 1, 3);
    System.out.println(ret.position()); // 6

    System.out.println(Arrays.toString(dst)); // [ , d, e, f,  ]
}
{
    final var dst = new char[5];
    System.out.println(Arrays.toString(dst)); // [ ,  ,  ,  ,  ]

    final var ret = buffer.get(dst, 3, 2);
    System.out.println(ret.position()); // 8

    System.out.println(Arrays.toString(dst)); // [ ,  ,  , g, h]
}

abstract char get (int index)

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

final char[] array = {'a', 'b', 'c'};
final var buffer = CharBuffer.wrap(array);

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

System.out.println(buffer.get(0)); // a
System.out.println(buffer.get(1)); // b
System.out.println(buffer.get(2)); // c

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

CharBuffer get (int index, char[] dst)

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

final char[] array = {'a', 'b', 'c', 'd', 'e'};
final var buffer = CharBuffer.wrap(array);

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

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

    final var ret = buffer.get(0, dst);
    System.out.println(ret.position()); // 0

    System.out.println(Arrays.toString(dst)); // [a, b, c]
}
{
    final var dst = new char[3];
    System.out.println(Arrays.toString(dst)); // [ ,  ,  ]

    final var ret = buffer.get(1, dst);
    System.out.println(ret.position()); // 0

    System.out.println(Arrays.toString(dst)); // [b, c, d]
}
{
    final var dst = new char[3];
    System.out.println(Arrays.toString(dst)); // [ ,  ,  ]

    final var ret = buffer.get(2, dst);
    System.out.println(ret.position()); // 0

    System.out.println(Arrays.toString(dst)); // [c, d, e]
}

CharBuffer get (int index, char[] dst, int offset, int length)

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

final char[] array = {'a', 'b', 'c', 'd', 'e', 'f'};
final var buffer = CharBuffer.wrap(array);

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

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

    final var ret = buffer.get(0, dst, 0, 3);
    System.out.println(ret.position()); // 0

    System.out.println(Arrays.toString(dst)); // [a, b, c,  ,  ]
}
{
    final var dst = new char[5];
    System.out.println(Arrays.toString(dst)); // [ ,  ,  ,  ,  ]

    final var ret = buffer.get(2, dst, 1, 3);
    System.out.println(ret.position()); // 0

    System.out.println(Arrays.toString(dst)); // [ , c, d, e,  ]
}
{
    final var dst = new char[5];
    System.out.println(Arrays.toString(dst)); // [ ,  ,  ,  ,  ]

    final var ret = buffer.get(4, dst, 3, 2);
    System.out.println(ret.position()); // 0

    System.out.println(Arrays.toString(dst)); // [ ,  ,  , e, f]
}

final boolean hasArray ()

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

final var buffer = CharBuffer.allocate(5)
        .put('a')
        .put('b')
        .put('c');

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

if (ret) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [a, b, c,  ,  ]
}
final var buffer = CharBuffer.allocate(5)
        .put('a')
        .put('b')
        .put('c')
        .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 = CharBuffer.allocate(0);
System.out.println(buffer.hashCode()); // 1
final var buffer = CharBuffer.allocate(3)
        .put('a')
        .put('b')
        .put('c')
        .clear();

System.out.println(buffer.hashCode()); // 128065
final var buffer = CharBuffer.allocate(3)
        .put('X')
        .put('Y')
        .put('Z')
        .clear();

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

abstract boolean isDirect ()

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

final var buffer = CharBuffer.allocate(5);
System.out.println(buffer.isDirect()); // false
final char[] array = {'a', 'b', 'c'};
final var buffer = CharBuffer.wrap(array);
System.out.println(buffer.isDirect()); // false
final var buffer = ByteBuffer.allocateDirect(12).asCharBuffer();
System.out.println(buffer.isDirect()); // true

final boolean isEmpty ()

Devuelve verdadero si este búfer de caracteres está vacío. (Traducción automática)

final char[] array = {'a', 'b', 'c', 'd'};
final var buffer = CharBuffer.wrap(array);

buffer.limit(3);

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

System.out.println(buffer.remaining()); // 3
System.out.println(buffer.length()); // 3
System.out.println(buffer.isEmpty()); // false

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

System.out.println(buffer.remaining()); // 2
System.out.println(buffer.length()); // 2
System.out.println(buffer.isEmpty()); // false

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

System.out.println(buffer.remaining()); // 1
System.out.println(buffer.length()); // 1
System.out.println(buffer.isEmpty()); // false

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

System.out.println(buffer.remaining()); // 0
System.out.println(buffer.length()); // 0
System.out.println(buffer.isEmpty()); // true

final int length ()

Devuelve la longitud de este búfer de caracteres. (Traducción automática)

final char[] array = {'a', 'b', 'c', 'd'};
final var buffer = CharBuffer.wrap(array);

buffer.limit(3);

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

System.out.println(buffer.remaining()); // 3
System.out.println(buffer.length()); // 3
System.out.println(buffer.isEmpty()); // false

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

System.out.println(buffer.remaining()); // 2
System.out.println(buffer.length()); // 2
System.out.println(buffer.isEmpty()); // false

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

System.out.println(buffer.remaining()); // 1
System.out.println(buffer.length()); // 1
System.out.println(buffer.isEmpty()); // false

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

System.out.println(buffer.remaining()); // 0
System.out.println(buffer.length()); // 0
System.out.println(buffer.isEmpty()); // true

final CharBuffer limit (int newLimit)

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

final char[] array = {'a', 'b', 'c', 'd', 'e'};
final var buffer = CharBuffer.wrap(array);

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

buffer.limit(3);

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

buffer.clear();

System.out.println(buffer.position()); // 0
System.out.println(buffer.limit()); // 5
final char[] array = {'a', 'b', 'c', 'd', 'e'};
final var buffer = CharBuffer.wrap(array);

final var ret1 = buffer.limit(3);
System.out.println(ret1.limit()); // 3

System.out.println(buffer.get()); // a
System.out.println(buffer.get()); // b
System.out.println(buffer.get()); // c

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

// Result
// ↓
//BufferUnderflowException!

final CharBuffer mark ()

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

final char[] array = {'a', 'b', 'c', 'd', 'e'};
final var buffer = CharBuffer.wrap(array);

System.out.println(buffer.get()); // a
System.out.println(buffer.get()); // b

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

final var ret1 = buffer.mark();
System.out.println(ret1.position()); // 2

System.out.println(buffer.get()); // c
System.out.println(buffer.get()); // d
System.out.println(buffer.get()); // e

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

final var ret2 = buffer.reset();
System.out.println(ret2.position()); // 2

System.out.println(buffer.get()); // c
System.out.println(buffer.get()); // d
System.out.println(buffer.get()); // e

int mismatch (CharBuffer 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 char[] array = {'a', 'b', 'c', 'd'};
final var buffer = CharBuffer.wrap(array);

{
    final char[] a = {'a'};
    final var that = CharBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // 1
}
{
    final char[] a = {'a', 'b'};
    final var that = CharBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // 2
}
{
    final char[] a = {'a', 'b', 'c'};
    final var that = CharBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // 3
}
{
    final char[] a = {'a', 'b', 'c', 'd'};
    final var that = CharBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // -1
}
{
    final char[] a = {'a', 'b', 'c', 'd', 'e'};
    final var that = CharBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // 4
}
{
    final char[] a = {'a', 'b', 'X'};
    final var that = CharBuffer.wrap(a); // 2

    System.out.println(buffer.mismatch(that));
}
{
    final char[] a = {'Y', 'Z'};
    final var that = CharBuffer.wrap(a);

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

abstract ByteOrder order ()

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

final var os = System.getProperty("os.name");
System.out.println(os); // Windows 10

final var buffer = CharBuffer.allocate(5);
System.out.println(buffer.order()); // LITTLE_ENDIAN

final CharBuffer position (int newPosition)

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

final char[] array = {'a', 'b', 'c', 'd', 'e'};
final var buffer = CharBuffer.wrap(array);

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

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

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

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

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

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

final var ret = buffer.position(3);
System.out.println(ret.position()); // 3

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

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

abstract CharBuffer put (char c)

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

final var buffer = CharBuffer.allocate(3);

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

final var ret1 = buffer.put('a');
System.out.println(ret1.position()); // 1

final var ret2 = buffer.put('b');
System.out.println(ret2.position()); // 2

final var ret3 = buffer.put('c');
System.out.println(ret3.position()); // 3

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

final CharBuffer put (char[] src)

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

final var buffer = CharBuffer.allocate(6);

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

{
    final char[] src = {'a'};

    final var ret = buffer.put(src);
    System.out.println(ret.position()); // 1
}
{
    final char[] src = {'b', 'c'};

    final var ret = buffer.put(src);
    System.out.println(ret.position()); // 3
}
{
    final char[] src = {'d', 'e', 'f'};

    final var ret = buffer.put(src);
    System.out.println(ret.position()); // 6
}

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [a, b, c, d, e, f]
}

CharBuffer put (char[] src, int offset, int length)

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

final var array = new char[6];
final var buffer = CharBuffer.wrap(array);

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

final char[] src = {'a', 'b', 'c', 'd', 'e', 'f'};

final var ret1 = buffer.put(src, 0, 1);
System.out.println(ret1.position()); // 1
System.out.println(Arrays.toString(array)); // [a,  ,  ,  ,  ,  ]

final var ret2 = buffer.put(src, 1, 2);
System.out.println(ret2.position()); // 3
System.out.println(Arrays.toString(array)); // [a, b, c,  ,  ,  ]

final var ret3 = buffer.put(src, 3, 3);
System.out.println(ret3.position()); // 6
System.out.println(Arrays.toString(array)); // [a, b, c, d, e, f]

abstract CharBuffer put (int index, char c)

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

final var buffer = CharBuffer.allocate(3);

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

final var ret1 = buffer.put(0, 'a');
System.out.println(ret1.position()); // 0

final var ret2 = buffer.put(1, 'b');
System.out.println(ret2.position()); // 0

final var ret3 = buffer.put(2, 'c');
System.out.println(ret3.position()); // 0

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

CharBuffer put (int index, char[] src)

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

final var buffer = CharBuffer.allocate(6);

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

{
    final char[] src = {'a'};

    final var ret = buffer.put(0, src);
    System.out.println(ret.position()); // 0
}
{
    final char[] src = {'b', 'c'};

    final var ret = buffer.put(1, src);
    System.out.println(ret.position()); // 0
}
{
    final char[] src = {'d', 'e', 'f'};

    final var ret = buffer.put(3, src);
    System.out.println(ret.position()); // 0
}

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [a, b, c, d, e, f]
}

CharBuffer put (int index, char[] src, int offset, int length)

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

final var array = new char[6];
final var buffer = CharBuffer.wrap(array);

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

final char[] src = {'a', 'b', 'c', 'd', 'e', 'f'};

final var ret1 = buffer.put(0, src, 0, 1);
System.out.println(ret1.position()); // 0
System.out.println(Arrays.toString(array)); // [a,  ,  ,  ,  ,  ]

final var ret2 = buffer.put(1, src, 1, 2);
System.out.println(ret2.position()); // 0
System.out.println(Arrays.toString(array)); // [a, b, c,  ,  ,  ]

final var ret3 = buffer.put(3, src, 3, 3);
System.out.println(ret3.position()); // 0
System.out.println(Arrays.toString(array)); // [a, b, c, d, e, f]

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

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

final var array = new char[6];
final var buffer = CharBuffer.wrap(array);

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

final char[] srcArray = {'a', 'b', 'c', 'd', 'e', 'f'};
final var src = CharBuffer.wrap(srcArray);

final var ret1 = buffer.put(0, src, 0, 1);
System.out.println(ret1.position()); // 0
System.out.println(Arrays.toString(array)); // [a,  ,  ,  ,  ,  ]

final var ret2 = buffer.put(1, src, 1, 2);
System.out.println(ret2.position()); // 0
System.out.println(Arrays.toString(array)); // [a, b, c,  ,  ,  ]

final var ret3 = buffer.put(3, src, 3, 3);
System.out.println(ret3.position()); // 0
System.out.println(Arrays.toString(array)); // [a, b, c, d, e, f]

final CharBuffer put (String src)

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

final var buffer = CharBuffer.allocate(6);

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

final var ret1 = buffer.put("a");
System.out.println(ret1.position()); // 1

final var ret2 = buffer.put("bc");
System.out.println(ret2.position()); // 3

final var ret3 = buffer.put("XYZ");
System.out.println(ret3.position()); // 6

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [a, b, c, X, Y, Z]
}

CharBuffer put (String src, int start, int end)

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

final var array = new char[6];
final var buffer = CharBuffer.wrap(array);

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

final var src = "abcXYZ";

final var ret1 = buffer.put(src, 0, 1);
System.out.println(ret1.position()); // 1
System.out.println(Arrays.toString(array)); // [a,  ,  ,  ,  ,  ]

final var ret2 = buffer.put(src, 1, 3);
System.out.println(ret2.position()); // 3
System.out.println(Arrays.toString(array)); // [a, b, c,  ,  ,  ]

final var ret3 = buffer.put(src, 3, 6);
System.out.println(ret3.position()); // 6
System.out.println(Arrays.toString(array)); // [a, b, c, X, Y, Z]

CharBuffer put (CharBuffer src)

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

final var buffer = CharBuffer.allocate(5);

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

{
    final char[] array = {'a', 'b'};
    final var src = CharBuffer.wrap(array);

    final var ret = buffer.put(src);
    System.out.println(ret.position()); // 2
}
{
    final char[] array = {'c', 'd', 'e'};
    final var src = CharBuffer.wrap(array);

    final var ret = buffer.put(src);
    System.out.println(ret.position()); // 5
}

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [a, b, c, d, e]
}

int read (CharBuffer target)

Intenta leer caracteres en el búfer de caracteres especificado. (Traducción automática)

final var buffer = CharBuffer.wrap("abcde");

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

{
    final var array = new char[2];
    final var target = CharBuffer.wrap(array);

    System.out.println(buffer.read(target)); // 2
    System.out.println(buffer.position()); // 2
    System.out.println(Arrays.toString(array)); // [a, b]
}
{
    final var array = new char[3];
    final var target = CharBuffer.wrap(array);

    System.out.println(buffer.read(target)); // 3
    System.out.println(buffer.position()); // 5
    System.out.println(Arrays.toString(array)); // [c, d, e]
}

final CharBuffer reset ()

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

final char[] array = {'a', 'b', 'c', 'd', 'e'};
final var buffer = CharBuffer.wrap(array);

System.out.println(buffer.get()); // a
System.out.println(buffer.get()); // b

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

final var ret1 = buffer.mark();
System.out.println(ret1.position()); // 2

System.out.println(buffer.get()); // c
System.out.println(buffer.get()); // d
System.out.println(buffer.get()); // e

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

final var ret2 = buffer.reset();
System.out.println(ret2.position()); // 2

System.out.println(buffer.get()); // c
System.out.println(buffer.get()); // d
System.out.println(buffer.get()); // e

final CharBuffer rewind ()

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

final char[] array = {'a', 'b', 'c', 'd', 'e'};
final var buffer = CharBuffer.wrap(array);

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

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

final var ret = buffer.rewind();
System.out.println(ret.limit()); // 4
System.out.println(ret.position()); // 0

abstract CharBuffer slice ()

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

final char[] array = {'a', 'b', 'c', 'd', 'e'};
final var buffer = CharBuffer.wrap(array);

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

final var sliced = buffer.slice();

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

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

System.out.println(buffer.get()); // b
System.out.println(buffer.get()); // c
System.out.println(buffer.get()); // d
System.out.println(buffer.hasRemaining()); // false

System.out.println(sliced.get()); // b
System.out.println(sliced.get()); // c
System.out.println(sliced.get()); // d
System.out.println(sliced.hasRemaining()); // false

abstract CharBuffer slice (int index, int length)

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

final char[] array = {'a', 'b', 'c', 'd', 'e'};
final var buffer = CharBuffer.wrap(array);

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

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

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

System.out.println(buffer.get()); // a
System.out.println(buffer.get()); // b
System.out.println(buffer.get()); // c
System.out.println(buffer.get()); // d
System.out.println(buffer.get()); // e
System.out.println(buffer.hasRemaining()); // false

System.out.println(sliced.get()); // c
System.out.println(sliced.get()); // d
System.out.println(sliced.get()); // e
System.out.println(sliced.hasRemaining()); // false

abstract CharBuffer subSequence (int start, int end)

Crea un nuevo búfer de caracteres que representa la subsecuencia especificada de este búfer, en relación con la posición actual. (Traducción automática)

final char[] array = {'a', 'b', 'c', 'd', 'e'};
final var buffer = CharBuffer.wrap(array);

{
    final var ret = buffer.subSequence(0, 2);
    System.out.println(ret.capacity()); // 5
    System.out.println(ret.limit()); // 2
    System.out.println(ret.position()); // 0

    System.out.println(ret.get()); // a
    System.out.println(ret.get()); // b
    System.out.println(ret.hasRemaining()); // false
}

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

{
    final var ret = buffer.subSequence(2, 5);
    System.out.println(ret.capacity()); // 5
    System.out.println(ret.limit()); // 5
    System.out.println(ret.position()); // 2

    System.out.println(ret.get()); // c
    System.out.println(ret.get()); // d
    System.out.println(ret.get()); // e
    System.out.println(ret.hasRemaining()); // false
}

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

String toString ()

Devuelve una cadena que contiene los caracteres en este búfer. (Traducción automática)

final var buffer = CharBuffer.wrap("abcXYZ");
System.out.println(buffer.position()); // 0

final var str1 = buffer.toString();
System.out.println(str1); // abcXYZ

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

final var str2 = buffer.toString();
System.out.println(str2); // XYZ

static CharBuffer wrap (char[] array)

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

final char[] array = {'a', 'b', 'c'};
final var buffer = CharBuffer.wrap(array);

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

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

    System.out.println(buffer.get()); // a
    System.out.println(buffer.get()); // b
    System.out.println(buffer.get()); // c
}

static CharBuffer wrap (char[] array, int offset, int length)

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

final char[] array = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
final var buffer = CharBuffer.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()); // c
    System.out.println(buffer.get()); // d
    System.out.println(buffer.get()); // e
    System.out.println(buffer.get()); // f
}

static CharBuffer wrap (CharSequence csq)

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

final var buffer = CharBuffer.wrap("abc");

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

System.out.println(buffer.get()); // a
System.out.println(buffer.get()); // b
System.out.println(buffer.get()); // c

static CharBuffer wrap (CharSequence csq, int start, int end)

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

final var buffer = CharBuffer.wrap("abcdefg", 2, 6);

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

System.out.println(buffer.get()); // c
System.out.println(buffer.get()); // d
System.out.println(buffer.get()); // e
System.out.println(buffer.get()); // f

Methods declared in Buffer

capacity, hasRemaining, isReadOnly, limit, position, remaining

Consulte el siguiente enlace.

Methods declared in CharSequence

chars, codePoints

Consulte el siguiente enlace.


Related posts

To top of page