Created
October 21, 2012 11:09
-
-
Save kjunine/3926699 to your computer and use it in GitHub Desktop.
DelimiterAppender
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
| import static org.jboss.netty.buffer.ChannelBuffers.*; | |
| import org.jboss.netty.buffer.ChannelBuffer; | |
| import org.jboss.netty.buffer.ChannelBuffers; | |
| import org.jboss.netty.channel.Channel; | |
| import org.jboss.netty.channel.ChannelHandlerContext; | |
| import org.jboss.netty.handler.codec.oneone.OneToOneEncoder; | |
| public class DelimiterAppender extends OneToOneEncoder { | |
| private static final ChannelBuffer DEFAULT_DELIMITER = ChannelBuffers | |
| .wrappedBuffer(new byte[] { '\n' }); | |
| private final ChannelBuffer delimiter; | |
| public DelimiterAppender() { | |
| this(DEFAULT_DELIMITER); | |
| } | |
| public DelimiterAppender(ChannelBuffer delimiter) { | |
| this.delimiter = delimiter; | |
| } | |
| @Override | |
| protected Object encode(ChannelHandlerContext ctx, Channel channel, | |
| Object msg) throws Exception { | |
| if (!(msg instanceof ChannelBuffer)) { | |
| return msg; | |
| } | |
| ChannelBuffer buffer = (ChannelBuffer) msg; | |
| return wrappedBuffer(buffer, delimiter); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment