StringReader 源码分析


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;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值