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; }
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()); }
/** * <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); }
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; }
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(')'); }
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);
/** * 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(); }
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(); }
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(); }
char j; StringBuilder tmp = new StringBuilder(); tmp.ensureCapacity(content.length() * 6);
char j; StringBuilder tmp = new StringBuilder(); tmp.ensureCapacity(content.length() * 6);
sb.ensureCapacity(sb.length() + size + 1); sb.append(begin); for (int i = 0; i < numberOfArguments; i++) {
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; } }
if (collectorTextChild != null) { if (collectorTextChildBuff.length() == 0) { collectorTextChildBuff.ensureCapacity( collectorTextChild.getNodeValue().length() + child.getNodeValue().length()); collectorTextChildBuff.append(collectorTextChild.getNodeValue());
builder.ensureCapacity(estimatedLength); for (String displayString : aggregatedCommandsExecuted.keySet()) { if (builder.length() > 0) {
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, "&"); break; case '"': pos = appendAndEscape(pValue, pos, i, builder, """); break; default: break; } } builder.append(pValue.substring(pos)); return builder.toString(); } }
builder.ensureCapacity(pValue.length() + 30);
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); }
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)); } }
@Override @JTranscAsync public synchronized void ensureCapacity(int minimumCapacity) { super.ensureCapacity(minimumCapacity); }