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>.
28   *
29   * @author The Apache MINA Project (dev@mina.apache.org)
30   * @version $Rev: 714210 $, $Date: 2008-11-15 02:30:50 +0100 (Sat, 15 Nov 2008) $
31   */
32  public class LineDelimiter {
33      /**
34       * the line delimiter constant of the current O/S.
35       */
36      public static final LineDelimiter DEFAULT;
37  
38      static {
39          ByteArrayOutputStream bout = new ByteArrayOutputStream();
40          PrintWriter out = new PrintWriter(bout);
41          out.println();
42          DEFAULT = new LineDelimiter(new String(bout.toByteArray()));
43      }
44  
45      /**
46       * A special line delimiter which is used for auto-detection of
47       * EOL in {@link TextLineDecoder}.  If this delimiter is used,
48       * {@link TextLineDecoder} will consider both  <tt>'\r'</tt> and
49       * <tt>'\n'</tt> as a delimiter.
50       */
51      public static final LineDelimiter AUTO = new LineDelimiter("");
52  
53      /**
54       * The CRLF line delimiter constant (<tt>"\r\n"</tt>)
55       */
56      public static final LineDelimiter CRLF = new LineDelimiter("\r\n");
57          
58      /**
59       * The line delimiter constant of UNIX (<tt>"\n"</tt>)
60       */
61      public static final LineDelimiter UNIX = new LineDelimiter("\n");
62  
63      /**
64       * The line delimiter constant of MS Windows/DOS (<tt>"\r\n"</tt>)
65       */
66      public static final LineDelimiter WINDOWS = CRLF;
67  
68      /**
69       * The line delimiter constant of Mac OS (<tt>"\r"</tt>)
70       */
71      public static final LineDelimiter MAC = new LineDelimiter("\r");
72  
73      /**
74       * The line delimiter constant for NUL-terminated text protocols
75       * such as Flash XML socket (<tt>"\0"</tt>)
76       */
77      public static final LineDelimiter NUL = new LineDelimiter("\0");
78  
79      private final String value;
80  
81      /**
82       * Creates a new line delimiter with the specified <tt>value</tt>.
83       */
84      public LineDelimiter(String value) {
85          if (value == null) {
86              throw new NullPointerException("delimiter");
87          }
88          this.value = value;
89      }
90  
91      /**
92       * Return the delimiter string.
93       */
94      public String getValue() {
95          return value;
96      }
97  
98      @Override
99      public int hashCode() {
100         return value.hashCode();
101     }
102 
103     @Override
104     public boolean equals(Object o) {
105         if (!(o instanceof LineDelimiter)) {
106             return false;
107         }
108 
109         LineDelimiter that = (LineDelimiter) o;
110         return this.value.equals(that.value);
111     }
112 
113     @Override
114     public String toString() {
115         StringBuffer buf = new StringBuffer();
116         buf.append("delimiter:");
117         if (value.length() == 0) {
118             buf.append(" auto");
119         } else {
120             for (int i = 0; i < value.length(); i++) {
121                 buf.append(" 0x");
122                 buf.append(Integer.toHexString(value.charAt(i)));
123             }
124         }
125         return buf.toString();
126     }
127 }