Class IOUtils.ScratchChars

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

static final class IOUtils.ScratchChars 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 (ScratchChars scratch = ScratchChars.get()) {
     // use the buffer
     char[] bytes = scratch.array();
     // ...
 }
 
  • Field Details

    • LOCAL

      private static final ThreadLocal<Object[]> LOCAL
      Wraps an internal char array. [0] boolean in use. [1] char[] buffer.
    • INSTANCE

      private static final IOUtils.ScratchChars INSTANCE
    • buffer

      private final char[] buffer
      The buffer, or null if using the thread-local buffer.
  • Constructor Details

    • ScratchChars

      private ScratchChars(char[] buffer)
  • Method Details

    • get

      static IOUtils.ScratchChars 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:
      close in interface AutoCloseable