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

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

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

StringBuffer.ensureCapacity介绍

[英]Ensures that the capacity of the buffer is at least equal to the specified minimum. If the current capacity of this string buffer is less than the argument, then a new internal buffer is allocated with greater capacity. The new capacity is the larger of:

  • The minimumCapacity argument.
  • Twice the old capacity, plus 2.
    If the minimumCapacity argument is nonpositive, this method takes no action and simply returns.
    [中]确保缓冲区的容量至少等于指定的最小值。如果此字符串缓冲区的当前容量小于参数,则会分配一个容量更大的新内部缓冲区。新容量为以下两项中的较大者:
    *minimumCapacity参数。
    *两倍于旧容量,再加上2
    如果minimumCapacity参数为非正参数,则此方法不执行任何操作,只返回。

代码示例

代码示例来源: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(buf, "")            = buf.append("java.lang.String@1e23"
 * ObjectUtils.identityToString(buf, Boolean.TRUE)  = buf.append("java.lang.Boolean@7fa"
 * ObjectUtils.identityToString(buf, Boolean.TRUE)  = buf.append("java.lang.Boolean@7fa")
 * </pre>
 *
 * @param buffer  the buffer to append to
 * @param object  the object to create a toString for
 * @since 2.4
 */
public static void identityToString(final StringBuffer buffer, 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));
  buffer.ensureCapacity(buffer.length() + name.length() + 1 + hexString.length());
  buffer.append(name)
     .append(AT_SIGN)
     .append(hexString);
}

代码示例来源:origin: javax.activation/activation

private static String fixEscapeSequences(String inputString) {
int inputLength = inputString.length();
StringBuffer buffer = new StringBuffer();
buffer.ensureCapacity(inputLength);
for (int i = 0; i < inputLength; ++i) {
  char currentChar = inputString.charAt(i);
  if (currentChar != '\\') {
  buffer.append(currentChar);
  } else {
  if (i < inputLength - 1) {
    char nextChar = inputString.charAt(i + 1);
    buffer.append(nextChar);
    //  force a skip over the next character too
    ++i;
  } else {
    buffer.append(currentChar);
  }
  }
}
return buffer.toString();
}

代码示例来源:origin: javax.activation/activation

/**
   * A routine that knows how to strip the quotes and
   * escape sequences from the given value.
   */
  private static String unquote(String value) {
    int valueLength = value.length();
    StringBuffer buffer = new StringBuffer();
    buffer.ensureCapacity(valueLength);

    boolean escaped = false;
    for (int i = 0; i < valueLength; ++i) {
      char currentChar = value.charAt(i);
      if (!escaped && (currentChar != '\\')) {
        buffer.append(currentChar);
      } else if (escaped) {
        buffer.append(currentChar);
        escaped = false;
      } else {
        escaped = true;
      }
    }

    return buffer.toString();
  }
}

代码示例来源:origin: javax.activation/activation

/**
 * Return a string representation of this object.
 */
public String toString() {
  StringBuffer buffer = new StringBuffer();
  buffer.ensureCapacity(parameters.size() * 16);
    //    heuristic: 8 characters per field
  Enumeration keys = parameters.keys();
  while (keys.hasMoreElements()) {
    String key = (String)keys.nextElement();
    buffer.append("; ");
    buffer.append(key);
    buffer.append('=');
  buffer.append(quote((String)parameters.get(key)));
  }
  return buffer.toString();
}

代码示例来源:origin: javax.activation/activation

/**
 * A routine that knows how and when to quote and escape the given value.
 */
private static String quote(String value) {
  boolean needsQuotes = false;
  //    check to see if we actually have to quote this thing
  int length = value.length();
  for (int i = 0; (i < length) && !needsQuotes; i++) {
    needsQuotes = !isTokenChar(value.charAt(i));
  }
  if (needsQuotes) {
    StringBuffer buffer = new StringBuffer();
    buffer.ensureCapacity((int)(length * 1.5));
    //    add the initial quote
    buffer.append('"');
    //    add the properly escaped text
    for (int i = 0; i < length; ++i) {
      char c = value.charAt(i);
      if ((c == '\\') || (c == '"'))
        buffer.append('\\');
      buffer.append(c);
    }
    //    add the closing quote
    buffer.append('"');
    return buffer.toString();
  } else {
    return value;
  }
}

代码示例来源:origin: internetarchive/heritrix3

/**
    Ensures that the capacity is at least equal to the specified minimum.
    @param minCapacity the minimum desired capacity
  */
  private void ensureCapacity(int minCapacity) {
    assert checkInvariants();
    if (minCapacity > aux.length) {
      int nc = 2 * aux.length; // New capacity.
      while (nc < minCapacity) {
        nc *= 2;
      }
      byte[] oldAux = aux;
      aux = new byte[nc];
      System.arraycopy(oldAux, 0, aux, 0, string.length());
    }
    string.ensureCapacity(minCapacity);
    assert checkInvariants();
  }
}

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

private static String fixEscapeSequences(String inputString) {
int inputLength = inputString.length();
StringBuffer buffer = new StringBuffer();
buffer.ensureCapacity(inputLength);
for (int i = 0; i < inputLength; ++i) {
  char currentChar = inputString.charAt(i);
  if (currentChar != '\\') {
  buffer.append(currentChar);
  } else {
  if (i < inputLength - 1) {
    char nextChar = inputString.charAt(i + 1);
    buffer.append(nextChar);
    //  force a skip over the next character too
    ++i;
  } else {
    buffer.append(currentChar);
  }
  }
}
return buffer.toString();
}

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

/**
   * A routine that knows how to strip the quotes and
   * escape sequences from the given value.
   */
  private static String unquote(String value) {
    int valueLength = value.length();
    StringBuffer buffer = new StringBuffer();
    buffer.ensureCapacity(valueLength);

    boolean escaped = false;
    for (int i = 0; i < valueLength; ++i) {
      char currentChar = value.charAt(i);
      if (!escaped && (currentChar != '\\')) {
        buffer.append(currentChar);
      } else if (escaped) {
        buffer.append(currentChar);
        escaped = false;
      } else {
        escaped = true;
      }
    }

    return buffer.toString();
  }
}

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

/**
 * Return a string representation of this object.
 */
public String toString() {
  StringBuffer buffer = new StringBuffer();
  buffer.ensureCapacity(parameters.size() * 16);
    //    heuristic: 8 characters per field
  Enumeration keys = parameters.keys();
  while (keys.hasMoreElements()) {
    String key = (String)keys.nextElement();
    buffer.append("; ");
    buffer.append(key);
    buffer.append('=');
  buffer.append(quote((String)parameters.get(key)));
  }
  return buffer.toString();
}

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

/**
 * A routine that knows how and when to quote and escape the given value.
 */
private static String quote(String value) {
  boolean needsQuotes = false;
  //    check to see if we actually have to quote this thing
  int length = value.length();
  for (int i = 0; (i < length) && !needsQuotes; i++) {
    needsQuotes = !isTokenChar(value.charAt(i));
  }
  if (needsQuotes) {
    StringBuffer buffer = new StringBuffer();
    buffer.ensureCapacity((int)(length * 1.5));
    //    add the initial quote
    buffer.append('"');
    //    add the properly escaped text
    for (int i = 0; i < length; ++i) {
      char c = value.charAt(i);
      if ((c == '\\') || (c == '"'))
        buffer.append('\\');
      buffer.append(c);
    }
    //    add the closing quote
    buffer.append('"');
    return buffer.toString();
  } else {
    return value;
  }
}

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

final StringBuffer buffer = writer.getBuffer();
  buffer.setLength(0);
  buffer.ensureCapacity(capacity);
} else {
  writer = new StringWriter(capacity);

代码示例来源:origin: org.glassfish.external/schema2beans

/**
   * Ensures the capacity of every buffer is at least @param minimumCapacity.
   */
  public void ensureCapacity(int minimumCapacity) {
    for (int i = 0; i < bufferCount; i++)
      listOut[i].ensureCapacity(minimumCapacity);
  }
}

