Class IOUtils.ScratchBytes

java.lang.Object
org.apache.commons.io.IOUtils.ScratchBytes
All Implemented Interfaces:
AutoCloseable
Enclosing class:
IOUtils

static final class IOUtils.ScratchBytes extends Object implements AutoCloseable
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 Details

    • LOCAL

      private static final ThreadLocal<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.
  • Constructor Details

    • ScratchBytes

      private ScratchBytes(byte[] buffer)
  • Method Details

    • 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:
      close in interface AutoCloseable