View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.filter.codec.prefixedstring;
21  
22  import org.apache.mina.core.buffer.BufferDataException;
23  import org.apache.mina.core.buffer.IoBuffer;
24  import org.apache.mina.core.session.IoSession;
25  import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
26  import org.apache.mina.filter.codec.ProtocolDecoder;
27  import org.apache.mina.filter.codec.ProtocolDecoderOutput;
28  
29  import java.nio.charset.Charset;
30  
31  /**
32   * A {@link ProtocolDecoder} which decodes a String using a fixed-length length prefix.
33   *
34   * @author The Apache MINA Project (dev@mina.apache.org)
35   * @version $Rev: 591182 $, $Date: 2007-11-02 01:59:27 +0100 (vr, 02 nov 2007) $,
36   */
37  public class PrefixedStringDecoder extends CumulativeProtocolDecoder {
38  
39      public final static int DEFAULT_PREFIX_LENGTH = 4;
40  
41      public final static int DEFAULT_MAX_DATA_LENGTH = 2048;
42  
43      private final Charset charset;
44  
45      private int prefixLength = DEFAULT_PREFIX_LENGTH;
46  
47      private int maxDataLength = DEFAULT_MAX_DATA_LENGTH;
48  
49      /**
50       * @param charset       the charset to use for encoding
51       * @param prefixLength  the length of the prefix
52       * @param maxDataLength maximum number of bytes allowed for a single String
53       */
54      public PrefixedStringDecoder(Charset charset, int prefixLength, int maxDataLength) {
55          this.charset = charset;
56          this.prefixLength = prefixLength;
57          this.maxDataLength = maxDataLength;
58      }
59  
60      public PrefixedStringDecoder(Charset charset, int prefixLength) {
61          this(charset, prefixLength, DEFAULT_MAX_DATA_LENGTH);
62      }
63  
64      public PrefixedStringDecoder(Charset charset) {
65          this(charset, DEFAULT_PREFIX_LENGTH);
66      }
67  
68      /**
69       * Sets the number of bytes used by the length prefix
70       *
71       * @param prefixLength the length of the length prefix (1, 2, or 4)
72       */
73      public void setPrefixLength(int prefixLength) {
74          this.prefixLength = prefixLength;
75      }
76  
77      /**
78       * Gets the length of the length prefix (1, 2, or 4)
79       *
80       * @return length of the length prefix
81       */
82      public int getPrefixLength() {
83          return prefixLength;
84      }
85  
86      /**
87       * Sets the maximum allowed value specified as data length in the incoming data
88       * <p>
89       * Useful for preventing an OutOfMemory attack by the peer.
90       * The decoder will throw a {@link BufferDataException} when data length
91       * specified in the incoming data is greater than maxDataLength
92       * The default value is {@link PrefixedStringDecoder#DEFAULT_MAX_DATA_LENGTH}.
93       * </p>
94       *
95       * @param maxDataLength maximum allowed value specified as data length in the incoming data
96       */
97      public void setMaxDataLength(int maxDataLength) {
98          this.maxDataLength = maxDataLength;
99      }
100 
101     /**
102      * Gets the maximum number of bytes allowed for a single String
103      *
104      * @return maximum number of bytes allowed for a single String
105      */
106     public int getMaxDataLength() {
107         return maxDataLength;
108     }
109 
110     protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
111         if (in.prefixedDataAvailable(prefixLength, maxDataLength)) {
112             String msg = in.getPrefixedString(prefixLength, charset.newDecoder());
113             out.write(msg);
114             return true;
115         } else {
116             return false;
117         }
118     }
119 }