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; }
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; }
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; }
/** * 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(); } }
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; }
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; }
/** * 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(); }
if (buf.capacity() > UPPER_LIMIT) { buf = new StringBuilder(DEFAULT_SIZE); } else {
/** {@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(); } }
@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); }
@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());
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; }
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; }
if (buf.capacity() > UPPER_LIMIT) { buf = new StringBuilder(DEFAULT_SIZE); } else {
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; }
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; }
@Override @JTranscAsync public synchronized int capacity() { return super.capacity(); }
/** * 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); } }
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; }
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(); }