Top Description Inners Fields Constructors Methods
io.netty.handler.codec.compression

public final Class BrotliEncoder

extends MessageToByteEncoder<ByteBuf>
Class Inheritance
Annotations
@Sharable
Imports
com.aayushatharva.brotli4j.encoder.BrotliEncoderChannel, .Encoder, io.netty.buffer.ByteBuf, .Unpooled, io.netty.channel.Channel, .ChannelFuture, .ChannelHandler, .ChannelHandlerContext, .ChannelPromise, io.netty.handler.codec.MessageToByteEncoder, io.netty.util.AttributeKey, .ReferenceCountUtil, io.netty.util.internal.ObjectUtil, java.io.IOException, java.nio.ByteBuffer, java.nio.channels.ClosedChannelException, .WritableByteChannel

Compress a ByteBuf with the Brotli compression.

See brotli.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
BrotliEncoder.Writer

Writer is the implementation of WritableByteChannel which encodes Brotli data and stores it into ByteBuf.

Field Summary

Modifier and TypeField and Description
private static final AttributeKey<BrotliEncoder.Writer>
private final boolean
private final Encoder.Parameters
private BrotliEncoder.Writer

Constructor Summary

AccessConstructor and Description
public
BrotliEncoder()

Create a new BrotliEncoder Instance with BrotliOptions#DEFAULT and isSharable() set to true

public
BrotliEncoder(BrotliOptions
BrotliOptions to use and isSharable() set to true
brotliOptions
)

Create a new BrotliEncoder Instance

public
BrotliEncoder(Encoder.Parameters parameters)

Create a new BrotliEncoder Instance and isSharable() set to true

public
BrotliEncoder(Encoder.Parameters parameters, boolean
Set to true if this instance is shared else set to false
isSharable
)

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

Method Summary

Modifier and TypeMethod and Description
protected ByteBuf
public void
close(final ChannelHandlerContext
the ChannelHandlerContext for which the close operation is made
ctx
,
final ChannelPromise
the ChannelPromise to notify once the operation completes
promise
)

Overrides io.netty.channel.ChannelOutboundHandlerAdapter.close.

Implements io.netty.channel.ChannelOutboundHandler.close.

Calls ChannelHandlerContext#close(ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline.
protected void
encode(ChannelHandlerContext ctx, ByteBuf
the message to encode
msg
,
ByteBuf
the ByteBuf into which the encoded message will be written
out
)

Implements abstract io.netty.handler.codec.MessageToByteEncoder.encode.

Encode a message into a ByteBuf.
public void
finish(ChannelHandlerContext
ChannelHandlerContext which we want to close
ctx
)

Finish the encoding, close streams and write final ByteBuf to the channel.

private ChannelFuture
public void
handlerAdded(ChannelHandlerContext ctx)

Overrides io.netty.channel.ChannelHandlerAdapter.handlerAdded.

Implements io.netty.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.
public void
handlerRemoved(ChannelHandlerContext ctx)

Overrides io.netty.channel.ChannelHandlerAdapter.handlerRemoved.

Implements io.netty.channel.ChannelHandler.handlerRemoved.

Gets called after the ChannelHandler was removed from the actual context and it doesn't handle events anymore.
public boolean
isSharable()

Overrides io.netty.channel.ChannelHandlerAdapter.isSharable.

Return true if the implementation is Sharable and so can be added to different ChannelPipelines.
Inherited from io.netty.handler.codec.MessageToByteEncoder:
acceptOutboundMessageisPreferDirectwrite

Field Detail

ATTRback to summary
private static final AttributeKey<BrotliEncoder.Writer> ATTR
isSharableback to summary
private final boolean isSharable
parametersback to summary
private final Encoder.Parameters parameters
writerback to summary
private BrotliEncoder.Writer writer

Constructor Detail

BrotliEncoderback to summary
public BrotliEncoder()

Create a new BrotliEncoder Instance with BrotliOptions#DEFAULT and isSharable() set to true

BrotliEncoderback to summary
public BrotliEncoder(BrotliOptions brotliOptions)

Create a new BrotliEncoder Instance

Parameters
brotliOptions:BrotliOptions

BrotliOptions to use and isSharable() set to true

BrotliEncoderback to summary
public BrotliEncoder(Encoder.Parameters parameters)

Create a new BrotliEncoder Instance and isSharable() set to true

Parameters
parameters:Encoder.Parameters

Encoder.Parameters to use

BrotliEncoderback to summary
public BrotliEncoder(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 Writer will create, and it will be mapped using Channel#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 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

Encoder.Parameters to use

isSharable:boolean

Set to true if this instance is shared else set to false

Method Detail

allocateBufferback to summary
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect) throws Exception

Overrides io.netty.handler.codec.MessageToByteEncoder.allocateBuffer.

Doc from io.netty.handler.codec.MessageToByteEncoder.allocateBuffer.

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.

