Package com.atlassian.confluence.util
Class LengthLimitedStringBuilder
- java.lang.Object
-
- com.atlassian.confluence.util.LengthLimitedStringBuilder
-
@NotThreadSafe public class LengthLimitedStringBuilder extends Object
A length limited string builder which dynamically or statically limits the capacity of the underlyingStringBuilder
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.- Since:
- 7.20
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
LengthLimitedStringBuilder.LengthLimitProvider
static class
LengthLimitedStringBuilder.LIMIT_BEHAVIOUR
Behaviour of the string builder when the limit is reached.
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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()
-
-
-
Constructor Detail
-
LengthLimitedStringBuilder
public LengthLimitedStringBuilder(LengthLimitedStringBuilder.LengthLimitProvider lengthLimitProvider)
Creates a length limited string builder, which will silently stop appending if the limit is reached.- Parameters:
lengthLimitProvider
- the provider of the length limit
-
LengthLimitedStringBuilder
public LengthLimitedStringBuilder(LengthLimitedStringBuilder.LengthLimitProvider lengthLimitProvider, LengthLimitedStringBuilder.LIMIT_BEHAVIOUR limitBehaviour)
Creates a length limited string builder, which has configurable limiting behaviour.- Parameters:
lengthLimitProvider
- the provider of the length limitlimitBehaviour
- the behaviour to undertake when the limit is reached
-
-
Method Detail
-
setLength
public void setLength(int l)
-
length
public int length()
-
append
public LengthLimitedStringBuilder append(char[] str, int offset, int len)
-
append
public LengthLimitedStringBuilder append(char c)
-
append
public LengthLimitedStringBuilder append(String s)
-
limitReached
protected boolean limitReached(int length)
Checks if appending length characters to the string builder will reach the limit.- Parameters:
length
- of the characters to be appended- Returns:
- true if the limit is reached, false otherwise
-
capacity
public int capacity()
-
isLimitReached
public boolean isLimitReached()
-
-