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

public abstract Class HttpObjectEncoder<H extends HttpMessage>

extends MessageToMessageEncoder<Object>
Class Inheritance
Known Direct Subclasses
io.netty.handler.codec.http.HttpRequestEncoder, io.netty.handler.codec.http.HttpResponseEncoder, io.netty.handler.codec.rtsp.RtspEncoder, io.netty.handler.codec.rtsp.RtspObjectEncoder
Imports
io.netty.buffer.ByteBuf, .ByteBufUtil, .Unpooled, io.netty.channel.ChannelHandlerContext, .ChannelPromise, .FileRegion, io.netty.handler.codec.EncoderException, .MessageToMessageEncoder, io.netty.util.CharsetUtil, .ReferenceCountUtil, io.netty.util.concurrent.PromiseCombiner, io.netty.util.internal.StringUtil, java.util.ArrayList, .Iterator, .List, .Map.Entry

Encodes an HttpMessage or an HttpContent into a ByteBuf.

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 int
private static final ByteBuf
pack-priv static final int
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 final List<Object>
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 ByteBuf
private static final int

Constructor Summary

AccessConstructor and Description
public

Method Summary

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

Overrides io.netty.handler.codec.MessageToMessageEncoder.acceptOutboundMessage.

Returns true if the given message should be handled.
private static void
addEncodedLengthHex(ChannelHandlerContext ctx, long contentLength, List<Object> out)

private static boolean
private static boolean
checkContentState(int state)

protected void
encode(ChannelHandlerContext ctx, Object
the message to encode to an other one
msg
,
List<Object>
the List into which the encoded msg should be added needs to do some kind of aggregation
out
)

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

Encode from one message to an other.
protected static void
encodeAscii(String s, ByteBuf buf)

Deprecated
private void
encodeByteBufAndTrailers(int state, ChannelHandlerContext ctx, List<Object> out, ByteBuf content, HttpHeaders trailingHeaders)

private void
private void
encodeByteBufHttpContent(int state, ChannelHandlerContext ctx, ByteBuf buf, ByteBuf content, HttpHeaders trailingHeaders, List<Object> out)

private void
private static boolean
private static void
private static int
private void
private void
protected void
encodeHeaders(HttpHeaders headers, ByteBuf buf)

Encode the HttpHeaders into a ByteBuf.

private void
private void
private void
private ByteBuf
protected abstract void
encodeInitialLine(ByteBuf buf, H message)

private void
private void
private void
private void
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.

private static void
public void
write(ChannelHandlerContext
the ChannelHandlerContext for which the write operation is made
ctx
,
Object
the message to write
msg
,
ChannelPromise
the ChannelPromise to notify once the operation completes
promise
)

Overrides io.netty.handler.codec.MessageToMessageEncoder.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Calls ChannelHandlerContext#write(Object, ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline.
private static void
private static void
private static void

Field Detail

COPY_CONTENT_THRESHOLDback to summary
private static final int COPY_CONTENT_THRESHOLD
CRLF_BUFback to summary
private static final ByteBuf CRLF_BUF
CRLF_SHORTback to summary
pack-priv static final int CRLF_SHORT
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.

outback to summary
private final List<Object> out
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_CRLF_BUFback to summary
private static final ByteBuf ZERO_CRLF_CRLF_BUF
ZERO_CRLF_MEDIUMback to summary
private static final int ZERO_CRLF_MEDIUM

Constructor Detail

HttpObjectEncoderback to summary
public HttpObjectEncoder()

Method Detail

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

Overrides io.netty.handler.codec.MessageToMessageEncoder.acceptOutboundMessage.

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

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

Annotations
@Override
@SuppressWarnings:ConditionCoveredByFurtherCondition
addEncodedLengthHexback to summary
private static void addEncodedLengthHex(ChannelHandlerContext ctx, long contentLength, List<Object> out)
bypassEncoderIfEmptyback to summary
private static boolean bypassEncoderIfEmpty(ByteBuf msg, List<Object> out)
checkContentStateback to summary
private static boolean checkContentState(int state)
encodeback to summary
protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception

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

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

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

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this MessageToMessageEncoder belongs to

msg:Object

the message to encode to an other one

out:List<Object>

the List into which the encoded msg should be added needs to do some kind of aggregation

Annotations
@Override
@SuppressWarnings:ConditionCoveredByFurtherCondition
Exceptions
Exception:
is thrown if an error occurs
encodeAsciiback to summary
protected static void encodeAscii(String s, ByteBuf buf)

