Package org.apache.commons.io
Class IOUtils.ScratchBytes
- java.lang.Object
-
- org.apache.commons.io.IOUtils.ScratchBytes
-
- All Implemented Interfaces:
java.lang.AutoCloseable
- Enclosing class:
- IOUtils
static final class IOUtils.ScratchBytes extends java.lang.Object implements java.lang.AutoCloseableHolder 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
Fields Modifier and Type Field Description private byte[]bufferThe buffer, or null if using the thread-local buffer.private static IOUtils.ScratchBytesINSTANCEprivate static java.lang.ThreadLocal<java.lang.Object[]>LOCALWraps an internal byte array.
-
Constructor Summary
Constructors Modifier Constructor Description privateScratchBytes(byte[] buffer)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (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 Detail
-
LOCAL
private static final java.lang.ThreadLocal<java.lang.Object[]> LOCAL
Wraps an internal byte array. [0] boolean in use. [1] byte[] buffer.
-
INSTANCE
private static final IOUtils.ScratchBytes INSTANCE
-
buffer
private final byte[] buffer
The buffer, or null if using the thread-local buffer.
-
-
Method Detail
-
get
static IOUtils.ScratchBytes 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 interfacejava.lang.AutoCloseable
-
-