java - ReplayingDecoder 在解码时抛出异常

标签 java netty nio

我创建了一个解码器来处理客户端发送的字节。 这是它

import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

public class MessageDecoder extends ReplayingDecoder<DecoderState> {

    private int length;

    public MessageDecoder()
    {
        super(DecoderState.READ_LENGTH);
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception{
        System.out.println(buf.readableBytes());
        switch(state()){
            case READ_LENGTH:
                length=buf.readInt();
                System.out.println("length is: "+length);
                checkpoint(DecoderState.READ_CONTENT);
            case READ_CONTENT:
                ByteBuf frame = buf.readBytes(length);
                checkpoint(DecoderState.READ_LENGTH);
                out.add(frame);
                break;
            default:
                throw new Error("Shouldn't reach here");
        }
    }
}

当客户端发送字节时,它会抛出下一个错误

io.netty.handler.codec.DecoderException: java.lang.IllegalArgumentException: minimumReadableBytes: -603652096 (expected: >= 0) at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:431) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:245) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:292) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:278) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:962) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:528) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:485) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:399) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:371) at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112) at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137) at java.lang.Thread.run(Unknown Source)

该代码来自官方文档http://netty.io/4.0/api/io/netty/handler/codec/ReplayingDecoder.html所以我真的不明白为什么它不起作用

最佳答案

可能远程对等方确实将 int 写为无符号。也许您想要的是使用 readUnsignedInt() ?

关于java - ReplayingDecoder 在解码时抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37511595/

相关文章:

java - SocketChannel 问题

Java - 如何创建看起来像 word 或 excel 2007 的选项卡

java - 当元素重新排序时,使用 XMLunit 进行 XML 比较不起作用

java - io.netty.util.IllegalReferenceCountException : refCnt: 0 in Netty

java.nio.channels.DatagramChannel 基本 UDP RX 客户端连接

jvm - 为什么 java.nio.FileChannel TransferTo() 和 TransferFrom() 更快???它使用DMA吗?

c# - 如何获取 MIDI 设备的当前状态?

java - mvn tomcat7 :deploy not work if no restart tomcat

java - 如何在Netty channel 中调度HttpRequest?

java - 在Java中使用Netty发送连续数据的最佳方式