CharArrayReader和CharArrayWriter是字符数组流。它和ByteArrayInputStream、ByteArrayOutputStream类似,只不过ByteArrayXXputStream是字节数组流,而CharArrayXX是字符数组流。
CharArrayWriter是用于读取字符数组,它继承于Writer类。以字符为单位进行数组的操作。下面来看一下主要方法实现的源代码:
1、创建及初始化
看一下CharArrayWritrer中定义的重要变量及构造函数:
protected char buf[]; // 字符数组,用于数组的存取 protected int count; // 数组中数据元素个数 public CharArrayWriter() { this(32); } public CharArrayWriter(int initialSize) { if (initialSize < 0) { throw new IllegalArgumentException("Negative initial size: " + initialSize); } buf = new char[initialSize]; }默认buf数组的大小为32,也可以自己进行指定。
CharArrayReader中定义的重要变量及构造函数:
protected char buf[]; // 字符数组,用于数组的存取 protected int pos; // 当前读取的位置 protected int markedPos = 0; // 设置的标识位 protected int count; // 数组中数据元素个数 public CharArrayReader(char buf[]) { this.buf = buf; this.pos = 0; this.count = buf.length; } public CharArrayReader(char buf[], int offset, int length) { if ((offset < 0) || (offset > buf.length) || (length < 0) || ((offset + length) < 0)) { throw new IllegalArgumentException(); } this.buf = buf; this.pos = offset; this.count = Math.min(offset + length, buf.length); this.markedPos = offset; }
2、CharArrayWritrer写入数据
public void write(int c) { synchronized (lock) { int newcount = count + 1; if (newcount > buf.length) { buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount)); } buf[count] = (char)c; count = newcount; } } public void write(char c[], int off, int len) { if ((off < 0) || (off > c.length) || (len < 0) || ((off + len) > c.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; } synchronized (lock) { int newcount = count + len; if (newcount > buf.length) { buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount)); } System.arraycopy(c, off, buf, count, len); count = newcount; } } public void write(String str, int off, int len) { synchronized (lock) { int newcount = count + len; if (newcount > buf.length) { buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount)); } str.getChars(off, off + len, buf, count); count = newcount; } }
实现了Write类中定义的write()方法和append()方法。其实现代码还是比较简单的,在前面类似的方法已经说过多遍了,这里不再赘述。
3、CharArrayReader读取数据
CharArrayReader 是用于读取字符数组,它继承于Reader。操作的数据是以字符为单位。下面来看一下主要方法实现的源代码:
public int read() throws IOException { synchronized (lock) { ensureOpen(); if (pos >= count) return -1; else return buf[pos++]; } } public int read(char b[], int off, int len) throws IOException { synchronized (lock) { ensureOpen(); if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return 0; } if (pos >= count) { return -1; } if (pos + len > count) { len = count - pos; } if (len <= 0) { return 0; } System.arraycopy(buf, pos, b, off, len); pos += len; return len; } }
与操作字符串的类StringReader、StringWriter类似,其实字符数组与字符串操作有相通性,字符串在底层实际上就是由字符数组来实现的。如果理解了StringReader和StringWriter类,这个类会非常容易理解。另外提醒一点,这两个类也是线程安全的。
下面来编写一个简单的测试程序:
char[] x={'a','d','p'}; CharArrayWriter cw=new CharArrayWriter(); cw.write(x,0,2); cw.append("x"); System.out.println(cw.toString()); // adx CharArrayReader cr=new CharArrayReader(cw.toCharArray()); System.out.println((char)cr.read());// a