Java : ByteBuffer 示例

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

注解 :

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

简介

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

Class diagram

final var buffer = ByteBuffer.allocate(5);

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

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

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

buffer.put((byte) 30);
System.out.println(buffer.position()); // 3

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

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

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

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

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

Methods

final ByteBuffer alignedSlice (int unitSize)

创建一个新的字节缓冲区,其内容是此缓冲区内容的共享和对齐子序列。 (机器翻译)

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

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

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

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

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

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

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

test(2);

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

test(4);

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

final int alignmentOffset (int index, int unitSize)

返回指向给定索引处的字节的内存地址,对给定的单位大小取模。 (机器翻译)

Please see also : alignedSlice(int unitSize)

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

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

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

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

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

static ByteBuffer allocate (int capacity)

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

Please see also : allocateDirect(int capacity)

final var buffer = ByteBuffer.allocate(5);

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

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

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

static ByteBuffer allocateDirect (int capacity)

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

Please see also : allocate(int capacity)

final var buffer = ByteBuffer.allocateDirect(5);

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

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

final byte[] array ()

返回支持此缓冲区的字节数组(可选操作)。 (机器翻译)

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

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

final int arrayOffset ()

返回此缓冲区的支持数组中缓冲区第一个元素的偏移量(可选操作)。 (机器翻译)

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

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

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
if (buffer.hasArray()) {
    System.out.println(buffer.arrayOffset()); // 0
    System.out.println(buffer.get()); // 10
    System.out.println(buffer.get()); // 20
    System.out.println(buffer.get()); // 30
    System.out.println(buffer.get()); // 40
    System.out.println(buffer.get()); // 50
}

System.out.println(sliced); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=3]
if (sliced.hasArray()) {
    System.out.println(sliced.arrayOffset()); // 2
    System.out.println(sliced.get()); // 30
    System.out.println(sliced.get()); // 40
    System.out.println(sliced.get()); // 50
}

abstract CharBuffer asCharBuffer ()

将此字节缓冲区的视图创建为字符缓冲区。 (机器翻译)

final var array = new byte[6];

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

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

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

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

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

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

abstract DoubleBuffer asDoubleBuffer ()

将此字节缓冲区的视图创建为双缓冲区。 (机器翻译)

final var array = new byte[16];

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

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

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

doubleBuffer.put(123.45);

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

doubleBuffer.put(-0.789);

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

abstract FloatBuffer asFloatBuffer ()

将此字节缓冲区的视图创建为浮点缓冲区。 (机器翻译)

final var array = new byte[8];

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

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

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

floatBuffer.put(123.45f);

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

floatBuffer.put(-0.789f);

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

abstract IntBuffer asIntBuffer ()

将此字节缓冲区的视图创建为 int 缓冲区。 (机器翻译)

final var array = new byte[8];

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

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

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

intBuffer.put(123456789);

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

intBuffer.put(-1);

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

abstract LongBuffer asLongBuffer ()

将此字节缓冲区的视图创建为长缓冲区。 (机器翻译)

final var array = new byte[16];

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

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

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

longBuffer.put(1234567890123456789L);

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

longBuffer.put(-1L);

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

abstract ByteBuffer asReadOnlyBuffer ()

创建一个新的、只读的字节缓冲区,并共享此缓冲区的内容。 (机器翻译)

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

final var readOnlyBuffer = buffer.asReadOnlyBuffer();

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

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

buffer.put((byte) 20);

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

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

// Result
// ↓
//ReadOnlyBufferException!

abstract ShortBuffer asShortBuffer ()

将此字节缓冲区的视图创建为短缓冲区。 (机器翻译)

final var array = new byte[4];

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

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

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

shortBuffer.put((short) 1234);

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

shortBuffer.put((short) -1);

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

ByteBuffer clear ()

清除此缓冲区。 (机器翻译)

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

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

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

System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.hasRemaining()); // false

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

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
System.out.println(buffer.hasRemaining()); // false

abstract ByteBuffer compact ()

压缩此缓冲区(可选操作)。 (机器翻译)

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

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

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

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

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

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

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

int compareTo (ByteBuffer that)

将此缓冲区与另一个缓冲区进行比较。 (机器翻译)

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

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

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

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

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

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

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

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

abstract ByteBuffer duplicate ()

创建一个共享此缓冲区内容的新字节缓冲区。 (机器翻译)

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

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

final var duplicated = buffer.duplicate();

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=1 lim=4 cap=5]
System.out.println(duplicated); // java.nio.HeapByteBuffer[pos=1 lim=4 cap=5]

System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40

