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
    24  import org.apache.mina.common.BufferDataException;
    25  import org.apache.mina.filter.codec.ProtocolCodecFactory;
    26  import org.apache.mina.filter.codec.ProtocolDecoder;
    27  import org.apache.mina.filter.codec.ProtocolEncoder;
    28
    29  /**
    30   * A {@link ProtocolCodecFactory} that performs encoding and decoding between
    31   * a text line data and a Java string object.  This codec is useful especially
    32   * when you work with a text-based protocols such as SMTP and IMAP.
    33   *
    34   * @author The Apache Directory Project (mina-dev@directory.apache.org)
    35   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $
    36   */
    37  public class TextLineCodecFactory implements ProtocolCodecFactory {
    38      private final TextLineEncoder encoder;
    39
    40      private final TextLineDecoder decoder;
    41
    42      /**
    43       * Creates a new instance with the current default {@link Charset}.
    44       */
    45      public TextLineCodecFactory() {
    46          this(Charset.defaultCharset());
    47      }
    48
    49      /**
    50       * Creates a new instance with the specified {@link Charset}.
    51       */
    52      public TextLineCodecFactory(Charset charset) {
    53          encoder = new TextLineEncoder(charset, LineDelimiter.UNIX);
    54          decoder = new TextLineDecoder(charset, LineDelimiter.AUTO);
    55      }
    56
    57      public ProtocolEncoder getEncoder() {
    58          return encoder;
    59      }
    60
    61      public ProtocolDecoder getDecoder() {
    62          return decoder;
    63      }
    64
    65      /**
    66       * Returns the allowed maximum size of the encoded line.
    67       * If the size of the encoded line exceeds this value, the encoder
    68       * will throw a {@link IllegalArgumentException}.  The default value
    69       * is {@link Integer#MAX_VALUE}.
    70       * <p>
    71       * This method does the same job with {@link TextLineEncoder#getMaxLineLength()}.
    72       */
    73      public int getEncoderMaxLineLength() {
    74          return encoder.getMaxLineLength();
    75      }
    76
    77      /**
    78       * Sets 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       * <p>
    83       * This method does the same job with {@link TextLineEncoder#setMaxLineLength(int)}.
    84       */
    85      public void setEncoderMaxLineLength(int maxLineLength) {
    86          encoder.setMaxLineLength(maxLineLength);
    87      }
    88
    89      /**
    90       * Returns the allowed maximum size of the line to be decoded.
    91       * If the size of the line to be decoded exceeds this value, the
    92       * decoder will throw a {@link BufferDataException}.  The default
    93       * value is <tt>1024</tt> (1KB).
    94       * <p>
    95       * This method does the same job with {@link TextLineDecoder#getMaxLineLength()}.
    96       */
    97      public int getDecoderMaxLineLength() {
    98          return decoder.getMaxLineLength();
    99      }
    100
    101     /**
    102      * Sets the allowed maximum size of the line to be decoded.
    103      * If the size of the line to be decoded exceeds this value, the
    104      * decoder will throw a {@link BufferDataException}.  The default
    105      * value is <tt>1024</tt> (1KB).
    106      * <p>
    107      * This method does the same job with {@link TextLineDecoder#setMaxLineLength(int)}.
    108      */
    109     public void setDecoderMaxLineLength(int maxLineLength) {
    110         decoder.setMaxLineLength(maxLineLength);
    111     }
    112 }