org.apache.mina.filter.codec.prefixedstring

## 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

  A `ProtocolDecoder` which decodes a String using a fixed-length length prefix.
  Author:
    Apache MINA Project

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `DEFAULT_MAX_DATA_LENGTH`
    `static int` | `DEFAULT_PREFIX_LENGTH`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `PrefixedStringDecoder(Charset charset)`
    `PrefixedStringDecoder(Charset charset, int prefixLength)`
    `PrefixedStringDecoder(Charset charset, int prefixLength, int maxDataLength)`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected boolean` | `doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)` Implement this method to consume the specified cumulative buffer and decode its content into message(s).
    `int` | `getMaxDataLength()` Gets the maximum number of bytes allowed for a single String
    `int` | `getPrefixLength()` Gets the length of the length prefix (1, 2, or 4)
    `void` | `setMaxDataLength(int maxDataLength)` Sets the maximum allowed value specified as data length in the incoming data
    `void` | `setPrefixLength(int prefixLength)` Sets the number of bytes used by the length prefix

    - ### Methods inherited from class org.apache.mina.filter.codec.CumulativeProtocolDecoder

      `decode, dispose`

    - ### Methods inherited from class org.apache.mina.filter.codec.ProtocolDecoderAdapter

      `finishDecode`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### DEFAULT_PREFIX_LENGTH

          public static final int DEFAULT_PREFIX_LENGTH

      See Also:
        Constant Field Values

    - #### DEFAULT_MAX_DATA_LENGTH

          public static final int DEFAULT_MAX_DATA_LENGTH

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### PrefixedStringDecoder

          public PrefixedStringDecoder(Charset charset,
                               int prefixLength,
                               int maxDataLength)

      Parameters:
        `charset` - the charset to use for encoding
        `prefixLength` - the length of the prefix
        `maxDataLength` - maximum number of bytes allowed for a single String

    - #### PrefixedStringDecoder

          public PrefixedStringDecoder(Charset charset,
                               int prefixLength)

    - #### PrefixedStringDecoder

          public PrefixedStringDecoder(Charset charset)

  - ### 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 data

      Useful for preventing an OutOfMemory attack by the peer. The decoder will throw a `BufferDataException` when data length specified in the incoming data is greater than maxDataLength The default value is `DEFAULT_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

      Description copied from class: `CumulativeProtocolDecoder`
      Implement this method to consume the specified cumulative buffer and decode its content into message(s).
      Specified by:
        `doDecode` in class `CumulativeProtocolDecoder`
        `in` - the cumulative buffer
      Returns:
        `true` if and only if there's more to decode in the buffer and you want to have `doDecode` method invoked again. Return `false` if remaining data is not enough to decode, then this method will be invoked again when more data is cumulated.
      Throws:
        `Exception` - if cannot decode `in`.

Copyright © 2004-2012 Apache MINA Project. All Rights Reserved.