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 */ 020package org.apache.mina.filter.codec.textline; 021 022import java.io.ByteArrayOutputStream; 023import 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>. This class defines default delimiters for various 028 * OS : 029 * <ul> 030 * <li><b>Unix/Linux</b> : LineDelimiter.UNIX ("\n")</li> 031 * <li><b>Windows</b> : LineDelimiter.WINDOWS ("\r\n")</li> 032 * <li><b>MAC</b> : LineDelimiter.MAC ("\r")</li> 033 * </ul> 034 * 035 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 036 */ 037public class LineDelimiter { 038 /** the line delimiter constant of the current O/S. */ 039 public static final LineDelimiter DEFAULT; 040 041 /** Compute the default delimiter on he current OS */ 042 static { 043 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 044 PrintWriter out = new PrintWriter(bout, true); 045 out.println(); 046 DEFAULT = new LineDelimiter(new String(bout.toByteArray())); 047 } 048 049 /** 050 * A special line delimiter which is used for auto-detection of 051 * EOL in {@link TextLineDecoder}. If this delimiter is used, 052 * {@link TextLineDecoder} will consider both <tt>'\r'</tt> and 053 * <tt>'\n'</tt> as a delimiter. 054 */ 055 public static final LineDelimiter AUTO = new LineDelimiter(""); 056 057 /** 058 * The CRLF line delimiter constant (<tt>"\r\n"</tt>) 059 */ 060 public static final LineDelimiter CRLF = new LineDelimiter("\r\n"); 061 062 /** 063 * The line delimiter constant of UNIX (<tt>"\n"</tt>) 064 */ 065 public static final LineDelimiter UNIX = new LineDelimiter("\n"); 066 067 /** 068 * The line delimiter constant of MS Windows/DOS (<tt>"\r\n"</tt>) 069 */ 070 public static final LineDelimiter WINDOWS = CRLF; 071 072 /** 073 * The line delimiter constant of Mac OS (<tt>"\r"</tt>) 074 */ 075 public static final LineDelimiter MAC = new LineDelimiter("\r"); 076 077 /** 078 * The line delimiter constant for NUL-terminated text protocols 079 * such as Flash XML socket (<tt>"\0"</tt>) 080 */ 081 public static final LineDelimiter NUL = new LineDelimiter("\0"); 082 083 /** Stores the selected Line delimiter */ 084 private final String value; 085 086 /** 087 * Creates a new line delimiter with the specified <tt>value</tt>. 088 * 089 * @param value The new Line Delimiter 090 */ 091 public LineDelimiter(String value) { 092 if (value == null) { 093 throw new IllegalArgumentException("delimiter"); 094 } 095 096 this.value = value; 097 } 098 099 /** 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}