Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider the StringBuilder's capacity instead of content length when being trimmed #92

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static boolean equalsIgnoreCase(final CharSequence left, final int leftOf
* @since 2.9
*/
public static void trimToMaxSize(final StringBuilder stringBuilder, final int maxSize) {
if (stringBuilder != null && stringBuilder.length() > maxSize) {
if (stringBuilder != null && stringBuilder.capacity() > maxSize) {
stringBuilder.setLength(maxSize);
stringBuilder.trimToSize();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,18 @@ public void trimToMaxSize() throws Exception {
StringBuilders.trimToMaxSize(sb, Constants.MAX_REUSABLE_MESSAGE_SIZE);
assertTrue("trimmed OK", sb.length() <= Constants.MAX_REUSABLE_MESSAGE_SIZE);
}

@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);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,31 @@ public String toSerializable(final LogEvent event) {
@Test
public void testGetStringBuilderCapacityRestrictedToMax() throws Exception {
final StringBuilder sb = ConcreteStringLayout.getStringBuilder();
int initialCapacity = sb.capacity();
assertEquals("initial capacity", ConcreteStringLayout.DEFAULT_STRING_BUILDER_SIZE, sb.capacity());

final int MEDIUM = ConcreteStringLayout.DEFAULT_STRING_BUILDER_SIZE + 100;
final String mediumMessage = new String(new char[MEDIUM]);
final int SMALL = 100;
final String mediumMessage = new String(new char[SMALL]);
sb.append(mediumMessage);
final int GROWN = sb.capacity();
assertTrue("capacity has grown", GROWN >= MEDIUM);
assertEquals("length=msg length", MEDIUM, sb.length());
assertTrue("capacity not grown", sb.capacity() == initialCapacity);
assertEquals("length=msg length", SMALL, sb.length());

final int LARGE = 4096;
final String largeMessage = new String(new char[LARGE]);
final StringBuilder sb2 = ConcreteStringLayout.getStringBuilder();
assertEquals("resized capacity", GROWN, sb2.capacity());
assertEquals("capacity unchanged", sb2.capacity(), initialCapacity);
assertEquals("empty, ready for use", 0, sb2.length());

final int LARGE = ConcreteStringLayout.MAX_STRING_BUILDER_SIZE * 2;
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
assertEquals("empty, cleared", 0, sb2.length());
assertTrue("capacity remains very large", sb2.capacity() >= ConcreteStringLayout.MAX_STRING_BUILDER_SIZE);

final StringBuilder next = ConcreteStringLayout.getStringBuilder();
assertEquals("max capacity", ConcreteStringLayout.MAX_STRING_BUILDER_SIZE, next.capacity());
assertEquals("capacity, trimmed to MAX_STRING_BUILDER_SIZE", ConcreteStringLayout.MAX_STRING_BUILDER_SIZE, next.capacity());
assertEquals("empty, ready for use", 0, next.length());
}
}