📄 input.java
字号:
package lolo;import java.io.Reader;import java.io.IOException;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.StringReader;/** A <tt>Scanner</tt> gets the characters from an <tt>Input</tt> instance. * * @author <a href="http://www.inf.uos.de/bernd" target="_blank">Bernd Kühl</a> (<a href="mailto:bernd@informatik.uni-osnabrueck.de">bernd@informatik.uni-osnabrueck.de</a>) * @see Scanner * @see Scanner#scan(lolo.Input) */public class Input { /** The character source. */ protected final Reader reader; /** The characters are buffered in this buffer. */ protected char[] buffer; /** Creates an instance. * * @param reader the character source. * @param bufferSize sets the initial buffer size. */ public Input(Reader reader, int bufferSize) { if (reader == null) throw new IllegalArgumentException("reader == null"); if (bufferSize <= 0) throw new IllegalArgumentException("bufferSize <= 0"); this.reader = reader; buffer = new char[bufferSize]; } /** <tt>buffer[next]</tt> is the next character. */ protected int next; /** Marks the position of the first character of a symbol. */ protected int startOfSymbol = -1; /** Marks <tt>buffer[next]</tt> as the first character of a new symbol * and the <tt>mark</tt> flag is turned off. * All symbols characters will not be discarded. * * @see lolo.Input#mark * @see lolo.Input#unsetSymbol() */ protected void setStartSymbol() { startOfSymbol = next; mark = -1; } /** Unmarks which characters belong to a symbol. The characters can be discarde from the buffer. */ protected void unsetSymbol() { startOfSymbol = -1; } /** if <tt>mark</tt> is greater then 0, then all characters from <tt>buffer[mark]</tt> * up to <tt>buffer[next-1]</tt> are not discarded. */ protected int mark = -1; /** <tt>buffer[filled]</tt> is the last character in the buffer. */ protected int filled; /** Returns the character read, as an integer in the range 0 to 65535 (0x00-0xffff), * or -1 if the end of the stream has been reached *@return The character read, as an integer in the range 0 to 65535 (0x00-0xffff), * or -1 if the end of the stream has been reached */ public int next() throws IOException { // neue Zeichen vom Reader holen? if (next == filled) { if (mark >= 0 || startOfSymbol >= 0) { // ist eine Position markiert? int position; if (mark >= 0) if (startOfSymbol >=0) position = startOfSymbol > mark ? mark : startOfSymbol; else position = mark; else position = startOfSymbol; if (position == 0 && filled == buffer.length) { char[] help = new char[buffer.length*2]; System.arraycopy(buffer, 0, help, 0, buffer.length); buffer = help; } else { // abschneiden mit mark oder startOfSymbol System.arraycopy(buffer, position, buffer, 0, filled-position); next -= position; filled -= position; if (mark >= 0) mark -= position; if (startOfSymbol >= 0) startOfSymbol -= position; } } else { // abschneiden next = filled = 0; } // Zeichen holen int n = reader.read(buffer, filled, buffer.length-filled); if (n < 0) return -1; if (n == 0) throw new RuntimeException("Can not happen: n == 0"); filled += n; } return buffer[next++]; } /** Marks the position of the next character. */ public void mark() { mark = next; } /** Unmarks the buffer. */ public void unmark() { mark = -1; } /** Resets <tt>next</tt> to the marked position. */ public void pushBackToMarked() { if (mark < 0) throw new IllegalArgumentException("mark < 0"); next = mark; } /** returns <tt>"Input["+reader+"]"</tt>. * @return <tt>"Input["+reader+"]"</tt>. */ public String toString() { return "Input["+reader+"]"; } /** A simple test for the <tt>Input</tt> class. */ public static void main(String args[]) throws Exception { Input input = new Input(new StringReader("abcdefghijklmnopqrstuvwxyz"), 2); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = in.readLine()) != null) { char cmd = line.charAt(0); switch (cmd) { case 's': input.setStartSymbol(); break; case 'e': input.unsetSymbol(); break; case 'm': input.mark(); break; case 'u': input.unmark(); break; case 'p': input.pushBackToMarked(); break; case '?': System.err.println("next "+input.next+" filled "+input.filled+ " mark "+input.mark+" startOfSymbol "+input.startOfSymbol+ " buffer -"+new String(input.buffer, 0, input.filled)+"-"); break; case 'n': int ch = input.next(); System.err.println("ch is "+ch+" "+(char)ch); break; default: System.err.println("unkown command "+cmd); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -