Java : Buffer - API使用例
Buffer (Java SE 22 & JDK 22) の使い方まとめです。
ほとんどのメソッドにサンプルコードがあります。
APIドキュメントのおともにどうぞ。
概要
Buffer クラスは、byte や int といった プリミティブ型 のバッファです。
具体的な実装として、byte 型を扱う ByteBuffer や、int 型を扱う IntBuffer などがあります。
配列と違い、
- 容量(capacity) : バッファのサイズ。配列の length に相当
- リミット(limit) : どの位置まで読み書き可能にするか
- 位置(position) : 次に読み書きする位置
という3つのプロパティを持ちます。
final var buffer = IntBuffer.allocate(5);
System.out.println(buffer instanceof Buffer); // true
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.limit()); // 5
System.out.println(buffer.position()); // 0
buffer.put(10);
System.out.println(buffer.position()); // 1
buffer.put(20);
System.out.println(buffer.position()); // 2
buffer.put(30);
System.out.println(buffer.position()); // 3
if (buffer.hasArray()) {
final var ret = buffer.array();
System.out.println(Arrays.toString(ret)); // [10, 20, 30, 0, 0]
}
final byte[] array = {10, 20, 30};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
System.out.println(buffer.capacity()); // 3
System.out.println(buffer.limit()); // 3
System.out.println(buffer.position()); // 0
System.out.println(buffer.get()); // 10
System.out.println(buffer.position()); // 1
System.out.println(buffer.get()); // 20
System.out.println(buffer.position()); // 2
System.out.println(buffer.get()); // 30
System.out.println(buffer.position()); // 3
メソッド
abstract Object array ()
final Buffer buffer = IntBuffer.allocate(5)
.put(10)
.put(20)
.put(30);
if (buffer.hasArray()) {
if (buffer.array() instanceof int[] array) {
System.out.println(Arrays.toString(array)); // [10, 20, 30, 0, 0]
}
}
abstract int arrayOffset ()
final byte[] bytes = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(bytes);
System.out.println(buffer instanceof Buffer); // true
final var sliced = buffer.slice(2, 3);
System.out.println(sliced instanceof Buffer); // true
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
if (buffer.hasArray()) {
System.out.println(buffer.arrayOffset()); // 0
System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
}
System.out.println(sliced); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=3]
if (sliced.hasArray()) {
System.out.println(sliced.arrayOffset()); // 2
System.out.println(sliced.get()); // 30
System.out.println(sliced.get()); // 40
System.out.println(sliced.get()); // 50
}
final int capacity ()
final Buffer buffer = ByteBuffer.allocate(100);
System.out.println(buffer.capacity()); // 100
final byte[] bytes = {10, 20, 30, 40, 50};
final Buffer buffer = ByteBuffer.wrap(bytes);
System.out.println(buffer.capacity()); // 5
Buffer clear ()
関連 : rewind()
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
buffer.position(2);
buffer.limit(4);
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=2 lim=4 cap=5]
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.hasRemaining()); // false
System.out.println(buffer.clear()); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
System.out.println(buffer.hasRemaining()); // false
abstract Buffer duplicate ()
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
buffer.position(1);
buffer.limit(4);
final var duplicated = buffer.duplicate();
System.out.println(duplicated instanceof Buffer); // true
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=1 lim=4 cap=5]
System.out.println(duplicated); // java.nio.HeapByteBuffer[pos=1 lim=4 cap=5]
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(duplicated.get()); // 20
System.out.println(duplicated.get()); // 30
System.out.println(duplicated.get()); // 40
Buffer flip ()
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=3 lim=5 cap=5]
System.out.println(buffer.flip()); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=5]
System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
abstract boolean hasArray ()
final var buffer = IntBuffer.allocate(5)
.put(10)
.put(20)
.put(30);
System.out.println(buffer instanceof Buffer); // true
final var ret = buffer.hasArray();
System.out.println(ret); // true
if (ret) {
final var array = buffer.array();
System.out.println(Arrays.toString(array)); // [10, 20, 30, 0, 0]
}
final var buffer = ByteBuffer.allocateDirect(5);
System.out.println(buffer instanceof Buffer); // true
System.out.println(buffer.hasArray()); // false
try {
var _ = buffer.array();
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// 結果
// ↓
//UnsupportedOperationException!
final boolean hasRemaining ()
final byte[] array = {10, 20, 30, 40};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
buffer.limit(3);
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // true
System.out.println(buffer.remaining()); // 3
System.out.println(buffer.get()); // 10
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=1 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // true
System.out.println(buffer.remaining()); // 2
System.out.println(buffer.get()); // 20
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=2 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // true
System.out.println(buffer.remaining()); // 1
System.out.println(buffer.get()); // 30
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=3 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // false
System.out.println(buffer.remaining()); // 0
abstract boolean isDirect ()
final Buffer buffer = ByteBuffer.allocate(5);
System.out.println(buffer.isDirect()); // false
final Buffer buffer = ByteBuffer.allocateDirect(5);
System.out.println(buffer.isDirect()); // true
abstract boolean isReadOnly ()
final var buffer = IntBuffer.allocate(5);
System.out.println(buffer instanceof Buffer); // true
System.out.println(buffer.isReadOnly()); // false
buffer.put(10);
buffer.put(20);
buffer.put(30);
System.out.println(buffer); // java.nio.HeapIntBuffer[pos=3 lim=5 cap=5]
final var buffer = IntBuffer.allocate(5).asReadOnlyBuffer();
System.out.println(buffer instanceof Buffer); // true
System.out.println(buffer.isReadOnly()); // true
try {
buffer.put(10);
} catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException!");
}
// 結果
// ↓
//ReadOnlyBufferException!
final int limit ()
final byte[] array = {10, 20, 30, 40, 50};
final Buffer buffer = ByteBuffer.wrap(array);
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.limit()); // 5
System.out.println(buffer.limit(3)); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=5]
System.out.println(buffer.limit()); // 3
System.out.println(buffer.clear()); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.limit()); // 5
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
System.out.println(buffer.limit(3)); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=5]
System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
try {
var _ = buffer.get();
} catch (BufferUnderflowException e) {
System.out.println("BufferUnderflowException!");
}
// 結果
// ↓
//BufferUnderflowException!
Buffer limit (int newLimit)
final byte[] array = {10, 20, 30, 40, 50};
final Buffer buffer = ByteBuffer.wrap(array);
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.limit()); // 5
System.out.println(buffer.limit(3)); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=5]
System.out.println(buffer.limit()); // 3
System.out.println(buffer.clear()); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.limit()); // 5
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
System.out.println(buffer.limit(3)); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=5]
System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
try {
var _ = buffer.get();
} catch (BufferUnderflowException e) {
System.out.println("BufferUnderflowException!");
}
// 結果
// ↓
//BufferUnderflowException!
Buffer mark ()
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.position()); // 2
System.out.println(buffer.mark()); // java.nio.HeapByteBuffer[pos=2 lim=5 cap=5]
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
System.out.println(buffer.position()); // 5
System.out.println(buffer.reset()); // java.nio.HeapByteBuffer[pos=2 lim=5 cap=5]
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
final int position ()
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
System.out.println(buffer.position()); // 0
System.out.println(buffer.get()); // 10
System.out.println(buffer.position()); // 1
System.out.println(buffer.get()); // 20
System.out.println(buffer.position()); // 2
System.out.println(buffer.get()); // 30
System.out.println(buffer.position()); // 3
System.out.println(buffer.get()); // 40
System.out.println(buffer.position()); // 4
System.out.println(buffer.get()); // 50
System.out.println(buffer.position()); // 5
System.out.println(buffer.position(3)); // java.nio.HeapByteBuffer[pos=3 lim=5 cap=5]
System.out.println(buffer.get()); // 40
System.out.println(buffer.position()); // 4
System.out.println(buffer.get()); // 50
System.out.println(buffer.position()); // 5
Buffer position (int newPosition)
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
System.out.println(buffer.position()); // 0
System.out.println(buffer.get()); // 10
System.out.println(buffer.position()); // 1
System.out.println(buffer.get()); // 20
System.out.println(buffer.position()); // 2
System.out.println(buffer.get()); // 30
System.out.println(buffer.position()); // 3
System.out.println(buffer.get()); // 40
System.out.println(buffer.position()); // 4
System.out.println(buffer.get()); // 50
System.out.println(buffer.position()); // 5
System.out.println(buffer.position(3)); // java.nio.HeapByteBuffer[pos=3 lim=5 cap=5]
System.out.println(buffer.get()); // 40
System.out.println(buffer.position()); // 4
System.out.println(buffer.get()); // 50
System.out.println(buffer.position()); // 5
final int remaining ()
final byte[] array = {10, 20, 30, 40};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
buffer.limit(3);
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // true
System.out.println(buffer.remaining()); // 3
System.out.println(buffer.get()); // 10
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=1 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // true
System.out.println(buffer.remaining()); // 2
System.out.println(buffer.get()); // 20
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=2 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // true
System.out.println(buffer.remaining()); // 1
System.out.println(buffer.get()); // 30
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=3 lim=3 cap=4]
System.out.println(buffer.hasRemaining()); // false
System.out.println(buffer.remaining()); // 0
Buffer reset ()
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.position()); // 2
System.out.println(buffer.mark()); // java.nio.HeapByteBuffer[pos=2 lim=5 cap=5]
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
System.out.println(buffer.position()); // 5
System.out.println(buffer.reset()); // java.nio.HeapByteBuffer[pos=2 lim=5 cap=5]
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
Buffer rewind ()
関連 : clear()
final int[] array = {10, 20, 30, 40, 50};
final Buffer buffer = IntBuffer.wrap(array);
buffer.position(2);
buffer.limit(4);
System.out.println(buffer.position()); // 2
System.out.println(buffer.limit()); // 4
System.out.println(buffer.rewind()); // java.nio.HeapIntBuffer[pos=0 lim=4 cap=5]
System.out.println(buffer.position()); // 0
System.out.println(buffer.limit()); // 4
abstract Buffer slice ()
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
buffer.position(1);
buffer.limit(4);
final var sliced = buffer.slice();
System.out.println(sliced instanceof Buffer); // true
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=1 lim=4 cap=5]
System.out.println(sliced); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=3]
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.hasRemaining()); // false
System.out.println(sliced.get()); // 20
System.out.println(sliced.get()); // 30
System.out.println(sliced.get()); // 40
System.out.println(sliced.hasRemaining()); // false
abstract Buffer slice (int index, int length)
final byte[] array = {10, 20, 30, 40, 50};
final var buffer = ByteBuffer.wrap(array);
System.out.println(buffer instanceof Buffer); // true
final var sliced = buffer.slice(2, 3);
System.out.println(sliced instanceof Buffer); // true
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
System.out.println(sliced); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=3]
System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
System.out.println(buffer.hasRemaining()); // false
System.out.println(sliced.get()); // 30
System.out.println(sliced.get()); // 40
System.out.println(sliced.get()); // 50
System.out.println(sliced.hasRemaining()); // false