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