在我测试期间,我正在从浏览器加载JPG图像,基于netty的littleproxy位于服务器和客户端之间。在加载此特定图像时。通道缓冲区是CompositChannelBuffer。我正在阅读以下方式,但无法正常工作。
if (chanBuff instanceof CompositeChannelBuffer) {
CompositeChannelBuffer compChanBuf = ((CompositeChannelBuffer) chanBuff);
int noOfComps = compChanBuf.numComponents();
List<ChannelBuffer> buffList = compChanBuf.decompose(0, noOfComps);
ListIterator<ChannelBuffer> itr = buffList.listIterator();
int offSet = 0;
int bytesRead = -1;
while (itr.hasNext()) {
ChannelBuffer buf = (ChannelBuffer) itr.next();
bytesRead = buf.array().length;
outputStream.write(buf.array(), offSet, bytesRead);
offSet += bytesRead;
}
}
我也试过下面的代码,但这也无法写入文件。
if (chanBuff instanceof CompositeChannelBuffer){
FileOutputStream outputStream = new FileOutputStream(outputFileName);
CompositeChannelBuffer compChanBuf = ((CompositeChannelBuffer) chanBuff);
int noOfComps = compChanBuf.numComponents();
compChanBuf.getBytes(0, outputStream, noOfComps);
ChannelBuffer dynamicBuf = dynamicBuffer();
compChanBuf.getBytes(0, dynamicBuf);
array = dynamicBuf.array();
outputStream.write(array);
}
阅读CompositChannelBuffer的正确方法是什么?
答案 0 :(得分:2)
就像你为其他缓冲区做的那样:
ChannelBuffer buf = ...
OutputStream out = ...
buf.getBytes(0, out, buf.readableBytes());