Class BufferRecycler


  • public class BufferRecycler
    extends java.lang.Object
    This is a small utility class, whose main functionality is to allow simple reuse of raw byte/char buffers. It is usually used through ThreadLocal member of the owning class pointing to instance of this class through a SoftReference. The end result is a low-overhead GC-cleanable recycling: hopefully ideal for use by stream readers.

    Rewritten in 2.10 to be thread-safe (see [jackson-core#479] for details), to not rely on ThreadLocal access.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.ArrayList<byte[]>[] _byteBuffers  
      protected java.util.ArrayList<char[]>[] _charBuffers  
      protected java.util.ArrayList<int[]> _intBuffers  
      (package private) static boolean ENABLED  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        BufferRecycler()
      Default constructor used for creating instances of this default implementation.
      protected BufferRecycler​(int bbCount, int cbCount)
      Alternate constructor to be used by sub-classes, to allow customization of number of low-level buffers in use.
    • Field Detail

      • ENABLED

        static final boolean ENABLED
      • _byteBuffers

        protected final java.util.ArrayList<byte[]>[] _byteBuffers
      • _charBuffers

        protected final java.util.ArrayList<char[]>[] _charBuffers
      • _intBuffers

        protected final java.util.ArrayList<int[]> _intBuffers
    • Constructor Detail

      • BufferRecycler

        public BufferRecycler()
        Default constructor used for creating instances of this default implementation.
      • BufferRecycler

        protected BufferRecycler​(int bbCount,
                                 int cbCount)
        Alternate constructor to be used by sub-classes, to allow customization of number of low-level buffers in use.
        Parameters:
        bbCount - Number of byte[] buffers to allocate
        cbCount - Number of char[] buffers to allocate
        Since:
        2.4
    • Method Detail

      • allocByteBuffer

        public final byte[] allocByteBuffer​(int ix)
        Parameters:
        ix - One of READ_IO_BUFFER constants.
        Returns:
        Buffer allocated (possibly recycled)
      • allocintBuffer

        public final int[] allocintBuffer​(int minSize)
      • allocByteBuffer

        public byte[] allocByteBuffer​(int ix,
                                      int minSize)
      • releaseByteBuffer

        public void releaseByteBuffer​(int ix,
                                      byte[] buffer)
      • releaseIntBuffer

        public void releaseIntBuffer​(int[] buffer)
      • allocCharBuffer

        public final char[] allocCharBuffer​(int ix)
      • allocCharBuffer

        public char[] allocCharBuffer​(int ix,
                                      int minSize)
      • releaseCharBuffer

        public void releaseCharBuffer​(int ix,
                                      char[] buffer)
      • balloc

        protected byte[] balloc​(int size)
      • calloc

        protected char[] calloc​(int size)