0

在我的测试过程中,我从浏览器加载了一个 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 的正确方法是什么?

4

1 回答 1

2

就像你对其他缓冲区一样做它,比如:

ChannelBuffer buf = ...
OutputStream out = ...
buf.getBytes(0, out, buf.readableBytes());
于 2013-05-22T08:11:47.577 回答