Package io.netty.handler.codec
Class MessageToMessageEncoder<I>
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelOutboundHandlerAdapter
io.netty.handler.codec.MessageToMessageEncoder<I>
- All Implemented Interfaces:
ChannelHandler
,ChannelOutboundHandler
- Direct Known Subclasses:
AbstractMemcacheObjectEncoder
,Base64Encoder
,ByteArrayEncoder
,DatagramDnsQueryEncoder
,DatagramDnsResponseEncoder
,DatagramPacketEncoder
,HttpObjectEncoder
,LengthFieldPrepender
,LineEncoder
,MqttEncoder
,RedisEncoder
,SctpOutboundByteStreamHandler
,SmtpRequestEncoder
,SpdyHttpEncoder
,StompSubframeEncoder
,StringEncoder
,TcpDnsResponseEncoder
,WebSocket00FrameEncoder
,WebSocket08FrameEncoder
,WebSocketExtensionEncoder
ChannelOutboundHandlerAdapter
which encodes from one message to an other message
For example here is an implementation which decodes an Integer
to an String
.
public class IntegerToStringEncoder extendsBe aware that you need to callMessageToMessageEncoder
<Integer
> {@Override
public void encode(ChannelHandlerContext
ctx,Integer
message, List<Object> out) throwsException
{ out.add(message.toString()); } }
ReferenceCounted.retain()
on messages that are just passed through if they
are of type ReferenceCounted
. This is needed as the MessageToMessageEncoder
will call
ReferenceCounted.release()
on encoded messages.-
Nested Class Summary
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Create a new instance which will try to detect the types to match out of the type parameter of the class.protected
MessageToMessageEncoder
(Class<? extends I> outboundMessageType) Create a new instance -
Method Summary
Modifier and TypeMethodDescriptionboolean
Returnstrue
if the given message should be handled.protected abstract void
encode
(ChannelHandlerContext ctx, I msg, List<Object> out) Encode from one message to an other.void
write
(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.private static void
writePromiseCombiner
(ChannelHandlerContext ctx, CodecOutputList out, ChannelPromise promise) private static void
Methods inherited from class io.netty.channel.ChannelOutboundHandlerAdapter
bind, close, connect, deregister, disconnect, flush, read
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, exceptionCaught, handlerAdded, handlerRemoved, isSharable
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.netty.channel.ChannelHandler
exceptionCaught, handlerAdded, handlerRemoved
-
Field Details
-
matcher
-
-
Constructor Details
-
MessageToMessageEncoder
protected MessageToMessageEncoder()Create a new instance which will try to detect the types to match out of the type parameter of the class. -
MessageToMessageEncoder
Create a new instance- Parameters:
outboundMessageType
- The type of messages to match and so encode
-
-
Method Details
-
acceptOutboundMessage
Returnstrue
if the given message should be handled. Iffalse
it will be passed to the nextChannelOutboundHandler
in theChannelPipeline
.- Throws:
Exception
-
write
Description copied from class:ChannelOutboundHandlerAdapter
CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
write
in interfaceChannelOutboundHandler
- Overrides:
write
in classChannelOutboundHandlerAdapter
- Parameters:
ctx
- theChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- theChannelPromise
to notify once the operation completes- Throws:
Exception
- thrown if an error occurs
-
writeVoidPromise
-
writePromiseCombiner
private static void writePromiseCombiner(ChannelHandlerContext ctx, CodecOutputList out, ChannelPromise promise) -
encode
Encode from one message to an other. This method will be called for each written message that can be handled by this encoder.- Parameters:
ctx
- theChannelHandlerContext
which thisMessageToMessageEncoder
belongs tomsg
- the message to encode to an other oneout
- theList
into which the encoded msg should be added needs to do some kind of aggregation- Throws:
Exception
- is thrown if an error occurs
-