Class BrotliEncoder

  • All Implemented Interfaces:
    io.netty.channel.ChannelHandler, io.netty.channel.ChannelOutboundHandler

    @Sharable
    public final class BrotliEncoder
    extends MessageToByteEncoder<io.netty.buffer.ByteBuf>
    Compress a ByteBuf with the Brotli compression.

    See brotli.

    • Nested Class Summary

      • Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler

        io.netty.channel.ChannelHandler.Sharable
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected io.netty.buffer.ByteBuf allocateBuffer​(io.netty.channel.ChannelHandlerContext ctx, io.netty.buffer.ByteBuf msg, boolean preferDirect)
      Allocate a ByteBuf which will be used as argument of #encode(ChannelHandlerContext, I, ByteBuf).
      void close​(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise)  
      protected void encode​(io.netty.channel.ChannelHandlerContext ctx, io.netty.buffer.ByteBuf msg, io.netty.buffer.ByteBuf out)
      Encode a message into a ByteBuf.
      void finish​(io.netty.channel.ChannelHandlerContext ctx)
      Finish the encoding, close streams and write final ByteBuf to the channel.
      void handlerAdded​(io.netty.channel.ChannelHandlerContext ctx)  
      void handlerRemoved​(io.netty.channel.ChannelHandlerContext ctx)  
      boolean isSharable()  
      • Methods inherited from class io.netty.channel.ChannelOutboundHandlerAdapter

        bind, connect, deregister, disconnect, flush, read
      • Methods inherited from class io.netty.channel.ChannelHandlerAdapter

        ensureNotSharable, exceptionCaught
      • Methods inherited from interface io.netty.channel.ChannelHandler

        exceptionCaught
    • Constructor Detail

      • BrotliEncoder

        public BrotliEncoder​(com.aayushatharva.brotli4j.encoder.Encoder.Parameters parameters)
        Create a new BrotliEncoder Instance and isSharable() set to true
        Parameters:
        parameters - Encoder.Parameters to use
      • BrotliEncoder

        public BrotliEncoder​(com.aayushatharva.brotli4j.encoder.Encoder.Parameters parameters,
                             boolean isSharable)

        Create a new BrotliEncoder Instance and specify whether this instance will be shared with multiple pipelines or not.

        If isSharable() is true then on handlerAdded(ChannelHandlerContext) call, a new BrotliEncoder.Writer will create, and it will be mapped using AttributeMap.attr(AttributeKey) so BrotliEncoder can be shared with multiple pipelines. This works fine but there on every encode(ChannelHandlerContext, ByteBuf, ByteBuf) call, we have to get the BrotliEncoder.Writer associated with the appropriate channel. And this will add a overhead. So it is recommended to set isSharable() to false and create new BrotliEncoder instance for every pipeline.
        Parameters:
        parameters - Encoder.Parameters to use
        isSharable - Set to true if this instance is shared else set to false
    • Method Detail

      • handlerAdded

        public void handlerAdded​(io.netty.channel.ChannelHandlerContext ctx)
                          throws Exception
        Specified by:
        handlerAdded in interface io.netty.channel.ChannelHandler
        Overrides:
        handlerAdded in class io.netty.channel.ChannelHandlerAdapter
        Throws:
        Exception
      • handlerRemoved

        public void handlerRemoved​(io.netty.channel.ChannelHandlerContext ctx)
                            throws Exception
        Specified by:
        handlerRemoved in interface io.netty.channel.ChannelHandler
        Overrides:
        handlerRemoved in class io.netty.channel.ChannelHandlerAdapter
        Throws:
        Exception
      • encode

        protected void encode​(io.netty.channel.ChannelHandlerContext ctx,
                              io.netty.buffer.ByteBuf msg,
                              io.netty.buffer.ByteBuf out)
                       throws Exception
        Description copied from class: MessageToByteEncoder
        Encode a message into a ByteBuf. This method will be called for each written message that can be handled by this encoder.
        Specified by:
        encode in class MessageToByteEncoder<io.netty.buffer.ByteBuf>
        Parameters:
        ctx - the ChannelHandlerContext which this MessageToByteEncoder belongs to
        msg - the message to encode
        out - the ByteBuf into which the encoded message will be written
        Throws:
        Exception - is thrown if an error occurs
      • allocateBuffer

        protected io.netty.buffer.ByteBuf allocateBuffer​(io.netty.channel.ChannelHandlerContext ctx,
                                                         io.netty.buffer.ByteBuf msg,
                                                         boolean preferDirect)
                                                  throws Exception
        Description copied from class: MessageToByteEncoder
        Allocate a ByteBuf which will be used as argument of #encode(ChannelHandlerContext, I, ByteBuf). Sub-classes may override this method to return ByteBuf with a perfect matching initialCapacity.
        Overrides:
        allocateBuffer in class MessageToByteEncoder<io.netty.buffer.ByteBuf>
        Throws:
        Exception
      • isSharable

        public boolean isSharable()
        Overrides:
        isSharable in class io.netty.channel.ChannelHandlerAdapter
      • finish

        public void finish​(io.netty.channel.ChannelHandlerContext ctx)
                    throws IOException
        Finish the encoding, close streams and write final ByteBuf to the channel.
        Parameters:
        ctx - ChannelHandlerContext which we want to close
        Throws:
        IOException - If an error occurred during closure
      • close

        public void close​(io.netty.channel.ChannelHandlerContext ctx,
                          io.netty.channel.ChannelPromise promise)
                   throws Exception
        Specified by:
        close in interface io.netty.channel.ChannelOutboundHandler
        Overrides:
        close in class io.netty.channel.ChannelOutboundHandlerAdapter
        Throws:
        Exception