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 :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
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)
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)
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)
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)
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 ()
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 ()
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 ()
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)
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 ()
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 ()
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)
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 ()
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)
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 ()
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 ()
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)
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)
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)
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)
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)
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 ()
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 ()
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 ()
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 ()
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 ()
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)
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 ()
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)
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 ()
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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 ()
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 ()
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 ()
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)
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)
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 ()
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)
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)
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)
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)
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
Related posts
- Ejemplos de API