本文整理了Java中java.lang.StringBuffer
类的一些代码示例,展示了StringBuffer
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。StringBuffer
类的具体详情如下:
包路径:java.lang.StringBuffer
类名称:StringBuffer
[英]A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.
String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.
The principal operations on a StringBuffer
are the append
and insert
methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string buffer. The append
method always adds these characters at the end of the buffer; the insert
method adds the characters at a specified point.
For example, if z
refers to a string buffer object whose current contents are "start
", then the method call z.append("le")
would cause the string buffer to contain "startle
", whereas z.insert(4, "le")
would alter the string buffer to contain "starlet
".
In general, if sb refers to an instance of a StringBuffer
, then sb.append(x)
has the same effect as sb.insert(sb.length(), x)
.
Whenever an operation occurs involving a source sequence (such as appending or inserting from a source sequence) this class synchronizes only on the string buffer performing the operation, not on the source.
Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger. As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.
[中]线程安全、可变的字符序列。字符串缓冲区类似于字符串,但可以修改。在任何时间点,它都包含一些特定的字符序列,但是序列的长度和内容可以通过某些方法调用来更改。
多线程使用字符串缓冲区是安全的。这些方法在必要时进行同步,以便任何特定实例上的所有操作的行为就像它们以某种串行顺序发生一样,这种顺序与所涉及的每个线程进行的方法调用的顺序一致。StringBuffer
上的主要操作是append
和insert
方法,它们被重载以接受任何类型的数据。每个都有效地将给定的数据转换为字符串,然后将该字符串的字符追加或插入字符串缓冲区。append
方法总是在缓冲区的末尾添加这些字符;insert
方法在指定点添加字符。
例如,如果z
引用当前内容为“start
”的字符串缓冲区对象,则方法调用z.append("le")
将导致字符串缓冲区包含“startle
”,而z.insert(4, "le")
将更改字符串缓冲区以包含“starlet
”。
一般来说,如果sb引用StringBuffer
的实例,则sb.append(x)
与sb.insert(sb.length(), x)
具有相同的效果。
每当发生涉及源序列的操作(例如从源序列追加或插入)时,此类仅在执行该操作的字符串缓冲区上同步,而不是在源上同步。
每个字符串缓冲区都有一个容量。只要字符串缓冲区中包含的字符序列的长度不超过容量,就不必分配新的内部缓冲区数组。如果内部缓冲区溢出,它会自动变大。从JDK5发行版开始,这个类已经得到了一个等效类的补充,该类是为单线程StringBuilder设计的。通常应优先使用StringBuilder类,因为它支持所有相同的操作,但速度更快,因为它不执行同步。
代码示例来源:origin: log4j/log4j
protected String getStatusText(int displayedRows, int totalRows) {
StringBuffer result = new StringBuffer();
result.append("Displaying: ");
result.append(displayedRows);
result.append(" records out of a total of: ");
result.append(totalRows);
result.append(" records.");
return result.toString();
}
代码示例来源:origin: alibaba/druid
@Override
public String getBatchSql() {
List<String> sqlList = getBatchSqlList();
StringBuffer buf = new StringBuffer();
for (String item : sqlList) {
if (buf.length() > 0) {
buf.append("\n;\n");
}
buf.append(item);
}
return buf.toString();
}
代码示例来源:origin: apache/incubator-dubbo
private String readLenString(int len)
throws IOException {
_isLastChunk = true;
_chunkLength = len;
_sbuf.setLength(0);
int ch;
while ((ch = parseChar()) >= 0)
_sbuf.append((char) ch);
return _sbuf.toString();
}
代码示例来源:origin: alibaba/arthas
public static void print(int number, List<Integer> primeFactors) {
StringBuffer sb = new StringBuffer(number + "=");
for (int factor : primeFactors) {
sb.append(factor).append('*');
}
if (sb.charAt(sb.length() - 1) == '*') {
sb.deleteCharAt(sb.length() - 1);
}
System.out.println(sb);
}
代码示例来源:origin: alibaba/druid
public String toString() {
StringBuffer buf = new StringBuffer();
output(buf);
return buf.toString();
}
代码示例来源:origin: apache/incubator-dubbo
private static void longArrayAppend(StringBuffer sbuf, long[] a) {
sbuf.append('[');
final int len = a.length;
for (int i = 0; i < len; i++) {
sbuf.append(a[i]);
if (i != len - 1) {
sbuf.append(", ");
}
}
sbuf.append(']');
}
代码示例来源:origin: skylot/jadx
public static String getStackTrace(Throwable throwable) {
if (throwable == null) {
return "";
}
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
filterRecursive(throwable);
throwable.printStackTrace(pw);
return sw.getBuffer().toString();
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
String s = (String) args[0];
return new StringBuffer(s).reverse().toString();
}
}
代码示例来源:origin: stackoverflow.com
public static String getFullURL(HttpServletRequest request) {
StringBuffer requestURL = request.getRequestURL();
String queryString = request.getQueryString();
if (queryString == null) {
return requestURL.toString();
} else {
return requestURL.append('?').append(queryString).toString();
}
}
代码示例来源:origin: apache/incubator-dubbo
@Override
public Object decode(Object jv) throws IOException {
return new StringBuffer(jv.toString());
}
};
代码示例来源:origin: spring-projects/spring-framework
@Override
public StringBuffer getRequestURL() {
String scheme = getScheme();
String server = getServerName();
int port = getServerPort();
String uri = getRequestURI();
StringBuffer url = new StringBuffer(scheme).append("://").append(server);
if (port > 0 && ((HTTP.equalsIgnoreCase(scheme) && port != 80) ||
(HTTPS.equalsIgnoreCase(scheme) && port != 443))) {
url.append(':').append(port);
}
if (StringUtils.hasText(uri)) {
url.append(uri);
}
return url;
}
代码示例来源:origin: log4j/log4j
/**
* {@inheritDoc}
*/
public void format(final LoggingEvent event, final StringBuffer toAppendTo) {
final int initialLength = toAppendTo.length();
toAppendTo.append(event.getLoggerName());
abbreviate(initialLength, toAppendTo);
}
}
代码示例来源:origin: jenkinsci/jenkins
private void reset() {
StringBuffer buf = getStringBuffer();
if (buf.length()>4096)
out = new StringWriter(256);
else
buf.setLength(0);
}
代码示例来源:origin: log4j/log4j
/**
Format event to string buffer.
@param sbuf string buffer to receive formatted event, may not be null.
@param e event to format, may not be null.
*/
public void format(final StringBuffer sbuf, final LoggingEvent e) {
for (int i = 0; i < patternConverters.length; i++) {
int startField = sbuf.length();
patternConverters[i].format(e, sbuf);
patternFields[i].format(startField, sbuf);
}
}
代码示例来源:origin: log4j/log4j
protected String stackTraceToString(Throwable t) {
String s = null;
synchronized (sw) {
t.printStackTrace(pw);
s = sw.toString();
sw.getBuffer().setLength(0);
}
return s;
}
代码示例来源:origin: alibaba/druid
public String toString() {
StringBuffer buf = new StringBuffer();
output(buf);
return buf.toString();
}
代码示例来源:origin: apache/incubator-dubbo
private static void booleanArrayAppend(StringBuffer sbuf, boolean[] a) {
sbuf.append('[');
final int len = a.length;
for (int i = 0; i < len; i++) {
sbuf.append(a[i]);
if (i != len - 1) {
sbuf.append(", ");
}
}
sbuf.append(']');
}
代码示例来源:origin: apache/incubator-dubbo
/**
* read string.
*
* @param reader Reader instance.
* @return String.
* @throws IOException
*/
public static String read(Reader reader) throws IOException {
StringWriter writer = new StringWriter();
try {
write(reader, writer);
return writer.getBuffer().toString();
} finally {
writer.close();
}
}
代码示例来源:origin: spring-projects/spring-framework
public String scramble(ProceedingJoinPoint pjp) throws Throwable {
String message = (String) pjp.proceed();
return new StringBuffer(message).reverse().toString();
}
代码示例来源:origin: spring-projects/spring-framework
private static URI initUri(HttpServletRequest request) throws URISyntaxException {
Assert.notNull(request, "'request' must not be null");
StringBuffer url = request.getRequestURL();
String query = request.getQueryString();
if (StringUtils.hasText(query)) {
url.append('?').append(query);
}
return new URI(url.toString());
}
内容来源于网络,如有侵权,请联系作者删除!