Java : GatheringByteChannel con ejemplos

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

Nota :


Summary

Un canal que puede escribir bytes desde una secuencia de buffers. (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

try (final GatheringByteChannel channel = FileChannel.open(
        path, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {

    final ByteBuffer[] srcs = {
            ByteBuffer.wrap(new byte[]{10, 20}),
            ByteBuffer.wrap(new byte[]{30, 40, 50}),
            ByteBuffer.wrap(new byte[]{60, 70, 80, 90}),
    };

    final var ret = channel.write(srcs);
    System.out.println(ret); // 9
}

final var bytes = Files.readAllBytes(path);

// [10, 20, 30, 40, 50, 60, 70, 80, 90]
System.out.println(Arrays.toString(bytes));

Methods

long write (ByteBuffer[] srcs)

Escribe una secuencia de bytes en este canal desde los buffers indicados. (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 GatheringByteChannel channel = FileChannel.open(
        path, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {

    final ByteBuffer[] srcs = {
            ByteBuffer.wrap(new byte[]{10, 20}),
            ByteBuffer.wrap(new byte[]{30, 40, 50}),
            ByteBuffer.wrap(new byte[]{60, 70, 80, 90}),
    };

    final var ret = channel.write(srcs);
    System.out.println(ret); // 9
}

final var bytes = Files.readAllBytes(path);

// [10, 20, 30, 40, 50, 60, 70, 80, 90]
System.out.println(Arrays.toString(bytes));

long write (ByteBuffer[] srcs, int offset, int length)

Escribe una secuencia de bytes en este canal desde una subsecuencia de los buffers dados. (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 GatheringByteChannel channel = FileChannel.open(
        path, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {

    final ByteBuffer[] srcs = {
            ByteBuffer.wrap(new byte[]{10}),
            ByteBuffer.wrap(new byte[]{20, 30}),
            ByteBuffer.wrap(new byte[]{40, 50, 60}),
            ByteBuffer.wrap(new byte[]{70, 80, 90, 100}),
    };

    final var ret = channel.write(srcs, 1, 2);
    System.out.println(ret); // 5
}

final var bytes = Files.readAllBytes(path);

// [20, 30, 40, 50, 60]
System.out.println(Arrays.toString(bytes));

Methods declared in Channel

close, isOpen

Consulte el siguiente enlace.

Methods declared in WritableByteChannel

write

Consulte el siguiente enlace.


Related posts

To top of page