java语言基础

【Java IO】 五、StringWriter ,String

2018-12-19  本文已影响0人  6d898101c4c9

这六个类放在一起是因为它们很像,都是直接继承与 writer 或Reader或InputStream,实现了其中的read,writer方法,其次就是它们都是在该对象内维护了一成员变量,作为'流'的源头或目的地。

一,StringWriter,StringReader

以下是StringWriter的源码(去掉了一些不重要的方法)

public class StringWriter extends Writer {
    //内部维护的StringBuffer,作为流的目的地
    private StringBuffer buf;
    //构造方法
    public StringWriter() {
        buf = new StringBuffer();
        lock = buf;
    }
   //构造方法,指定了StringBuffer的大小
    public StringWriter(int initialSize) {
        if (initialSize < 0) {
            throw new IllegalArgumentException("Negative buffer size");
        }
        buf = new StringBuffer(initialSize);
        lock = buf;
    }
    //直接调用Stringbuffer的方法
    public void write(int c) {
        buf.append((char) c);
    }

    //直接调用Stringbuffer的方法
    public void write(char cbuf[], int off, int len) {
        if ((off < 0) || (off > cbuf.length) || (len < 0) ||
            ((off + len) > cbuf.length) || ((off + len) < 0)) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return;
        }
        buf.append(cbuf, off, len);
    }

//将StringBuffer返回
    public StringBuffer getBuffer() {
        return buf;
    }
}

以下是StringReader的源码(去掉了一些不重要的方法)

public class StringReader extends Reader {

   //内部维护的String,需在new该对象时指定
    private String str;
   //长度
    private int length;
//下一个字符的下标
    private int next = 0;
    private int mark = 0;

    public StringReader(String s) {
        this.str = s;
        this.length = s.length();
    }

//如果String为null,当然这个流就是不可打开啦
    private void ensureOpen() throws IOException {
        if (str == null)
            throw new IOException("Stream closed");
    }
//如果next超出String长度,说明流已读取完,返回-1
    public int read() throws IOException {
        synchronized (lock) {
            ensureOpen();
            if (next >= length)
                return -1;
            return str.charAt(next++);
        }
    }

//读取指定的长度,放入char数组中
    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后移
            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);
            n = Math.max(-next, n);
            next += n;
            return n;
        }
    }
   //确定流是否准备好
    public boolean ready() throws IOException {
        synchronized (lock) {
        ensureOpen();
        return true;
        }
    }
//释放资源
    public void close() {
        str = null;
    }
}

CharArrayReader,CharArrayWriter
ByteArrayInputStream,ByteArrayOutputStream
这四个类也是同理,只是内部的成员变量有所不同而已。

上一篇下一篇

猜你喜欢

热点阅读