Java : CharBuffer with Examples

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


Summary

A char buffer.

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)

Allocates a new char buffer.

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)

Appends the specified char to this buffer (optional operation).

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)

Appends the specified character sequence to this buffer (optional operation).

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)

Appends a subsequence of the specified character sequence to this buffer (optional operation).

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

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

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

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

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

Creates a new, read-only char buffer that shares this buffer's content.

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)

Reads the character at the given index relative to the current position.

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

Clears this buffer.

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

Compacts this buffer (optional operation).

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)

Compares this buffer to another.

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

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

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)

Tells whether or not this buffer is equal to another object.

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

Flips this buffer.

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

Relative get method.

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)

Relative bulk get method.

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)

Relative bulk get method.

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)

Absolute get method.

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)

Absolute bulk get method.

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)

Absolute bulk get method.

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

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

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

Returns the current hash code of this buffer.

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

Tells whether or not this char buffer is direct.

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

Returns true if this character buffer is empty.

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

Returns the length of this character buffer.

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)

Sets this buffer's limit.

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

Sets this buffer's mark at its position.

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)

Finds and returns the relative index of the first mismatch between this buffer and a given buffer.

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

Retrieves this buffer's byte 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)

Sets this buffer's position.

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)

Relative put method (optional operation).

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)

Relative bulk put method (optional operation).

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)

Relative bulk put method (optional operation).

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)

Absolute put method (optional operation).

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)

Absolute bulk put method (optional operation).

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)

Absolute bulk put method (optional operation).

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)

Absolute bulk put method (optional operation).

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)

Relative bulk put method (optional operation).

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)

Relative bulk put method (optional operation).

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)

Relative bulk put method (optional operation).

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)

Attempts to read characters into the specified character buffer.

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

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

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

Rewinds this buffer.

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

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

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)

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

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)

Creates a new character buffer that represents the specified subsequence of this buffer, relative to the current position.

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

Returns a string containing the characters in this buffer.

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)

Wraps a char array into a buffer.

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)

Wraps a char array into a buffer.

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)

Wraps a character sequence into a buffer.

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)

Wraps a character sequence into a buffer.

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

Please see the link below.

Methods declared in CharSequence

chars, codePoints

Please see the link below.


Related posts

To top of page