Deprecated

Annotations
@Deprecated
encodeByteBufAndTrailersback to summary
private void encodeByteBufAndTrailers(int state, ChannelHandlerContext ctx, List<Object> out, ByteBuf content, HttpHeaders trailingHeaders)
encodeByteBufContentback to summary
private void encodeByteBufContent(ChannelHandlerContext ctx, ByteBuf content, List<Object> out)
encodeByteBufHttpContentback to summary
private void encodeByteBufHttpContent(int state, ChannelHandlerContext ctx, ByteBuf buf, ByteBuf content, HttpHeaders trailingHeaders, List<Object> out)
encodeChunkedHttpContentback to summary
private void encodeChunkedHttpContent(ChannelHandlerContext ctx, ByteBuf content, HttpHeaders trailingHeaders, List<Object> out)
encodeContentNonChunkback to summary
private static boolean encodeContentNonChunk(List<Object> out, ByteBuf buf, ByteBuf content)
encodedChunkedFileRegionContentback to summary
private static void encodedChunkedFileRegionContent(ChannelHandlerContext ctx, FileRegion msg, List<Object> out)
encodeEmptyLastHttpContentback to summary
private static int encodeEmptyLastHttpContent(int state, List<Object> out)
encodeFileRegionContentback to summary
private void encodeFileRegionContent(ChannelHandlerContext ctx, FileRegion msg, List<Object> out)
encodeFullHttpMessageback to summary
private void encodeFullHttpMessage(ChannelHandlerContext ctx, Object o, List<Object> out) throws Exception
encodeHeadersback to summary
protected void encodeHeaders(HttpHeaders headers, ByteBuf buf)

Encode the HttpHeaders into a ByteBuf.

encodeHttpContentback to summary
private void encodeHttpContent(ChannelHandlerContext ctx, HttpContent msg, List<Object> out)
encodeHttpMessageLastContentback to summary
private void encodeHttpMessageLastContent(ChannelHandlerContext ctx, H m, List<Object> out) throws Exception
encodeHttpMessageNotLastContentback to summary
private void encodeHttpMessageNotLastContent(ChannelHandlerContext ctx, H m, List<Object> out) throws Exception
encodeInitHttpMessageback to summary
private ByteBuf encodeInitHttpMessage(ChannelHandlerContext ctx, H m) throws Exception
encodeInitialLineback to summary
protected abstract void encodeInitialLine(ByteBuf buf, H message) throws Exception
encodeJustHttpMessageback to summary
private void encodeJustHttpMessage(ChannelHandlerContext ctx, H m, List<Object> out) throws Exception
encodeLastHttpContentback to summary
private void encodeLastHttpContent(ChannelHandlerContext ctx, LastHttpContent msg, List<Object> out)
encodeNotHttpMessageContentTypesback to summary
private void encodeNotHttpMessageContentTypes(ChannelHandlerContext ctx, Object msg, List<Object> out)
Annotations
@SuppressWarnings:ConditionCoveredByFurtherCondition
encodeTrailingHeadersback to summary
private void encodeTrailingHeaders(ChannelHandlerContext ctx, HttpHeaders trailingHeaders, List<Object> out)
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.

throwUnexpectedMessageTypeExback to summary
private static void throwUnexpectedMessageTypeEx(Object msg, int state)
writeback to summary
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception

Overrides io.netty.handler.codec.MessageToMessageEncoder.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Doc from io.netty.channel.ChannelOutboundHandlerAdapter.write.

Calls ChannelHandlerContext#write(Object, 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.write.

the ChannelHandlerContext for which the write operation is made

msg:Object

Doc from io.netty.channel.ChannelOutboundHandler.write.

the message to write

promise:ChannelPromise

Doc from io.netty.channel.ChannelOutboundHandler.write.

the ChannelPromise to notify once the operation completes

Annotations
@Override
Exceptions
Exception:

Doc from io.netty.channel.ChannelOutboundHandler.write.

thrown if an error occurs

writeOutListback to summary
private static void writeOutList(ChannelHandlerContext ctx, List<Object> out, ChannelPromise promise)
writePromiseCombinerback to summary
private static void writePromiseCombiner(ChannelHandlerContext ctx, List<Object> out, ChannelPromise promise)
writeVoidPromiseback to summary
private static void writeVoidPromise(ChannelHandlerContext ctx, List<Object> out)