简体   繁体   中英

Integer encoder decoder for Netty

I want to stream a series of integers across a Netty channel. Right now , in my code channel.write(Integer.valueOf(val) , I get the error java.lang.IllegalArgumentException: unsupported message type: class java.lang.Integer which I understand is because I do not have any integer encoder /decoder as a handler in the pipeline. Is this correct? Do I have to write my own integer decoder or there is one available to use ? Some guidance around this topic will be extremely helpful.

Yes this is the case... You can also just write it in a ChannelBuffer and then write the ChannelBuffer to the Channel.

Something like:

ChannelBuffer buf = ChannelBuffers.buffer(4);
buf.writeInt(Integer.valueOf(val));
channel.write(buf);

Yup, your understanding is correct. Without an appropriate FrameEncoder in your pipeline, Netty is going to throw up its hands and say it doesn't know how to deal with an Integer .

If you want to add an off-the-shelf component, you can add an ObjectEncoder and ObjectDecoder to your pipeline. Otherwise, you'll want to implement your own frame encoder and decoders.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM