0

我正在使用基于 Netty 框架开发的 Java HTTP 代理 (LittleProxy)。在一个简单的示例中,我添加了一个 HTTP 响应过滤器来处理图像内容,并将处理后的内容附加回响应消息。下面是我正在使用的代码片段。

// Get the buffered channel output writer.
DynamicChannelBuffer dyChannelBuf = new DynamicChannelBuffer(chanBuff.order(), chanBuff.capacity());
dyChannelBuf.clear();

log.debug("Capacity of the channel buffer is: " + dyChannelBuf.capacity());
ChannelBufferOutputStream buffOutStream = new ChannelBufferOutputStream(dyChannelBuf);

byte[] byteBuf = new byte[4096];
int bytesRead = -1;
int offSet = 0;

try {                       
while ((bytesRead = inStream.read(byteBuf)) != -1) {
    buffOutStream.write(byteBuf, offSet, bytesRead);
    offSet += bytesRead;
    }                       
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
// This buffer is not good. Do not process image.
log.debug("Marked file: " + markedFileName + " can not be sent");
                    buffOutStream.close();
inStream.close();
return false;
}

// modifies response
response.setContent(buffOutStream.buffer());
response.setHeader("Content-Type", contType);
response.setHeader("Content-Length", fileLen);

对于特定图像,我得到了以下异常。其他一些图像工作正常。有人可以帮助我了解我是否正确地处理了上面的响应消息吗?

================================== 异常================ ====

HTTP/1.1 200 OK
Server: Apache
Last-Modified: Tue, 14 May 2013 04:24:43 GMT
Accept-Ranges: bytes
Content-Length: 11193
Cneonction: close
Content-Type: image/jpeg
Cache-Control: private, max-age=605454
Expires: Wed, 22 May 2013 05:08:40 GMT
Date: Wed, 15 May 2013 04:57:46 GMT
Connection: keep-alive

java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at org.jboss.netty.buffer.HeapChannelBuffer.setBytes(HeapChannelBuffer.java:132)
    at org.jboss.netty.buffer.DynamicChannelBuffer.setBytes(DynamicChannelBuffer.java:182)
    at org.jboss.netty.buffer.AbstractChannelBuffer.writeBytes(AbstractChannelBuffer.java:444)
    at org.jboss.netty.buffer.DynamicChannelBuffer.writeBytes(DynamicChannelBuffer.java:236)
    at org.jboss.netty.buffer.ChannelBufferOutputStream.write(ChannelBufferOutputStream.java:67)
    at org.media.processor.LibraryImpl.processResponse(LibraryImpl.java:110)
    at org.media.processor.MediaHandler$1.filterResponse(MediaHandler.java:29)
    at org.littleshoot.proxy.HttpRelayingHandler.messageReceived(HttpRelayingHandler.java:191)
    at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
    at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java
:791)
    at org.jboss.netty.channel.SimpleChannelHandler.messageReceived(SimpleChannelHandler.java:142)
    at org.jboss.netty.channel.SimpleChannelHandler.handleUpstream(SimpleChannelHandler.java:88)
    at org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler.handleUpstream(IdleStateAwareChannelHandler.java:36)
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
    at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java
:791)
    at org.jboss.netty.handler.timeout.IdleStateHandler.messageReceived(IdleStateHandler.java:294)
    at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
    at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java
:791)
    at org.jboss.netty.handler.codec.http.HttpChunkAggregator.messageReceived(HttpChunkAggregator.java:148)
    at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
    at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java
:791)
    at org.jboss.netty.handler.codec.http.HttpContentDecoder.messageReceived(HttpContentDecoder.java:108)
    at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
    at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java
:791)
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296)
    at org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:459)
    at org.jboss.netty.handler.codec.replay.ReplayingDecoder.callDecode(ReplayingDecoder.java:536)
    at org.jboss.netty.handler.codec.replay.ReplayingDecoder.messageReceived(ReplayingDecoder.java:485)
    at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559)
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268)
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255)
    at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:107)
    at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:312)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:88)
    at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178)
    at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)
    at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
6771   2013-05-15 00:57:45,813 DEBUG [New I/O worker #1] proxy.HttpRelayingHandler.shouldCloseRemoteConnection (HttpRela
yingHandler.java:463) - Not closing -- response probably keep alive for:
DefaultHttpResponse(chunked: false)
4

1 回答 1

0

你的问题可能在这里:

while ((bytesRead = inStream.read(byteBuf)) != -1) {
    buffOutStream.write(byteBuf, offSet, bytesRead);
    offSet += bytesRead;
}  

该参数offSet是在源数据(即 in byteBuf)中开始复制的偏移量。因为你有一个固定的数组,你总是用新的值填充它,你必须在0这里用作偏移量。

于 2013-07-31T15:28:14.867 回答