Top Description Inners Fields Constructors Methods
io.netty.handler.codec

public abstract Class ByteToMessageCodec<I>

extends ChannelDuplexHandler
Class Inheritance
Imports
io.netty.buffer.ByteBuf, io.netty.channel.ChannelDuplexHandler, .ChannelHandlerContext, .ChannelPromise, io.netty.util.internal.TypeParameterMatcher, java.util.List

A Codec for on-the-fly encoding/decoding of bytes to messages and vise-versa. This can be thought of as a combination of ByteToMessageDecoder and MessageToByteEncoder. Be aware that sub-classes of ByteToMessageCodec MUST NOT annotated with @Sharable.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private final ByteToMessageDecoder
private final MessageToByteEncoder<I>
private final TypeParameterMatcher

Constructor Summary

AccessConstructor and Description
protected
ByteToMessageCodec()

see ByteToMessageCodec(boolean) with true as boolean parameter.

protected
ByteToMessageCodec(Class<? extends I> outboundMessageType)

see ByteToMessageCodec(Class, boolean) with true as boolean value.

protected
ByteToMessageCodec(boolean
true if a direct ByteBuf should be tried to be used as target for the encoded messages. If false is used it will allocate a heap ByteBuf, which is backed by an byte array.
preferDirect
)

Create a new instance which will try to detect the types to match out of the type parameter of the class.

protected
ByteToMessageCodec(Class<? extends I>
The type of messages to match
outboundMessageType
,
boolean
true if a direct ByteBuf should be tried to be used as target for the encoded messages. If false is used it will allocate a heap ByteBuf, which is backed by an byte array.
preferDirect
)

Create a new instance

Method Summary

Modifier and TypeMethod and Description
public boolean
acceptOutboundMessage(Object
the message
msg
)

Returns true if and only if the specified message can be encoded by this codec.

public void
public void
public void
protected abstract void
protected void
protected abstract void
public void
handlerAdded(ChannelHandlerContext ctx)

Overrides io.netty.channel.ChannelHandlerAdapter.handlerAdded.

Implements io.netty.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.
public void
handlerRemoved(ChannelHandlerContext ctx)

Overrides io.netty.channel.ChannelHandlerAdapter.handlerRemoved.

Implements io.netty.channel.ChannelHandler.handlerRemoved.

Gets called after the ChannelHandler was removed from the actual context and it doesn't handle events anymore.
public void
write(ChannelHandlerContext
the ChannelHandlerContext for which the write operation is made
ctx
,
Object
the message to write
msg
,
ChannelPromise
the ChannelPromise to notify once the operation completes
promise
)

Overrides io.netty.channel.ChannelDuplexHandler.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Calls ChannelHandlerContext#write(Object, ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline.
Inherited from io.netty.channel.ChannelDuplexHandler:
bindcloseconnectderegisterdisconnectflushread

Field Detail

decoderback to summary
private final ByteToMessageDecoder decoder
encoderback to summary
private final MessageToByteEncoder<I> encoder
outboundMsgMatcherback to summary
private final TypeParameterMatcher outboundMsgMatcher

Constructor Detail

ByteToMessageCodecback to summary
protected ByteToMessageCodec()

see ByteToMessageCodec(boolean) with true as boolean parameter.

ByteToMessageCodecback to summary
protected ByteToMessageCodec(Class<? extends I> outboundMessageType)

see ByteToMessageCodec(Class, boolean) with true as boolean value.

ByteToMessageCodecback to summary
protected ByteToMessageCodec(boolean preferDirect)

Create a new instance which will try to detect the types to match out of the type parameter of the class.

Parameters
preferDirect:boolean

true if a direct ByteBuf should be tried to be used as target for the encoded messages. If false is used it will allocate a heap ByteBuf, which is backed by an byte array.

ByteToMessageCodecback to summary
protected ByteToMessageCodec(Class<? extends I> outboundMessageType, boolean preferDirect)

Create a new instance

Parameters
outboundMessageType:Class<? extends I>

The type of messages to match

preferDirect:boolean

true if a direct ByteBuf should be tried to be used as target for the encoded messages. If false is used it will allocate a heap ByteBuf, which is backed by an byte array.

Method Detail

acceptOutboundMessageback to summary
public boolean acceptOutboundMessage(Object msg) throws Exception

Returns true if and only if the specified message can be encoded by this codec.

Parameters
msg:Object

the message

