public abstract class ByteToMessageDecoder extends 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 extendsBe aware that sub-classes ofByteToMessageDecoder
{@Override
public void decode(ChannelHandlerContext
ctx,ByteBuf
in, List<Object> out) throwsException
{ out.add(in.readBytes(in.readableBytes())); } }
ByteToMessageDecoder
MUST NOT
annotated with @Sharable
.ChannelHandler.Sharable
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(ChannelHandlerContext ctx,
ByteBuf in,
List<Object> out)
Called once data should be decoded from the given
ByteBuf . |
void |
channelInactive(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelInactive() to forward
to the next ChannelInboundHandler in the ChannelPipeline . |
void |
channelRead(ChannelHandlerContext ctx,
Object msg)
Calls
ChannelHandlerContext.fireChannelRead(Object) to forward
to the next ChannelInboundHandler in the ChannelPipeline . |
void |
channelReadComplete(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelReadComplete() to forward
to the next ChannelInboundHandler in the ChannelPipeline . |
protected abstract void |
decode(ChannelHandlerContext ctx,
ByteBuf in,
List<Object> out)
Decode the from one
ByteBuf to an other. |
protected void |
decodeLast(ChannelHandlerContext ctx,
ByteBuf in,
List<Object> out)
Is called one last time when the
ChannelHandlerContext goes in-active. |
void |
handlerRemoved(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.
|
protected void |
handlerRemoved0(ChannelHandlerContext ctx)
Gets called after the
ByteToMessageDecoder was removed from the actual context and it doesn't handle
events anymore. |
protected ByteBuf |
internalBuffer()
Returns the internal cumulative buffer of this decoder.
|
boolean |
isSingleDecode()
If
true then only one message is decoded on each
channelRead(ChannelHandlerContext, Object) call. |
void |
setSingleDecode(boolean singleDecode)
If set then only one message is decoded on each
channelRead(ChannelHandlerContext, Object)
call. |
channelActive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
handlerAdded, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
handlerAdded
public void setSingleDecode(boolean singleDecode)
channelRead(ChannelHandlerContext, Object)
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
channelRead(ChannelHandlerContext, Object)
call.
Default is false
as this has performance impacts.protected int actualReadableBytes()
internalBuffer().readableBytes()
.protected ByteBuf internalBuffer()
public final void handlerRemoved(ChannelHandlerContext ctx) throws Exception
ChannelHandlerAdapter
handlerRemoved
in interface ChannelHandler
handlerRemoved
in class ChannelHandlerAdapter
Exception
protected void handlerRemoved0(ChannelHandlerContext ctx) throws Exception
ByteToMessageDecoder
was removed from the actual context and it doesn't handle
events anymore.Exception
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelRead(Object)
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelRead
in interface ChannelInboundHandler
channelRead
in class ChannelInboundHandlerAdapter
Exception
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelReadComplete()
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelReadComplete
in interface ChannelInboundHandler
channelReadComplete
in class ChannelInboundHandlerAdapter
Exception
public void channelInactive(ChannelHandlerContext ctx) throws Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelInactive()
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelInactive
in interface ChannelInboundHandler
channelInactive
in class ChannelInboundHandlerAdapter
Exception
protected void callDecode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
ByteBuf
. This method will call
decode(ChannelHandlerContext, ByteBuf, List)
as long as decoding should take place.ctx
- the ChannelHandlerContext
which this ByteToMessageDecoder
belongs toin
- the ByteBuf
from which to read dataout
- the List
to which decoded messages should be addedprotected abstract void decode(ChannelHandlerContext ctx, ByteBuf in, List<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 List
to which decoded messages should be addedException
- is thrown if an error accourprotected void decodeLast(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception
ChannelHandlerContext
goes in-active. Which means the
channelInactive(ChannelHandlerContext)
was triggered.
By default this will just call decode(ChannelHandlerContext, ByteBuf, List)
but sub-classes may
override this for some special cleanup operation.Exception
Copyright © 2008–2013 The Netty Project. All rights reserved.