Abstract class for reading character streams. The only methods that a
subclass must implement are read(char[], int, int) and close(). Most
subclasses, however, will override some of the methods defined here in order
to provide higher efficiency, additional functionality, or both.
字元輸入流
程式 <<<----(輸入)---- 檔案
IO體系圖
Java IO體系之Reader
源碼分析
繼承關系
成員變量
/**
* The object used to synchronize operations on this stream.
*/
protected Object lock;
/** Maximum skip-buffer size */
private static final int maxSkipBufferSize = 8192;
/** Skip buffer, null until allocated */
private char skipBuffer[] = null;
構造函數
/**
* Creates a new character-stream reader whose critical sections will
* synchronize on the reader itself.
*/
protected Reader() {
this.lock = this;
}
/**
* Creates a new character-stream reader whose critical sections will
* synchronize on the given object.
* @param lock The Object to synchronize on.
*/
protected Reader(Object lock) {
if (lock == null) {
throw new NullPointerException();
}
this.lock = lock;
}
成員方法
/**
* Attempts to read characters into the specified character buffer.
*/
public int read(java.nio.CharBuffer target) throws IOException {
int len = target.remaining();
char[] cbuf = new char[len];
int n = read(cbuf, 0, len);
if (n > 0)
target.put(cbuf, 0, n);
return n;
}
/**
* Reads a single character.
*/
public int read() throws IOException {
char cb[] = new char[1];
if (read(cb, 0, 1) == -1)
return -1;
else
return cb[0];
}
/**
* Reads characters into an array.
*/
public int read(char cbuf[]) throws IOException {
return read(cbuf, 0, cbuf.length);
}
/**
* Reads characters into a portion of an array.
*/
abstract public int read(char cbuf[], int off, int len) throws IOException;
public long skip(long n) throws IOException {
if (n < 0L)
throw new IllegalArgumentException("skip value is negative");
int nn = (int) Math.min(n, maxSkipBufferSize);
synchronized (lock) {
if ((skipBuffer == null) || (skipBuffer.length < nn))
skipBuffer = new char[nn];
long r = n;
while (r > 0) {
int nc = read(skipBuffer, 0, (int)Math.min(r, nn));
if (nc == -1)
break;
r -= nc;
}
return n - r;
}
}
/**
* Tells whether this stream is ready to be read.
*/
public boolean ready() throws IOException {
return false;
}
/**
* Tells whether this stream supports the mark() operation.
*/
public boolean markSupported() {
return false;
}
/**
* Marks the present position in the stream.
*/
public void mark(int readAheadLimit) throws IOException {
throw new IOException("mark() not supported");
}
/**
* Resets the stream.
*/
public void reset() throws IOException {
throw new IOException("reset() not supported");
}
/**
* Closes the stream and releases any system resources associated with
* it.
*/
abstract public void close() throws IOException;