This code is similar to example code in the JavaDoc of ByteBuffer.compact()
package com.study.nio;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class BufferCopy {
public static void main(String[] args) {
var from = "Hello World".getBytes();
var buf = ByteBuffer.allocate(5);
buf.clear();
try (var outputStream = new ByteArrayOutputStream();
var inChannel = Channels.newChannel(new ByteArrayInputStream(from));
var outChannel = Channels.newChannel(outputStream);) {
while (inChannel.read(buf) >= 0 || buf.position() != 0) {
buf.flip();
outChannel.write(buf);
buf.compact();
}
System.out.println(outputStream.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
My question is do we need the "|| buf.position() !=0" in the while condition check?
Just for reference, the code example provided in the documentation of Buffer.compact() is this:
buf.clear(); // Prepare buffer for use
while (in.read(buf) >= 0 || buf.position != 0) {
buf.flip();
out.write(buf);
buf.compact(); // In case of partial write
}
Scenario 1: Condition is Finally Met
in
reaches its EOF and it adds its final 100 bytes to the buffer buff
.in.read(buff) >= 0
, the second condition is not even evaluated.out.write(buff)
only 50 of the 100 bytes in the buffer are sent (perhaps the output channel has a very low bandwidth or its buffer is too small).in.read(buff) >= 0
is false because in
channel is at its EOF, so it evaluates buf.position != 0
and we discover that there are still bytes in the buffer.Scenario 2: Slow Output Building up
Another way to look at how this condition gradually builds up is by picturing a scenario when the output channel is slower than the input channel, eg low bandwidth and/or a smaller output buffer.
in.read(buff) >= 0
, the second condition is not even evaluated. It reads 100 bytes.out.write(buff)
only 50 of the 100 bytes in the buffer are sent.in.read(buff) >= 0
adds 50 more bytes into the buffer and we are once again at 100 bytes total.buf.position != 0
is finally evaluated.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.