Java : ReadableByteChannel con ejemplos
ReadableByteChannel (Java SE 23 & JDK 23) en Java con ejemplos.
Encontrará muestras de código para la mayoría de los métodos ReadableByteChannel.
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 leer bytes. (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 ReadableByteChannel channel =
Files.newByteChannel(path, StandardOpenOption.READ)) {
final var dst = ByteBuffer.allocate(5);
System.out.println(dst); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
if (dst.hasArray()) {
System.out.println(Arrays.toString(dst.array())); // [0, 0, 0, 0, 0]
}
final var ret = channel.read(dst);
System.out.println(ret); // 3
System.out.println(dst); // java.nio.HeapByteBuffer[pos=3 lim=5 cap=5]
if (dst.hasArray()) {
System.out.println(Arrays.toString(dst.array())); // [10, 20, 30, 0, 0]
}
}
Methods
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 ReadableByteChannel channel =
Files.newByteChannel(path, StandardOpenOption.READ)) {
final var dst = ByteBuffer.allocate(5);
System.out.println(dst); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
if (dst.hasArray()) {
System.out.println(Arrays.toString(dst.array())); // [0, 0, 0, 0, 0]
}
final var ret = channel.read(dst);
System.out.println(ret); // 3
System.out.println(dst); // java.nio.HeapByteBuffer[pos=3 lim=5 cap=5]
if (dst.hasArray()) {
System.out.println(Arrays.toString(dst.array())); // [10, 20, 30, 0, 0]
}
}
Methods declared in Channel
Related posts
- Ejemplos de API