Java : SeekableByteChannel with Examples

SeekableByteChannel (Java SE 20 & JDK 20) API Examples.
You will find code examples on most SeekableByteChannel methods.


Summary

A byte channel that maintains a current position and allows the position to be changed.

Class diagram

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

Methods

long position ()

Returns this channel's 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)

Sets this channel's position.

Please see position().

int read (ByteBuffer dst)

Reads a sequence of bytes from this channel into the given buffer.

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

Returns the current size of entity to which this channel is connected.

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)

Truncates the entity, to which this channel is connected, to the given 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)

Writes a sequence of bytes to this channel from the given buffer.

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]

Methods declared in Channel

close, isOpen

Please see the link below.


Related posts

To top of page