我正在使用通过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)
答案 0 :(得分:0)
你的问题可能就在这里:
while ((bytesRead = inStream.read(byteBuf)) != -1) {
buffOutStream.write(byteBuf, offSet, bytesRead);
offSet += bytesRead;
}
参数offSet
是在源数据中开始复制的偏移量(即在byteBuf
中)。因为你有一个固定的数组并且你总是用新的值填充它,你必须在这里使用0
作为offSet。