- 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
ChannelHandlerwhich encodes message in a stream-like fashion from one message to aBuffer.Example implementation which encodes
Integers to aBuffer.public class IntegerEncoder extendsMessageToByteEncoder<Integer> {@Overridepublic void encode(ChannelHandlerContextctx,Integermsg,Bufferout) throwsException{ out.writeInt(msg); } }
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedMessageToByteEncoder()Create a new instance which will try to detect the types to match out of the type parameter of the class.protectedMessageToByteEncoder(Class<? extends I> outboundMessageType)Create a new instance.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanacceptOutboundMessage(Object msg)Returnstrueif the given message should be handled.protected abstract BufferallocateBuffer(ChannelHandlerContext ctx, I msg)Allocate aBufferwhich will be used as argument of#encode(ChannelHandlerContext, I, Buffer).protected abstract voidencode(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
Returnstrueif the given message should be handled. Iffalseit will be passed to the nextChannelHandlerin theChannelPipeline.- Throws:
Exception
-
write
public Future<Void> write(ChannelHandlerContext ctx, Object msg)
Description copied from interface:ChannelHandlerCalled once a write operation is made. The write operation will write the messages through theChannelPipeline. Those are then ready to be flushed to the actualChannelonceChannel.flush()is called.- Parameters:
ctx- theChannelHandlerContextfor which the write operation is mademsg- the message to write- Returns:
- the
Futurewhich will be notified once the operation completes.
-
allocateBuffer
protected abstract Buffer allocateBuffer(ChannelHandlerContext ctx, I msg) throws Exception
Allocate aBufferwhich will be used as argument of#encode(ChannelHandlerContext, I, Buffer).- Parameters:
ctx- theChannelHandlerContextwhich thisMessageToByteEncoderbelongs 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- theChannelHandlerContextwhich thisMessageToByteEncoderbelongs tomsg- the message to encodeout- theBufferinto which the encoded message will be written- Throws:
Exception- is thrown if an error occurs
-
-