Class IOUtils.ScratchBytes

  • All Implemented Interfaces:
    java.lang.AutoCloseable
    Enclosing class:
    IOUtils

    static final class IOUtils.ScratchBytes
    extends java.lang.Object
    implements java.lang.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 Summary

      Fields 
      Modifier and Type Field Description
      private byte[] buffer
      The buffer, or null if using the thread-local buffer.
      private static IOUtils.ScratchBytes INSTANCE  
      private static java.lang.ThreadLocal<java.lang.Object[]> LOCAL
      Wraps an internal byte array.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ScratchBytes​(byte[] buffer)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) byte[] array()  
      void close()
      If the buffer is the internal array, clear and release it for reuse.
      (package private) static IOUtils.ScratchBytes get()
      Gets the internal byte array buffer.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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.
      • buffer

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

      • ScratchBytes

        private ScratchBytes​(byte[] 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:
        close in interface java.lang.AutoCloseable