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.IoBuffer;
23  import org.apache.mina.core.session.IoSession;
24  import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
25  import org.apache.mina.filter.codec.ProtocolEncoderOutput;
26  import org.apache.mina.filter.codec.ProtocolEncoder;
27  
28  import java.nio.charset.Charset;
29  
30  /**
31   * A {@link ProtocolEncoder} which encodes a string
32   * 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 PrefixedStringEncoder extends ProtocolEncoderAdapter {
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      public PrefixedStringEncoder(Charset charset, int prefixLength, int maxDataLength) {
50          this.charset = charset;
51          this.prefixLength = prefixLength;
52          this.maxDataLength = maxDataLength;
53      }
54  
55      public PrefixedStringEncoder(Charset charset, int prefixLength) {
56          this(charset, prefixLength, DEFAULT_MAX_DATA_LENGTH);
57      }
58  
59      public PrefixedStringEncoder(Charset charset) {
60          this(charset, DEFAULT_PREFIX_LENGTH);
61      }
62  
63      public PrefixedStringEncoder() {
64          this(Charset.defaultCharset());
65      }
66  
67      /**
68       * Sets the number of bytes used by the length prefix
69       *
70       * @param prefixLength the length of the length prefix (1, 2, or 4)
71       */
72      public void setPrefixLength(int prefixLength) {
73          if (prefixLength != 1 && prefixLength != 2 && prefixLength != 4) {
74              throw new IllegalArgumentException("prefixLength: " + prefixLength);
75          }
76          this.prefixLength = prefixLength;
77      }
78  
79      /**
80       * Gets the length of the length prefix (1, 2, or 4)
81       *
82       * @return length of the length prefix
83       */
84      public int getPrefixLength() {
85          return prefixLength;
86      }
87  
88      /**
89       * Sets the maximum number of bytes allowed for encoding a single String
90       * (including the prefix)
91       * <p>
92       * The encoder will throw a {@link IllegalArgumentException} when more bytes
93       * are needed to encode a String value.
94       * The default value is {@link PrefixedStringEncoder#DEFAULT_MAX_DATA_LENGTH}.
95       * </p>
96       *
97       * @param maxDataLength maximum number of bytes allowed for encoding a single String
98       */
99      public void setMaxDataLength(int maxDataLength) {
100         this.maxDataLength = maxDataLength;
101     }
102 
103     /**
104      * Gets the maximum number of bytes allowed for encoding a single String     *
105      *
106      * @return maximum number of bytes allowed for encoding a single String (prefix included)
107      */
108     public int getMaxDataLength() {
109         return maxDataLength;
110     }
111 
112 
113     public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
114         String value = (String) message;
115         IoBuffer buffer = IoBuffer.allocate(value.length()).setAutoExpand(true);
116         buffer.putPrefixedString(value, prefixLength, charset.newEncoder());
117         if (buffer.position() > maxDataLength) {
118             throw new IllegalArgumentException("Data length: " + buffer.position());
119         }
120         buffer.flip();
121         out.write(buffer);
122     }
123 }