java.lang.StringBuilder.capacity()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(201)

本文整理了Java中java.lang.StringBuilder.capacity()方法的一些代码示例,展示了StringBuilder.capacity()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。StringBuilder.capacity()方法的具体详情如下:
包路径:java.lang.StringBuilder
类名称:StringBuilder
方法名:capacity

StringBuilder.capacity介绍

暂无

代码示例

代码示例来源:origin: netty/netty

public StringBuilder stringBuilder() {
  StringBuilder sb = stringBuilder;
  if (sb == null) {
    return stringBuilder = new StringBuilder(STRING_BUILDER_INITIAL_SIZE);
  }
  if (sb.capacity() > STRING_BUILDER_MAX_SIZE) {
    sb.setLength(STRING_BUILDER_INITIAL_SIZE);
    sb.trimToSize();
  }
  sb.setLength(0);
  return sb;
}

代码示例来源:origin: redisson/redisson

public StringBuilder stringBuilder() {
  StringBuilder sb = stringBuilder;
  if (sb == null) {
    return stringBuilder = new StringBuilder(STRING_BUILDER_INITIAL_SIZE);
  }
  if (sb.capacity() > STRING_BUILDER_MAX_SIZE) {
    sb.setLength(STRING_BUILDER_INITIAL_SIZE);
    sb.trimToSize();
  }
  sb.setLength(0);
  return sb;
}

代码示例来源:origin: wildfly/wildfly

public StringBuilder stringBuilder() {
  StringBuilder sb = stringBuilder;
  if (sb == null) {
    return stringBuilder = new StringBuilder(STRING_BUILDER_INITIAL_SIZE);
  }
  if (sb.capacity() > STRING_BUILDER_MAX_SIZE) {
    sb.setLength(STRING_BUILDER_INITIAL_SIZE);
    sb.trimToSize();
  }
  sb.setLength(0);
  return sb;
}

代码示例来源:origin: org.apache.logging.log4j/log4j-api

/**
 * Ensures that the char[] array of the specified StringBuilder does not exceed the specified number of characters.
 * This method is useful to ensure that excessively long char[] arrays are not kept in memory forever.
 *
 * @param stringBuilder the StringBuilder to check
 * @param maxSize the maximum number of characters the StringBuilder is allowed to have
 * @since 2.9
 */
public static void trimToMaxSize(final StringBuilder stringBuilder, final int maxSize) {
  if (stringBuilder != null && stringBuilder.capacity() > maxSize) {
    stringBuilder.setLength(maxSize);
    stringBuilder.trimToSize();
  }
}

代码示例来源:origin: fengjiachun/Jupiter

public StringBuilder stringBuilder() {
  StringBuilder builder = stringBuilder;
  if (builder == null) {
    stringBuilder = builder = new StringBuilder(DEFAULT_STRING_BUILDER_INITIAL_CAPACITY);
  } else {
    if (builder.capacity() > DEFAULT_STRING_BUILDER_MAX_CAPACITY) {
      // ensure memory overhead
      stringBuilderValueUpdater.set(builder, new char[DEFAULT_STRING_BUILDER_INITIAL_CAPACITY]);
    }
    builder.setLength(0);
  }
  return builder;
}

代码示例来源:origin: fengjiachun/Jupiter

public StringBuilder stringBuilder() {
  StringBuilder builder = stringBuilder;
  if (builder == null) {
    stringBuilder = builder = new StringBuilder(DEFAULT_STRING_BUILDER_INITIAL_CAPACITY);
  } else {
    if (builder.capacity() > DEFAULT_STRING_BUILDER_MAX_CAPACITY) {
      // ensure memory overhead
      stringBuilderValueUpdater.set(builder, new char[DEFAULT_STRING_BUILDER_INITIAL_CAPACITY]);
    }
    builder.setLength(0);
  }
  return builder;
}

代码示例来源:origin: guoguibing/librec

/**
 * Write contents in {@code Collection<T>} to a file with the help of a writer helper.
 *
 * @param filePath path of the file to write
 * @param ts       content to write
 * @param lw       writer helper
 * @param append   whether to append
 * @param <T>      the type parameter
 * @throws Exception if error occurs
 */
public static <T> void writeList(String filePath, Collection<T> ts, Converter<T, String> lw, boolean append)
    throws Exception {
  BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, append), "UTF-8"));
  StringBuilder contents = new StringBuilder();
  int count = 0;
  for (T t : ts) {
    contents.append(lw != null ? lw.transform(t) : t);
    contents.append("\n");
    count++;
    if (count >= 1000) {
      bw.write(contents.toString());
      count = 0;
      contents = new StringBuilder();
    }
  }
  if (contents.capacity() > 0)
    bw.write(contents.toString());
  bw.close();
}

代码示例来源:origin: ch.qos.logback/logback-classic

