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 <a href="http://mina.apache.org">Apache MINA Project</a>
35   */
36  public class PrefixedStringEncoder extends ProtocolEncoderAdapter {
37      /** The default length for the prefix */
38      public static final int DEFAULT_PREFIX_LENGTH = 4;
39  
40      /** The default maximum data length */ 
41      public static final 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       * Creates a new PrefixedStringEncoder instance
51       * 
52       * @param charset       the {@link Charset} to use for encoding
53       * @param prefixLength  the length of the prefix
54       * @param maxDataLength maximum number of bytes allowed for a single String
55       */
56      public PrefixedStringEncoder(Charset charset, int prefixLength, int maxDataLength) {
57          this.charset = charset;
58          this.prefixLength = prefixLength;
59          this.maxDataLength = maxDataLength;
60      }
61  
62      /**
63       * Creates a new PrefixedStringEncoder instance
64       * 
65       * @param charset       the {@link Charset} to use for encoding
66       * @param prefixLength  the length of the prefix
67       */
68      public PrefixedStringEncoder(Charset charset, int prefixLength) {
69          this(charset, prefixLength, DEFAULT_MAX_DATA_LENGTH);
70      }
71  
72      /**
73       * Creates a new PrefixedStringEncoder instance
74       * 
75       * @param charset       the {@link Charset} to use for encoding
76       */
77      public PrefixedStringEncoder(Charset charset) {
78          this(charset, DEFAULT_PREFIX_LENGTH);
79      }
80  
81      /**
82       * Creates a new PrefixedStringEncoder instance
83       */
84      public PrefixedStringEncoder() {
85          this(Charset.defaultCharset());
86      }
87  
88      /**
89       * Sets the number of bytes used by the length prefix
90       *
91       * @param prefixLength the length of the length prefix (1, 2, or 4)
92       */
93      public void setPrefixLength(int prefixLength) {
94          if (prefixLength != 1 && prefixLength != 2 && prefixLength != 4) {
95              throw new IllegalArgumentException("prefixLength: " + prefixLength);
96          }
97          this.prefixLength = prefixLength;
98      }
99  
100     /**
101      * Gets the length of the length prefix (1, 2, or 4)
102      *
103      * @return length of the length prefix
104      */
105     public int getPrefixLength() {
106         return prefixLength;
107     }
108 
109     /**
110      * Sets the maximum number of bytes allowed for encoding a single String
111      * (including the prefix)
112      * <p>
113      * The encoder will throw a {@link IllegalArgumentException} when more bytes
114      * are needed to encode a String value.
115      * The default value is {@link PrefixedStringEncoder#DEFAULT_MAX_DATA_LENGTH}.
116      * </p>
117      *
118      * @param maxDataLength maximum number of bytes allowed for encoding a single String
119      */
120     public void setMaxDataLength(int maxDataLength) {
121         this.maxDataLength = maxDataLength;
122     }
123 
124     /**
125      * Gets the maximum number of bytes allowed for encoding a single String     *
126      *
127      * @return maximum number of bytes allowed for encoding a single String (prefix included)
128      */
129     public int getMaxDataLength() {
130         return maxDataLength;
131     }
132 
133     /**
134      * {@inheritDoc}
135      */
136     @Override
137     public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
138         String value = (String) message;
139         IoBuffer buffer = IoBuffer.allocate(value.length()).setAutoExpand(true);
140         buffer.putPrefixedString(value, prefixLength, charset.newEncoder());
141         if (buffer.position() > maxDataLength) {
142             throw new IllegalArgumentException("Data length: " + buffer.position());
143         }
144         buffer.flip();
145         out.write(buffer);
146     }
147 }