Last active
August 29, 2015 13:56
-
-
Save NiteshKant/9059247 to your computer and use it in GitHub Desktop.
RxNetty-ByteBufConfigurator
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
package io.reactivex.netty.pipeline; | |
import io.netty.buffer.ByteBuf; | |
import io.netty.channel.ChannelDuplexHandler; | |
import io.netty.channel.ChannelHandlerContext; | |
import io.netty.channel.ChannelPipeline; | |
import io.netty.channel.ChannelPromise; | |
/** | |
* @author Nitesh Kant | |
*/ | |
public class ByteBufPipelineConfigurator implements PipelineConfigurator<byte[], byte[]> { | |
@Override | |
public void configureNewPipeline(ChannelPipeline pipeline) { | |
pipeline.addLast(new ChannelDuplexHandler() { | |
@Override | |
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { | |
boolean handled = false; | |
if (ByteBuf.class.isAssignableFrom(msg.getClass())) { | |
ByteBuf byteBuf = (ByteBuf) msg; | |
if (byteBuf.isReadable()) { | |
int readableBytes = byteBuf.readableBytes(); | |
byte[] msgToPass = new byte[readableBytes]; | |
byteBuf.readBytes(msgToPass); | |
handled = true; | |
ctx.fireChannelRead(msgToPass); | |
} | |
} | |
if (!handled) { | |
super.channelRead(ctx, msg); | |
} | |
} | |
@Override | |
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { | |
if (msg instanceof byte[]) { | |
byte[] msgAsBytes = (byte[]) msg; | |
ByteBuf byteBuf = ctx.alloc().buffer(msgAsBytes.length).writeBytes(msgAsBytes); | |
super.write(ctx, byteBuf, promise); | |
} else { | |
super.write(ctx, msg, promise); // pass-through | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment