热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

StringBuffer源码分析

StringBuffer里面的方法几乎都是AbstractStringBuilder里面的方法、具体的实现也都是在AbstractStringBuilder里面,需要

StringBuffer里面的方法几乎都是AbstractStringBuilder里面的方法、具体的实现也都是在AbstractStringBuilder里面,需要结合AbstractStringBuilder源码一起查看。

AbstractStringBuilder源码分析:https://blog.csdn.net/qq_26896085/article/details/95593388

package java.lang;import java.util.Arrays;
public final class StringBufferextends AbstractStringBuilderimplements java.io.Serializable, CharSequence
{// transient 让某些被修饰的成员属性变量不被序列化// 在toString方法中使用到&#xff0c;private transient char[] toStringCache;static final long serialVersionUID &#61; 3388685877147921107L;// 初始化长度默认为16public StringBuffer() {super(16);}// 传入参数初始化默认为capacitypublic StringBuffer(int capacity) {super(capacity);}// 构造函数, 传入一个字符串public StringBuffer(String str) {// 初始化的长度为传入长度&#43;16super(str.length() &#43; 16);append(str);}// 构造函数, 传入一个字符序列public StringBuffer(CharSequence seq) {// 初始化的长度为传入长度&#43;16this(seq.length() &#43; 16);append(seq);}// 获取当前StringBuffer的长度&#64;Overridepublic synchronized int length() {return count;}// 获取当前StringBuffer的容量&#64;Overridepublic synchronized int capacity() {return value.length;}// 是否进行扩容&#64;Overridepublic synchronized void ensureCapacity(int minimumCapacity) {super.ensureCapacity(minimumCapacity);}// 减少字符序列的存储&#64;Overridepublic synchronized void trimToSize() {super.trimToSize();}// 设置容量&#64;Overridepublic synchronized void setLength(int newLength) {toStringCache &#61; null;super.setLength(newLength);}// 获取当前字符串中的某个字符&#64;Overridepublic synchronized char charAt(int index) {if ((index <0) || (index >&#61; count))throw new StringIndexOutOfBoundsException(index);return value[index];}// 返回指定索引处的字符的编码&#64;Overridepublic synchronized int codePointAt(int index) {return super.codePointAt(index);}// 获取到value[index]前面一位的字符编码&#xff0c;即value[--index]的字符编码&#64;Overridepublic synchronized int codePointBefore(int index) {return super.codePointBefore(index);}// 计算beginIndex与endIndex索引之间字符的数量&#xff0c;[beginIndex, endIndex),&#64;Overridepublic synchronized int codePointCount(int beginIndex, int endIndex) {return super.codePointCount(beginIndex, endIndex);}// 返回偏移位置的字符&#xff0c;返回value[(index&#43;codePointOffset)]&#64;Overridepublic synchronized int offsetByCodePoints(int index, int codePointOffset) {return super.offsetByCodePoints(index, codePointOffset);}/*** 将字符串中的数据复制到dst中* &#64;param srcBegin 字符串中开始的下标* &#64;param srcEnd 字符串中结束的下标* &#64;param dst 将字符串中的字符串复制到dst中* &#64;param dstBegin dst中接收字符开始的位置*/// System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);&#64;Overridepublic synchronized void getChars(int srcBegin, int srcEnd, char[] dst,int dstBegin){super.getChars(srcBegin, srcEnd, dst, dstBegin);}// 用ch替换value[index]&#64;Overridepublic synchronized void setCharAt(int index, char ch) {if ((index <0) || (index >&#61; count))throw new StringIndexOutOfBoundsException(index);toStringCache &#61; null;value[index] &#61; ch;}// 在当前字符串末尾追加字符串// 调用父类中的append方法// toStringCache &#61; null;暂时不明白这句的意义&#64;Overridepublic synchronized StringBuffer append(Object obj) {toStringCache &#61; null;super.append(String.valueOf(obj));return this;}&#64;Overridepublic synchronized StringBuffer append(String str) {toStringCache &#61; null;super.append(str);return this;}public synchronized StringBuffer append(StringBuffer sb) {toStringCache &#61; null;super.append(sb);return this;}&#64;Overridesynchronized StringBuffer append(AbstractStringBuilder asb) {toStringCache &#61; null;super.append(asb);return this;}&#64;Overridepublic synchronized StringBuffer append(CharSequence s) {toStringCache &#61; null;super.append(s);return this;}&#64;Overridepublic synchronized StringBuffer append(CharSequence s, int start, int end){toStringCache &#61; null;super.append(s, start, end);return this;}&#64;Overridepublic synchronized StringBuffer append(char[] str) {toStringCache &#61; null;super.append(str);return this;}&#64;Overridepublic synchronized StringBuffer append(char[] str, int offset, int len) {toStringCache &#61; null;super.append(str, offset, len);return this;}&#64;Overridepublic synchronized StringBuffer append(boolean b) {toStringCache &#61; null;super.append(b);return this;}&#64;Overridepublic synchronized StringBuffer append(char c) {toStringCache &#61; null;super.append(c);return this;}&#64;Overridepublic synchronized StringBuffer append(int i) {toStringCache &#61; null;super.append(i);return this;}&#64;Overridepublic synchronized StringBuffer append(long lng) {toStringCache &#61; null;super.append(lng);return this;}&#64;Overridepublic synchronized StringBuffer append(float f) {toStringCache &#61; null;super.append(f);return this;}&#64;Overridepublic synchronized StringBuffer append(double d) {toStringCache &#61; null;super.append(d);return this;}// 在当前字符串的末尾追加字符&#xff0c;codePoint为字符的编码&#64;Overridepublic synchronized StringBuffer appendCodePoint(int codePoint) {toStringCache &#61; null;super.appendCodePoint(codePoint);return this;}// 删除当前字符下标为[start, end)的字符&#64;Overridepublic synchronized StringBuffer delete(int start, int end) {toStringCache &#61; null;super.delete(start, end);return this;}// 删除当前字符串下标为index的字符&#64;Overridepublic synchronized StringBuffer deleteCharAt(int index) {toStringCache &#61; null;super.deleteCharAt(index);return this;}// 用str替换掉当前字符串下标为[start, end)的字符串&#64;Overridepublic synchronized StringBuffer replace(int start, int end, String str) {toStringCache &#61; null;super.replace(start, end, str);return this;}// 截取当前字符串&#xff0c;下标为start至最后&#64;Overridepublic synchronized String substring(int start) {return substring(start, count);}// 截取当前字符串&#xff0c;下标为start至最后&#64;Overridepublic synchronized CharSequence subSequence(int start, int end) {return super.substring(start, end);}// 截取当前字符串&#xff0c;下标为start至最后&#64;Overridepublic synchronized String substring(int start, int end) {return super.substring(start, end);}//插入操作&#64;Overridepublic synchronized StringBuffer insert(int index, char[] str, int offset,int len){toStringCache &#61; null;super.insert(index, str, offset, len);return this;}&#64;Overridepublic synchronized StringBuffer insert(int offset, Object obj) {toStringCache &#61; null;super.insert(offset, String.valueOf(obj));return this;}&#64;Overridepublic synchronized StringBuffer insert(int offset, String str) {toStringCache &#61; null;super.insert(offset, str);return this;}&#64;Overridepublic synchronized StringBuffer insert(int offset, char[] str) {toStringCache &#61; null;super.insert(offset, str);return this;}&#64;Overridepublic StringBuffer insert(int dstOffset, CharSequence s) {// 注意&#xff0c;通过调用其他StringBuffer方法实现同步super.insert(dstOffset, s);return this;}&#64;Overridepublic synchronized StringBuffer insert(int dstOffset, CharSequence s,int start, int end){toStringCache &#61; null;super.insert(dstOffset, s, start, end);return this;}&#64;Overridepublic StringBuffer insert(int offset, boolean b) {// 通过调用StringBuffer insert&#xff08;int&#xff0c;String&#xff09;实现同步super.insert(offset, b);return this;}&#64;Overridepublic synchronized StringBuffer insert(int offset, char c) {toStringCache &#61; null;super.insert(offset, c);return this;}&#64;Overridepublic StringBuffer insert(int offset, int i) {// 通过调用StringBuffer insert&#xff08;int&#xff0c;String&#xff09;实现同步super.insert(offset, i);return this;}&#64;Overridepublic StringBuffer insert(int offset, long l) {// 通过调用StringBuffer insert&#xff08;int&#xff0c;String&#xff09;实现同步super.insert(offset, l);return this;}&#64;Overridepublic StringBuffer insert(int offset, float f) {// 通过调用StringBuffer insert&#xff08;int&#xff0c;String&#xff09;实现同步super.insert(offset, f);return this;}&#64;Overridepublic StringBuffer insert(int offset, double d) {// 通过调用StringBuffer insert&#xff08;int&#xff0c;String&#xff09;实现同步super.insert(offset, d);return this;}// 获取str在当前字符串中第一次出现的位置&#64;Overridepublic int indexOf(String str) {// 注意&#xff0c;通过调用其他StringBuffer方法实现同步return super.indexOf(str);}// 获取str在当前字符串fromIndex下标以后第一次出现的位置&#64;Overridepublic synchronized int indexOf(String str, int fromIndex) {return super.indexOf(str, fromIndex);}// 获取str在当前字符串中最后一次出现的位置&#64;Overridepublic int lastIndexOf(String str) {// 注意&#xff0c;通过调用其他StringBuffer方法实现同步return lastIndexOf(str, count);}// 获取str在当前字符串fromIndex下标之前最后一次出现的位置&#64;Overridepublic synchronized int lastIndexOf(String str, int fromIndex) {return super.lastIndexOf(str, fromIndex);}// 将当前字符串反转&#64;Overridepublic synchronized StringBuffer reverse() {toStringCache &#61; null;super.reverse();return this;}// 将当前的StringBuffer转换成String// 先将StringBuffer中的数据存放在toStringCache中&#xff0c;在new一个String对象&#64;Overridepublic synchronized String toString() {if (toStringCache &#61;&#61; null) {toStringCache &#61; Arrays.copyOfRange(value, 0, count);}return new String(toStringCache, true);}private static final java.io.ObjectStreamField[] serialPersistentFields &#61;{new java.io.ObjectStreamField("value", char[].class),new java.io.ObjectStreamField("count", Integer.TYPE),new java.io.ObjectStreamField("shared", Boolean.TYPE),};private synchronized void writeObject(java.io.ObjectOutputStream s)throws java.io.IOException {java.io.ObjectOutputStream.PutField fields &#61; s.putFields();fields.put("value", value);fields.put("count", count);fields.put("shared", false);s.writeFields();}private void readObject(java.io.ObjectInputStream s)throws java.io.IOException, ClassNotFoundException {java.io.ObjectInputStream.GetField fields &#61; s.readFields();value &#61; (char[])fields.get("value", null);count &#61; fields.get("count", 0);}
}

 


推荐阅读
author-avatar
King347
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有