Package io.netty.handler.codec
Class MessageToByteEncoder<I>
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelOutboundHandlerAdapter
io.netty.handler.codec.MessageToByteEncoder<I>
- All Implemented Interfaces:
ChannelHandler
,ChannelOutboundHandler
- Direct Known Subclasses:
ByteToMessageCodec.Encoder
,Bzip2Encoder
,CompatibleObjectEncoder
,FastLzFrameEncoder
,HAProxyMessageEncoder
,ObjectEncoder
,SnappyFrameEncoder
,Socks4ClientEncoder
,Socks4ServerEncoder
,Socks5ClientEncoder
,Socks5ServerEncoder
,SocksMessageEncoder
,TcpDnsQueryEncoder
,ZlibEncoder
ChannelOutboundHandlerAdapter
which encodes message in a stream-like fashion from one message to an
ByteBuf
.
Example implementation which encodes Integer
s to a ByteBuf
.
public class IntegerEncoder extendsMessageToByteEncoder
<Integer
> {@Override
public void encode(ChannelHandlerContext
ctx,Integer
msg,ByteBuf
out) throwsException
{ out.writeInt(msg); } }
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
seeMessageToByteEncoder(boolean)
withtrue
as boolean parameter.protected
MessageToByteEncoder
(boolean preferDirect) Create a new instance which will try to detect the types to match out of the type parameter of the class.protected
MessageToByteEncoder
(Class<? extends I> outboundMessageType) seeMessageToByteEncoder(Class, boolean)
withtrue
as boolean value.protected
MessageToByteEncoder
(Class<? extends I> outboundMessageType, boolean preferDirect) Create a new instance -
Method Summary
Modifier and TypeMethodDescriptionboolean
Returnstrue
if the given message should be handled.protected ByteBuf
allocateBuffer
(ChannelHandlerContext ctx, I msg, boolean preferDirect) Allocate aByteBuf
which will be used as argument ofencode(ChannelHandlerContext, I, ByteBuf)
.protected abstract void
encode
(ChannelHandlerContext ctx, I msg, ByteBuf out) Encode a message into aByteBuf
.protected boolean
void
write
(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.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
-
preferDirect
private final boolean preferDirect
-
-
Constructor Details
-
MessageToByteEncoder
protected MessageToByteEncoder()seeMessageToByteEncoder(boolean)
withtrue
as boolean parameter. -
MessageToByteEncoder
seeMessageToByteEncoder(Class, boolean)
withtrue
as boolean value. -
MessageToByteEncoder
protected MessageToByteEncoder(boolean preferDirect) Create a new instance which will try to detect the types to match out of the type parameter of the class. -
MessageToByteEncoder
Create a new instance
-
-
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
-
allocateBuffer
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, I msg, boolean preferDirect) throws Exception Allocate aByteBuf
which will be used as argument ofencode(ChannelHandlerContext, I, ByteBuf)
. Sub-classes may override this method to returnByteBuf
with a perfect matchinginitialCapacity
.- Throws:
Exception
-
encode
Encode a message into aByteBuf
. This method will be called for each written message that can be handled by this encoder.- Parameters:
ctx
- theChannelHandlerContext
which thisMessageToByteEncoder
belongs tomsg
- the message to encodeout
- theByteBuf
into which the encoded message will be written- Throws:
Exception
- is thrown if an error occurs
-
isPreferDirect
protected boolean isPreferDirect()
-