J2ME 技術的學習與實踐者

          [導入]字段輸入流FieldInuptStream


          網站: 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;
          	}
          }
          

          請參看我的另一篇文章:字段輸出流FieldOutputStreamhttp://iwinyeah.javaeye.com/admin/blogs/174644
          本文的討論也很精彩,瀏覽討論>>


          JavaEye推薦




          文章來源:http://iwinyeah.javaeye.com/blog/174645

          posted on 2008-03-21 22:19 iwinyeah 閱讀(175) 評論(0)  編輯  收藏 所屬分類: Java 基礎知識

          主站蜘蛛池模板: 乌兰县| 溆浦县| 鹤峰县| 余江县| 江华| 中宁县| 仙桃市| 鄂伦春自治旗| 麻阳| 和龙市| 乌兰县| 竹北市| 南丰县| 合山市| 怀集县| 犍为县| 马龙县| 宁晋县| 南投市| 和田市| 盐边县| 麻城市| 雷州市| 静安区| 报价| 加查县| 衡阳市| 西丰县| 科技| 什邡市| 肃南| 沾化县| 巨鹿县| 开封市| 苍溪县| 宣武区| 原阳县| 枣阳市| 阿克陶县| 长沙县| 施秉县|