jdk类库源码分析-StringBuffer、StringBuilder

时间:2021-06-19 04:04:44

StringBuffer源码分析
这里主要是拿出一些常用方法的分析,StringBuffer中很多的方法都使用Synchronized关键字修饰,所以是线程安全的。

package java.lang;

public final class StringBuffer extends AbstractStringBuilder implements
java.io.Serializable, CharSequence {


static final long serialVersionUID = 3388685877147921107L;

/*
* 默认大小是16
*/

public StringBuffer() {
super(16);
}

/*
* 创建capacity大小的StringBuffer的对象
*/

public StringBuffer(int capacity) {
super(capacity);
}

/*
* String类型 字符串转为StringBuffer类型,字符串大小扩大16
*/

public StringBuffer(String str) {
super(str.length() + 16);
append(str);
}

/*
* 返回字符串长度,使用synchronized同步关键字修饰,线程安全
*/

public synchronized int length() {
return count;
}

/*
* 返回字符串数组大小,使用synchronized同步关键字修饰,线程安全
*/

public synchronized int capacity() {
return value.length;
}

/*
* 返回指定索引处的字符,使用synchronized同步关键字修饰,线程安全
*/

public synchronized char charAt(int index) {
if ((index < 0) || (index >= count))
throw new StringIndexOutOfBoundsException(index);
return value[index];
}

/*
* 把Object对象转为字符串添加进目标对象
*/

public synchronized StringBuffer append(Object obj) {
super.append(String.valueOf(obj));
return this;
}

/*
* 把String类型字符串添加到目标对象
*
* @see java.lang.AbstractStringBuilder#append(java.lang.String)
*/

public synchronized StringBuffer append(String str) {
super.append(str);
return this;
}

/*
* StringBuffer类型字符串添加到目标对象(non-Javadoc)
*
* @see java.lang.AbstractStringBuilder#append(java.lang.StringBuffer)
*/

public synchronized StringBuffer append(StringBuffer sb) {
super.append(sb);
return this;
}

/*
* 调用的String类型的toString方法(non-Javadoc)
*
* @see java.lang.AbstractStringBuilder#toString()
*/

public synchronized String toString() {
return new String(value, 0, count);
}
/*
* 把Object对象插入目标字符串,调用父类的insert方法,下面贴出父类的insert方法non-Javadoc)
* @see java.lang.AbstractStringBuilder#insert(int, java.lang.Object)
*/

public synchronized StringBuffer insert(int offset, Object obj) {
super.insert(offset, String.valueOf(obj));
return this;
}

/*
* (non-Javadoc)
* 这个方法在StringBuffer类中经常调用这里贴出来参考
* @see java.lang.AbstractStringBuilder#insert(int, char[], int, int)
*/

public AbstractStringBuilder insert(int index, char[] str, int offset,
int len) {
if ((index < 0) || (index > length()))
throw new StringIndexOutOfBoundsException(index);
if ((offset < 0) || (len < 0) || (offset > str.length - len))
throw new StringIndexOutOfBoundsException("offset " + offset
+ ", len " + len + ", str.length " + str.length);
ensureCapacityInternal(count + len);
System.arraycopy(value, index, value, index + len, count - index);
System.arraycopy(str, offset, value, index, len);
count += len;
return this;
}
}

StringBuilder源码分析


package java.lang;


public final class StringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{


/** use serialVersionUID for interoperability */
static final long serialVersionUID = 4383685877147921099L;

/**
* 默认大小16
*/

public StringBuilder() {
super(16);
}

/*
* 创建自定capacity大小的StringBuilder的字符串
*/

public StringBuilder(int capacity) {
super(capacity);
}
/*
* 把String类型字符串转为StringBuilder类型的字符串
*/

public StringBuilder(String str) {
super(str.length() + 16);
append(str);
}
/*
* 把Object对象添加到目标对象(non-Javadoc)
* @see java.lang.AbstractStringBuilder#append(java.lang.Object)
*/

public StringBuilder append(Object obj) {
return append(String.valueOf(obj));
}
/*
* 调用父类的delete方法,下面贴出来父类的方法(non-Javadoc)
* @see java.lang.AbstractStringBuilder#delete(int, int)
*/



public StringBuilder delete(int start, int end) {
super.delete(start, end);
return this;
}
/**
* 指定删除的字符
*/

public AbstractStringBuilder delete(int start, int end) {
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (end > count)
end = count;
if (start > end)
throw new StringIndexOutOfBoundsException();
int len = end - start;
if (len > 0) {
System.arraycopy(value, start+len, value, start, count-end);
count -= len;
}
return this;
}

}