Top Description Fields Constructors Methods
io.netty5.handler.codec.http

public abstract Class HttpObjectEncoder<H extends HttpMessage>

extends MessageToMessageEncoder<Object>
Class Inheritance
Known Direct Subclasses
io.netty5.handler.codec.http.HttpRequestEncoder, io.netty5.handler.codec.http.HttpResponseEncoder, io.netty5.handler.codec.rtsp.RtspEncoder
Imports
io.netty5.buffer.Buffer, .BufferAllocator, io.netty5.channel.ChannelHandlerContext, .FileRegion, io.netty5.handler.codec.MessageToMessageEncoder, io.netty5.handler.codec.http.headers.HttpHeaders, io.netty5.util.Resource, io.netty5.util.internal.StringUtil, java.nio.charset.StandardCharsets, java.util.List, .Map.Entry, java.util.function.Supplier

Encodes an HttpMessage or an HttpContent into a Buffer.

Extensibility

Please note that this encoder is designed to be extended to implement a protocol derived from HTTP, such as RTSP and ICAP. To implement the encoder of such a derived protocol, extend this class and implement all abstract methods properly.

Field Summary

Modifier and TypeField and Description
private static final byte[]
pack-priv static final short
private Supplier<Buffer>
private static final float
private static final float
private float
headersEncodedSizeAccumulator

Used to calculate an exponential moving average of the encoded size of the initial line and the headers for a guess for future buffer allocations.

private static final int
private static final int
private static final int
private static final int
private int
private static final float
private static final float
private float
trailersEncodedSizeAccumulator

Used to calculate an exponential moving average of the encoded size of the trailers for a guess for future buffer allocations.

private static final byte[]
private static final int
private Supplier<Buffer>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
acceptOutboundMessage(Object msg)

Overrides io.netty5.handler.codec.MessageToMessageEncoder.acceptOutboundMessage.

Returns true if the given message should be handled.
private static long
protected Buffer
private static Object
protected void
encodeAndClose(ChannelHandlerContext ctx, Object
the message to encode to another one.
msg
,
List<Object>
the List into which produced output messages should be added.
out
)

Overrides io.netty5.handler.codec.MessageToMessageEncoder.encodeAndClose.

Encode from one message to another.
private void
encodeChunkedContent(ChannelHandlerContext ctx, Object msg, long contentLength, List<Object> out)

protected void
encodeHeaders(HttpHeaders headers, Buffer buf)

Encode the HttpHeaders into a Buffer.

protected abstract void
encodeInitialLine(Buffer buf, H message)

protected boolean

Returns:

true to signal the message has no content
isContentAlwaysEmpty
(H
the message to test
msg
)

Determine whether a message has a content or not.

private static int

Returns:

The readableBytes with some additional padding.
padSizeForAccumulation
(int
The readable bytes in the buffer.
readableBytes
)

Add some additional overhead to the buffer.

protected void
sanitizeHeadersBeforeEncode(H msg, boolean isAlwaysEmpty)

Allows to sanitize headers of the message before encoding these.

protected Buffer
Inherited from io.netty5.handler.codec.MessageToMessageEncoder:
encodewrite

Field Detail

CRLFback to summary
private static final byte[] CRLF
CRLF_SHORTback to summary
pack-priv static final short CRLF_SHORT
crlfBufferSupplierback to summary
private Supplier<Buffer> crlfBufferSupplier
HEADERS_WEIGHT_HISTORICALback to summary
private static final float HEADERS_WEIGHT_HISTORICAL
HEADERS_WEIGHT_NEWback to summary
private static final float HEADERS_WEIGHT_NEW
headersEncodedSizeAccumulatorback to summary
private float headersEncodedSizeAccumulator

Used to calculate an exponential moving average of the encoded size of the initial line and the headers for a guess for future buffer allocations.

ST_CONTENT_ALWAYS_EMPTYback to summary
private static final int ST_CONTENT_ALWAYS_EMPTY
ST_CONTENT_CHUNKback to summary
private static final int ST_CONTENT_CHUNK
ST_CONTENT_NON_CHUNKback to summary
private static final int ST_CONTENT_NON_CHUNK
ST_INITback to summary
private static final int ST_INIT
stateback to summary
private int state
Annotations
@SuppressWarnings:RedundantFieldInitialization
TRAILERS_WEIGHT_HISTORICALback to summary
private static final float TRAILERS_WEIGHT_HISTORICAL
TRAILERS_WEIGHT_NEWback to summary
private static final float TRAILERS_WEIGHT_NEW
trailersEncodedSizeAccumulatorback to summary
private float trailersEncodedSizeAccumulator

Used to calculate an exponential moving average of the encoded size of the trailers for a guess for future buffer allocations.

ZERO_CRLF_CRLFback to summary
private static final byte[] ZERO_CRLF_CRLF
ZERO_CRLF_MEDIUMback to summary
private static final int ZERO_CRLF_MEDIUM
zeroCrlfCrlfBufferSupplierback to summary
private Supplier<Buffer> zeroCrlfCrlfBufferSupplier

Constructor Detail

HttpObjectEncoderback to summary
public HttpObjectEncoder()

Method Detail

acceptOutboundMessageback to summary
public boolean acceptOutboundMessage(Object msg) throws Exception

Overrides io.netty5.handler.codec.MessageToMessageEncoder.acceptOutboundMessage.

Doc from io.netty5.handler.codec.MessageToMessageEncoder.acceptOutboundMessage.

Returns true if the given message should be handled. If false it will be passed to the next ChannelHandler in the ChannelPipeline.

Annotations
@Override
contentLengthback to summary
private static long contentLength(Object msg)
crlfBufferback to summary
protected Buffer crlfBuffer(BufferAllocator allocator)
encodeback to summary
private static Object encode(Object msg)
encodeAndCloseback to summary
protected void encodeAndClose(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception

Overrides io.netty5.handler.codec.MessageToMessageEncoder.encodeAndClose.

Doc from io.netty5.handler.codec.MessageToMessageEncoder.encodeAndClose.

Encode from one message to another. This method will be called for each written message that can be handled by this encoder.

The message will not be automatically disposed of after this call. Instead, the responsibility of ensuring that messages are disposed of falls upon the implementor of this method.

Subclasses that wish to have incoming messages automatically disposed of should instead override the encode(ChannelHandlerContext, Object, List) method.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this MessageToMessageEncoder belongs to.

msg:Object

the message to encode to another one.

out:List<Object>

the List into which produced output messages should be added.

Annotations
@Override
Exceptions
Exception:
is thrown if an error occurs.
encodeChunkedContentback to summary
private void encodeChunkedContent(ChannelHandlerContext ctx, Object msg, long contentLength, List<Object> out)
encodeHeadersback to summary
protected void encodeHeaders(HttpHeaders headers, Buffer buf)

Encode the HttpHeaders into a Buffer.

encodeInitialLineback to summary
protected abstract void encodeInitialLine(Buffer buf, H message) throws Exception
isContentAlwaysEmptyback to summary
protected boolean isContentAlwaysEmpty(H msg)

Determine whether a message has a content or not. Some message may have headers indicating a content without having an actual content, e.g the response to an HEAD or CONNECT request.

Parameters
msg:H

the message to test

Returns:boolean

true to signal the message has no content

padSizeForAccumulationback to summary
private static int padSizeForAccumulation(int readableBytes)

Add some additional overhead to the buffer. The rational is that it is better to slightly over allocate and waste some memory, rather than under allocate and require a resize/copy.

Parameters
readableBytes:int

The readable bytes in the buffer.

Returns:int

The readableBytes with some additional padding.

sanitizeHeadersBeforeEncodeback to summary
protected void sanitizeHeadersBeforeEncode(H msg, boolean isAlwaysEmpty)

Allows to sanitize headers of the message before encoding these.

zeroCrlfCrlfBufferback to summary
protected Buffer zeroCrlfCrlfBuffer(BufferAllocator allocator)