広告

Java : ByteBuffer (バイトバッファ) - API使用例

ByteBuffer (Java SE 22 & JDK 22) の使い方まとめです。
ほとんどのメソッドにサンプルコードがあります。
APIドキュメントのおともにどうぞ。


概要

byteバッファです。

クラス構成

ByteBuffer クラスは、プリミティブ型byte を扱う Buffer の実装です。

配列と違い、

  • 容量(capacity) : バッファのサイズ。配列の length に相当
  • リミット(limit) : どの位置まで読み書き可能にするか
  • 位置(position) : 次に読み書きする位置

という3つのプロパティを持ちます。

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

メソッド

final ByteBuffer alignedSlice (int unitSize)

コンテンツが、このバッファのコンテンツの共有および整列されたサブシーケンスである新しいバイト・バッファを作成します。

関連 : 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);

// 結果
// ↓
//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);

// 結果
// ↓
//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);

// 結果
// ↓
//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)

指定されたインデックスのバイトを指すメモリー・アドレスを返します。指定された単位サイズを変更します。

関連 : 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);
}

// 結果
// ↓
//--- 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)

新しいbyteバッファを割り当てます。

関連 : 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)

新しいダイレクトbyteバッファを割り当てます。

関連 : 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 ()

このバッファを補助するbyte配列を返します (オプションの操作)。

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

charバッファとしてこのbyteバッファのビューを作成します。

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

doubleバッファとしてこのbyteバッファのビューを作成します。

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

floatバッファとしてこのbyteバッファのビューを作成します。

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バッファとしてこのbyteバッファのビューを作成します。

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

longバッファとしてこのbyteバッファのビューを作成します。

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

このバッファの内容を共有する新しい読込み専用byteバッファを作成します。

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!");
}

// 結果
// ↓
//ReadOnlyBufferException!

abstract ShortBuffer asShortBuffer ()

shortバッファとしてこのbyteバッファのビューを作成します。

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

このバッファの内容を共有する新しいbyteバッファを作成します。

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

相対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)

相対一括getメソッドです。

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!");
}

// 結果
// ↓
//BufferUnderflowException!

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

相対一括getメソッドです。

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)

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

絶対一括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

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

絶対一括getメソッド。

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

char値を読み取る相対getメソッドです。

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)

char値を読み取る絶対getメソッドです。

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

double値を読み取る相対getメソッドです。

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)

double値を読み取る絶対getメソッドです。

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

float値を読み取る相対getメソッドです。

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)

float値を読み取る絶対getメソッドです。

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値を読み取る相対getメソッドです。

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値を読み取る絶対getメソッドです。

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

long値を読み取る相対getメソッドです。

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)

long値を読み取る絶対getメソッドです。

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

short値を読み取る相対getメソッドです。

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)

short値を読み取る絶対getメソッドです。

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

このバッファがアクセス可能なbyte配列に連動するかどうかを判断します。

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!");
}

// 結果
// ↓
//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 ()

このbyteバッファがダイレクト・バッファであるかどうかを判断します。

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!");
}

// 結果
// ↓
//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 ()

このバッファのbyte順序を取得します。

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)

このバッファのbyte順序を変更します。

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)

相対putメソッドです (オプションの操作)。

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)

相対一括putメソッドです (オプションの操作)。

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)

相対一括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 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)

(optional operation)の絶対一括putメソッド。

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)

(optional operation)の絶対一括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 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)

(optional operation)の絶対一括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 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)

相対一括putメソッドです (オプションの操作)。

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)

double値を書き込む相対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)

double値を書き込む絶対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)

float値を書き込む相対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)

float値を書き込む絶対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)

long値を書き込む絶対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(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)

long値を書き込む相対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)

short値を書き込む絶対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)

short値を書き込む相対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 ()

このバッファの共有のサブシーケンスを内容とする新しいbyteバッファを作成します。

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)

このバッファの共有のサブシーケンスを内容とする新しいbyteバッファを作成します。

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)

byte配列をバッファにラップします。

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)

byte配列をバッファにラップします。

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
}

Bufferで宣言されたメソッド

capacity, hasRemaining, isReadOnly, limit, position, remaining

Java API 使用例 : Buffer」をご参照ください。


関連記事

ページの先頭へ