views:

35

answers:

1

I have the following simple code in my netty project, it expects to read an integer from the upstream. No encoder is in the pipeline.

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

        m.readInt() 
    }

When the data comes in from the network the method is fired correctly (good sign) but when attempting to read, it gives the following error:

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)

Any ideas?

A: 

Perhaps you don't have a decoder in your pipeline? Even if your peer wrote 4 bytes, you might get less than 4 bytes, and therefore there is high chance of getting an IOOBE. Actually, a section in the official user guide explains exactly same case.

Trustin Lee