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.codec.textline;
21  
22  import java.nio.ByteBuffer;
23  import java.nio.CharBuffer;
24  import java.nio.charset.CharacterCodingException;
25  import java.nio.charset.Charset;
26  import java.nio.charset.CharsetEncoder;
27  
28  import org.apache.mina.codec.ProtocolEncoder;
29  import org.apache.mina.codec.StatelessProtocolEncoder;
30  
31  /**
32   * A {@link ProtocolEncoder} which encodes a string into a text line
33   * which ends with the delimiter.
34   *
35   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
36   */
37  public class TextLineEncoder implements StatelessProtocolEncoder<String, ByteBuffer> {
38      private final CharsetEncoder charsetEncoder;
39  
40      private final LineDelimiter delimiter;
41  
42      private int maxLineLength = Integer.MAX_VALUE;
43  
44      /**
45       * Creates a new instance with the current default {@link Charset}
46       * and {@link LineDelimiter#UNIX} delimiter.
47       */
48      public TextLineEncoder() {
49          this(Charset.defaultCharset(), LineDelimiter.UNIX);
50      }
51  
52      /**
53       * Creates a new instance with the current default {@link Charset}
54       * and the specified <tt>delimiter</tt>.
55       */
56      public TextLineEncoder(String delimiter) {
57          this(new LineDelimiter(delimiter));
58      }
59  
60      /**
61       * Creates a new instance with the current default {@link Charset}
62       * and the specified <tt>delimiter</tt>.
63       */
64      public TextLineEncoder(LineDelimiter delimiter) {
65          this(Charset.defaultCharset(), delimiter);
66      }
67  
68      /**
69       * Creates a new instance with the spcified <tt>charset</tt>
70       * and {@link LineDelimiter#UNIX} delimiter.
71       */
72      public TextLineEncoder(Charset charset) {
73          this(charset, LineDelimiter.UNIX);
74      }
75  
76      /**
77       * Creates a new instance with the spcified <tt>charset</tt>
78       * and the specified <tt>delimiter</tt>.
79       */
80      public TextLineEncoder(Charset charset, String delimiter) {
81          this(charset, new LineDelimiter(delimiter));
82      }
83  
84      /**
85       * Creates a new instance with the spcified <tt>charset</tt>
86       * and the specified <tt>delimiter</tt>.
87       */
88      public TextLineEncoder(Charset charset, LineDelimiter delimiter) {
89          if (charset == null) {
90              throw new IllegalArgumentException("charset");
91          }
92          if (delimiter == null) {
93              throw new IllegalArgumentException("delimiter");
94          }
95          if (LineDelimiter.AUTO.equals(delimiter)) {
96              throw new IllegalArgumentException("AUTO delimiter is not allowed for encoder.");
97          }
98  
99          this.charsetEncoder = charset.newEncoder();
100         this.delimiter = delimiter;
101     }
102 
103     /**
104      * Returns the allowed maximum size of the encoded line.
105      * If the size of the encoded line exceeds this value, the encoder
106      * will throw a {@link IllegalArgumentException}.  The default value
107      * is {@link Integer#MAX_VALUE}.
108      */
109     public int getMaxLineLength() {
110         return maxLineLength;
111     }
112 
113     /**
114      * Sets the allowed maximum size of the encoded line.
115      * If the size of the encoded line exceeds this value, the encoder
116      * will throw a {@link IllegalArgumentException}.  The default value
117      * is {@link Integer#MAX_VALUE}.
118      */
119     public void setMaxLineLength(int maxLineLength) {
120         if (maxLineLength <= 0) {
121             throw new IllegalArgumentException("maxLineLength: " + maxLineLength);
122         }
123 
124         this.maxLineLength = maxLineLength;
125     }
126 
127     @Override
128     public Void createEncoderState() {
129         return null;
130     }
131 
132     @Override
133     public ByteBuffer encode(String message, Void context) {
134         try {
135             String value = (message == null ? "" : message);
136 
137             if (value.length() > maxLineLength) {
138                 throw new IllegalArgumentException("Line length: " + message.length());
139             }
140             return charsetEncoder.encode(CharBuffer.wrap(value + delimiter.getValue()));
141         } catch (CharacterCodingException e) {
142             throw new RuntimeException(e);
143         }
144     }
145 
146 }