我是第一次使用 Jetty 9 EndPoint。
但我对如何使用 EndPoint.flush(ByteBuffer) 方法返回码感到困惑。
我应该无限循环直到调用成功吗?
Javadoc 只是说
Returns: True IFF all the buffers have been consumed and the endpoint has flushed the data to its destination (ie is not buffering any data).
顺便说一句,我正在调用的实例的类型是SslConnection$DecryptedEndPoint
任何见解都值得赞赏,因为我找不到任何文档来说明为什么不鼓励使用 SocketEndpoint 而首选 SelectChannelEndpoint。
<小时/>有点离题,但无论如何;
令我惊讶的是,我在NetworkTrafficSelectChannelEndPoint中发现了这个:
使用操作|=代替&=(在jetty-all-9.0.3.v20130506-sources.jar中找到)
@Override
public boolean flush(ByteBuffer... buffers) throws IOException
{
boolean flushed=true;
for (ByteBuffer b : buffers)
{
if (b.hasRemaining())
{
int position = b.position();
flushed|=super.flush(b); // <<-- shouldn't it be &=
int l=b.position()-position;
notifyOutgoing(b, position, l);
if (!flushed)
break;
}
}
return flushed;
}
最佳答案
如果 EndPoint.flush() 无法写入整个缓冲区,则返回 false。因此调用者知道它必须再次调用flush,直到所有数据都写入EndPoint。
查看 WriteFlusher.write() 和 WriteFlusher.completeWrite() 并阅读其中的 javadoc 以了解它的使用方式。
关于线路: 冲洗|=super.flush(b);//<<-- 不应该是&=
我想你是对的。 lushed 被初始化为 true,因此将始终为 true。我会仔细检查并可能修复它。
关于java - Jetty EndPoint.flush(ByteBuffer b) 返回 false 时该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17450537/