Package org.apache.commons.io
Class IOUtils.ScratchBytes
java.lang.Object
org.apache.commons.io.IOUtils.ScratchBytes
- 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 (ScratchBytes scratch = ScratchBytes.get()) {
// use the buffer
byte[] bytes = scratch.array();
// ...
}
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final byte[]The buffer, or null if using the thread-local buffer.private static final IOUtils.ScratchBytesprivate static final ThreadLocal<Object[]>Wraps an internal byte array. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) byte[]array()voidclose()If the buffer is the internal array, clear and release it for reuse.(package private) static IOUtils.ScratchBytesget()Gets the internal byte array buffer.
-
Field Details
-
LOCAL
Wraps an internal byte array. [0] boolean in use. [1] byte[] buffer. -
INSTANCE
-
buffer
private final byte[] bufferThe buffer, or null if using the thread-local buffer.
-
-
Constructor Details
-
ScratchBytes
private ScratchBytes(byte[] buffer)
-
-
Method Details
-
get
Gets the internal byte array buffer.- Returns:
- the internal byte array buffer.
-
array
byte[] array() -
close
public void close()If the buffer is the internal array, clear and release it for reuse.- Specified by:
closein interfaceAutoCloseable
-