Java7之传统I/O-字符类CharArrayReader和CharArrayWriter

转载自:http://www.it165.net/pro/html/201402/9354.html

CharArrayReader和CharArrayWriter是字符数组流。它和ByteArrayInputStream、ByteArrayOutputStream类似,只不过ByteArrayXXputStream是字节数组流,而CharArrayXX是字符数组流。

CharArrayWriter是用于读取字符数组,它继承于Writer类。以字符为单位进行数组的操作。下面来看一下主要方法实现的源代码:

1、创建及初始化

看一下CharArrayWritrer中定义的重要变量及构造函数:

view source print ?
01. protected char buf[];  // 字符数组,用于数组的存取
02. protected int count;   // 数组中数据元素个数
03. public CharArrayWriter() {
04.     this(32);
05. }
06. public CharArrayWriter(int initialSize) {
07.     if (initialSize < 0) {
08.         throw new IllegalArgumentException("Negative initial size: " + initialSize);
09.     }
10.     buf = new char[initialSize];
11. }

默认buf数组的大小为32,也可以自己进行指定。

CharArrayReader中定义的重要变量及构造函数:

view source print ?
01. protected char buf[];         // 字符数组,用于数组的存取
02. protected int pos;            // 当前读取的位置
03. protected int markedPos = 0// 设置的标识位
04.  
05. protected int count;          // 数组中数据元素个数
06.  
07. public CharArrayReader(char buf[]) {
08.     this.buf = buf;
09.     this.pos = 0;
10.     this.count = buf.length;
11. }
12.  
13. public CharArrayReader(char buf[], int offset, int length) {
14.     if ((offset < 0) || (offset > buf.length) || (length < 0) || ((offset + length) < 0)) {
15.         throw new IllegalArgumentException();
16.     }
17.     this.buf = buf;
18.     this.pos = offset;
19.     this.count = Math.min(offset + length, buf.length);
20.     this.markedPos = offset;
21. }



2、CharArrayWritrer写入数据

view source print ?
01. public void write(int c) {
02.     synchronized (lock) {
03.         int newcount = count + 1;
04.         if (newcount > buf.length) {
05.             buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
06.         }
07.         buf[count] = (char)c;
08.         count = newcount;
09.     }
10. }
11.  
12. public void write(char c[], int off, int len) {
13.     if ((off < 0) || (off > c.length) || (len < 0) || ((off + len) > c.length) || ((off + len) < 0)) {
14.         throw new IndexOutOfBoundsException();
15.     } else if (len == 0) {
16.         return;
17.     }
18.     synchronized (lock) {
19.         int newcount = count + len;
20.         if (newcount > buf.length) {
21.             buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
22.         }
23.         System.arraycopy(c, off, buf, count, len);
24.         count = newcount;
25.     }
26. }
27.  
28. public void write(String str, int off, int len) {
29.     synchronized (lock) {
30.         int newcount = count + len;
31.         if (newcount > buf.length) {
32.             buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
33.         }
34.         str.getChars(off, off + len, buf, count);
35.         count = newcount;
36.     }
37. }

实现了Write类中定义的write()方法和append()方法。其实现代码还是比较简单的,在前面类似的方法已经说过多遍了,这里不再赘述。

3、CharArrayReader读取数据

CharArrayReader 是用于读取字符数组,它继承于Reader。操作的数据是以字符为单位。下面来看一下主要方法实现的源代码:

view source print ?
01. public int read() throws IOException {
02.     synchronized (lock) {
03.         ensureOpen();
04.         if (pos >= count)
05.             return -1;
06.         else
07.             return buf[pos++];
08.     }
09. }
10.  
11. public int read(char b[], int off, int len) throws IOException {
12.     synchronized (lock) {
13.         ensureOpen();
14.         if ((off < 0) || (off > b.length) || (len < 0) ||
15.             ((off + len) > b.length) || ((off + len) < 0)) {
16.             throw new IndexOutOfBoundsException();
17.         } else if (len == 0) {
18.             return 0;
19.         }
20.  
21.         if (pos >= count) {
22.             return -1;
23.         }
24.         if (pos + len > count) {
25.             len = count - pos;
26.         }
27.         if (len <= 0) {
28.             return 0;
29.         }
30.         System.arraycopy(buf, pos, b, off, len);
31.         pos += len;
32.         return len;
33.     }
34. }

与操作字符串的类StringReader、StringWriter类似,其实字符数组与字符串操作有相通性,字符串在底层实际上就是由字符数组来实现的。如果理解了StringReader和StringWriter类,这个类会非常容易理解。另外提醒一点,这两个类也是线程安全的。

下面来编写一个简单的测试程序:

view source print ?
1. char[] x={'a','d','p'};
2. CharArrayWriter cw=new CharArrayWriter();
3. cw.write(x,0,2);
4. cw.append("x");
5. System.out.println(cw.toString()); // adx
6.  
7. CharArrayReader cr=new CharArrayReader(cw.toCharArray());
8. System.out.println((char)cr.read());// a


你可能感兴趣的:(Java7之传统I/O-字符类CharArrayReader和CharArrayWriter)