001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.filter.codec.textline;
021:
022: import java.io.ByteArrayOutputStream;
023: import java.io.PrintWriter;
024:
025: /**
026: * A delimiter which is appended to the end of a text line, such as
027: * <tt>CR/LF</tt>.
028: *
029: * @author The Apache MINA Project (dev@mina.apache.org)
030: * @version $Rev: 576217 $, $Date: 2007-09-16 17:55:27 -0600 (Sun, 16 Sep 2007) $
031: */
032: public class LineDelimiter {
033: /**
034: * the line delimiter constant of the current O/S.
035: */
036: public static final LineDelimiter DEFAULT;
037:
038: static {
039: ByteArrayOutputStream bout = new ByteArrayOutputStream();
040: PrintWriter out = new PrintWriter(bout);
041: out.println();
042: DEFAULT = new LineDelimiter(new String(bout.toByteArray()));
043: }
044:
045: /**
046: * A special line delimiter which is used for auto-detection of
047: * EOL in {@link TextLineDecoder}. If this delimiter is used,
048: * {@link TextLineDecoder} will consider both <tt>'\r'</tt> and
049: * <tt>'\n'</tt> as a delimiter.
050: */
051: public static final LineDelimiter AUTO = new LineDelimiter("");
052:
053: /**
054: * The line delimiter constant of UNIX (<tt>"\n"</tt>)
055: */
056: public static final LineDelimiter UNIX = new LineDelimiter("\n");
057:
058: /**
059: * The line delimiter constant of MS Windows/DOS (<tt>"\r\n"</tt>)
060: */
061: public static final LineDelimiter WINDOWS = new LineDelimiter(
062: "\r\n");
063:
064: /**
065: * The line delimiter constant of Mac OS (<tt>"\r"</tt>)
066: */
067: public static final LineDelimiter MAC = new LineDelimiter("\r");
068:
069: /**
070: * The line delimiter constant for NUL-terminated text protocols
071: * such as Flash XML socket (<tt>"\0"</tt>)
072: */
073: public static final LineDelimiter NUL = new LineDelimiter("\0");
074:
075: private final String value;
076:
077: /**
078: * Creates a new line delimiter with the specified <tt>value</tt>.
079: */
080: public LineDelimiter(String value) {
081: if (value == null) {
082: throw new NullPointerException("delimiter");
083: }
084: this .value = value;
085: }
086:
087: /**
088: * Return the delimiter string.
089: */
090: public String getValue() {
091: return value;
092: }
093:
094: @Override
095: public int hashCode() {
096: return value.hashCode();
097: }
098:
099: @Override
100: public boolean equals(Object o) {
101: if (!(o instanceof LineDelimiter)) {
102: return false;
103: }
104:
105: LineDelimiter that = (LineDelimiter) o;
106: return this .value.equals(that.value);
107: }
108:
109: @Override
110: public String toString() {
111: StringBuffer buf = new StringBuffer();
112: buf.append("delimiter:");
113: if (value.length() == 0) {
114: buf.append(" auto");
115: } else {
116: for (int i = 0; i < value.length(); i++) {
117: buf.append(" 0x");
118: buf.append(Integer.toHexString(value.charAt(i)));
119: }
120: }
121: return buf.toString();
122: }
123: }
|