⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lookaheadreader.java

📁 J2ME核心类及MIDlet类 MIDP用户界面对象 图形处理及低级事件处理 多线程编程 I/O及网络编程 数据库RMS编程 浮点数编程 多媒体及GAME API编程 安全、加密及
💻 JAVA
字号:
package org.kxml.io;import java.io.*;import java.util.*;/** Like a reader, but has a peek function that shows the next char.    Useful for building all kind of parsers */public class LookAheadReader extends Reader {    protected Reader reader;    int next;    int lineNumber;    int columnNumber;    /** Creates a new LookAheadReader instance based on the 	given reader */    public LookAheadReader (Reader reader) throws IOException {	this.reader = reader;	read ();    }    /** closes the LookAheadReader by closing the underlying reader */        public void close () throws IOException {	try {	    reader.close ();	}	catch (IOException e) {	    throw new ParseException (null, e, lineNumber, columnNumber);	}    }    /** returns true when the end of the stream is reached */    public boolean eof () {	return next == -1;    }    /** returns the current line number */    public int getLineNumber () {	return lineNumber;    }    /** returns the current column number */    public int getColumnNumber () {	return columnNumber;    }        /** skips unicode whitespace chars */     public void skipWhitespace () throws IOException {	while (next != -1 && next <= 32) {	    read ();	}    }    /** unlike Reader.read, this method does not return	before all bytes are read (make this consistent with	datainput readAll?!?). */    public int read (char [] buf, int off, int len) throws IOException {		int ok = 0;	while (ready () && ok < len) {	    int i = read ();	    if (i == -1) break;	    buf [off++] = (char) i;	    ok++;	}		return ok;    }    public String readN (int count) throws IOException {	char [] buf = new char [count];	read (buf, 0, count);	return new String (buf);    }    /** Returns true if the stream is ready for reading, false        otherwise. */        public boolean ready () throws IOException {	try {	    return reader.ready ();	}	catch (IOException e) {	    throw new ParseException (null, e, lineNumber, columnNumber);	}    }    /** Reads an identifier from the stream */    public String readIdentifier () throws IOException {	StringBuffer result = new StringBuffer ();	while ((next >= '0' && next <= '9' && result.length () > 0)	       || (next >= 'a' && next <= 'z')	       || (next >= 'A' && next <= 'Z')	       || (next >= 128))	    	       result.append ((char) read ());		return result.toString ();    }           /** Reads a String until any of the given stopChars is reached.	The stopChar itself is not consumed. */    public String readTo (String stopChars) throws IOException {	StringBuffer result = new StringBuffer ();	while (next != -1 	       && stopChars.indexOf ((char) next) == -1)	    	    result.append ((char) read ());	return result.toString ();    }        /** Reads a String until the given stopChar is reached. The	stopChar itself is not consumed. */    public String readTo (char stopChar) throws IOException {	StringBuffer result = new StringBuffer ();	while (next != -1 && next != stopChar) 	    result.append ((char) read ());    	return result.toString ();    }    /** Consumes characters from the stream until one of the given        stopChars is reached. */    public int skipTo (String stopChars) throws IOException {	while (next != -1 	       && stopChars.indexOf ((char) next) == -1) 	    read ();		return next;    }    /** Reads a String from the stream until a char not in 	acceptChars is reached. */    public String readWhile (String acceptChars) throws IOException {	StringBuffer result = new StringBuffer ();		while (next != -1 && 	       acceptChars.indexOf ((char) next) != -1) 	    result.append ((char) read ());     	return result.toString ();    }    /** Returns the next character in the stream without consuming it. */    public int peek () {	return next;    }    /** Reads a single character from the stream. Returns -1 if the	end of the stream has been reached. */    public int read () throws IOException {	int result = next;	if (next != -1) {	    try {		next = reader.read ();	    }	    catch (IOException e) {		throw new ParseException 		    (null, e, lineNumber, columnNumber);	    }	}	if (next == 10) {	    lineNumber++;	    columnNumber = 0;	}	else columnNumber++;	return result;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -