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 :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
Un canal que puede escribir bytes desde una secuencia de buffers. (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));
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));