public abstract class ByteToMessageDecoder
extends io.netty.channel.ChannelInboundHandlerAdapter
ChannelInboundHandlerAdapter which decodes bytes in a stream-like fashion from one ByteBuf to an
other Message type.
For example here is an implementation which reads all readable bytes from
the input ByteBuf and create a new ByteBuf.
public class SquareDecoder extends ByteToMessageDecoder {
@Override
public void decode(ChannelHandlerContext ctx, ByteBuf in, MessageList out)
throws Exception {
out.add(in.readBytes(in.readableBytes()));
}
}
Be aware that sub-classes of ByteToMessageDecoder MUST NOT
annotated with @Sharable.| Modifier and Type | Field and Description |
|---|---|
protected io.netty.buffer.ByteBuf |
cumulation |
| Modifier | Constructor and Description |
|---|---|
protected |
ByteToMessageDecoder() |
| Modifier and Type | Method and Description |
|---|---|
protected int |
actualReadableBytes()
Returns the actual number of readable bytes in the internal cumulative
buffer of this decoder.
|
protected void |
callDecode(io.netty.channel.ChannelHandlerContext ctx,
io.netty.buffer.ByteBuf in,
io.netty.channel.MessageList<Object> out) |
void |
channelInactive(io.netty.channel.ChannelHandlerContext ctx) |
void |
channelReadSuspended(io.netty.channel.ChannelHandlerContext ctx) |
protected abstract void |
decode(io.netty.channel.ChannelHandlerContext ctx,
io.netty.buffer.ByteBuf in,
io.netty.channel.MessageList<Object> out)
Decode the from one
ByteBuf to an other. |
protected void |
decodeLast(io.netty.channel.ChannelHandlerContext ctx,
io.netty.buffer.ByteBuf in,
io.netty.channel.MessageList<Object> out)
Is called one last time when the
ChannelHandlerContext goes in-active. |
void |
handlerRemoved(io.netty.channel.ChannelHandlerContext ctx) |
protected void |
handlerRemoved0(io.netty.channel.ChannelHandlerContext ctx)
Gets called after the
ByteToMessageDecoder was removed from the actual context and it doesn't handle
events anymore. |
protected io.netty.buffer.ByteBuf |
internalBuffer()
Returns the internal cumulative buffer of this decoder.
|
boolean |
isSingleDecode()
If
true then only one message is decoded on each
messageReceived(ChannelHandlerContext, MessageList) call. |
void |
messageReceived(io.netty.channel.ChannelHandlerContext ctx,
io.netty.channel.MessageList<Object> msgs) |
void |
setSingleDecode(boolean singleDecode)
If set then only one message is decoded on each
messageReceived(ChannelHandlerContext, MessageList)
call. |
channelActive, channelRegistered, channelUnregistered, channelWritabilityChanged, userEventTriggeredpublic void setSingleDecode(boolean singleDecode)
messageReceived(ChannelHandlerContext, MessageList)
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
messageReceived(ChannelHandlerContext, MessageList) call.
Default is false as this has performance impacts.protected int actualReadableBytes()
internalBuffer().readableBytes().protected io.netty.buffer.ByteBuf internalBuffer()
public final void handlerRemoved(io.netty.channel.ChannelHandlerContext ctx)
throws Exception
handlerRemoved in interface io.netty.channel.ChannelHandlerhandlerRemoved in class io.netty.channel.ChannelHandlerAdapterExceptionprotected void handlerRemoved0(io.netty.channel.ChannelHandlerContext ctx)
throws Exception
ByteToMessageDecoder was removed from the actual context and it doesn't handle
events anymore.Exceptionpublic void messageReceived(io.netty.channel.ChannelHandlerContext ctx,
io.netty.channel.MessageList<Object> msgs)
throws Exception
messageReceived in interface io.netty.channel.ChannelInboundHandlermessageReceived in class io.netty.channel.ChannelInboundHandlerAdapterExceptionpublic void channelReadSuspended(io.netty.channel.ChannelHandlerContext ctx)
throws Exception
channelReadSuspended in interface io.netty.channel.ChannelInboundHandlerchannelReadSuspended in class io.netty.channel.ChannelInboundHandlerAdapterExceptionpublic void channelInactive(io.netty.channel.ChannelHandlerContext ctx)
throws Exception
channelInactive in interface io.netty.channel.ChannelInboundHandlerchannelInactive in class io.netty.channel.ChannelInboundHandlerAdapterExceptionprotected void callDecode(io.netty.channel.ChannelHandlerContext ctx,
io.netty.buffer.ByteBuf in,
io.netty.channel.MessageList<Object> out)
protected abstract void decode(io.netty.channel.ChannelHandlerContext ctx,
io.netty.buffer.ByteBuf in,
io.netty.channel.MessageList<Object> out)
throws Exception
ByteBuf to an other. This method will be called till either the input
ByteBuf has nothing to read anymore, till nothing was read from the input ByteBuf or till
this method returns null.ctx - the ChannelHandlerContext which this ByteToMessageDecoder belongs toin - the ByteBuf from which to read dataout - the MessageList to which decoded messages should be addedException - is thrown if an error accourprotected void decodeLast(io.netty.channel.ChannelHandlerContext ctx,
io.netty.buffer.ByteBuf in,
io.netty.channel.MessageList<Object> out)
throws Exception
ChannelHandlerContext goes in-active. Which means the
channelInactive(ChannelHandlerContext) was triggered.
By default this will just call decode(ChannelHandlerContext, ByteBuf, MessageList) but sub-classes may
override this for some special cleanup operation.ExceptionCopyright © 2008-2013 The Netty Project. All Rights Reserved.