java io StringWriter源码分析

目录

简介

字段buf,2个构造函数

4个write方法,3个append方法

方法toString,getBuffer,flush,close


简介

package java.io;


/**
 * 在字符串缓冲区中收集其输出的字符流,然后可用于构造字符串。
 * <p>
 * 关闭StringWriter没有效果。这个类中的方法可以在流被关闭后调用,而不会产生IOException。
 *
 * @author      Mark Reinhold
 * @since       JDK1.1
 */

public class StringWriter extends Writer 

字段buf,2个构造函数

    private StringBuffer buf;

    /**
     * 使用默认的初始字符串缓存长度(16个字符),创建一个新的字符串写入器。
     */
    public StringWriter() {
        buf = new StringBuffer();
        lock = buf;
    }

    /**
     * 使用指定的初始字符串缓存大小,创建一个新的字符串写入器。
     *
     * @param initialSize
     *        The number of <tt>char</tt> values that will fit into this buffer
     *        before it is automatically expanded
     *
     * @throws IllegalArgumentException
     *         If <tt>initialSize</tt> is negative
     */
    public StringWriter(int initialSize) {
        if (initialSize < 0) {
            throw new IllegalArgumentException("Negative buffer size");
        }
        buf = new StringBuffer(initialSize);
        lock = buf;
    }

4个write方法,3个append方法

    /**
     * 只写一个字符。
     */
    public void write(int c) {
    	// buf后面加上c
        buf.append((char) c);
    }

    /**
     * 写入一个字符数组的一部分。
     *
     * @param  cbuf  Array of characters
     * @param  off   Offset from which to start writing characters
     * @param  len   Number of characters to write
     */
    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后面加上cbuf的一部分
        buf.append(cbuf, off, len);
    }

    /**
     * 写入一个字符串。
     */
    public void write(String str) {
        buf.append(str);
    }

    /**
     * 写入一个字符串的一部分。
     *
     * @param  str  String to be written
     * @param  off  Offset from which to start writing characters
     * @param  len  Number of characters to write
     */
    public void write(String str, int off, int len)  {
        buf.append(str.substring(off, off + len));
    }

    /**
     * 向写入器追加指定的字符序列。
     *
     * <p> 对out.append(csq)表单的这个方法的调用行为与调用的方式完全相同
     *
     * <pre>
     *     out.write(csq.toString()) </pre>
     *
     * <p> 根据字符序列csq的toString规范,整个序列可能不会被附加。
     * 例如,调用字符缓冲区的toString方法将返回一个子序列,其内容取决于缓冲区的位置和限制。
     *
     * @param  csq
     *         The character sequence to append.  If <tt>csq</tt> is
     *         <tt>null</tt>, then the four characters <tt>"null"</tt> are
     *         appended to this writer.
     *
     * @return  This writer
     *
     * @since  1.5
     */
    public StringWriter append(CharSequence csq) {
        if (csq == null)
            write("null");
        else
            write(csq.toString());
        return this;
    }

    /**
     * 向写入器追加指定字符序列的子序列。
     *
     * <p> 当csq不是null时,调用此方法的形式out.append(csq, start,end)与调用的方式完全相同
     *
     * <pre>
     *     out.write(csq.subSequence(start, end).toString()) </pre>
     *
     * @param  csq
     *         The character sequence from which a subsequence will be
     *         appended.  If <tt>csq</tt> is <tt>null</tt>, then characters
     *         will be appended as if <tt>csq</tt> contained the four
     *         characters <tt>"null"</tt>.
     *
     * @param  start
     *         The index of the first character in the subsequence
     *
     * @param  end
     *         The index of the character following the last character in the
     *         subsequence
     *
     * @return  This writer
     *
     * @throws  IndexOutOfBoundsException
     *          If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
     *          is greater than <tt>end</tt>, or <tt>end</tt> is greater than
     *          <tt>csq.length()</tt>
     *
     * @since  1.5
     */
    public StringWriter append(CharSequence csq, int start, int end) {
        CharSequence cs = (csq == null ? "null" : csq);
        write(cs.subSequence(start, end).toString());
        return this;
    }

    /**
     * 向写入器追加单个字符。
     *
     * <p> 当csq不是null时,调用此方法的形式out.append(c)与调用的方式完全相同
     *
     * <pre>
     *     out.write(c) </pre>
     *
     * @param  c
     *         The 16-bit character to append
     *
     * @return  This writer
     *
     * @since 1.5
     */
    public StringWriter append(char c) {
        write(c);
        return this;
    }

方法toString,getBuffer,flush,close

    /**
     * 以字符串形式返回缓冲区的当前值。
     */
    public String toString() {
        return buf.toString();
    }

    /**
     * 返回字符串缓冲区本身。
     *
     * @return StringBuffer holding the current buffer value.
     */
    public StringBuffer getBuffer() {
        return buf;
    }

    /**
     * 刷新流
     */
    public void flush() {
    }

    /**
     * 关闭StringWriter没有效果。
     * 这个类中的方法可以在流被关闭后调用而不会产生IOException异常。
     */
    public void close() throws IOException {
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值