/** * <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); }
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(); }
/** * 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(); } }
/** * 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(); }
/** * 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; } }
/** 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(); } }
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(); }
/** * 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(); } }
/** * 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(); }
/** * 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; } }
final StringBuffer buffer = writer.getBuffer(); buffer.setLength(0); buffer.ensureCapacity(capacity); } else { writer = new StringWriter(capacity);
/** * 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); } }
private static StringBuffer getStringBuffer() { _stringBuffer.ensureCapacity(1024); return _stringBuffer; }
private void privateInit() { for (int i = 0; i < bufferCount; i++) { listOut[i] = new StringBuffer(); listOut[i].ensureCapacity(INITIAL_BUFFER_CAPACITY); } }
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]); } }
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]); } }
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]); } }
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]); } }
/** * 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(); }
// 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