网站:
JavaEye
作者:
iwinyeah
链接:
http://iwinyeah.javaeye.com/blog/174645
发表时间: 2008年03月21日
声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!
/**
* --------------------------------------------------
* 字段输入流
* --------------------------------------------------
* 从DataInputStream继承
* 主要增加了从文本格式输入流中读入数据字段的能力
* --------------------------------------------------
*
* @author iwinyeah 李永超
* @version 1.0.0
* */
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
public class FieldInputStream extends DataInputStream {
public final static int BIN_MODE = 0;
public final static int TXT_MODE = 1;
int mode;
public FieldInputStream(InputStream in, int mode) {
super(in);
this.mode = mode;
}
public boolean getBoolean() throws IOException {
if (mode == 0) {
return readBoolean();
} else {
if ("1".equals(next())) {
return true;
}
return false;
}
}
public byte getByte() throws IOException {
if (mode == 0) {
return readByte();
} else {
return (byte) Integer.parseInt(next());
}
}
public short getShort() throws IOException {
if (mode == 0) {
return readShort();
} else {
return (short) Integer.parseInt(next());
}
}
public int getInt() throws IOException {
if (mode == 0) {
return readInt();
} else {
return Integer.parseInt(next());
}
}
public long getLong() throws IOException {
if (mode == 0) {
return readLong();
} else {
return Long.parseLong(next());
}
}
public String getString() throws IOException {
if (mode == 0) {
if (read() == 0) {
return null;
} else {
return readUTF();
}
} else {
return next();
}
}
// 取下一标识符
private byte[] buffer = new byte[255];
private int length = 0;
private boolean eos = false;
private final static int INITIAL = 0;
private final static int ESCAPE = 1;
private final static int COMMENT_START = 2;
private final static int LINE_COMMENT = 3;
private final static String WHITESPACE = "\n\r\t";
public String next() throws IOException {
length = 0;
int c = in.read();
int status = INITIAL;
READWHILE: while (c != -1) { // 一直读到文件尾
switch (status) {
case INITIAL:
if (c == '\n' || c == '\t') { // 如果是分隔符
break READWHILE;
} else if (c == '\\') {
status = ESCAPE; // 设转义字符标志
} else if (c == '/') {
status = COMMENT_START; // 设注释标志
} else {
if (WHITESPACE.indexOf(c) < 0) {
append(c);
}
}
break;
case ESCAPE: // 处理转义字符
switch (c) {
case 'n':
append('\n');
break;
case 'r':
append('\r');
break;
case 't':
append('\t');
break;
case 'b':
append('\b');
break;
case 'f':
append('\f');
break;
default:
append(c);
break;
}
status = INITIAL; // 设正常情况标志
break;
case COMMENT_START: // 处理注释
if (c == '/') {
status = LINE_COMMENT; // 是行式注释
} else {
status = INITIAL;
// 如果都不是则把注释起始符和刚读入的字符都加入到标识符中
append('/');
append(c);
}
break;
case LINE_COMMENT:
if (c == '\n') {
status = INITIAL; // 如果当前为行注释状态则要一直读到行尾才恢复正常情况标志
break READWHILE;
}
break;
}
c = in.read(); // 读入下一字符
}
if (c == -1) {
eos = true;
}
// 如果读到文件尾时,标识符长度大于零,则返回标识符,否则返回NULL值
if (length <= 0) {
return null;
} else {
return new String(buffer, 0, length, "UTF-8");
}
}
// 将读入的字符加入缓冲区
private void append(int c) {
// 缓冲区不足时自动扩展
if (length >= buffer.length) {
byte[] xBuffer = new byte[buffer.length + 16];
System.arraycopy(buffer, 0, xBuffer, 0, buffer.length);
buffer = null;
buffer = xBuffer;
}
buffer[length++] = (byte) c;
}
public boolean eos() {
return eos;
}
}
请参看我的另一篇文章:字段输出流FieldOutputStream
http://iwinyeah.javaeye.com/admin/blogs/174644
本文的讨论也很精彩,浏览讨论>>
JavaEye推荐
文章来源:
http://iwinyeah.javaeye.com/blog/174645