Last active
August 29, 2015 14:17
-
-
Save scottcagno/0a8bcc156e9c2f78cc4b to your computer and use it in GitHub Desktop.
Message decoder for netty4...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MessageDecoder extends LineBasedFrameDecoder { | |
| private static final byte D = (byte)':'; | |
| public MessageDecoder(int maxLength) { | |
| super(maxLength); | |
| } | |
| protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception { | |
| ByteBuf m = (ByteBuf) super.decode(ctx, buffer); // run newline decoder | |
| if (m == null) // check to see if the buffered line was properly terminated ('\n' || '\r','\n' ) | |
| return null; // ...because if not, then we don't have a line to further decode | |
| int[] n = new int[2]; // array to save deliminator positions | |
| int d = 0; // deliminator counter | |
| while(m.isReadable()) // iterate array backing the buffer | |
| if(m.readByte() == D) { // look for a deliminator | |
| int i = m.readerIndex(); // found deliminator, save current index in buffer | |
| n[d] = (d == 0) ? i-1 : (i-2) - n[0]; // if first deliminator, save (index-1) to position n[0], | |
| // otherwise save (index-2)-n[0] to position n[1] | |
| d++; // increment deliminator count | |
| } | |
| if(d != 2) // check to see if we counted at least 2 deliminators | |
| return null; // ...because if we didn't, then we don't have valid data | |
| m.resetReaderIndex(); // reset index pointer on array, so we can start to read the byte chunks into new buffers | |
| ByteBuf cmd = m.readBytes(n[0]); // read the first n[0] bytes | |
| ByteBuf set = m.skipBytes(1).readBytes(n[1]); // skip the first deliminator, then read first n[1] bytes | |
| ByteBuf dat = m.skipBytes(1).readBytes(m.readableBytes()); // skip the second deliminator, then read remaining bytes | |
| return new Message(cmd, set, dat); // create a new message object, passing in the newly parsed buffers | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment