Java : ScatteringByteChannel 示例

Java 中的 ScatteringByteChannel (Java SE 24 & JDK 24) 及其示例。
您将找到大多数 ScatteringByteChannel 方法的代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

可以将字节读入缓冲区序列的通道。 (机器翻译)

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, 60, 70, 80, 90};
Files.write(path, bytes);

try (final ScatteringByteChannel channel = FileChannel.open(
        path, StandardOpenOption.READ)) {

    final ByteBuffer[] dsts = {
            ByteBuffer.allocate(2),
            ByteBuffer.allocate(3),
            ByteBuffer.allocate(4),
    };

    final var ret = channel.read(dsts);
    System.out.println(ret); // 9

    System.out.println("-- dsts --");
    for (final var dst : dsts) {
        if (dst.hasArray()) {
            System.out.println(Arrays.toString(dst.array()));
        }
    }

    // Result
    // ↓
    //-- dsts --
    //[10, 20]
    //[30, 40, 50]
    //[60, 70, 80, 90]
}

Methods

long read (ByteBuffer[] dsts)

将字节序列从该通道读入给定的缓冲区。 (机器翻译)

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, 60, 70, 80, 90};
Files.write(path, bytes);

try (final ScatteringByteChannel channel = FileChannel.open(
        path, StandardOpenOption.READ)) {

    final ByteBuffer[] dsts = {
            ByteBuffer.allocate(2),
            ByteBuffer.allocate(3),
            ByteBuffer.allocate(4),
    };

    final var ret = channel.read(dsts);
    System.out.println(ret); // 9

    System.out.println("-- dsts --");
    for (final var dst : dsts) {
        if (dst.hasArray()) {
            System.out.println(Arrays.toString(dst.array()));
        }
    }

    // Result
    // ↓
    //-- dsts --
    //[10, 20]
    //[30, 40, 50]
    //[60, 70, 80, 90]
}

long read (ByteBuffer[] dsts, int offset, int length)

将字节序列从该通道读入给定缓冲区的子序列。 (机器翻译)

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, 60, 70, 80, 90};
Files.write(path, bytes);

try (final ScatteringByteChannel channel = FileChannel.open(
        path, StandardOpenOption.READ)) {

    final ByteBuffer[] dsts = {
            ByteBuffer.allocate(1),
            ByteBuffer.allocate(2),
            ByteBuffer.allocate(3),
            ByteBuffer.allocate(4),
            ByteBuffer.allocate(5),
    };

    final var ret = channel.read(dsts, 1, 3);
    System.out.println(ret); // 9

    System.out.println("-- dsts --");
    for (final var dst : dsts) {
        if (dst.hasArray()) {
            System.out.println(Arrays.toString(dst.array()));
        }
    }

    // Result
    // ↓
    //-- dsts --
    //[0]
    //[10, 20]
    //[30, 40, 50]
    //[60, 70, 80, 90]
    //[0, 0, 0, 0, 0]
}

Methods declared in Channel

close, isOpen

请参阅下面的链接。

Methods declared in ReadableByteChannel

read

请参阅下面的链接。


相关文章

To top of page