Java NIO Framework Netty Tutorial (ix) Object object encoding/decoding

Source: Internet
Author: User

See the topic, some students may think, last said the object passed it? Yes, just in the Java NIO Framework Netty Tutorial (eight) The object object is passed, we just show how to use the Netty provided by the codec/Decode tool to complete the serialization of objects. This is to tell you netty specifically how to do, perhaps some students want to complete the serialization of their own? Moreover, the serialization of objects is available everywhere:)

First look at how to encode.

View Sourceprint?01.@Override02.protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {03.ChannelBufferOutputStream bout =04.new ChannelBufferOutputStream(dynamicBuffer(05.estimatedLength, ctx.getChannel().getConfig().getBufferFactory()));06.bout.write(LENGTH_PLACEHOLDER);07.ObjectOutputStream oout = new CompactObjectOutputStream(bout);08.oout.writeObject(msg);09.oout.flush();10.oout.close();11. 12.ChannelBuffer encoded = bout.buffer();13.encoded.setInt(0, encoded.writerIndex() - 4);14.return encoded;15.}

In fact, you should have thought of, in Java in the serialization of the object is naturally not objectoutputstream. Netty This is just another layer of packaging that adds a 4-byte flag to the beginning of the stream. Therefore, Netty declares that the encoding and decoding classes must be used in a complementary, incompatible with the simple objectintputstream.

* An encoder which serializes a Java object into a {@link channelbuffer}.
* <p>
* Please note that the serialized form this encoder produces are not
* Compatible with the standard {@link ObjectInputStream}. Please use
* {@link Objectdecoder} or {@link Objectdecoderinputstream} to ensure the
* Interoperability with this encoder.

Decoding nature is to parse out the extra 4 bits first and then parse through ObjectInputStream.

The details about the serialization of Java objects are beyond the scope of the text discussion, but I wonder if you're interested in trying to write one yourself? So-called, more hands-on.

View Sourceprint?01./**02.* Object编码类03.*04.* @author lihzh05.* @alia OneCoder06.* @blog http://www.it165.net07.*/08.public class MyObjEncoder implements ChannelDownstreamHandler {09. 10.@Override11.public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e)12.throws Exception {13.// 处理收发信息的情形14.if (e instanceof MessageEvent) {15.MessageEvent mEvent = (MessageEvent) e;16.Object obj = mEvent.getMessage();17.if (!(obj instanceof Command)) {18.ctx.sendDownstream(e);19.return;20.}21.ByteArrayOutputStream out = new ByteArrayOutputStream();22.ObjectOutputStream oos = new ObjectOutputStream(out);23.oos.writeObject(obj);24.oos.flush();25.oos.close();26.ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();27.buffer.writeBytes(out.toByteArray());28.e.getChannel().write(buffer);29.} else {30.// 其他事件,自动流转。比如,bind,connected31.ctx.sendDownstream(e);32.}33.}34.}35./**36.* Object解码类37.*38.* @author lihzh39.* @alia OneCoder40.* @blog http://www.it165.net41.*/42.public class MyObjDecoder implements ChannelUpstreamHandler {43. 44.@Override45.public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e)46.throws Exception {47.if (e instanceof MessageEvent) {48.MessageEvent mEvent = (MessageEvent) e;49.if (!(mEvent.getMessage() instanceof ChannelBuffer)) {50.ctx.sendUpstream(mEvent);51.return;52.}53.ChannelBuffer buffer = (ChannelBuffer) mEvent.getMessage();54.ByteArrayInputStream input = new ByteArrayInputStream(buffer.array());55.ObjectInputStream ois = new ObjectInputStream(input);56.Object obj = ois.readObject();57.Channels.fireMessageReceived(e.getChannel(), obj);58.}59.}60.}

How, is it easy to use? The so-called imitation, to apply.

However, to remind you that this implementation has a lot of hard-coded things, do not imitate, just to show the object, codec processing and application in Netty.

Java NIO Framework Netty Tutorial (ix) Object object encoding/decoding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.