CharArrayReader 介绍
CharArrayReader 是字符数组输入流。它和ByteArrayInputStream类似,只不过ByteArrayInputStream是字节数组输入流,而CharArray是字符数组输入流。CharArrayReader 是用于读取字符数组,它继承于Reader。操作的数据是以字符为单位!
CharArrayReader 函数列表
CharArrayReader(char[] buf) CharArrayReader(char[] buf, int offset, int length) void close() void mark(int readLimit) boolean markSupported() int read() int read(char[] buffer, int offset, int len) boolean ready() void reset() long skip(long charCount)
Reader和CharArrayReader源码分析
Reader是CharArrayReader的父类,我们先看看Reader的源码,然后再学CharArrayReader的源码。
1. Reader源码分析(基于jdk1.7.40)
package java.io; public abstract class Reader implements Readable, Closeable { protected Object lock; protected Reader() { this.lock = this; } protected Reader(Object lock) { if (lock == null) { throw new NullPointerException(); } this.lock = lock; } 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; } public int read() throws IOException { char cb[] = new char[1]; if (read(cb, 0, 1) == -1) return -1; else return cb[0]; } public int read(char cbuf[]) throws IOException { return read(cbuf, 0, cbuf.length); } abstract public int read(char cbuf[], int off, int len) throws IOException; private static final int maxSkipBufferSize = 8192; private char skipBuffer[] = null; 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; } } public boolean ready() throws IOException { return false; } public boolean markSupported() { return false; } public void mark(int readAheadLimit) throws IOException { throw new IOException("mark() not supported"); } public void reset() throws IOException { throw new IOException("reset() not supported"); } abstract public void close() throws IOException; }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索int
, char
, public
, throws
, Closeable
, ioException
CharArrayReader
,以便于您获取更多的相关知识。
时间: 2024-12-27 18:16:49