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       * @param value The new Line Delimiter
90       */
91      public LineDelimiter(String value) {
92          if (value == null) {
93              throw new IllegalArgumentException("delimiter");
94          }
95  
96          this.value = value;
97      }
98  
99      /**
100      * @return the delimiter string.
101      */
102     public String getValue() {
103         return value;
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     @Override
110     public int hashCode() {
111         return value.hashCode();
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public boolean equals(Object o) {
119         if (this == o) {
120             return true;
121         }
122 
123         if (!(o instanceof LineDelimiter)) {
124             return false;
125         }
126 
127         LineDelimiter that = (LineDelimiter) o;
128 
129         return this.value.equals(that.value);
130     }
131 
132     /**
133      * {@inheritDoc}
134      */
135     @Override
136     public String toString() {
137         if (value.length() == 0) {
138             return "delimiter: auto";
139         } else {
140             StringBuilder buf = new StringBuilder();
141             buf.append("delimiter:");
142 
143             for (int i = 0; i < value.length(); i++) {
144                 buf.append(" 0x");
145                 buf.append(Integer.toHexString(value.charAt(i)));
146             }
147 
148             return buf.toString();
149         }
150     }
151 }