Java : ByteBuffer 示例
Java 中的 ByteBuffer (Java SE 22 & JDK 22) 及其示例。
您将找到大多数 ByteBuffer 方法的代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
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 ()
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 ()
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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
}