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

public Class LineBasedFrameDecoder

extends ByteToMessageDecoder
Class Inheritance
Imports
io.netty.buffer.ByteBuf, io.netty.channel.ChannelHandlerContext, io.netty.util.ByteProcessor, java.util.List

A decoder that splits the received ByteBufs on line endings.

Both "\n" and "\r\n" are handled.

The byte stream is expected to be in UTF-8 character encoding or ASCII. The current implementation uses direct byte to char cast and then compares that char to a few low range ASCII characters like '\n' or '\r'. UTF-8 is not using low range [0..0x7F] byte values for multibyte codepoint representations therefore fully supported by this implementation.

For a more general delimiter-based decoder, see DelimiterBasedFrameDecoder.

Field Summary

Modifier and TypeField and Description
private int
private boolean
discarding

True if we're discarding input because we're already over maxLength.

private final boolean
failFast

Whether or not to throw an exception as soon as we exceed maxLength.

private final int
maxLength

Maximum length of a frame we're willing to decode.

private int
offset

Last scan position.

private final boolean
Inherited from io.netty.handler.codec.ByteToMessageDecoder:
COMPOSITE_CUMULATORcumulationMERGE_CUMULATOR

Constructor Summary

AccessConstructor and Description
public
LineBasedFrameDecoder(final int
the maximum length of the decoded frame. A TooLongFrameException is thrown if the length of the frame exceeds this value.
maxLength
)

Creates a new decoder.

public
LineBasedFrameDecoder(final int
the maximum length of the decoded frame. A TooLongFrameException is thrown if the length of the frame exceeds this value.
maxLength
,
final boolean
whether the decoded frame should strip out the delimiter or not
stripDelimiter
,
final boolean
If true, a TooLongFrameException is thrown as soon as the decoder notices the length of the frame will exceed maxFrameLength regardless of whether the entire frame has been read. If false, a TooLongFrameException is thrown after the entire frame that exceeds maxFrameLength has been read.
failFast
)

Creates a new decoder.

Method Summary

Modifier and TypeMethod and Description
protected final void
decode(ChannelHandlerContext ctx, ByteBuf
the ByteBuf from which to read data
in
,
List<Object>
the List to which decoded messages should be added
out
)

Implements abstract io.netty.handler.codec.ByteToMessageDecoder.decode.

Decode the from one ByteBuf to an other.
protected Object

Returns:

frame the ByteBuf which represent the frame or null if no frame could be created.
decode
(ChannelHandlerContext ctx, ByteBuf
the ByteBuf from which to read data
buffer
)

Create a frame out of the ByteBuf and return it.

private void
fail(final ChannelHandlerContext ctx, int length)

private void
fail(final ChannelHandlerContext ctx, String length)

private int
findEndOfLine(final ByteBuf buffer)

Returns the index in the buffer of the end of line found.

Inherited from io.netty.handler.codec.ByteToMessageDecoder:
actualReadableBytescallDecodechannelInactivechannelInputClosedchannelReadchannelReadCompletedecodeLastdecodeRemovalReentryProtectiondiscardSomeReadBytesexpandCumulationfireChannelReadfireChannelReadhandlerRemovedhandlerRemoved0internalBufferisSingleDecodesetCumulatorsetDiscardAfterReadssetSingleDecodeuserEventTriggered

Field Detail

discardedBytesback to summary
private int discardedBytes
discardingback to summary
private boolean discarding

True if we're discarding input because we're already over maxLength.

failFastback to summary
private final boolean failFast

Whether or not to throw an exception as soon as we exceed maxLength.

maxLengthback to summary
private final int maxLength

Maximum length of a frame we're willing to decode.

offsetback to summary
private int offset

Last scan position.

stripDelimiterback to summary
private final boolean stripDelimiter

Constructor Detail

LineBasedFrameDecoderback to summary
public LineBasedFrameDecoder(final int maxLength)

Creates a new decoder.

Parameters
maxLength:int

the maximum length of the decoded frame. A TooLongFrameException is thrown if the length of the frame exceeds this value.

LineBasedFrameDecoderback to summary
public LineBasedFrameDecoder(final int maxLength, final boolean stripDelimiter, final boolean failFast)

Creates a new decoder.

Parameters
maxLength:int

the maximum length of the decoded frame. A TooLongFrameException is thrown if the length of the frame exceeds this value.

stripDelimiter:boolean

whether the decoded frame should strip out the delimiter or not

failFast:boolean

If true, a TooLongFrameException is thrown as soon as the decoder notices the length of the frame will exceed maxFrameLength regardless of whether the entire frame has been read. If false, a TooLongFrameException is thrown after the entire frame that exceeds maxFrameLength has been read.

Method Detail

decodeback to summary
protected final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception

Implements abstract io.netty.handler.codec.ByteToMessageDecoder.decode.

Doc from io.netty.handler.codec.ByteToMessageDecoder.decode.

Decode the from one ByteBuf to an other. This method will be called till either the input ByteBuf has nothing to read when return from this method or till nothing was read from the input ByteBuf.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this ByteToMessageDecoder belongs to

in:ByteBuf

the ByteBuf from which to read data

out:List<Object>

the List to which decoded messages should be added

Annotations
@Override
Exceptions
Exception:
is thrown if an error occurs
decodeback to summary
protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception

Create a frame out of the ByteBuf and return it.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this ByteToMessageDecoder belongs to

buffer:ByteBuf

the ByteBuf from which to read data

Returns:Object

frame the ByteBuf which represent the frame or null if no frame could be created.

failback to summary
private void fail(final ChannelHandlerContext ctx, int length)
failback to summary
private void fail(final ChannelHandlerContext ctx, String length)
findEndOfLineback to summary
private int findEndOfLine(final ByteBuf buffer)

Returns the index in the buffer of the end of line found. Returns -1 if no end of line was found in the buffer.