[英](Java NIO) Should I check both bytes read and buffer position in copying between channels?
This code is similar to example code in the JavaDoc of ByteBuffer.compact()此代码类似于 ByteBuffer.compact() 的 JavaDoc 中的示例代码
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?我的问题是我们是否需要在 while 条件检查中使用“|| buf.position() !=0” ?
Just for reference, the code example provided in the documentation of Buffer.compact() is this:仅供参考, Buffer.compact()文档中提供的代码示例如下:
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场景 1:条件终于满足
in
reaches its EOF and it adds its final 100 bytes to the buffer buff
.in
达到其 EOF 并将其最后 100 个字节添加到缓冲区buff
的时刻。in.read(buff) >= 0
, the second condition is not even evaluated.in.read(buff) >= 0
,甚至没有评估第二个条件。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).out.write(buff)
时,仅发送缓冲区中 100 个字节中的 50 个(可能 output 通道的带宽非常低或者其缓冲区太小)。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.in.read(buff) >= 0
为假,因为in
通道处于其 EOF,因此它评估buf.position != 0
并且我们发现缓冲区中仍有字节. Scenario 2: Slow Output Building up场景 2:缓慢 Output 建立
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.了解这种情况如何逐渐建立的另一种方法是通过想象 output 通道比输入通道慢的场景,例如低带宽和/或较小的 output 缓冲区。
in.read(buff) >= 0
, the second condition is not even evaluated.in.read(buff) >= 0
,甚至没有评估第二个条件。 It reads 100 bytes.out.write(buff)
only 50 of the 100 bytes in the buffer are sent.out.write(buff)
时,缓冲区中的 100 个字节中只有 50 个被发送。in.read(buff) >= 0
adds 50 more bytes into the buffer and we are once again at 100 bytes total.in.read(buff) >= 0
将 50 个字节添加到缓冲区中,我们再次达到总共 100 个字节。buf.position != 0
is finally evaluated. buf.position != 0
最终被评估。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.