详细解读AbstractStringBuilder类源码
因为看stringbuffer 和 stringbuilder 的源码时发现两者都继承了abstractstringbuilder,并且很多方法都是直接super的父类abstractstringbuilder的方法,所以还是决定先看abstractstringbuilder的源码,然后再看stringbuffer 和 stringbuilder.
位置:java.lang包中
声明: abstract class abstractstringbuilderimplements appendable, charsequence
abstractstringbuilder 类有abstract 修饰,可知它不能被实例化。
abstractstringbuilder 类有两个子类:stringbuilder和stringbuffer。
字段
/** * the value is used for character storage. */ char value[]; /** * the count is the number of characters used. */ int count;
构造器
1、无参构造器
abstractstringbuilder() { }
2、创建abstractstringbuilder实现类的对象时指定缓冲区大小为capacity。
abstractstringbuilder(int capacity) { value = new char[capacity]; }
当子类stringbuilder或stringbuffer实例化时,会在构造器中调用此构造器。
扩充容量
void expandcapacity(int minimumcapacity)
此方法有包访问权限,类中有多个方法会调用此方法,在容量不足时扩充容量。
源码:
void expandcapacity(int minimumcapacity) { int newcapacity = (value.length + 1) * 2; if (newcapacity < 0) { newcapacity = integer.max_value; } else if (minimumcapacity > newcapacity) { newcapacity = minimumcapacity; } value = arrays.copyof(value, newcapacity); }
将缓冲区长度加1乘2的值赋予变量newcapacity, 然后将此值与指定的值比较,将较大值确定为缓冲区的新容量;然后调用arrays类的copyof方法,此方法会创建一个新数组,然后将原数组中的字符全部复制进新数组中。
ensurecapacity(int minimumcapacity)
public void ensurecapacity(int minimumcapacity)
确保容量至少等于指定的最小值。如果当前容量小于指定值,则创建新数组,新数组的容量为指定值的两倍加2;如果当前容量不小于指定值,则直接不做处理。
源码:
public void ensurecapacity(int minimumcapacity) { if (minimumcapacity > value.length) { expandcapacity(minimumcapacity); } }
测试:
stringbuffer s = new stringbuffer(); system.out.println("容量:" + s.capacity());// 容量:16 s.ensurecapacity(10); system.out.println("容量:" + s.capacity());// 容量:16 s.ensurecapacity(30); system.out.println("容量:" + s.capacity());// 容量:34 s.ensurecapacity(80); system.out.println("容量:" + s.capacity());// 容量:80
方法
codepointat方法中都是用character.codepointatimpl(value, index, count)来实现的
public int codepointat(int index) { if ((index < 0) || (index >= count)) { throw new stringindexoutofboundsexception(index); } return character.codepointatimpl(value, index, count); }
getchars方法的实现用的是system.arraycopy()方法
public 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); }
append方法都牵扯到了ensurecapacityinternal()方法和getchars()方法来实现
public abstractstringbuilder append(string str) { if (str == null) return appendnull(); int len = str.length(); ensurecapacityinternal(count + len); str.getchars(0, len, value, count); count += len; return this; }
使用了arrays.copyof()来实现
void expandcapacity(int minimumcapacity) { int newcapacity = value.length * 2 + 2; if (newcapacity - minimumcapacity < 0) newcapacity = minimumcapacity; if (newcapacity < 0) { if (minimumcapacity < 0) // overflow throw new outofmemoryerror(); newcapacity = integer.max_value; } value = arrays.copyof(value, newcapacity); }
arrays.fill(value, count, newlength, ‘\0');字符串之间的复制
public void setlength(int newlength) { if (newlength < 0) throw new stringindexoutofboundsexception(newlength); ensurecapacityinternal(newlength); if (count < newlength) { arrays.fill(value, count, newlength, '\0'); } count = newlength; }
delete() 仅改变字符串的大小并未真正的删除字符串
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; }
学会灵活的运用system.arraycopy()方法
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; }
总结
以上就是本文关于源码详细解读abstractstringbuilder类源码详细解读的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
下一篇: SpringBoot自动配置的实现原理