Java : SeekableByteChannel con ejemplos

SeekableByteChannel (Java SE 24 & JDK 24) en Java con ejemplos.
Encontrará muestras de código para la mayoría de los métodos SeekableByteChannel.

Nota :


Summary

Un canal de bytes que mantiene una posición actual y permite cambiarla. (Traducción automática)

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

Devuelve la posición de este canal. (Traducción automática)

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)

Establece la posición de este canal. (Traducción automática)

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

int read (ByteBuffer dst)

Lee una secuencia de bytes de este canal en el búfer indicado. (Traducción automática)

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

Devuelve el tamaño actual de la entidad a la que está conectado este canal. (Traducción automática)

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)

Trunca la entidad a la que está conectado este canal al tamaño dado. (Traducción automática)

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)

Escribe una secuencia de bytes en este canal desde el búfer indicado. (Traducción automática)

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

Consulte el siguiente enlace.


Related posts

To top of page