public class StringReader extends Reader {
private String str;
private int length;
private int next = 0;
private int mark = 0;
// 创建一个新字符串 reader
public StringReader(String s) {
this.str = s;
this.length = s.length();
}
// 如果字符串为null,说明已经被关闭了,抛异常
private void ensureOpen() throws IOException {
if (str == null)
throw new IOException("Stream closed");
}
// 读取单个字符。如果已到达流的末尾,则返回 -1
public int read() throws IOException {
synchronized (lock) {
ensureOpen();
if (next >= length)
return -1;
return str.charAt(next++);
}
}
// 从当前字符串中读取指定数量的字符,放到字符数组中。
public int read(char cbuf[], int off, int len) throws IOException {
synchronized (lock) {
ensureOpen();
if ((off < 0) || (off > cbuf.length) || (len < 0) ||
((off + len) > cbuf.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
if (next >= length)
return -1;
int n = Math.min(length - next, len);
str.getChars(next, next + n, cbuf, off);
next += n;
return n;
}
}
// 跳过流中指定数量的字符。返回跳过的字符数。
public long skip(long ns) throws IOException {
synchronized (lock) {
ensureOpen();
if (next >= length)
return 0;
// Bound skip by beginning and end of the source
long n = Math.min(length - next, ns); // 当ns为正数时
n = Math.max(-next, n); // 当ns为负数时
next += n;
return n;
}
}
// 判断此流是否已经准备好用于读取。
public boolean ready() throws IOException {
synchronized (lock) {
ensureOpen();
return true;
}
}
// 判断此流是否支持 mark() 操作。
public boolean markSupported() {
return true;
}
// 标记流中的当前位置。对reset的后续调用会将该流重新定位到此点。
public void mark(int readAheadLimit) throws IOException {
if (readAheadLimit < 0){
throw new IllegalArgumentException("Read-ahead limit < 0");
}
synchronized (lock) {
ensureOpen();
mark = next;
}
}
// 将该流重置为最新的标记,如果从未标记过,则将其重置到该字符串的开头。
public void reset() throws IOException {
synchronized (lock) {
ensureOpen();
next = mark;
}
}
// 关闭该流并释放与之关联的所有系统资源。在关闭该流后,再调用 read()、ready()、mark() 或 reset() 将抛出 IOException。关闭以前关闭的流无效。
// 这里的实现是将字符串的引用赋为null。
public void close() {
str = null;
}
}
StringReader 源码分析
最新推荐文章于 2024-09-15 09:47:04 发布