Java : CharBuffer 示例

Java 中的 CharBuffer (Java SE 22 & JDK 22) 及其示例。
您将找到大多数 CharBuffer 方法的代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

一个字符缓冲区。 (机器翻译)

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)

分配一个新的字符缓冲区。 (机器翻译)

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

如果此字符缓冲区为空,则返回 true。 (机器翻译)

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)

绝对put方法(可选操作)。 (机器翻译)

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

请参阅下面的链接。

Methods declared in CharSequence

chars, codePoints

请参阅下面的链接。


相关文章

To top of page