Java : Buffer 示例
Java 中的 Buffer (Java SE 22 & JDK 22) 及其示例。
您将找到大多数 Buffer 方法的代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
特定原始类型数据的容器。 (机器翻译)
final var buffer = IntBuffer.allocate(5);
System.out.println(buffer instanceof Buffer); // true
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.limit()); // 5
System.out.println(buffer.position()); // 0
buffer.put(10);
System.out.println(buffer.position()); // 1
buffer.put(20);
System.out.println(buffer.position()); // 2
buffer.put(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 instanceof Buffer); // true
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
abstract Object array ()
返回支持此缓冲区的数组(可选操作)。 (机器翻译)
final Buffer buffer = IntBuffer.allocate(5)
.put(10)
.put(20)
.put(30);
if (buffer.hasArray()) {
if (buffer.array() instanceof int[] array) {
System.out.println(Arrays.toString(array)); // [10, 20, 30, 0, 0]
}
}
abstract int arrayOffset ()
返回此缓冲区的支持数组中缓冲区第一个元素的偏移量(可选操作)。 (机器翻译)
final byte[] bytes = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(bytes);
System.out.println(buffer instanceof Buffer); // true
final var sliced = buffer.slice(2, 3);
System.out.println(sliced instanceof Buffer); // true
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
}
final int capacity ()
返回此缓冲区的容量。 (机器翻译)
final Buffer buffer = ByteBuffer.allocate(100);
System.out.println(buffer.capacity()); // 100
final byte[] bytes = {10, 20, 30, 40, 50};
final Buffer buffer = ByteBuffer.wrap(bytes);
System.out.println(buffer.capacity()); // 5
Buffer clear ()
清除此缓冲区。 (机器翻译)
Please see also : rewind()
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
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 Buffer duplicate ()
创建一个共享此缓冲区内容的新缓冲区。 (机器翻译)
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
buffer.position(1);
buffer.limit(4);
final var duplicated = buffer.duplicate();
System.out.println(duplicated instanceof Buffer); // true
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
Buffer flip ()
翻转此缓冲区。 (机器翻译)
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
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 boolean hasArray ()
告知此缓冲区是否由可访问数组支持。 (机器翻译)
final var buffer = IntBuffer.allocate(5)
.put(10)
.put(20)
.put(30);
System.out.println(buffer instanceof Buffer); // true
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.allocateDirect(5);
System.out.println(buffer instanceof Buffer); // true
System.out.println(buffer.hasArray()); // false
try {
var _ = buffer.array();
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// Result
// ↓
//UnsupportedOperationException!
final boolean hasRemaining ()
判断当前位置和限制之间是否存在任何元素。 (机器翻译)
final byte[] array = {10, 20, 30, 40};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
buffer.limit(3);
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // true
System.out.println(buffer.remaining()); // 3
System.out.println(buffer.get()); // 10
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=1 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // true
System.out.println(buffer.remaining()); // 2
System.out.println(buffer.get()); // 20
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=2 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // true
System.out.println(buffer.remaining()); // 1
System.out.println(buffer.get()); // 30
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=3 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // false
System.out.println(buffer.remaining()); // 0
abstract boolean isDirect ()
告知该缓冲区是否是直接的。 (机器翻译)
final Buffer buffer = ByteBuffer.allocate(5);
System.out.println(buffer.isDirect()); // false
final Buffer buffer = ByteBuffer.allocateDirect(5);
System.out.println(buffer.isDirect()); // true
abstract boolean isReadOnly ()
告知该缓冲区是否是只读的。 (机器翻译)
final var buffer = IntBuffer.allocate(5);
System.out.println(buffer instanceof Buffer); // true
System.out.println(buffer.isReadOnly()); // false
buffer.put(10);
buffer.put(20);
buffer.put(30);
System.out.println(buffer); // java.nio.HeapIntBuffer[pos=3 lim=5 cap=5]
final var buffer = IntBuffer.allocate(5).asReadOnlyBuffer();
System.out.println(buffer instanceof Buffer); // true
System.out.println(buffer.isReadOnly()); // true
try {
buffer.put(10);
} catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException!");
}
// Result
// ↓
//ReadOnlyBufferException!
final int limit ()
返回此缓冲区的限制。 (机器翻译)
final byte[] array = {10, 20, 30, 40, 50};
final Buffer 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 instanceof Buffer); // true
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!
Buffer limit (int newLimit)
设置此缓冲区的限制。 (机器翻译)
final byte[] array = {10, 20, 30, 40, 50};
final Buffer 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 instanceof Buffer); // true
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!
Buffer mark ()
在此缓冲区的位置设置其标记。 (机器翻译)
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
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
final int position ()
返回此缓冲区的位置。 (机器翻译)
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
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
Buffer position (int newPosition)
设置此缓冲区的位置。 (机器翻译)
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
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
final int remaining ()
返回当前位置和限制之间的元素数。 (机器翻译)
final byte[] array = {10, 20, 30, 40};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
buffer.limit(3);
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // true
System.out.println(buffer.remaining()); // 3
System.out.println(buffer.get()); // 10
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=1 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // true
System.out.println(buffer.remaining()); // 2
System.out.println(buffer.get()); // 20
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=2 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // true
System.out.println(buffer.remaining()); // 1
System.out.println(buffer.get()); // 30
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=3 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // false
System.out.println(buffer.remaining()); // 0
Buffer reset ()
将此缓冲区的位置重置为先前标记的位置。 (机器翻译)
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
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
Buffer rewind ()
倒回此缓冲区。 (机器翻译)
Please see also : clear()
final int[] array = {10, 20, 30, 40, 50};
final Buffer buffer = IntBuffer.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.HeapIntBuffer[pos=0 lim=4 cap=5]
System.out.println(buffer.position()); // 0
System.out.println(buffer.limit()); // 4
abstract Buffer slice ()
创建一个新的缓冲区,其内容是此缓冲区内容的共享子序列。 (机器翻译)
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
buffer.position(1);
buffer.limit(4);
final var sliced = buffer.slice();
System.out.println(sliced instanceof Buffer); // true
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 Buffer slice (int index, int length)
创建一个新的缓冲区,其内容是此缓冲区内容的共享子序列。 (机器翻译)
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
final var sliced = buffer.slice(2, 3);
System.out.println(sliced instanceof Buffer); // true
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