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

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

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

StringBuilder.ensureCapacity介绍

暂无

代码示例

代码示例来源:origin: alibaba/Sentinel

public static StringBuilder appendLog(String str, StringBuilder appender, char delimiter) {
  if (str != null) {
    int len = str.length();
    appender.ensureCapacity(appender.length() + len);
    for (int i = 0; i < len; i++) {
      char c = str.charAt(i);
      if (c == '\n' || c == '\r' || c == delimiter) {
        c = ' ';
      }
      appender.append(c);
    }
  }
  return appender;
}

代码示例来源:origin: square/picasso

static void updateThreadName(Request data) {
 String name = data.getName();
 StringBuilder builder = NAME_BUILDER.get();
 builder.ensureCapacity(Utils.THREAD_PREFIX.length() + name.length());
 builder.replace(Utils.THREAD_PREFIX.length(), builder.length(), name);
 Thread.currentThread().setName(builder.toString());
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * <p>Appends the toString that would be produced by {@code Object}
 * if a class did not override toString itself. {@code null}
 * will throw a NullPointerException for either of the two parameters. </p>
 *
 * <pre>
 * ObjectUtils.identityToString(builder, "")            = builder.append("java.lang.String@1e23"
 * ObjectUtils.identityToString(builder, Boolean.TRUE)  = builder.append("java.lang.Boolean@7fa"
 * ObjectUtils.identityToString(builder, Boolean.TRUE)  = builder.append("java.lang.Boolean@7fa")
 * </pre>
 *
 * @param builder  the builder to append to
 * @param object  the object to create a toString for
 * @since 3.2
 */
public static void identityToString(final StringBuilder builder, final Object object) {
  Validate.notNull(object, "Cannot get the toString of a null object");
  final String name = object.getClass().getName();
  final String hexString = Integer.toHexString(System.identityHashCode(object));
  builder.ensureCapacity(builder.length() +  name.length() + 1 + hexString.length());
  builder.append(name)
     .append(AT_SIGN)
     .append(hexString);
}

代码示例来源:origin: org.postgresql/postgresql

private static boolean appendSingleIntervalCast(StringBuilder buf, String cmp, String type, String value, String pgType) {
 if (!areSameTsi(type, cmp)) {
  return false;
 }
 buf.ensureCapacity(buf.length() + 5 + 4 + 14 + value.length() + pgType.length());
 buf.append("CAST(").append(value).append("||' ").append(pgType).append("' as interval)");
 return true;
}

代码示例来源:origin: org.postgresql/postgresql

private static void singleArgumentFunctionCall(StringBuilder buf, String call, String functionName,
  List<? extends CharSequence> parsedArgs) throws PSQLException {
 if (parsedArgs.size() != 1) {
  throw new PSQLException(GT.tr("{0} function takes one and only one argument.", functionName),
    PSQLState.SYNTAX_ERROR);
 }
 CharSequence arg0 = parsedArgs.get(0);
 buf.ensureCapacity(buf.length() + call.length() + arg0.length() + 1);
 buf.append(call).append(arg0).append(')');
}

代码示例来源:origin: square/picasso

private String createKey(StringBuilder builder) {
 Request data = this;
 if (data.stableKey != null) {
  builder.ensureCapacity(data.stableKey.length() + KEY_PADDING);
  builder.append(data.stableKey);
 } else if (data.uri != null) {
  String path = data.uri.toString();
  builder.ensureCapacity(path.length() + KEY_PADDING);
  builder.append(path);
 } else {
  builder.ensureCapacity(KEY_PADDING);
  builder.append(data.resourceId);

代码示例来源:origin: pentaho/pentaho-kettle

/**
 * Turn a byte array into a version four UUID string. Adapted from org.apache.commons.id.uuid.UUID.java
 *
 * @param raw
 * @return
 */
private String getUUIDString( byte[] raw ) {
 StringBuilder buf = new StringBuilder( new String( encodeHex( raw ) ) );
 while ( buf.length() != 32 ) {
  buf.insert( 0, "0" );
 }
 buf.ensureCapacity( 32 );
 buf.insert( 8, '-' );
 buf.insert( 13, '-' );
 buf.insert( 18, '-' );
 buf.insert( 23, '-' );
 return buf.toString();
}

代码示例来源:origin: qiujuer/Genius-Android

private String convertValueToMessage(int value) {
  String format = mIndicatorFormatter != null ? mIndicatorFormatter : DEFAULT_FORMATTER;
  //We're trying to re-use the Formatter here to avoid too much memory allocations
  //But I'm not completey sure if it's doing anything good... :(
  //Previously, this condition was wrong so the Formatter was always re-created
  //But as I fixed the condition, the formatter started outputting trash characters from previous
  //calls, so I mark the StringBuilder as empty before calling format again.
  //Anyways, I see the memory usage still go up on every call to this method
  //and I have no clue on how to fix that... damn Strings...
  if (mFormatter == null || !mFormatter.locale().equals(Locale.getDefault())) {
    int bufferSize = format.length() + String.valueOf(mMax).length();
    if (mFormatBuilder == null) {
      mFormatBuilder = new StringBuilder(bufferSize);
    } else {
      mFormatBuilder.ensureCapacity(bufferSize);
    }
    mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
  } else {
    mFormatBuilder.setLength(0);
  }
  return mFormatter.format(format, value).toString();
}

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

public static String createRandomString(Random rnd, int length) {
  final StringBuilder sb = new StringBuilder();
  sb.ensureCapacity(length);
  
  for (int i = 0; i < length; i++) {
    sb.append((char) (rnd.nextInt(26) + 65));
  }
  return sb.toString();
}

代码示例来源:origin: looly/hutool

char j;
StringBuilder tmp = new StringBuilder();
tmp.ensureCapacity(content.length() * 6);

代码示例来源:origin: looly/hutool

char j;
StringBuilder tmp = new StringBuilder();
tmp.ensureCapacity(content.length() * 6);

代码示例来源:origin: org.postgresql/postgresql

sb.ensureCapacity(sb.length() + size + 1);
sb.append(begin);
for (int i = 0; i < numberOfArguments; i++) {

代码示例来源:origin: org.freemarker/freemarker

static private void mergeAdjacentText(Node parent, StringBuilder collectorBuf) {
  Node child = parent.getFirstChild();
  while (child != null) {
    Node next = child.getNextSibling();
    if (child instanceof Text) {
      boolean atFirstText = true;
      while (next instanceof Text) { //
        if (atFirstText) {
          collectorBuf.setLength(0);
          collectorBuf.ensureCapacity(child.getNodeValue().length() + next.getNodeValue().length());
          collectorBuf.append(child.getNodeValue());
          atFirstText = false;
        }
        collectorBuf.append(next.getNodeValue());
        
        parent.removeChild(next);
        
        next = child.getNextSibling();
      }
      if (!atFirstText && collectorBuf.length() != 0) {
        ((CharacterData) child).setData(collectorBuf.toString());
      }
    } else {
      mergeAdjacentText(child, collectorBuf);
    }
    child = next;
  }
}

代码示例来源:origin: org.freemarker/freemarker

if (collectorTextChild != null) {
  if (collectorTextChildBuff.length() == 0) {
    collectorTextChildBuff.ensureCapacity(
        collectorTextChild.getNodeValue().length() + child.getNodeValue().length());
    collectorTextChildBuff.append(collectorTextChild.getNodeValue());

代码示例来源:origin: PipelineAI/pipeline

builder.ensureCapacity(estimatedLength);
for (String displayString : aggregatedCommandsExecuted.keySet()) {
  if (builder.length() > 0) {

代码示例来源:origin: haraldk/TwelveMonkeys

private static String maybeEscapeAttributeValue(final String pValue) {
  int startEscape = needsEscapeAttribute(pValue);
  if (startEscape < 0) {
    return pValue;
  }
  else {
    StringBuilder builder = new StringBuilder(pValue.substring(0, startEscape));
    builder.ensureCapacity(pValue.length() + 16);
    int pos = startEscape;
    for (int i = pos; i < pValue.length(); i++) {
      switch (pValue.charAt(i)) {
        case '&':
          pos = appendAndEscape(pValue, pos, i, builder, "&amp;");
          break;
        case '"':
          pos = appendAndEscape(pValue, pos, i, builder, "&quot;");
          break;
        default:
          break;
      }
    }
    builder.append(pValue.substring(pos));
    return builder.toString();
  }
}

代码示例来源:origin: haraldk/TwelveMonkeys

builder.ensureCapacity(pValue.length() + 30);

代码示例来源:origin: org.jruby/jruby-complete

private static StringBuilder padding(final StringBuilder out, CharSequence sequence,
                   final int width, final char padder) {
  final int len = sequence.length();
  if (len >= width) return out.append(sequence);
  if (width > SMALLBUF) throw new IndexOutOfBoundsException("padding width " + width + " too large");
  out.ensureCapacity(width + len);
  for (int i = len; i < width; i++) out.append(padder);
  return out.append(sequence);
}

代码示例来源:origin: hypercube1024/firefly

public static final void bytesToHexAppend(byte[] bs, int off, int length,
                     StringBuilder sb) {
  if (bs.length <= off || bs.length < off + length)
    throw new IllegalArgumentException();
  sb.ensureCapacity(sb.length() + length * 2);
  for (int i = off; i < (off + length); i++) {
    sb.append(Character.forDigit((bs[i] >>> 4) & 0xf, 16));
    sb.append(Character.forDigit(bs[i] & 0xf, 16));
  }
}

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

@Override
@JTranscAsync
public synchronized void ensureCapacity(int minimumCapacity) {
  super.ensureCapacity(minimumCapacity);
}

相关文章

微信公众号

最新文章

更多