@NotThreadSafe public class LengthLimitedStringBuilder extends Object
StringBuilder implementation. Useful for preventing OOMs.
Easy misconception is that it will always limit the number of chars to the length provided, which is not always true if increasing the capacity of the StringBuilder will cause it to become greater than the provided limit.
e.g Limit is 20, current StringBuilder capacity is 16, current number of chars is 16. Appending 1 more char would not go over the limit. However, depending on StringBuilder
implementation, it's capacity could be increased past the limit. Appending would stop here.
| Modifier and Type | Class and Description |
|---|---|
static interface |
LengthLimitedStringBuilder.LengthLimitProvider |
static class |
LengthLimitedStringBuilder.LIMIT_BEHAVIOUR
Behaviour of the string builder when the limit is reached.
|
| Constructor and Description |
|---|
LengthLimitedStringBuilder(LengthLimitedStringBuilder.LengthLimitProvider lengthLimitProvider)
Creates a length limited string builder, which will silently stop appending if the limit is reached.
|
LengthLimitedStringBuilder(LengthLimitedStringBuilder.LengthLimitProvider lengthLimitProvider,
LengthLimitedStringBuilder.LIMIT_BEHAVIOUR limitBehaviour)
Creates a length limited string builder, which has configurable limiting behaviour.
|
| Modifier and Type | Method and Description |
|---|---|
LengthLimitedStringBuilder |
append(char c) |
LengthLimitedStringBuilder |
append(char[] str,
int offset,
int len) |
LengthLimitedStringBuilder |
append(String s) |
int |
capacity() |
boolean |
isLimitReached() |
int |
length() |
protected boolean |
limitReached(int length)
Checks if appending length characters to the string builder will reach the limit.
|
void |
setLength(int l) |
String |
toString() |
public LengthLimitedStringBuilder(LengthLimitedStringBuilder.LengthLimitProvider lengthLimitProvider)
lengthLimitProvider - the provider of the length limitpublic LengthLimitedStringBuilder(LengthLimitedStringBuilder.LengthLimitProvider lengthLimitProvider, LengthLimitedStringBuilder.LIMIT_BEHAVIOUR limitBehaviour)
lengthLimitProvider - the provider of the length limitlimitBehaviour - the behaviour to undertake when the limit is reachedpublic void setLength(int l)
public int length()
public LengthLimitedStringBuilder append(char[] str, int offset, int len)
public LengthLimitedStringBuilder append(char c)
public LengthLimitedStringBuilder append(String s)
protected boolean limitReached(int length)
length - of the characters to be appendedpublic int capacity()
public boolean isLimitReached()
Copyright © 2003–2022 Atlassian. All rights reserved.