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.textline;
21  
22  import java.nio.charset.Charset;
23  import java.nio.charset.CharsetEncoder;
24  
25  import org.apache.mina.common.ByteBuffer;
26  import org.apache.mina.common.IoSession;
27  import org.apache.mina.filter.codec.ProtocolEncoder;
28  import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
29  import org.apache.mina.filter.codec.ProtocolEncoderOutput;
30  
31  /**
32   * A {@link ProtocolEncoder} which encodes a string into a text line
33   * which ends with the delimiter.
34   *
35   * @author The Apache Directory Project (mina-dev@directory.apache.org)
36   * @version $Rev: 555855 $, $Date: 2007-07-13 05:19:00 +0200 (Fri, 13 Jul 2007) $,
37   */
38  public class TextLineEncoder extends ProtocolEncoderAdapter {
39      private static final String ENCODER = TextLineEncoder.class.getName()
40              + ".encoder";
41  
42      private final Charset charset;
43  
44      private final LineDelimiter delimiter;
45  
46      private int maxLineLength = Integer.MAX_VALUE;
47  
48      public TextLineEncoder() {
49          this(Charset.defaultCharset(), LineDelimiter.UNIX);
50      }
51  
52      public TextLineEncoder(LineDelimiter delimiter) {
53          this(Charset.defaultCharset(), delimiter);
54      }
55  
56      public TextLineEncoder(Charset charset) {
57          this(charset, LineDelimiter.UNIX);
58      }
59  
60      public TextLineEncoder(Charset charset, LineDelimiter delimiter) {
61          if (charset == null) {
62              throw new NullPointerException("charset");
63          }
64          if (delimiter == null) {
65              throw new NullPointerException("delimiter");
66          }
67          if (LineDelimiter.AUTO.equals(delimiter)) {
68              throw new IllegalArgumentException(
69                      "AUTO delimiter is not allowed for encoder.");
70          }
71  
72          this.charset = charset;
73          this.delimiter = delimiter;
74      }
75  
76      /**
77       * Returns the allowed maximum size of the encoded line.
78       * If the size of the encoded line exceeds this value, the encoder
79       * will throw a {@link IllegalArgumentException}.  The default value
80       * is {@link Integer#MAX_VALUE}.
81       */
82      public int getMaxLineLength() {
83          return maxLineLength;
84      }
85  
86      /**
87       * Sets the allowed maximum size of the encoded line.
88       * If the size of the encoded line exceeds this value, the encoder
89       * will throw a {@link IllegalArgumentException}.  The default value
90       * is {@link Integer#MAX_VALUE}.
91       */
92      public void setMaxLineLength(int maxLineLength) {
93          if (maxLineLength <= 0) {
94              throw new IllegalArgumentException("maxLineLength: "
95                      + maxLineLength);
96          }
97  
98          this.maxLineLength = maxLineLength;
99      }
100 
101     public void encode(IoSession session, Object message,
102             ProtocolEncoderOutput out) throws Exception {
103         CharsetEncoder encoder = (CharsetEncoder) session.getAttribute(ENCODER);
104         if (encoder == null) {
105             encoder = charset.newEncoder();
106             session.setAttribute(ENCODER, encoder);
107         }
108 
109         String value = message.toString();
110         ByteBuffer buf = ByteBuffer.allocate(value.length())
111                 .setAutoExpand(true);
112         buf.putString(value, encoder);
113         if (buf.position() > maxLineLength) {
114             throw new IllegalArgumentException("Line length: " + buf.position());
115         }
116         buf.putString(delimiter.getValue(), encoder);
117         buf.flip();
118         out.write(buf);
119     }
120 
121     public void dispose() throws Exception {
122     }
123 }