?? textinputstream.java
字號:
/* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */import java.io.*;
public class TextInputStream extends FilterInputStream {
public TextInputStream (InputStream in) {
super (in);
}
protected int pushback = -2;
public synchronized int read () throws IOException {
int datum = pushback;
if (datum == -2)
datum = super.read();
else
pushback = -2;
return datum;
}
public synchronized int read (byte[] data, int offset, int length)
throws IOException {
if (length <= 0) {
return 0;
} else if (pushback == -2) {
return super.read (data, offset, length);
} else if (pushback == -1) {
pushback = -2;
return -1;
} else {
data[offset] = (byte) pushback;
pushback = -2;
return 1;
}
}
public synchronized long skip (long amount) throws IOException {
if (amount <= 0) {
return 0;
} else if (pushback == -2) {
return super.skip (amount);
} else if (pushback == -1) {
pushback = -2;
return 0;
} else {
pushback = -2;
return 1;
}
}
public synchronized int available () throws IOException {
return (pushback == -2) ? super.available () : (pushback >= 0) ? 1 : 0;
}
protected synchronized void unread (int chr) throws IOException {
if (pushback != -2)
throw new IOException ("Pushback overflow");
pushback = chr;
}
public void skipWhitespace () throws IOException {
int chr;
do {
chr = read ();
} while ((chr != -1) && Character.isWhitespace ((char) chr));
unread (chr);
}
public int readInt () throws IOException, NumberFormatException {
StringBuffer result = new StringBuffer ();
skipWhitespace ();
int chr = read ();
if ((chr == '-') || Character.isDigit ((char) chr)) {
result.append ((char) chr);
while (((chr = read ()) != -1) && Character.isDigit ((char) chr))
result.append ((char) chr);
}
unread (chr);
String value = result.toString ();
if ((value.equals ("") || value.equals ("-")) && (chr == -1))
throw new EOFException ("EOF reading int");
return Integer.parseInt (value);
}
public String readWord () throws IOException {
StringBuffer result = new StringBuffer ();
skipWhitespace ();
int chr;
while (((chr = read ()) != -1) && !Character.isWhitespace ((char) chr))
result.append ((char) chr);
unread (chr);
if (result.length () == 0)
throw new EOFException ("EOF reading word");
return result.toString ();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -