- java.lang.Object
-
- io.netty5.channel.ChannelHandlerAdapter
-
- io.netty5.handler.codec.MessageToByteEncoder<I>
-
- All Implemented Interfaces:
ChannelHandler
- Direct Known Subclasses:
TcpDnsQueryEncoder
public abstract class MessageToByteEncoder<I> extends ChannelHandlerAdapter
ChannelHandler
which encodes message in a stream-like fashion from one message to aBuffer
.Example implementation which encodes
Integer
s to aBuffer
.public class IntegerEncoder extends
MessageToByteEncoder
<Integer
> {@Override
public void encode(ChannelHandlerContext
ctx,Integer
msg,Buffer
out) throwsException
{ out.writeInt(msg); } }
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
MessageToByteEncoder()
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)
Create a new instance.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
acceptOutboundMessage(Object msg)
Returnstrue
if the given message should be handled.protected abstract Buffer
allocateBuffer(ChannelHandlerContext ctx, I msg)
Allocate aBuffer
which will be used as argument of#encode(ChannelHandlerContext, I, Buffer)
.protected abstract void
encode(ChannelHandlerContext ctx, I msg, Buffer out)
Encode a message into aBuffer
.Future<Void>
write(ChannelHandlerContext ctx, Object msg)
Called once a write operation is made.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty5.channel.ChannelHandler
bind, channelActive, channelExceptionCaught, channelInactive, channelInboundEvent, channelRead, channelReadComplete, channelRegistered, channelShutdown, channelUnregistered, channelWritabilityChanged, close, connect, deregister, disconnect, flush, handlerAdded, handlerRemoved, isSharable, pendingOutboundBytes, read, register, sendOutboundEvent, shutdown
-
-
-
-
Method Detail
-
acceptOutboundMessage
public boolean acceptOutboundMessage(Object msg) throws Exception
Returnstrue
if the given message should be handled. Iffalse
it will be passed to the nextChannelHandler
in theChannelPipeline
.- Throws:
Exception
-
write
public Future<Void> write(ChannelHandlerContext ctx, Object msg)
Description copied from interface:ChannelHandler
Called once a write operation is made. The write operation will write the messages through theChannelPipeline
. Those are then ready to be flushed to the actualChannel
onceChannel.flush()
is called.- Parameters:
ctx
- theChannelHandlerContext
for which the write operation is mademsg
- the message to write- Returns:
- the
Future
which will be notified once the operation completes.
-
allocateBuffer
protected abstract Buffer allocateBuffer(ChannelHandlerContext ctx, I msg) throws Exception
Allocate aBuffer
which will be used as argument of#encode(ChannelHandlerContext, I, Buffer)
.- Parameters:
ctx
- theChannelHandlerContext
which thisMessageToByteEncoder
belongs tomsg
- the message to be encoded- Throws:
Exception
-
encode
protected abstract void encode(ChannelHandlerContext ctx, I msg, Buffer out) throws Exception
Encode a message into aBuffer
. 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
- theBuffer
into which the encoded message will be written- Throws:
Exception
- is thrown if an error occurs
-
-