StringBuffer源码浅析(insert方法)

一、insert,其实跟replace差不多,都是先通过System.arraycopy Method在字符数组value中预留一个空间,再用多一次这方法插入str或char[]。
1、insert(int index, char str[], int offset,int len)围观源码
// StringBuffer里面,调用父类的方法
public synchronized StringBuffer insert(int index, char str[], int offset,
                                            int len) 
    {
        super.insert(index, str, offset, len);
        return this;
    }

进入AbstractStringBuilder查看
    public AbstractStringBuilder insert(int index, char str[], int offset,
                                        int len)
    {
        // bounds checking
        if ((index < 0) || (index > length()))
	    throw new StringIndexOutOfBoundsException(index);
        // 注意:offset的值,offset + len不应大于str.length
        if ((offset < 0) || (len < 0) || (offset > str.length - len)) 
            throw new StringIndexOutOfBoundsException(
                "offset " + offset + ", len " + len + ", str.length " 
                + str.length);
	int newCount = count + len;
	if (newCount > value.length)
	    expandCapacity(newCount);
        // 从index开始,预留了长度为len的空间;从index+len开始,将长度为count-index的后半截补充。
	System.arraycopy(value, index, value, index + len, count - index);
        // str字符串数组,从索引offset开始复制,长度为len,复制到以索引index位置的value字符串数组。
	System.arraycopy(str, offset, value, index, len);
	count = newCount; //记得更新实际字符长度(字符数量)。
	return this;
    }


2、顺便看insert(int offset,String str) 对比 insert(int offset, Object obj)
  
// 将obj返回字符串形式,一样是调用父类insert(int offset,String str) 
public synchronized StringBuffer insert(int offset, Object obj) {
	super.insert(offset, String.valueOf(obj));
        return this;
    }

进入AbstractStringBuilder查看,跟第1点一样
public AbstractStringBuilder insert(int offset, String str) {
	if ((offset < 0) || (offset > length()))
	    throw new StringIndexOutOfBoundsException(offset);
	if (str == null)
	    str = "null";
	int len = str.length();
	int newCount = count + len;
	if (newCount > value.length)
	    expandCapacity(newCount);
	System.arraycopy(value, offset, value, offset + len, count - offset);
	str.getChars(value, offset); //getChars封装了System.arraycopy Method
	count = newCount;
	return this;
    }

  insert(int offset, char str[])操作一样不解释。

3、insert(int dstOffset, CharSequence s)
public StringBuffer insert(int dstOffset, CharSequence s) {
        // Note, synchronization achieved via other invocations
        if (s == null)
            s = "null";
        if (s instanceof String)
            // 调用第2点,insert(int offset,String s)
            return this.insert(dstOffset, (String)s);
           // 看抽象类的方法定义
        return this.insert(dstOffset, s, 0, s.length());
    }

进入抽象类
public AbstractStringBuilder insert(int dstOffset, CharSequence s,
                                           int start, int end) {
        if (s == null)
            s = "null";
	if ((dstOffset < 0) || (dstOffset > this.length()))
	    throw new IndexOutOfBoundsException("dstOffset "+dstOffset);
	if ((start < 0) || (end < 0) || (start > end) || (end > s.length()))
            throw new IndexOutOfBoundsException(
                "start " + start + ", end " + end + ", s.length() " 
                + s.length());
	int len = end - start;
        if (len == 0)
            return this;
	int newCount = count + len;
	if (newCount > value.length)
	    expandCapacity(newCount);
        // 以上大同小异,不说
        // 预留空间
	System.arraycopy(value, dstOffset, value, dstOffset + len,
                         count - dstOffset);
        // start 到 end,逐个字符赋值到预留的空间上。
	for (int i=start; i<end; i++)
            value[dstOffset++] = s.charAt(i);
	count = newCount;
        return this;
    }

你可能感兴趣的:(StringBuffer)