Java : SeekableByteChannel - API使用例
SeekableByteChannel (Java SE 20 & JDK 20) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
SeekableByteChannel インタフェースは、好きな位置のバイトデータを読み書きできる Channel です。
final var path = Path.of("R:", "java-work", "test.data");
System.out.println(path); // R:\java-work\test.data
final byte[] bytes = {10, 20, 30, 40, 50};
Files.write(path, bytes);
try (final SeekableByteChannel channel =
Files.newByteChannel(path, StandardOpenOption.READ)) {
channel.position(2);
{
System.out.println(channel.position()); // 2
final var buffer = ByteBuffer.allocate(3);
final var ret = channel.read(buffer);
System.out.println(ret); // 3
System.out.println(channel.position()); // 5
if (buffer.hasArray()) {
// [30, 40, 50]
System.out.println(Arrays.toString(buffer.array()));
}
}
channel.position(0);
{
System.out.println(channel.position()); // 0
final var buffer = ByteBuffer.allocate(5);
final var ret = channel.read(buffer);
System.out.println(ret); // 5
System.out.println(channel.position()); // 5
if (buffer.hasArray()) {
// [10, 20, 30, 40, 50]
System.out.println(Arrays.toString(buffer.array()));
}
}
}
メソッド
long position ()
final var path = Path.of("R:", "java-work", "test.data");
System.out.println(path); // R:\java-work\test.data
final byte[] bytes = {10, 20, 30, 40, 50};
Files.write(path, bytes);
try (final SeekableByteChannel channel =
Files.newByteChannel(path, StandardOpenOption.READ)) {
final var ret1 = channel.position(2);
System.out.println(channel.equals(ret1)); // true
{
System.out.println(channel.position()); // 2
final var buffer = ByteBuffer.allocate(3);
final var ret = channel.read(buffer);
System.out.println(ret); // 3
System.out.println(channel.position()); // 5
if (buffer.hasArray()) {
// [30, 40, 50]
System.out.println(Arrays.toString(buffer.array()));
}
}
final var ret2 = channel.position(0);
System.out.println(channel.equals(ret2)); // true
{
System.out.println(channel.position()); // 0
final var buffer = ByteBuffer.allocate(5);
final var ret = channel.read(buffer);
System.out.println(ret); // 5
System.out.println(channel.position()); // 5
if (buffer.hasArray()) {
// [10, 20, 30, 40, 50]
System.out.println(Arrays.toString(buffer.array()));
}
}
}
SeekableByteChannel position (long newPosition)
このメソッドの使用例は、position() にまとめて記載しました。
そちらのAPI使用例をご参照ください。
int read (ByteBuffer dst)
final var path = Path.of("R:", "java-work", "test.data");
System.out.println(path); // R:\java-work\test.data
final byte[] bytes = {10, 20, 30};
Files.write(path, bytes);
try (final SeekableByteChannel channel =
Files.newByteChannel(path, StandardOpenOption.READ)) {
final var array = new byte[5];
final var dst = ByteBuffer.wrap(array);
System.out.println(dst); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
System.out.println(Arrays.toString(array)); // [0, 0, 0, 0, 0]
System.out.println(channel.position()); // 0
final var ret = channel.read(dst);
System.out.println(ret); // 3
System.out.println(channel.position()); // 3
System.out.println(dst); // java.nio.HeapByteBuffer[pos=3 lim=5 cap=5]
System.out.println(Arrays.toString(array)); // [10, 20, 30, 0, 0]
}
long size ()
final var path = Path.of("R:", "java-work", "test.data");
System.out.println(path); // R:\java-work\test.data
final byte[] bytes = {10, 20, 30, 40, 50};
Files.write(path, bytes);
try (final SeekableByteChannel channel =
Files.newByteChannel(path, StandardOpenOption.READ)) {
final var size = channel.size();
System.out.println(size); // 5
final var dst = ByteBuffer.allocate(Math.toIntExact(size));
System.out.println(channel.read(dst)); // 5
if (dst.hasArray()) {
// [10, 20, 30, 40, 50]
System.out.println(Arrays.toString(dst.array()));
}
}
SeekableByteChannel truncate (long size)
final var path = Path.of("R:", "java-work", "test.data");
System.out.println(path); // R:\java-work\test.data
final byte[] bytes = {10, 20, 30, 40, 50};
Files.write(path, bytes);
try (final SeekableByteChannel channel =
Files.newByteChannel(path, StandardOpenOption.WRITE)) {
final var ret = channel.truncate(3);
System.out.println(channel.equals(ret)); // true
final var size = channel.size();
System.out.println(size); // 3
}
// [10, 20, 30]
System.out.println(Arrays.toString(Files.readAllBytes(path)));
int write (ByteBuffer src)
final var path = Path.of("R:", "java-work", "test.data");
System.out.println(path); // R:\java-work\test.data
try (final SeekableByteChannel channel = Files.newByteChannel(path,
StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
final byte[] array = {10, 20, 30, 40, 50};
final var src = ByteBuffer.wrap(array);
src.limit(3);
System.out.println(src); // java.nio.HeapByteBuffer[pos=0 lim=3 cap=5]
System.out.println(channel.position()); // 0
final var ret = channel.write(src);
System.out.println(ret); // 3
System.out.println(src); // java.nio.HeapByteBuffer[pos=3 lim=3 cap=5]
System.out.println(channel.position()); // 3
}
final var bytes = Files.readAllBytes(path);
System.out.println(Arrays.toString(bytes)); // [10, 20, 30]
Channelで宣言されたメソッド
close, isOpen
「Java API 使用例 : Channel」をご参照ください。