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.core.buffer.IoBuffer;
    26  import org.apache.mina.core.session.AttributeKey;
    27  import org.apache.mina.core.session.IoSession;
    28  import org.apache.mina.filter.codec.ProtocolEncoder;
    29  import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
    30  import org.apache.mina.filter.codec.ProtocolEncoderOutput;
    31
    32  /**
    33   * A {@link ProtocolEncoder} which encodes a string into a text line
    34   * which ends with the delimiter.
    35   *
    36   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    37   */
    38  public class TextLineEncoder extends ProtocolEncoderAdapter {
    39      private final AttributeKey ENCODER = new AttributeKey(getClass(), "encoder");
    40
    41      private final Charset charset;
    42
    43      private final LineDelimiter delimiter;
    44
    45      private int maxLineLength = Integer.MAX_VALUE;
    46
    47      /**
    48       * Creates a new instance with the current default {@link Charset}
    49       * and {@link LineDelimiter#UNIX} delimiter.
    50       */
    51      public TextLineEncoder() {
    52          this(Charset.defaultCharset(), LineDelimiter.UNIX);
    53      }
    54
    55      /**
    56       * Creates a new instance with the current default {@link Charset}
    57       * and the specified <tt>delimiter</tt>.
    58       */
    59      public TextLineEncoder(String delimiter) {
    60          this(new LineDelimiter(delimiter));
    61      }
    62
    63      /**
    64       * Creates a new instance with the current default {@link Charset}
    65       * and the specified <tt>delimiter</tt>.
    66       */
    67      public TextLineEncoder(LineDelimiter delimiter) {
    68          this(Charset.defaultCharset(), delimiter);
    69      }
    70
    71      /**
    72       * Creates a new instance with the spcified <tt>charset</tt>
    73       * and {@link LineDelimiter#UNIX} delimiter.
    74       */
    75      public TextLineEncoder(Charset charset) {
    76          this(charset, LineDelimiter.UNIX);
    77      }
    78
    79      /**
    80       * Creates a new instance with the spcified <tt>charset</tt>
    81       * and the specified <tt>delimiter</tt>.
    82       */
    83      public TextLineEncoder(Charset charset, String delimiter) {
    84          this(charset, new LineDelimiter(delimiter));
    85      }
    86
    87      /**
    88       * Creates a new instance with the spcified <tt>charset</tt>
    89       * and the specified <tt>delimiter</tt>.
    90       */
    91      public TextLineEncoder(Charset charset, LineDelimiter delimiter) {
    92          if (charset == null) {
    93              throw new IllegalArgumentException("charset");
    94          }
    95          if (delimiter == null) {
    96              throw new IllegalArgumentException("delimiter");
    97          }
    98          if (LineDelimiter.AUTO.equals(delimiter)) {
    99              throw new IllegalArgumentException("AUTO delimiter is not allowed for encoder.");
    100         }
    101
    102         this.charset = charset;
    103         this.delimiter = delimiter;
    104     }
    105
    106     /**
    107      * Returns the allowed maximum size of the encoded line.
    108      * If the size of the encoded line exceeds this value, the encoder
    109      * will throw a {@link IllegalArgumentException}.  The default value
    110      * is {@link Integer#MAX_VALUE}.
    111      */
    112     public int getMaxLineLength() {
    113         return maxLineLength;
    114     }
    115
    116     /**
    117      * Sets the allowed maximum size of the encoded line.
    118      * If the size of the encoded line exceeds this value, the encoder
    119      * will throw a {@link IllegalArgumentException}.  The default value
    120      * is {@link Integer#MAX_VALUE}.
    121      */
    122     public void setMaxLineLength(int maxLineLength) {
    123         if (maxLineLength <= 0) {
    124             throw new IllegalArgumentException("maxLineLength: " + maxLineLength);
    125         }
    126
    127         this.maxLineLength = maxLineLength;
    128     }
    129
    130     public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
    131         CharsetEncoder encoder = (CharsetEncoder) session.getAttribute(ENCODER);
    132
    133         if (encoder == null) {
    134             encoder = charset.newEncoder();
    135             session.setAttribute(ENCODER, encoder);
    136         }
    137
    138         String value = (message == null ? "" : message.toString());
    139         IoBuffer buf = IoBuffer.allocate(value.length()).setAutoExpand(true);
    140         buf.putString(value, encoder);
    141
    142         if (buf.position() > maxLineLength) {
    143             throw new IllegalArgumentException("Line length: " + buf.position());
    144         }
    145
    146         buf.putString(delimiter.getValue(), encoder);
    147         buf.flip();
    148         out.write(buf);
    149     }
    150
    151     public void dispose() throws Exception {
    152         // Do nothing
    153     }
    154 }