java.lang.StringBuffer

package java.lang;

/**
 *
 * 封装了一个char数组,用来做改变操作
 *
 * comment by liqiang
 *
 * @author Arthur van Hoff
 *
 */
 
public final class StringBuffer
    implements java.io.Serializable, CharSequence
{
    //所含数据存放的数组
    private char value[];

    //此对象中包含的字符个数
    private int count;

    //此对象是否共享,如果共享需要做copy操作
    private boolean shared;

    static final long serialVersionUID = 3388685877147921107L;

    /**
     *
     * 构造函数,初始大小为16个字符
     * 
     */
    public StringBuffer() {
 this(16);
    }

    /**
     *
     * 构造函数,设置内部数组的大小为length长度
     *
     */
    public StringBuffer(int length) {
 value = new char[length];
 shared = false;
    }

    /**
     *
     * 通过一个字符串构造一个StringBuffer对象
     *
     */
    public StringBuffer(String str) {
    //构造新的StringBuffer对象
    //内部数组长度为字符串的长度+16
 this(str.length() + 16);
 append(str);
    }

    /**
     *
     * 返回字符个数
     *
     */
    public synchronized int length() {
 return count;
    }

    /**
     *
     * 返回内部数组的长度
     *
     */
    public synchronized int capacity() {
 return value.length;
    }

    /**
     *
     * 将当前对象的数据进行拷贝,这个实在shared的情况下才发生,它应该在
     * 同步的方法中使用
     *
     */
    private final void copy() {
    //生成新数组,并将此对象中的数据拷贝过去
 char newValue[] = new char[value.length];
 System.arraycopy(value, 0, newValue, 0, count);
 //将新数组赋给被对象,放弃了原来数组对象的引用
 value = newValue;
 //共享标志设为fale
 shared = false;
    }

    /**
     *
     * 判断数组长度是否不够
     *
     */
    public synchronized void ensureCapacity(int minimumCapacity) {
    //如果数组长度不够则扩展
 if (minimumCapacity > value.length) {
     expandCapacity(minimumCapacity);
 }
    }

    /**
     *
     * 通过minimumCapacity扩展内部数组
     *
     */
    private void expandCapacity(int minimumCapacity) {
    //算出一个新长度
 int newCapacity = (value.length + 1) * 2;
 
        if (newCapacity < 0) {//溢出,则使用最大的int值
            newCapacity = Integer.MAX_VALUE;
        } else if (minimumCapacity > newCapacity) {
        //使用minimumCapacity和newCapacity中最大的
     newCapacity = minimumCapacity;
 }
 
    //身成新数组,并拷贝数据,将新生成数组赋给当前对象
 char newValue[] = new char[newCapacity];
 System.arraycopy(value, 0, newValue, 0, count);
 value = newValue;
 shared = false;
    }

    /**
     *
     * 设置当前对象的长度
     *
     */
    public synchronized void setLength(int newLength) {
 if (newLength < 0) {
  //长度小于0抛出异常
     throw new StringIndexOutOfBoundsException(newLength);
 }
 
 //如果新长度大于原数组长度,则扩展数组
 if (newLength > value.length) {
     expandCapacity(newLength);
 }

 if (count < newLength) {
  //如果设置的长度比原长度大,将扩充的部分用空字符填充
  //共享则做拷贝
     if (shared) copy();
     for (; count < newLength; count++) {
  value[count] = '/0';
     }
 } else {
   //设置当前长度
            count = newLength;
            if (shared) {//如果共享
                if (newLength > 0) {//表示新长度大于0且没有原来的数组长
                    copy();
                } else {
                 //如果新长度小于0则创建一个16个字符的数组
                    value = new char[16];
                    shared = false;
                }
            }
        }
    }

    /**
     *
     * 取得指定位置的字符
     *
     */
    public synchronized char charAt(int index) {
 //范围不符抛出异常
    if ((index < 0) || (index >= count)) {
     throw new StringIndexOutOfBoundsException(index);
 }
 
    //取数组中指定位置的字符
 return value[index];
    }

    /**
     *
     * 拷贝当前对象的一段数据到指定数组的指定位置后
     *
     * @param      srcBegin   当前数组的拷贝起始位置,包括
     * @param      srcEnd     当前数组的拷贝终止位置,不包扩
     * @param      dst        拷贝到的目标数组
     * @param      dstBegin   目标数组的起始位置
     *
     */
    public synchronized void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
 //范围不符抛出异常
    if (srcBegin < 0) {
     throw new StringIndexOutOfBoundsException(srcBegin);
 }
 if ((srcEnd < 0) || (srcEnd > count)) {
     throw new StringIndexOutOfBoundsException(srcEnd);
 }
        if (srcBegin > srcEnd) {
            throw new StringIndexOutOfBoundsException("srcBegin > srcEnd");
        }
       
    //拷贝数据
 System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
    }

    /**
     *
     * 设置指定位置的字符
     *
     */
    public synchronized void setCharAt(int index, char ch) {
    //范围不符抛出异常
 if ((index < 0) || (index >= count)) {
     throw new StringIndexOutOfBoundsException(index);
 }
 
 //共享则拷贝此类的对象,放弃对原数组对象的引用
 if (shared) copy();
 //设置数组相应位置的字符
 value[index] = ch;
    }

    /**
     *
     * 将对象添加到StringBuffer后
     *
     */
    public synchronized StringBuffer append(Object obj) {
 //调用了对象的toString方法
    return append(String.valueOf(obj));
    }

    /**
     * 将字符串添加到此对象的后面
     */
    public synchronized StringBuffer append(String str) {
 if (str == null) {
  //如果字符串为空,使用"null"字符串
     str = String.valueOf(str);
 }

 //判断当前数组长度是否足够大,如果不够大则扩展数组长度
 int len = str.length();
 //新长度为当前对象的数据长度与添加字符串的数据长度和
 int newcount = count + len;
 if (newcount > value.length)
     expandCapacity(newcount);
 
 //将字符串的全部数据添加到当前对象的数组count后,起到连接效果
 str.getChars(0, len, value, count);
 //设置新的数据长度
 count = newcount;
 return this;
    }

    /**
     *
     * 添加一个StringBuffer
     *
     */
    public synchronized StringBuffer append(StringBuffer sb) {
 //如果添加的sb为null则使用内容为"null"数据的StringBuffer
    if (sb == null) {
     sb = NULL;
 }

    //得到连接后的新长度,判断是否过长
 int len = sb.length();
 int newcount = count + len; 
 if (newcount > value.length)
     expandCapacity(newcount);
 
 //将sb中的数据添加到value数组的count位置后
 sb.getChars(0, len, value, count);
 count = newcount;
 return this;
    }

    //用"null"字符初始化的StringBuffer对象
    private static final StringBuffer NULL =  new StringBuffer("null");

    /**
     *
     * 添加Char数组
     *
     */
    public synchronized StringBuffer append(char str[]) {
    //得到连接后的新长度,判断是否过长 
 int len = str.length;
 int newcount = count + len;
 if (newcount > value.length)
     expandCapacity(newcount);
 
 //拷贝数组中数据到当前对象的内部数组的count位置后
 System.arraycopy(str, 0, value, count, len);
 count = newcount;
 return this;
    }

    /**
     *
     * 添加数组中的指定段数据
     *
     */
    public synchronized StringBuffer append(char str[], int offset, int len) {
    //得到连接后的新长度,判断是否过长  
        int newcount = count + len;
 if (newcount > value.length)
     expandCapacity(newcount);
 
 //拷贝数组的指定段数据到当前对象的内部数组的count位置后
 System.arraycopy(str, offset, value, count, len);
 count = newcount;
 return this;
    }

    /**
     *
     * 添加一个boolean
     *
     */
    public synchronized StringBuffer append(boolean b) {
        if (b) {
         //添加true
            int newcount = count + 4;
            if (newcount > value.length)
                expandCapacity(newcount);
            value[count++] = 't';
            value[count++] = 'r';
            value[count++] = 'u';
            value[count++] = 'e';
        } else {
         //添加false
            int newcount = count + 5;
            if (newcount > value.length)
                expandCapacity(newcount);
            value[count++] = 'f';
            value[count++] = 'a';
            value[count++] = 'l';
            value[count++] = 's';
            value[count++] = 'e';
        }
 return this;
    }

    /**
     *
     * 添加一个字符
     *
     */
    public synchronized StringBuffer append(char c) {
    //因为只有一个字符,长度加1,判断是否过长   
        int newcount = count + 1;
 if (newcount > value.length)
     expandCapacity(newcount);
 
 //添加字符
 value[count++] = c;
 return this;
    }

    /**
     *
     * 添加一个int
     *
     */
    public synchronized StringBuffer append(int i) {
 Integer.appendTo(i, this);
        return this;
    }

    /**
     *
     * 添加一个long
     *
     */
    public synchronized StringBuffer append(long l) {
        Long.appendTo(l, this);
 return this;
    }

    /**
     *
     * 添加一个float
     *
     */
    public synchronized StringBuffer append(float f) {
        new FloatingDecimal(f).appendTo(this);
 return this;
    }

    /**
     *
     * 添加一个double
     *
     */
    public synchronized StringBuffer append(double d) {
        new FloatingDecimal(d).appendTo(this);
 return this;
    }

    /**
     *
     * 删除指定段数据
     * start为起始位置,包括
     * 恩德为终止位置,不包括
     */
    public synchronized StringBuffer delete(int start, int end) {
    //起始位置小于0抛出异常
 if (start < 0)
     throw new StringIndexOutOfBoundsException(start);
 //终止位置大于最大长度,则设置为最大长度
 if (end > count)
     end = count;
 //起始位置大于终止位置抛出异常
 if (start > end)
     throw new StringIndexOutOfBoundsException();

  //删除数据段的长度
        int len = end - start;
       
        //如果删除的数据段长度不为0,做删除操作
        if (len > 0) {
         //共享,做拷贝
            if (shared)
                copy();
           
            //将end后(保括end位)的字符序列拷贝到start位置
            System.arraycopy(value, start+len, value, start, count-end);
            //设置新的数据长度
            count -= len;
        }
       
        return this;
    }

    /**
     *
     * 删除指定位置的字符
     *
     */
    public synchronized StringBuffer deleteCharAt(int index) {
        //范围不符抛出异常
     if ((index < 0) || (index >= count))
     throw new StringIndexOutOfBoundsException();
       
    //共享,做拷贝,放弃原数组对象引用
 if (shared)
     copy();
 
 //向前移动一个位置
 System.arraycopy(value, index+1, value, index, count-index-1);
 count--;
 
        return this;
    }

    /**
     *
     * 用指定字符串替换指定区域字符段
     *
     * @param      start    开始位置,包括
     * @param      end      结束位置,不包括
     *
     */
    public synchronized StringBuffer replace(int start, int end, String str) {
        //范围不符抛出异常
     if (start < 0)
     throw new StringIndexOutOfBoundsException(start);
 if (end > count)
     end = count;
 if (start > end)
     throw new StringIndexOutOfBoundsException();

 //替换字符串长度
 int len = str.length();
 //替换后的新长度
 int newCount = count + len - (end - start);
 //判断是否过长
 if (newCount > value.length)
     expandCapacity(newCount);
 else if (shared) //没有过长,如果共享做拷贝
     copy();

  //将end位置后面的数据,拷贝到start + len位置上(表示中间留有len长度,用来为以后
     //填充St中的数据),移动的个数为count - end(无论向前向后,end位置以后的元素都
  //需移动count=end次,因为end后面有这么多个字符)
        System.arraycopy(value, end, value, start + len, count - end);
        str.getChars(0, len, value, start);
       
        count = newCount;
        return this;
    }

    /**
     *
     * 截取从start开始到结尾的字符串
     *
     */
    public synchronized String substring(int start) {
        return substring(start, count);
    }

    /**
     *
     * 截取指定段字符串
     *
     * 注意String实现了CharSequence接口
     *
     * start为开始位置,包括
     * end为结束位置,不包括
     */
    public CharSequence subSequence(int start, int end) {
        return this.substring(start, end);
    }

    /**
     *
     * 通过截取的指定段数据,生成对应的字符串
     *
     * @param      start  截取的开始位置,包括
     * @param      end    截取的结束位置,不包括
     *
     */
    public synchronized String substring(int start, int end) {
    //范围不符抛出异常
 if (start < 0)
     throw new StringIndexOutOfBoundsException(start);
 if (end > count)
     throw new StringIndexOutOfBoundsException(end);
 if (start > end)
     throw new StringIndexOutOfBoundsException(end - start);
 
  //用当前数组,和指定位置变量生成指定区域对应的字符串
        return new String(value, start, end - start);
    }

    /**
     *
     * 从index开始插入数组中的指定数据段
     *
     */
    public synchronized StringBuffer insert(int index, char str[], int offset,
                                                                   int len) {
    //范围不符抛出异常 
        if ((index < 0) || (index > count))
     throw new StringIndexOutOfBoundsException();
 if ((offset < 0) || (offset + len < 0) || (offset + len > str.length))
     throw new StringIndexOutOfBoundsException(offset);
 if (len < 0)
     throw new StringIndexOutOfBoundsException(len);
 
 //如果长度过长扩充
 int newCount = count + len;
 if (newCount > value.length)
     expandCapacity(newCount);
 
 else if (shared)
     copy();
 
 //为插入腾腾出len长度的位置
 System.arraycopy(value, index, value, index + len, count - index);
 //在腾出的len长度区域填充添加数组的指定数据
 System.arraycopy(str, offset, value, index, len);
 
 //设置新长度
 count = newCount;
 
 return this;
    }

    /**
     *
     * 在offset插入对象的字符串表示
     *
     */
    public synchronized StringBuffer insert(int offset, Object obj) {
 //如果对象不为null插入的是对象调用toSting后的字符串表示
    //为null插入"null"字符串
    return insert(offset, String.valueOf(obj));
    }

    /**
     *
     * 将字符串插入到offset起始的位置
     *
     */
    public synchronized StringBuffer insert(int offset, String str) {
 //范围不符抛出异常
    if ((offset < 0) || (offset > count)) {
     throw new StringIndexOutOfBoundsException();
 }

 if (str == null) {
  //如果字符串为空使用"null"字符串
     str = String.valueOf(str);
 }
 
 //新长度是否过长,过长则扩展
 int len = str.length();
 int newcount = count + len;
 if (newcount > value.length)
     expandCapacity(newcount);
 
 else if (shared)
     copy();
 
 //腾出str长度个位置
 System.arraycopy(value, offset, value, offset + len, count - offset);
 //将字符串填入腾出的len长度区域
 str.getChars(0, len, value, offset);
 
 count = newcount;
 return this;
    }

    /**
     *
     * 将char数组插入到offset位置
     *
     */
    public synchronized StringBuffer insert(int offset, char str[]) {
    //范围不符抛出异常 
 if ((offset < 0) || (offset > count)) {
     throw new StringIndexOutOfBoundsException();
 }
 
 //新长度是否过长,过长则扩展
 int len = str.length;
 int newcount = count + len;
 if (newcount > value.length)
     expandCapacity(newcount);
 
 else if (shared)
     copy();
 
 //腾出数组长度个位置
 System.arraycopy(value, offset, value, offset + len, count - offset);
 //在腾出的位置上插入数组
 System.arraycopy(str, 0, value, offset, len);
 
 count = newcount;
 return this;
    }

    /**
     *
     * 在offset位置插入一个boolean
     *
     */
    public StringBuffer insert(int offset, boolean b) {
    //在offset位置插入一个boolean的字符串表示
 return insert(offset, String.valueOf(b));
    }

    /**
     *
     * 在offset位置插入一个字符
     *
     */
    public synchronized StringBuffer insert(int offset, char c) {
    //算出新长度
    int newcount = count + 1;
 if (newcount > value.length)//新长度过长,扩展数组
     expandCapacity(newcount);
 else if (shared)//如果共享,则拷贝数组
     copy();
 
 //腾出一个位置
 System.arraycopy(value, offset, value, offset + 1, count - offset);
 //插入次字符
 value[offset] = c;
 
 //设置新位置
 count = newcount;
 return this;
    }

    /**
     *
     * 将一个int插入到offset位置
     *
     */
    public StringBuffer insert(int offset, int i) {
    //在offset位置插入一个int的字符串表示
 return insert(offset, String.valueOf(i));
    }

    /**
     *
     *  将一个long插入到offset位置
     *
     */
    public StringBuffer insert(int offset, long l) {
    //在offset位置插入一个long的字符串表示
 return insert(offset, String.valueOf(l));
    }

    /**
     *
     * 将一个float插入到offset位置
     *
     */
    public StringBuffer insert(int offset, float f) {
    //在offset位置插入一个float的字符串表示
 return insert(offset, String.valueOf(f));
    }

    /**
     *
     * 将一个double插入到offset位置
     *
     */
    public StringBuffer insert(int offset, double d) {
    //在offset位置插入一个double的字符串表示
 return insert(offset, String.valueOf(d));
    }

    /**
     *
     * 取得从开始位置向后查找,字符串在StringBuffer的位置
     *
     */
    public int indexOf(String str) {
 return indexOf(str, 0);
    }

    /**
     *
     * 取得从fromIndex位置向后查找,数组在StringBuffer的位置
     *
     */
    public synchronized int indexOf(String str, int fromIndex) {
        return String.indexOf(value, 0, count,
                              str.toCharArray(), 0, str.length(), fromIndex);
    }

    /**
     *
     * 取得从fromIndex位置向前查找,数组在StringBuffer的位置
     *
     */
    public synchronized int lastIndexOf(String str) {
        return lastIndexOf(str, count);
    }

    /**
     *
     * 取得从fromIndex位置向前查找,字符串在StringBuffer的位置
     *
     */
    public synchronized int lastIndexOf(String str, int fromIndex) {
        return String.lastIndexOf(value, 0, count,
                              str.toCharArray(), 0, str.length(), fromIndex);
    }

    /**
     *
     * 翻转StringBuffer
     *
     */
    public synchronized StringBuffer reverse() {
 if (shared) copy();
 
 //最后一个位置
 int n = count - 1;
 
 //>>表示除2
 //以n/2为起点交换左右对称数据
 for (int j = (n-1) >> 1; j >= 0; --j) {
     char temp = value[j];
    
     //左边向右移j位与右边向做移j位是对称的
     value[j] = value[n - j];
     value[n - j] = temp;
 }
 
 return this;
    }

    /**
     *
     * 字符串表示
     *
     */
    public String toString() {
 return new String(this);
    }

    //由String调用,便于将StringBuffer转成String
    final void setShared() { shared = true; }
    final char[] getValue() { return value; }

    private synchronized void readObject(java.io.ObjectInputStream s)
         throws java.io.IOException, ClassNotFoundException {
 s.defaultReadObject();
 value = (char[]) value.clone();
 shared = false;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值