Class TextLineDecoder
- java.lang.Object
-
- org.apache.mina.filter.codec.textline.TextLineDecoder
-
- All Implemented Interfaces:
ProtocolDecoder
public class TextLineDecoder extends java.lang.Object implements ProtocolDecoder
AProtocolDecoderwhich decodes a text line into a string.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classTextLineDecoder.ContextA Context used during the decoding of a lin.
-
Field Summary
Fields Modifier and Type Field Description private intbufferLengthThe default maximum buffer length.private java.nio.charset.Charsetcharsetprivate static AttributeKeyCONTEXTprivate IoBufferdelimBufAn IoBuffer containing the delimiterprivate LineDelimiterdelimiterThe delimiter used to determinate when a line has been fully decodedprivate intmaxLineLengthThe default maximum Line length.
-
Constructor Summary
Constructors Constructor Description TextLineDecoder()Creates a new instance with the current defaultCharsetandLineDelimiter.AUTOdelimiter.TextLineDecoder(java.lang.String delimiter)Creates a new instance with the current defaultCharsetand the specifieddelimiter.TextLineDecoder(java.nio.charset.Charset charset)Creates a new instance with the spcifiedcharsetandLineDelimiter.AUTOdelimiter.TextLineDecoder(java.nio.charset.Charset charset, java.lang.String delimiter)Creates a new instance with the spcifiedcharsetand the specifieddelimiter.TextLineDecoder(java.nio.charset.Charset charset, LineDelimiter delimiter)Creates a new instance with the specifiedcharsetand the specifieddelimiter.TextLineDecoder(LineDelimiter delimiter)Creates a new instance with the current defaultCharsetand the specifieddelimiter.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)Decodes binary or protocol-specific content into higher-level message objects.private voiddecodeAuto(TextLineDecoder.Context ctx, IoSession session, IoBuffer in, ProtocolDecoderOutput out)Decode a line using the default delimiter on the current systemprivate voiddecodeNormal(TextLineDecoder.Context ctx, IoSession session, IoBuffer in, ProtocolDecoderOutput out)Decode a line using the delimiter defined by the callervoiddispose(IoSession session)Releases all resources related with this decoder.voidfinishDecode(IoSession session, ProtocolDecoderOutput out)Invoked when the specifiedsessionis closed.intgetBufferLength()private TextLineDecoder.ContextgetContext(IoSession session)intgetMaxLineLength()voidsetBufferLength(int bufferLength)Sets the default buffer size.voidsetMaxLineLength(int maxLineLength)Sets the allowed maximum size of the line to be decoded.protected voidwriteText(IoSession session, java.lang.String text, ProtocolDecoderOutput out)By default, this method propagates the decoded line of text toProtocolDecoderOutput#write(Object).
-
-
-
Field Detail
-
CONTEXT
private static final AttributeKey CONTEXT
-
charset
private final java.nio.charset.Charset charset
-
delimiter
private final LineDelimiter delimiter
The delimiter used to determinate when a line has been fully decoded
-
delimBuf
private IoBuffer delimBuf
An IoBuffer containing the delimiter
-
maxLineLength
private int maxLineLength
The default maximum Line length. Default to 1024.
-
bufferLength
private int bufferLength
The default maximum buffer length. Default to 128 chars.
-
-
Constructor Detail
-
TextLineDecoder
public TextLineDecoder()
Creates a new instance with the current defaultCharsetandLineDelimiter.AUTOdelimiter.
-
TextLineDecoder
public TextLineDecoder(java.lang.String delimiter)
Creates a new instance with the current defaultCharsetand the specifieddelimiter.- Parameters:
delimiter- The line delimiter to use
-
TextLineDecoder
public TextLineDecoder(LineDelimiter delimiter)
Creates a new instance with the current defaultCharsetand the specifieddelimiter.- Parameters:
delimiter- The line delimiter to use
-
TextLineDecoder
public TextLineDecoder(java.nio.charset.Charset charset)
Creates a new instance with the spcifiedcharsetandLineDelimiter.AUTOdelimiter.- Parameters:
charset- TheCharsetto use
-
TextLineDecoder
public TextLineDecoder(java.nio.charset.Charset charset, java.lang.String delimiter)Creates a new instance with the spcifiedcharsetand the specifieddelimiter.- Parameters:
charset- TheCharsetto usedelimiter- The line delimiter to use
-
TextLineDecoder
public TextLineDecoder(java.nio.charset.Charset charset, LineDelimiter delimiter)Creates a new instance with the specifiedcharsetand the specifieddelimiter.- Parameters:
charset- TheCharsetto usedelimiter- The line delimiter to use
-
-
Method Detail
-
getMaxLineLength
public int getMaxLineLength()
- Returns:
- the allowed maximum size of the line to be decoded.
If the size of the line to be decoded exceeds this value, the
decoder will throw a
BufferDataException. The default value is1024(1KB).
-
setMaxLineLength
public void setMaxLineLength(int maxLineLength)
Sets the allowed maximum size of the line to be decoded. If the size of the line to be decoded exceeds this value, the decoder will throw aBufferDataException. The default value is1024(1KB).- Parameters:
maxLineLength- The maximum line length
-
setBufferLength
public void setBufferLength(int bufferLength)
Sets the default buffer size. This buffer is used in the Context to store the decoded line.- Parameters:
bufferLength- The default bufer size
-
getBufferLength
public int getBufferLength()
- Returns:
- the allowed buffer size used to store the decoded line in the Context instance.
-
decode
public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws java.lang.Exception
Decodes binary or protocol-specific content into higher-level message objects. MINA invokesProtocolDecoder.decode(IoSession, IoBuffer, ProtocolDecoderOutput)method with read data, and then the decoder implementation puts decoded messages intoProtocolDecoderOutput.- Specified by:
decodein interfaceProtocolDecoder- Parameters:
session- The current Sessionin- the buffer to decodeout- TheProtocolDecoderOutputthat will receive the decoded message- Throws:
java.lang.Exception- if the read data violated protocol specification
-
getContext
private TextLineDecoder.Context getContext(IoSession session)
- Parameters:
session- The session for which we want the context- Returns:
- the context for this session
-
finishDecode
public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws java.lang.Exception
Invoked when the specifiedsessionis closed. This method is useful when you deal with the protocol which doesn't specify the length of a message such as HTTP response withoutcontent-lengthheader. Implement this method to process the remaining data thatProtocolDecoder.decode(IoSession, IoBuffer, ProtocolDecoderOutput)method didn't process completely.- Specified by:
finishDecodein interfaceProtocolDecoder- Parameters:
session- The current Sessionout- TheProtocolDecoderOutputthat contains the decoded message- Throws:
java.lang.Exception- if the read data violated protocol specification
-
dispose
public void dispose(IoSession session) throws java.lang.Exception
Releases all resources related with this decoder.- Specified by:
disposein interfaceProtocolDecoder- Parameters:
session- The current Session- Throws:
java.lang.Exception- if failed to dispose all resources
-
decodeAuto
private void decodeAuto(TextLineDecoder.Context ctx, IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws java.nio.charset.CharacterCodingException, ProtocolDecoderException
Decode a line using the default delimiter on the current system- Throws:
java.nio.charset.CharacterCodingExceptionProtocolDecoderException
-
decodeNormal
private void decodeNormal(TextLineDecoder.Context ctx, IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws java.nio.charset.CharacterCodingException, ProtocolDecoderException
Decode a line using the delimiter defined by the caller- Throws:
java.nio.charset.CharacterCodingExceptionProtocolDecoderException
-
writeText
protected void writeText(IoSession session, java.lang.String text, ProtocolDecoderOutput out)
By default, this method propagates the decoded line of text toProtocolDecoderOutput#write(Object). You may override this method to modify the default behavior.- Parameters:
session- theIoSessionthe received data.text- the decoded textout- the upstreamProtocolDecoderOutput.
-
-