代码示例来源:origin: bcdev/beam

private static StringBuffer getStringBuffer() {
  _stringBuffer.ensureCapacity(1024);
  return _stringBuffer;
}

代码示例来源:origin: org.glassfish.external/schema2beans

private void privateInit() {
  for (int i = 0; i < bufferCount; i++) {
    listOut[i] = new StringBuffer();
    listOut[i].ensureCapacity(INITIAL_BUFFER_CAPACITY);
  }
}

代码示例来源:origin: com.sun.xml/com.springsource.com.sun.xml.fastinfoset

public final void convertToCharacters(Object data, StringBuffer s) {
  if (data == null) {
    return;
  }
  final byte[] value = (byte[]) data;
  if (value.length == 0) {
    return;
  }
  s.ensureCapacity(value.length * 2);
  for (int i = 0; i < value.length; ++i) {
    s.append(NIBBLE_TO_HEXADECIMAL_TABLE[(value[i] >>> 4) & 0xf]);
    s.append(NIBBLE_TO_HEXADECIMAL_TABLE[value[i] & 0xf]);
  }
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.fastinfoset

public final void convertToCharacters(Object data, StringBuffer s) {
  if (data == null) {
    return;
  }
  final byte[] value = (byte[]) data;
  if (value.length == 0) {
    return;
  }
  s.ensureCapacity(value.length * 2);
  for (int i = 0; i < value.length; ++i) {
    s.append(NIBBLE_TO_HEXADECIMAL_TABLE[(value[i] >>> 4) & 0xf]);
    s.append(NIBBLE_TO_HEXADECIMAL_TABLE[value[i] & 0xf]);
  }
}

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

public final void convertToCharacters(Object data, StringBuffer s) {
  if (data == null) {
    return;
  }
  final byte[] value = (byte[]) data;
  if (value.length == 0) {
    return;
  }
  s.ensureCapacity(value.length * 2);
  for (int i = 0; i < value.length; ++i) {
    s.append(NIBBLE_TO_HEXADECIMAL_TABLE[(value[i] >>> 4) & 0xf]);
    s.append(NIBBLE_TO_HEXADECIMAL_TABLE[value[i] & 0xf]);
  }
}

代码示例来源:origin: apache/servicemix-bundles

public final void convertToCharacters(Object data, StringBuffer s) {
  if (data == null) {
    return;
  }
  final byte[] value = (byte[]) data;
  if (value.length == 0) {
    return;
  }
  s.ensureCapacity(value.length * 2);
  for (int i = 0; i < value.length; ++i) {
    s.append(NIBBLE_TO_HEXADECIMAL_TABLE[(value[i] >>> 4) & 0xf]);
    s.append(NIBBLE_TO_HEXADECIMAL_TABLE[value[i] & 0xf]);
  }
}

代码示例来源:origin: jakarta.activation/jakarta.activation-api

/**
 * Return a string representation of this object.
 */
public String toString() {
  StringBuffer buffer = new StringBuffer();
  buffer.ensureCapacity(parameters.size() * 16);
    //    heuristic: 8 characters per field
  Enumeration keys = parameters.keys();
  while (keys.hasMoreElements()) {
    String key = (String)keys.nextElement();
    buffer.append("; ");
    buffer.append(key);
    buffer.append('=');
  buffer.append(quote((String)parameters.get(key)));
  }
  return buffer.toString();
}

代码示例来源:origin: stackoverflow.com

// initial capacity (of the array behind the scenes) will be 16. 
StringBuffer buff1 = new StringBuffer("tuts point");
//  The above constructor increases the capacity by 16 + stringArgument.length = 16 + 10 = 26 ( hence the output)
System.out.println("Old Capacity of buff1 = " + buff1.capacity()); // 26 ->check

buff1.ensureCapacity(28); // here 28 is minimumcapacity.
// the above line of code calls expandCapacity() of AbstractStringBuilder which does this:
//`(value.length + 1) * 2` i.e, 26+1 * 2 = 54 . Hence the output. 

System.out.println("New Capacity of buff1 = " + buff1.capacity()); //54 --> check

相关文章