来自Netty的ChannelBuffer.read的奇怪的indexOutOfBound错误?

时间:2010-07-12 21:54:25

标签: java nio netty

我的netty项目中有以下简单代码,它希望从上游读取一个整数。没有编码器在管道中。

public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        ChannelBuffer m = (ChannelBuffer) e.getMessage();

        m.readInt() 
    }

当数据从网络进入时,方法被正确触发(好兆头),但在尝试读取时,它会出现以下错误:

java.lang.IndexOutOfBoundsException
    at org.jboss.netty.buffer.AbstractChannelBuffer.checkReadableBytes(AbstractChannelBuffer.java:657)
    at org.jboss.netty.buffer.AbstractChannelBuffer.readInt(AbstractChannelBuffer.java:272)
    at PushServer.Netty.PushClientHandler.messageReceived(PushClientHandler.java:33)
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:274)
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:261)
    at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:349)
    at org.jboss.netty.channel.socket.nio.NioWorker.processSelectedKeys(NioWorker.java:281)
    at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:201)
    at org.jboss.netty.util.internal.IoWorkerRunnable.run(IoWorkerRunnable.java:46)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:637)

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

也许你的管道中没有解码器?即使您的对等体写入4个字节,您可能会得到少于4个字节,因此获得IOOBE的可能性很高。实际上,a section in the official user guide解释了完全相同的情况。