channelInactiveback to summary
public void channelInactive(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelInactive.

Implements io.netty.channel.ChannelInboundHandler.channelInactive.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelInactive.

Calls ChannelHandlerContext#fireChannelInactive() to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
channelReadback to summary
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelRead.

Implements io.netty.channel.ChannelInboundHandler.channelRead.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelRead.

Calls ChannelHandlerContext#fireChannelRead(Object) to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
channelReadCompleteback to summary
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelReadComplete.

Implements io.netty.channel.ChannelInboundHandler.channelReadComplete.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelReadComplete.

Calls ChannelHandlerContext#fireChannelReadComplete() to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
decodeback to summary
protected abstract void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception
See Also
ByteToMessageDecoder#decode(ChannelHandlerContext, ByteBuf, List)
decodeLastback to summary
protected void decodeLast(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception
See Also
ByteToMessageDecoder#decodeLast(ChannelHandlerContext, ByteBuf, List)
encodeback to summary
protected abstract void encode(ChannelHandlerContext ctx, I msg, ByteBuf out) throws Exception
See Also
MessageToByteEncoder#encode(ChannelHandlerContext, Object, ByteBuf)
handlerAddedback to summary
public void handlerAdded(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelHandlerAdapter.handlerAdded.

Implements io.netty.channel.ChannelHandler.handlerAdded.

Doc from io.netty.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.

Annotations
@Override
handlerRemovedback to summary
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelHandlerAdapter.handlerRemoved.

Implements io.netty.channel.ChannelHandler.handlerRemoved.

Doc from io.netty.channel.ChannelHandler.handlerRemoved.

Gets called after the ChannelHandler was removed from the actual context and it doesn't handle events anymore.

Annotations
@Override
writeback to summary
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception

Overrides io.netty.channel.ChannelDuplexHandler.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Doc from io.netty.channel.ChannelDuplexHandler.write.

Calls ChannelHandlerContext#write(Object, ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Parameters
ctx:ChannelHandlerContext

Doc from io.netty.channel.ChannelOutboundHandler.write.

the ChannelHandlerContext for which the write operation is made

msg:Object

Doc from io.netty.channel.ChannelOutboundHandler.write.

the message to write

promise:ChannelPromise

Doc from io.netty.channel.ChannelOutboundHandler.write.

the ChannelPromise to notify once the operation completes

Annotations
@Override
Exceptions
Exception:

Doc from io.netty.channel.ChannelOutboundHandler.write.

thrown if an error occurs

io.netty.handler.codec back to summary

private final Class ByteToMessageCodec.Encoder

extends MessageToByteEncoder<I>
Class Inheritance

Constructor Summary

AccessConstructor and Description
pack-priv
Encoder(boolean preferDirect)

Method Summary

Modifier and TypeMethod and Description
public boolean
acceptOutboundMessage(Object msg)

Overrides io.netty.handler.codec.MessageToByteEncoder.acceptOutboundMessage.

Returns true if the given message should be handled.
protected void
encode(ChannelHandlerContext ctx, I
the message to encode
msg
,
ByteBuf
the ByteBuf into which the encoded message will be written
out
)

Implements abstract io.netty.handler.codec.MessageToByteEncoder.encode.

Encode a message into a ByteBuf.
Inherited from io.netty.handler.codec.MessageToByteEncoder:
allocateBufferisPreferDirectwrite

Constructor Detail

Encoderback to summary
pack-priv Encoder(boolean preferDirect)

Method Detail

acceptOutboundMessageback to summary
public boolean acceptOutboundMessage(Object msg) throws Exception

Overrides io.netty.handler.codec.MessageToByteEncoder.acceptOutboundMessage.

Doc from io.netty.handler.codec.MessageToByteEncoder.acceptOutboundMessage.

Returns true if the given message should be handled. If false it will be passed to the next ChannelOutboundHandler in the ChannelPipeline.

Annotations
@Override
encodeback to summary
protected void encode(ChannelHandlerContext ctx, I msg, ByteBuf out) throws Exception

Implements abstract io.netty.handler.codec.MessageToByteEncoder.encode.

Doc from io.netty.handler.codec.MessageToByteEncoder.encode.

Encode a message into a ByteBuf. This method will be called for each written message that can be handled by this encoder.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this MessageToByteEncoder belongs to

msg:I

the message to encode

out:ByteBuf

the ByteBuf into which the encoded message will be written

Annotations
@Override
Exceptions
Exception:
is thrown if an error occurs