?? lookaheadreader.java
字號:
package org.kxml.io;import java.io.*;import java.util.*;/** Like a reader, but has a peek function that shows the next char. Unlike reader, it does not throw IOExceptions. 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 + -