Class PrefixedStringDecoder
- java.lang.Object
-
- org.apache.mina.filter.codec.ProtocolDecoderAdapter
-
- org.apache.mina.filter.codec.CumulativeProtocolDecoder
-
- org.apache.mina.filter.codec.prefixedstring.PrefixedStringDecoder
-
- All Implemented Interfaces:
ProtocolDecoder
public class PrefixedStringDecoder extends CumulativeProtocolDecoder
AProtocolDecoderwhich decodes a String using a fixed-length length prefix.- Author:
- Apache MINA Project
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_MAX_DATA_LENGTHThe default maximum data lengthstatic intDEFAULT_PREFIX_LENGTHThe default length for the prefix
-
Constructor Summary
Constructors Constructor Description PrefixedStringDecoder(Charset charset)Creates a new PrefixedStringDecoder instancePrefixedStringDecoder(Charset charset, int prefixLength)Creates a new PrefixedStringDecoder instancePrefixedStringDecoder(Charset charset, int prefixLength, int maxDataLength)Creates a new PrefixedStringDecoder instance
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleandoDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)Implement this method to consume the specified cumulative buffer and decode its content into message(s).intgetMaxDataLength()Gets the maximum number of bytes allowed for a single StringintgetPrefixLength()Gets the length of the length prefix (1, 2, or 4)voidsetMaxDataLength(int maxDataLength)Sets the maximum allowed value specified as data length in the incoming datavoidsetPrefixLength(int prefixLength)Sets the number of bytes used by the length prefix-
Methods inherited from class org.apache.mina.filter.codec.CumulativeProtocolDecoder
decode, dispose, setTransportMetadataFragmentation
-
Methods inherited from class org.apache.mina.filter.codec.ProtocolDecoderAdapter
finishDecode
-
-
-
-
Field Detail
-
DEFAULT_PREFIX_LENGTH
public static final int DEFAULT_PREFIX_LENGTH
The default length for the prefix- See Also:
- Constant Field Values
-
DEFAULT_MAX_DATA_LENGTH
public static final int DEFAULT_MAX_DATA_LENGTH
The default maximum data length- See Also:
- Constant Field Values
-
-
Constructor Detail
-
PrefixedStringDecoder
public PrefixedStringDecoder(Charset charset, int prefixLength, int maxDataLength)
Creates a new PrefixedStringDecoder instance- Parameters:
charset- theCharsetto use for decodingprefixLength- the length of the prefixmaxDataLength- maximum number of bytes allowed for a single String
-
PrefixedStringDecoder
public PrefixedStringDecoder(Charset charset, int prefixLength)
Creates a new PrefixedStringDecoder instance- Parameters:
charset- theCharsetto use for decodingprefixLength- the length of the prefix
-
-
Method Detail
-
setPrefixLength
public void setPrefixLength(int prefixLength)
Sets the number of bytes used by the length prefix- Parameters:
prefixLength- the length of the length prefix (1, 2, or 4)
-
getPrefixLength
public int getPrefixLength()
Gets the length of the length prefix (1, 2, or 4)- Returns:
- length of the length prefix
-
setMaxDataLength
public void setMaxDataLength(int maxDataLength)
Sets the maximum allowed value specified as data length in the incoming dataUseful for preventing an OutOfMemory attack by the peer. The decoder will throw a
BufferDataExceptionwhen data length specified in the incoming data is greater than maxDataLength The default value isDEFAULT_MAX_DATA_LENGTH.- Parameters:
maxDataLength- maximum allowed value specified as data length in the incoming data
-
getMaxDataLength
public int getMaxDataLength()
Gets the maximum number of bytes allowed for a single String- Returns:
- maximum number of bytes allowed for a single String
-
doDecode
protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception
Implement this method to consume the specified cumulative buffer and decode its content into message(s).- Specified by:
doDecodein classCumulativeProtocolDecoder- Parameters:
session- The current Sessionin- the cumulative bufferout- TheProtocolDecoderOutputthat will receive the decoded message- Returns:
trueif and only if there's more to decode in the buffer and you want to havedoDecodemethod invoked again. Returnfalseif remaining data is not enough to decode, then this method will be invoked again when more data is cumulated.- Throws:
Exception- if cannot decodein.
-
-