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