我正在使用 apache 异步客户端读取 http 响应。每次我读取一大块数据时,我都想以非阻塞模式将其写入 servletoutputstream。像这样的东西:
// decoder.read is executed when data available for reading
while (decoder.read(this.bbuf) > 0)
{
this.bbuf.flip();
arr = new byte[numbytesread];
this.bbuf.rewind();
this.bbuf.get(arr);
// Blocking write to servletoutputstream 'sos'
this.sos.write(arr);
this.bbuf.compact();
}
显然,即使我将“sos”包装在 WritableChannel 中,这也不起作用,因为我总是以 servletoutputstream 中的 bytearrayoutputstream 结束。
所以我应该在 servletoutputstream 中添加一个 WriteListener 以切换到 nio 模式,但是我无法解决的问题来了。如何将我的 http 回调中的每一块数据传递给 writelistener 以异步和非阻塞地工作?
这可能吗?如果是这样,谁能给我一个关于如何做的线索?