if (buf.capacity() > UPPER_LIMIT) {
  buf = new StringBuilder(DEFAULT_SIZE);
} else {

代码示例来源:origin: apache/ignite

/** {@inheritDoc} */
@Override public String toString() {
  if (tail == null)
    return super.toString();
  else {
    int tailLen = tail.length();
    StringBuilder res = new StringBuilder(impl().capacity() + tailLen + 100);
    res.append(impl());
    if (tail.getSkipped() > 0) {
      res.append("... and ").append(String.valueOf(tail.getSkipped() + tailLen))
        .append(" skipped ...");
    }
    res.append(tail.toString());
    return res.toString();
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-api

@Test
public void trimToMaxSizeWithLargeCapacity() throws Exception {
  final StringBuilder sb = new StringBuilder();
  final char[] value = new char[4 * 1024];
  sb.append(value);
  sb.setLength(0);
  assertTrue("needs trimming", sb.capacity() > Constants.MAX_REUSABLE_MESSAGE_SIZE);
  StringBuilders.trimToMaxSize(sb, Constants.MAX_REUSABLE_MESSAGE_SIZE);
  assertTrue("trimmed OK", sb.capacity() <= Constants.MAX_REUSABLE_MESSAGE_SIZE);
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@Test
public void testGetStringBuilderCapacityRestrictedToMax() throws Exception {
  final StringBuilder sb = ConcreteStringLayout.getStringBuilder();
  final int initialCapacity = sb.capacity();
  assertEquals("initial capacity", ConcreteStringLayout.DEFAULT_STRING_BUILDER_SIZE, sb.capacity());
  assertTrue("capacity not grown", sb.capacity() == initialCapacity);
  assertEquals("length=msg length", SMALL, sb.length());
  assertEquals("capacity unchanged", sb2.capacity(), initialCapacity);
  assertEquals("empty, ready for use", 0, sb2.length());
  final String largeMessage = new String(new char[LARGE]);
  sb2.append(largeMessage);
  assertTrue("capacity grown to fit msg length", sb2.capacity() >= LARGE);
  assertTrue("capacity is now greater than max length", sb2.capacity() >= ConcreteStringLayout.MAX_STRING_BUILDER_SIZE);
  assertEquals("length=msg length", LARGE, sb2.length());
  sb2.setLength(0); // set 0 before next getStringBuilder() call
  assertEquals("empty, cleared", 0, sb2.length());
  assertTrue("capacity remains very large", sb2.capacity() >= ConcreteStringLayout.MAX_STRING_BUILDER_SIZE);
  assertEquals("capacity, trimmed to MAX_STRING_BUILDER_SIZE", ConcreteStringLayout.MAX_STRING_BUILDER_SIZE, sb3.capacity());
  assertEquals("empty, ready for use", 0, sb3.length());

代码示例来源:origin: sd4324530/fastweixin

public void surroundWith(String tag) {
  StringBuilder sb = new StringBuilder(builder.capacity() + tag.length()
      * 2 + 5);
  sb.append("<").append(tag).append(">\n").append(builder).append("</")
      .append(tag).append(">\n");
  builder = sb;
}

代码示例来源:origin: io.netty/netty-common

public StringBuilder stringBuilder() {
  StringBuilder sb = stringBuilder;
  if (sb == null) {
    return stringBuilder = new StringBuilder(STRING_BUILDER_INITIAL_SIZE);
  }
  if (sb.capacity() > STRING_BUILDER_MAX_SIZE) {
    sb.setLength(STRING_BUILDER_INITIAL_SIZE);
    sb.trimToSize();
  }
  sb.setLength(0);
  return sb;
}

代码示例来源:origin: camunda/camunda-bpm-platform

if (buf.capacity() > UPPER_LIMIT) {
 buf = new StringBuilder(DEFAULT_SIZE);
} else {

代码示例来源:origin: org.apache.activemq/artemis-jms-client-all

public StringBuilder stringBuilder() {
  StringBuilder sb = stringBuilder;
  if (sb == null) {
    return stringBuilder = new StringBuilder(STRING_BUILDER_INITIAL_SIZE);
  }
  if (sb.capacity() > STRING_BUILDER_MAX_SIZE) {
    sb.setLength(STRING_BUILDER_INITIAL_SIZE);
    sb.trimToSize();
  }
  sb.setLength(0);
  return sb;
}

代码示例来源:origin: com.couchbase.client/core-io

public StringBuilder stringBuilder() {
  StringBuilder sb = stringBuilder;
  if (sb == null) {
    return stringBuilder = new StringBuilder(STRING_BUILDER_INITIAL_SIZE);
  }
  if (sb.capacity() > STRING_BUILDER_MAX_SIZE) {
    sb.setLength(STRING_BUILDER_INITIAL_SIZE);
    sb.trimToSize();
  }
  sb.setLength(0);
  return sb;
}

代码示例来源:origin: com.jtransc/jtransc-rt

@Override
@JTranscAsync
public synchronized int capacity() {
  return super.capacity();
}

代码示例来源:origin: com.googlecode.streamflyer/streamflyer-core

/**
 * Adjusts the capacity of the buffer.
 */
private void adjustCapacityOfBuffer() {
  // is the current capacity not big enough?
  if (characterBuffer.capacity() < requestedNumCharactersInBuffer) {
    // increase the capacity (we delegate to the default behavior of
    // StringBuffer)
    characterBuffer.ensureCapacity(requestedNumCharactersInBuffer);
  }
}

代码示例来源:origin: io.bitsensor/proto

public StringBuilder stringBuilder() {
  StringBuilder sb = stringBuilder;
  if (sb == null) {
    return stringBuilder = new StringBuilder(STRING_BUILDER_INITIAL_SIZE);
  }
  if (sb.capacity() > STRING_BUILDER_MAX_SIZE) {
    sb.setLength(STRING_BUILDER_INITIAL_SIZE);
    sb.trimToSize();
  }
  sb.setLength(0);
  return sb;
}

代码示例来源:origin: Baqend/Orestes-Bloomfilter

private String createRandomName() {
  String chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  StringBuilder builder = new StringBuilder(6);
  for (int i = 0; i < builder.capacity(); i += 1) {
    builder.append(chars.charAt(rnd.nextInt(chars.length())));
  }
  return builder.toString();
}

相关文章