public abstract class ByteToByteDecoder
extends io.netty.channel.ChannelInboundByteHandlerAdapter
ChannelInboundByteHandlerAdapter which decodes bytes in a stream-like fashion from one ByteBuf to an
other.
This kind of decoder is often useful for doing on-the-fly processiing like i.e. compression.
But you can also do other things with it. For example here is an implementation which reads Integers from
the input ByteBuf and square them before write them to the output ByteBuf.
public class SquareDecoder extends ByteToByteDecoder {
@Override
public void decode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out)
throws Exception {
if (in.readableBytes() < 4) {
return;
}
int value = in.readInt();
out.writeInt(value * value);
}
}
| Constructor and Description |
|---|
ByteToByteDecoder() |
| Modifier and Type | Method and Description |
|---|---|
void |
channelInactive(io.netty.channel.ChannelHandlerContext ctx) |
protected abstract void |
decode(io.netty.channel.ChannelHandlerContext ctx,
io.netty.buffer.ByteBuf in,
io.netty.buffer.ByteBuf out)
Decode the from one
ByteBuf to an other. |
protected void |
decodeLast(io.netty.channel.ChannelHandlerContext ctx,
io.netty.buffer.ByteBuf in,
io.netty.buffer.ByteBuf out)
Is called one last time when the
ChannelHandlerContext goes in-active. |
void |
inboundBufferUpdated(io.netty.channel.ChannelHandlerContext ctx,
io.netty.buffer.ByteBuf in) |
boolean |
isSingleDecode()
If
true then only one message is decoded on each
ChannelInboundByteHandlerAdapter.inboundBufferUpdated(ChannelHandlerContext) call. |
void |
setSingleDecode(boolean singleDecode)
If set then only one message is decoded on each
ChannelInboundByteHandlerAdapter.inboundBufferUpdated(ChannelHandlerContext) call. |
discardInboundReadBytes, freeInboundBuffer, inboundBufferUpdated, newInboundBufferchannelActive, channelReadSuspended, channelRegistered, channelUnregisteredafterAdd, afterRemove, beforeAdd, beforeRemove, exceptionCaught, userEventTriggeredclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitpublic void setSingleDecode(boolean singleDecode)
ChannelInboundByteHandlerAdapter.inboundBufferUpdated(ChannelHandlerContext) call.
This may be useful if you need to do some protocol upgrade and want to make sure nothing is mixed up.
Default is false as this has performance impacts.public boolean isSingleDecode()
true then only one message is decoded on each
ChannelInboundByteHandlerAdapter.inboundBufferUpdated(ChannelHandlerContext) call.
Default is false as this has performance impacts.public void inboundBufferUpdated(io.netty.channel.ChannelHandlerContext ctx,
io.netty.buffer.ByteBuf in)
throws Exception
inboundBufferUpdated in class io.netty.channel.ChannelInboundByteHandlerAdapterExceptionpublic void channelInactive(io.netty.channel.ChannelHandlerContext ctx)
throws Exception
channelInactive in interface io.netty.channel.ChannelStateHandlerchannelInactive in class io.netty.channel.ChannelStateHandlerAdapterExceptionprotected abstract void decode(io.netty.channel.ChannelHandlerContext ctx,
io.netty.buffer.ByteBuf in,
io.netty.buffer.ByteBuf out)
throws Exception
ByteBuf to an other. This method will be called till either the input
ByteBuf has nothing to read anymore or till nothing was read from the input ByteBuf.ctx - the ChannelHandlerContext which this ByteToByteDecoder belongs toin - the ByteBuf from which to read dataout - the ByteBuf to which the decoded data will be writtenException - is thrown if an error accourprotected void decodeLast(io.netty.channel.ChannelHandlerContext ctx,
io.netty.buffer.ByteBuf in,
io.netty.buffer.ByteBuf out)
throws Exception
ChannelHandlerContext goes in-active. Which means the
channelInactive(ChannelHandlerContext) was triggered.
By default this will just call decode(ChannelHandlerContext, ByteBuf, ByteBuf) but sub-classes may
override this for some special cleanup operation.ExceptionCopyright © 2008-2013 The Netty Project. All Rights Reserved.