System.out.println(duplicated.get()); // 20
System.out.println(duplicated.get()); // 30
System.out.println(duplicated.get()); // 40

boolean equals (Object ob)

判断该缓冲区是否等于另一个对象。 (机器翻译)

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

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

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

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

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

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

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

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

ByteBuffer flip ()

翻转此缓冲区。 (机器翻译)

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

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=3 lim=5 cap=5]
System.out.println(buffer.flip()); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=5]

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30

abstract byte get ()

相对获取方法。 (机器翻译)

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

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

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50

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

ByteBuffer get (byte[] dst)

相对批量获取方法。 (机器翻译)

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

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

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

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

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

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

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

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

// Result
// ↓
//BufferUnderflowException!

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

相对批量获取方法。 (机器翻译)

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

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

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

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

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

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

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

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

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

abstract byte get (int index)

绝对获取方法。 (机器翻译)

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

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

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

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

ByteBuffer get (int index, byte[] dst)

绝对批量获取方法。 (机器翻译)

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

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

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

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

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

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

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

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

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

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

绝对批量获取方法。 (机器翻译)

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

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

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

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

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

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

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

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

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

abstract char getChar ()

用于读取字符值的相对获取方法。 (机器翻译)

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

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

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

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

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

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

abstract char getChar (int index)

用于读取字符值的绝对获取方法。 (机器翻译)

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

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

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

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

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

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

abstract double getDouble ()

用于读取双精度值的相对获取方法。 (机器翻译)

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

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

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

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

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

abstract double getDouble (int index)

用于读取双精度值的绝对获取方法。 (机器翻译)

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

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

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

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

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

abstract float getFloat ()

用于读取浮点值的相对获取方法。 (机器翻译)

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

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

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

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

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

abstract float getFloat (int index)

读取浮点值的绝对获取方法。 (机器翻译)

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

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

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

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

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

abstract int getInt ()

用于读取 int 值的相对获取方法。 (机器翻译)

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

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

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

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

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

abstract int getInt (int index)

用于读取 int 值的绝对获取方法。 (机器翻译)

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

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

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

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

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

abstract long getLong ()

用于读取长值的相对获取方法。 (机器翻译)

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

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

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

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

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

abstract long getLong (int index)

用于读取长值的绝对获取方法。 (机器翻译)

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

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

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

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

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

abstract short getShort ()

用于读取短值的相对获取方法。 (机器翻译)

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

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

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

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

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

abstract short getShort (int index)

用于读取短值的绝对获取方法。 (机器翻译)

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

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

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

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

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

final boolean hasArray ()

告知此缓冲区是否由可访问的字节数组支持。 (机器翻译)

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

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

if (ret) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [10, 20, 30, 0, 0]
}
final var buffer = ByteBuffer.allocate(5)
        .put((byte) 10)
        .put((byte) 20)
        .put((byte) 30)
        .asReadOnlyBuffer();

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

// Result
// ↓
//UnsupportedOperationException!

int hashCode ()

返回此缓冲区的当前哈希码。 (机器翻译)

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

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

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

abstract boolean isDirect ()

告知该字节缓冲区是否是直接的。 (机器翻译)

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

ByteBuffer limit (int newLimit)

设置此缓冲区的限制。 (机器翻译)

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

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

System.out.println(buffer.limit(3)); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=5]
System.out.println(buffer.limit()); // 3

System.out.println(buffer.clear()); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.limit()); // 5
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);

System.out.println(buffer.limit(3)); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=5]

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30

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

// Result
// ↓
//BufferUnderflowException!

ByteBuffer mark ()

在此缓冲区的位置设置其标记。 (机器翻译)

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

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20

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

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

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

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

int mismatch (ByteBuffer that)

查找并返回此缓冲区与给定缓冲区之间第一个不匹配的相对索引。 (机器翻译)

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

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

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

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

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

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

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

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

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

final ByteOrder order ()

检索此缓冲区的字节顺序。 (机器翻译)

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

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

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

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

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

final ByteBuffer order (ByteOrder bo)

修改此缓冲区的字节顺序。 (机器翻译)

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

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

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

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

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

ByteBuffer position (int newPosition)

设置此缓冲区的位置。 (机器翻译)

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

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

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

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

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

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

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

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

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

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

abstract ByteBuffer put (byte b)

相对放置方法(可选操作)。 (机器翻译)

final var buffer = ByteBuffer.allocate(5);

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

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

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

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

final ByteBuffer put (byte[] src)

相对批量放入方法(可选操作)。 (机器翻译)

final var buffer = ByteBuffer.allocate(6);

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

{
    final byte[] src = {10};

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

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

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

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

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

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

相对批量放入方法(可选操作)。 (机器翻译)

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

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

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

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

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

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

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

abstract ByteBuffer put (int index, byte b)

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

final var buffer = ByteBuffer.allocate(5);

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

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

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

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

ByteBuffer put (int index, byte[] src)

绝对批量放入方法(可选操作)。 (机器翻译)

final var buffer = ByteBuffer.allocate(6);

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

{
    final byte[] src = {10};

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

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

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

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

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

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

绝对批量放入方法(可选操作)。 (机器翻译)

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

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

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

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

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

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

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

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

绝对批量放入方法(可选操作)。 (机器翻译)

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

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

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

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

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

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

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

ByteBuffer put (ByteBuffer src)

相对批量放入方法(可选操作)。 (机器翻译)

final var buffer = ByteBuffer.allocate(5);

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

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

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

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

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

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

abstract ByteBuffer putChar (char value)

用于写入 char 值的相对 put 方法(可选操作)。 (机器翻译)

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

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

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

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

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

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

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

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

abstract ByteBuffer putChar (int index, char value)

用于写入 char 值的绝对 put 方法(可选操作)。 (机器翻译)

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

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

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

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

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

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

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

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

abstract ByteBuffer putDouble (double value)

用于写入双精度值的相对 put 方法(可选操作)。 (机器翻译)

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

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

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

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

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

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

abstract ByteBuffer putDouble (int index, double value)

用于写入双精度值的绝对 put 方法(可选操作)。 (机器翻译)

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

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

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

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

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

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

abstract ByteBuffer putFloat (float value)

用于写入浮点值的相对 put 方法(可选操作)。 (机器翻译)

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

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

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

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

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

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

abstract ByteBuffer putFloat (int index, float value)

用于写入浮点值的绝对 put 方法(可选操作)。 (机器翻译)

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

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

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

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

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

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

abstract ByteBuffer putInt (int value)

用于写入 int 值的相对 put 方法(可选操作)。 (机器翻译)

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

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

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

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

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

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

abstract ByteBuffer putInt (int index, int value)

用于写入 int 值的绝对 put 方法(可选操作)。 (机器翻译)

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

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

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

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

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

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

abstract ByteBuffer putLong (int index, long value)

用于写入长值的绝对放入方法(可选操作)。 (机器翻译)

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

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

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

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

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

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

abstract ByteBuffer putLong (long value)

用于写入长值的相对 put 方法(可选操作)。 (机器翻译)

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

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

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

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

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

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

abstract ByteBuffer putShort (int index, short value)

用于写入短值的绝对 put 方法(可选操作)。 (机器翻译)

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

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

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

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

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

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

abstract ByteBuffer putShort (short value)

用于写入短值的相对 put 方法(可选操作)。 (机器翻译)

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

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

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

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

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

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

ByteBuffer reset ()

将此缓冲区的位置重置为先前标记的位置。 (机器翻译)

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

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20

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

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

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

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

ByteBuffer rewind ()

倒回此缓冲区。 (机器翻译)

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

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

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

System.out.println(buffer.rewind()); // java.nio.HeapByteBuffer[pos=0 lim=4 cap=5]

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

abstract ByteBuffer slice ()

创建一个新的字节缓冲区,其内容是此缓冲区内容的共享子序列。 (机器翻译)

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

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

final var sliced = buffer.slice();

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=1 lim=4 cap=5]
System.out.println(sliced); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=3]

System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.hasRemaining()); // false

System.out.println(sliced.get()); // 20
System.out.println(sliced.get()); // 30
System.out.println(sliced.get()); // 40
System.out.println(sliced.hasRemaining()); // false

abstract ByteBuffer slice (int index, int length)

创建一个新的字节缓冲区,其内容是此缓冲区内容的共享子序列。 (机器翻译)

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

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

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
System.out.println(sliced); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=3]

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
System.out.println(buffer.hasRemaining()); // false

System.out.println(sliced.get()); // 30
System.out.println(sliced.get()); // 40
System.out.println(sliced.get()); // 50
System.out.println(sliced.hasRemaining()); // false

String toString ()

返回概括此缓冲区状态的字符串。 (机器翻译)

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

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

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

buffer.clear();

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

static ByteBuffer wrap (byte[] array)

将字节数组包装到缓冲区中。 (机器翻译)

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

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

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

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

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

将字节数组包装到缓冲区中。 (机器翻译)

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

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

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

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

Methods declared in Buffer

capacity, hasRemaining, isReadOnly, limit, position, remaining

请参阅下面的链接。


相关文章

To top of page