Class SecureStringBuilder
- All Implemented Interfaces:
AutoCloseable
Standard String and StringBuilder classes leave sensitive data (like passwords
or encryption keys) in memory until garbage collection occurs. This class mitigates that risk
by ensuring that internal character arrays are explicitly zeroed out when the buffer is resized,
and when the resource is closed.
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionSecureStringBuilder(int initialCapacity) Constructs a newSecureStringBuilderwith the specified initial capacity. -
Method Summary
Modifier and TypeMethodDescription(package private) voidappendCodePoint(int codePoint) Appends a single Unicode code point to this buffer.voidclose()Securely wipes the internal buffer by overwriting all contents with null characters ('\0') and resets the length to zero.private voidensureCapacity(int minCapacity) Ensures that the internal buffer has enough capacity to hold the specified minimum number of characters.(package private) char[]Extracts a copy of the current buffer sized exactly to the appended content.
-
Field Details
-
buffer
private char[] buffer -
length
private int length
-
-
Constructor Details
-
SecureStringBuilder
SecureStringBuilder(int initialCapacity) Constructs a newSecureStringBuilderwith the specified initial capacity.- Parameters:
initialCapacity- the initial capacity of the secure buffer.- Throws:
IllegalArgumentException- ifinitialCapacityis negative.
-
-
Method Details
-
ensureCapacity
private void ensureCapacity(int minCapacity) Ensures that the internal buffer has enough capacity to hold the specified minimum number of characters. If a resize is required, the old array is securely wiped before being discarded.- Parameters:
minCapacity- the desired minimum capacity.- Throws:
OutOfMemoryError- if the required size exceeds JVM array limits.
-
appendCodePoint
void appendCodePoint(int codePoint) Appends a single Unicode code point to this buffer.This method correctly handles supplementary characters by converting them into their corresponding UTF-16 surrogate pairs if necessary.
- Parameters:
codePoint- the Unicode code point to append.- Throws:
IllegalArgumentException- if the specified code point is not a valid Unicode code point.IllegalStateException- if the builder has been closed.
-
toCharArray
char[] toCharArray()Extracts a copy of the current buffer sized exactly to the appended content.Security Warning: This method allocates a new array containing the sensitive data. The internal buffer remains intact until
close()is called. The caller assumes full responsibility for securely wiping the returned array (e.g., usingArrays.fill(char[], char)) as soon as it is no longer needed.- Returns:
- a new, exact-sized character array containing the buffer's contents.
- Throws:
IllegalStateException- if the builder has been closed.
-
close
public void close()Securely wipes the internal buffer by overwriting all contents with null characters ('\0') and resets the length to zero.This method should be called inside a
finallyblock or implicitly via atry-with-resourcesstatement to guarantee cleanup.- Specified by:
closein interfaceAutoCloseable
-