2010-07-12 71 views
0

我在我的netty項目中有以下簡單代碼,它期望從上游讀取一個整數。沒有編碼器在管道中。奇怪的indexOutOfBound從Netty的ChannelBuffer.read錯誤?

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) 

任何想法?

+0

Checkm for'm.readable()'。 – whiskeysierra 2010-07-13 09:26:24

回答

3

也許你的管道中沒有解碼器?即使您的對等體寫入了4個字節,您可能也會少於4個字節,因此獲得一個IOOBE的可能性很大。實際上,a section in the official user guide解釋完全相同的情況。