Package org.apache.commons.io
Class IOUtils.ScratchChars
java.lang.Object
org.apache.commons.io.IOUtils.ScratchChars
- All Implemented Interfaces:
AutoCloseable
- Enclosing class:
- IOUtils
Holder for per-thread internal scratch buffer.
Buffers are created lazily and reused within the same thread to reduce allocation overhead. In the rare case of reentrant access, a temporary buffer is allocated to avoid data corruption.
Typical usage:
try (ScratchChars scratch = ScratchChars.get()) {
// use the buffer
char[] bytes = scratch.array();
// ...
}
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final char[]The buffer, or null if using the thread-local buffer.private static final IOUtils.ScratchCharsprivate static final ThreadLocal<Object[]>Wraps an internal char array. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) char[]array()voidclose()If the buffer is the internal array, clear and release it for reuse.(package private) static IOUtils.ScratchCharsget()Gets the internal char array buffer.
-
Field Details
-
LOCAL
Wraps an internal char array. [0] boolean in use. [1] char[] buffer. -
INSTANCE
-
buffer
private final char[] bufferThe buffer, or null if using the thread-local buffer.
-
-
Constructor Details
-
ScratchChars
private ScratchChars(char[] buffer)
-
-
Method Details
-
get
Gets the internal char array buffer.- Returns:
- the internal char array buffer.
-
array
char[] array() -
close
public void close()If the buffer is the internal array, clear and release it for reuse.- Specified by:
closein interfaceAutoCloseable
-