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.io.ByteArrayOutputStream;
    23  import java.io.PrintWriter;
    24
    25  /**
    26   * A delimiter which is appended to the end of a text line, such as
    27   * <tt>CR/LF</tt>. This class defines default delimiters for various
    28   * OS :
    29   * <ul>
    30   * <li><b>Unix/Linux</b> : LineDelimiter.UNIX ("\n")</li>
    31   * <li><b>Windows</b> : LineDelimiter.WINDOWS ("\r\n")</li>
    32   * <li><b>MAC</b> : LineDelimiter.MAC ("\r")</li>
    33   * </ul>
    34   *
    35   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    36   */
    37  public class LineDelimiter {
    38      /** the line delimiter constant of the current O/S. */
    39      public static final LineDelimiter DEFAULT;
    40
    41      /** Compute the default delimiter on he current OS */
    42      static {
    43          ByteArrayOutputStream bout = new ByteArrayOutputStream();
    44          PrintWriter out = new PrintWriter(bout, true);
    45          out.println();
    46          DEFAULT = new LineDelimiter(new String(bout.toByteArray()));
    47      }
    48
    49      /**
    50       * A special line delimiter which is used for auto-detection of
    51       * EOL in {@link TextLineDecoder}.  If this delimiter is used,
    52       * {@link TextLineDecoder} will consider both  <tt>'\r'</tt> and
    53       * <tt>'\n'</tt> as a delimiter.
    54       */
    55      public static final LineDelimiter AUTO = new LineDelimiter("");
    56
    57      /**
    58       * The CRLF line delimiter constant (<tt>"\r\n"</tt>)
    59       */
    60      public static final LineDelimiter CRLF = new LineDelimiter("\r\n");
    61
    62      /**
    63       * The line delimiter constant of UNIX (<tt>"\n"</tt>)
    64       */
    65      public static final LineDelimiter UNIX = new LineDelimiter("\n");
    66
    67      /**
    68       * The line delimiter constant of MS Windows/DOS (<tt>"\r\n"</tt>)
    69       */
    70      public static final LineDelimiter WINDOWS = CRLF;
    71
    72      /**
    73       * The line delimiter constant of Mac OS (<tt>"\r"</tt>)
    74       */
    75      public static final LineDelimiter MAC = new LineDelimiter("\r");
    76
    77      /**
    78       * The line delimiter constant for NUL-terminated text protocols
    79       * such as Flash XML socket (<tt>"\0"</tt>)
    80       */
    81      public static final LineDelimiter NUL = new LineDelimiter("\0");
    82
    83      /** Stores the selected Line delimiter */
    84      private final String value;
    85
    86      /**
    87       * Creates a new line delimiter with the specified <tt>value</tt>.
    88       */
    89      public LineDelimiter(String value) {
    90          if (value == null) {
    91              throw new IllegalArgumentException("delimiter");
    92          }
    93
    94          this.value = value;
    95      }
    96
    97      /**
    98       * Return the delimiter string.
    99       */
    100     public String getValue() {
    101         return value;
    102     }
    103
    104     /**
    105      * {@inheritDoc}
    106      */
    107     @Override
    108     public int hashCode() {
    109         return value.hashCode();
    110     }
    111
    112     /**
    113      * {@inheritDoc}
    114      */
    115     @Override
    116     public boolean equals(Object o) {
    117         if (this == o) {
    118             return true;
    119         }
    120
    121         if (!(o instanceof LineDelimiter)) {
    122             return false;
    123         }
    124
    125         LineDelimiter that = (LineDelimiter) o;
    126
    127         return this.value.equals(that.value);
    128     }
    129
    130     /**
    131      * {@inheritDoc}
    132      */
    133     @Override
    134     public String toString() {
    135         if (value.length() == 0) {
    136             return "delimiter: auto";
    137         } else {
    138             StringBuilder buf = new StringBuilder();
    139             buf.append("delimiter:");
    140
    141             for (int i = 0; i < value.length(); i++) {
    142                 buf.append(" 0x");
    143                 buf.append(Integer.toHexString(value.charAt(i)));
    144             }
    145
    146             return buf.toString();
    147         }
    148     }
    149 }