Annotations
@Override
closeback to summary
public void close(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception

Overrides io.netty.channel.ChannelOutboundHandlerAdapter.close.

Implements io.netty.channel.ChannelOutboundHandler.close.

Doc from io.netty.channel.ChannelOutboundHandlerAdapter.close.

Calls ChannelHandlerContext#close(ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Parameters
ctx:ChannelHandlerContext

Doc from io.netty.channel.ChannelOutboundHandler.close.

the ChannelHandlerContext for which the close operation is made

promise:ChannelPromise

Doc from io.netty.channel.ChannelOutboundHandler.close.

the ChannelPromise to notify once the operation completes

Annotations
@Override
Exceptions
Exception:

Doc from io.netty.channel.ChannelOutboundHandler.close.

thrown if an error occurs

encodeback to summary
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception

Implements abstract io.netty.handler.codec.MessageToByteEncoder.encode.

Doc from io.netty.handler.codec.MessageToByteEncoder.encode.

Encode a message into a ByteBuf. This method will be called for each written message that can be handled by this encoder.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this MessageToByteEncoder belongs to

msg:ByteBuf

the message to encode

out:ByteBuf

the ByteBuf into which the encoded message will be written

Annotations
@Override
Exceptions
Exception:
is thrown if an error occurs
finishback to summary
public void finish(ChannelHandlerContext ctx) throws IOException

Finish the encoding, close streams and write final ByteBuf to the channel.

Parameters
ctx:ChannelHandlerContext

ChannelHandlerContext which we want to close

Exceptions
IOException:
If an error occurred during closure
finishEncodeback to summary
private ChannelFuture finishEncode(ChannelHandlerContext ctx, ChannelPromise promise) throws IOException
handlerAddedback to summary
public void handlerAdded(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelHandlerAdapter.handlerAdded.

Implements io.netty.channel.ChannelHandler.handlerAdded.

Doc from io.netty.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.

Annotations
@Override
handlerRemovedback to summary
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelHandlerAdapter.handlerRemoved.

Implements io.netty.channel.ChannelHandler.handlerRemoved.

Doc from io.netty.channel.ChannelHandler.handlerRemoved.

Gets called after the ChannelHandler was removed from the actual context and it doesn't handle events anymore.

Annotations
@Override
isSharableback to summary
public boolean isSharable()

Overrides io.netty.channel.ChannelHandlerAdapter.isSharable.

Doc from io.netty.channel.ChannelHandlerAdapter.isSharable.

Return true if the implementation is Sharable and so can be added to different ChannelPipelines.

Annotations
@Override
io.netty.handler.codec.compression back to summary

private final Class BrotliEncoder.Writer

extends Object
implements WritableByteChannel
Class Inheritance
All Implemented Interfaces
java.nio.channels.WritableByteChannel, java.nio.channels.Channel, java.io.Closeable, java.lang.AutoCloseable

Writer is the implementation of WritableByteChannel which encodes Brotli data and stores it into ByteBuf.

Field Summary

Modifier and TypeField and Description
private final BrotliEncoderChannel
private final ChannelHandlerContext
private boolean
private ByteBuf

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private void
allocate(boolean preferDirect)

public void
close()

Implements java.nio.channels.Channel.close.

Closes this channel.
private void
encode(ByteBuf msg, boolean preferDirect)

public void
finish(final ChannelPromise promise)

public boolean
isOpen()

Implements java.nio.channels.Channel.isOpen.

Tells whether or not this channel is open.
public int
write(ByteBuffer
The buffer from which bytes are to be retrieved
src
)

Implements java.nio.channels.WritableByteChannel.write.

Writes a sequence of bytes to this channel from the given buffer.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

brotliEncoderChannelback to summary
private final BrotliEncoderChannel brotliEncoderChannel
ctxback to summary
private final ChannelHandlerContext ctx
isClosedback to summary
private boolean isClosed
writableBufferback to summary
private ByteBuf writableBuffer

Constructor Detail

Writerback to summary
private Writer(Encoder.Parameters parameters, ChannelHandlerContext ctx) throws IOException

Method Detail

allocateback to summary
private void allocate(boolean preferDirect)
closeback to summary
public void close()

Implements java.nio.channels.Channel.close.

Doc from java.nio.channels.Channel.close.

Closes this channel.

After a channel is closed, any further attempt to invoke I/O operations upon it will cause a ClosedChannelException to be thrown.

If this channel is already closed then invoking this method has no effect.

This method may be invoked at any time. If some other thread has already invoked it, however, then another invocation will block until the first invocation is complete, after which it will return without effect.

Annotations
@Override
encodeback to summary
private void encode(ByteBuf msg, boolean preferDirect) throws Exception
finishback to summary
public void finish(final ChannelPromise promise) throws IOException
isOpenback to summary
public boolean isOpen()

Implements java.nio.channels.Channel.isOpen.

Doc from java.nio.channels.Channel.isOpen.

Tells whether or not this channel is open.

Returns:boolean

true if, and only if, this channel is open

Annotations
@Override
writeback to summary
public int write(ByteBuffer src) throws IOException

Implements java.nio.channels.WritableByteChannel.write.

Doc from java.nio.channels.WritableByteChannel.write.

Writes a sequence of bytes to this channel from the given buffer.

An attempt is made to write up to r bytes to the channel, where r is the number of bytes remaining in the buffer, that is, src.remaining(), at the moment this method is invoked.

Suppose that a byte sequence of length n is written, where 0 <= n <= r. This byte sequence will be transferred from the buffer starting at index p, where p is the buffer's position at the moment this method is invoked; the index of the last byte written will be p + n - 1. Upon return the buffer's position will be equal to p + n; its limit will not have changed.

Unless otherwise specified, a write operation will return only after writing all of the r requested bytes. Some types of channels, depending upon their state, may write only some of the bytes or possibly none at all. A socket channel in non-blocking mode, for example, cannot write any more bytes than are free in the socket's output buffer.

This method may be invoked at any time. If another thread has already initiated a write operation upon this channel, however, then an invocation of this method will block until the first operation is complete.

Parameters
src:ByteBuffer

The buffer from which bytes are to be retrieved

Returns:int

The number of bytes written, possibly zero

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs