MINA源码分析---可移植的操作系统分界符(换行符)
2014-12-10 15:58
267 查看
package org.apache.mina.filter.codec.textline;
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
/**
* A delimiter分界符 which is appended to the end of a text line, such as
* <tt>CR/LF</tt>. This class defines default delimiters for various
* OS :
* <ul>
* <li><b>Unix/Linux</b> : LineDelimiter.UNIX ("\n")</li>
* <li><b>Windows</b> : LineDelimiter.WINDOWS ("\r\n")</li>
* <li><b>MAC</b> : LineDelimiter.MAC ("\r")</li>
* </ul>
*
*/
public class LineDelimiter {
/** the line delimiter constant of the current O/S. */
public static final LineDelimiter DEFAULT;
/** Compute the default delimiter on the current OS
* 计算出本操作系统上默认的分界符(通常说的换行符)
*/
static {
//建立一个字节数组输出流
ByteArrayOutputStream bout = new ByteArrayOutputStream();
//建立一个打印流,参数1是一个标准输出流,决定输出到哪个地方,参数2指定是否刷新输出(不缓冲)
PrintWriter out = new PrintWriter(bout, true);
out.println();//输出一个“换行符”到bout中
DEFAULT = new LineDelimiter(new String(bout.toByteArray()));
}
/**
* A special line delimiter which is used for auto-detection of
* EOL in {@link TextLineDecoder}. If this delimiter is used,
* {@link TextLineDecoder} will consider both <tt>'\r'</tt> and
* <tt>'\n'</tt> as a delimiter.
*/
public static final LineDelimiter AUTO = new LineDelimiter("");
/**
* The CRLF line delimiter constant (<tt>"\r\n"</tt>)
*/
public static final LineDelimiter CRLF = new LineDelimiter("\r\n");
/**
* The line delimiter constant of UNIX (<tt>"\n"</tt>)
*/
public static final LineDelimiter UNIX = new LineDelimiter("\n");
/**
* The line delimiter constant of MS Windows/DOS (<tt>"\r\n"</tt>)
*/
public static final LineDelimiter WINDOWS = CRLF;
/**
* The line delimiter constant of Mac OS (<tt>"\r"</tt>)
*/
public static final LineDelimiter MAC = new LineDelimiter("\r");
/**
* The line delimiter constant for NUL-terminated text protocols
* such as Flash XML socket (<tt>"\0"</tt>)
*/
public static final LineDelimiter NUL = new LineDelimiter("\0");
/** Stores the selected Line delimiter */
private final String value;
/**
* Creates a new line delimiter with the specified <tt>value</tt>.
*/
public LineDelimiter(String value) {
if (value == null) {
throw new IllegalArgumentException("delimiter");
}
this.value = value;
}
/**
* Return the delimiter string.
*/
public String getValue() {
return value;
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return value.hashCode();
}
/**
* {@inheritDoc}
* 不仅比较地址,还比较值
*/
@Override
public boolean equals(Object o) {
if ( this == o) {
return true;
}
if (!(o instanceof LineDelimiter)) {
return false;
}
LineDelimiter that = (LineDelimiter) o;
return this.value.equals(that.value);
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
if (value.length() == 0) {
return "delimiter: auto";
} else {
StringBuilder buf = new StringBuilder();
buf.append("delimiter:");
for (int i = 0; i < value.length(); i++) {
buf.append(" 0x");
//整数转换成16进制字符
buf.append(Integer.toHexString(value.charAt(i)));
}
return buf.toString();
}
}
}
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
/**
* A delimiter分界符 which is appended to the end of a text line, such as
* <tt>CR/LF</tt>. This class defines default delimiters for various
* OS :
* <ul>
* <li><b>Unix/Linux</b> : LineDelimiter.UNIX ("\n")</li>
* <li><b>Windows</b> : LineDelimiter.WINDOWS ("\r\n")</li>
* <li><b>MAC</b> : LineDelimiter.MAC ("\r")</li>
* </ul>
*
*/
public class LineDelimiter {
/** the line delimiter constant of the current O/S. */
public static final LineDelimiter DEFAULT;
/** Compute the default delimiter on the current OS
* 计算出本操作系统上默认的分界符(通常说的换行符)
*/
static {
//建立一个字节数组输出流
ByteArrayOutputStream bout = new ByteArrayOutputStream();
//建立一个打印流,参数1是一个标准输出流,决定输出到哪个地方,参数2指定是否刷新输出(不缓冲)
PrintWriter out = new PrintWriter(bout, true);
out.println();//输出一个“换行符”到bout中
DEFAULT = new LineDelimiter(new String(bout.toByteArray()));
}
/**
* A special line delimiter which is used for auto-detection of
* EOL in {@link TextLineDecoder}. If this delimiter is used,
* {@link TextLineDecoder} will consider both <tt>'\r'</tt> and
* <tt>'\n'</tt> as a delimiter.
*/
public static final LineDelimiter AUTO = new LineDelimiter("");
/**
* The CRLF line delimiter constant (<tt>"\r\n"</tt>)
*/
public static final LineDelimiter CRLF = new LineDelimiter("\r\n");
/**
* The line delimiter constant of UNIX (<tt>"\n"</tt>)
*/
public static final LineDelimiter UNIX = new LineDelimiter("\n");
/**
* The line delimiter constant of MS Windows/DOS (<tt>"\r\n"</tt>)
*/
public static final LineDelimiter WINDOWS = CRLF;
/**
* The line delimiter constant of Mac OS (<tt>"\r"</tt>)
*/
public static final LineDelimiter MAC = new LineDelimiter("\r");
/**
* The line delimiter constant for NUL-terminated text protocols
* such as Flash XML socket (<tt>"\0"</tt>)
*/
public static final LineDelimiter NUL = new LineDelimiter("\0");
/** Stores the selected Line delimiter */
private final String value;
/**
* Creates a new line delimiter with the specified <tt>value</tt>.
*/
public LineDelimiter(String value) {
if (value == null) {
throw new IllegalArgumentException("delimiter");
}
this.value = value;
}
/**
* Return the delimiter string.
*/
public String getValue() {
return value;
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return value.hashCode();
}
/**
* {@inheritDoc}
* 不仅比较地址,还比较值
*/
@Override
public boolean equals(Object o) {
if ( this == o) {
return true;
}
if (!(o instanceof LineDelimiter)) {
return false;
}
LineDelimiter that = (LineDelimiter) o;
return this.value.equals(that.value);
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
if (value.length() == 0) {
return "delimiter: auto";
} else {
StringBuilder buf = new StringBuilder();
buf.append("delimiter:");
for (int i = 0; i < value.length(); i++) {
buf.append(" 0x");
//整数转换成16进制字符
buf.append(Integer.toHexString(value.charAt(i)));
}
return buf.toString();
}
}
}
相关文章推荐
- 基于ARM9处理器的linux-2.6.32.2操作系统内核移植手记part5.2(LCD驱动源码分析及移植之platform driver)
- 蔡军生先生第二人生的源码分析(七十八)LLOSInfo类实现获取操作系统信息
- Samba源码分析与移植
- U-BOOT源码分析及移植
- U-Boot源码分析与移植
- U-BOOT源码分析及移植
- mina2.0 源码分析--- 基于nio的服务端socket监听过程
- 第二人生的源码分析(七十八)LLOSInfo类实现获取操作系统信息
- U-BOOT源码分析及移植-转载
- Mina源码分析:为何过滤器源码中没有对下一个过滤器为null的处理
- 转载:U-BOOT源码分析及移植
- U-BOOT源码分析及移植
- uboot源码分析及移植
- U-Boot源码分析与移植
- U-BOOT源码分析及移植
- [mina源码分析]mina中的reactor模式(一)
- U-BOOT源码分析及移植
- 【转】U-BOOT源码分析及移植
- U-BOOT源码分析及移植
- [转]U-BOOT源码分析及移植