java.lang.Object
tools.jackson.core.util.BufferRecycler
- All Implemented Interfaces:
RecyclerPool.WithPool<BufferRecycler>
This is a small utility class, whose main functionality is to allow
simple reuse of raw byte/char buffers. It is usually allocated through
RecyclerPool (starting with 2.16): multiple pool
implementations exists.
The default pool implementation uses
ThreadLocal combined with 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.
Rewritten in 2.16 to work with RecyclerPool abstraction.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceTag-on interface to allow various other types to exposeBufferRecyclerthey are constructed with. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final AtomicReferenceArray<byte[]>protected final AtomicReferenceArray<char[]>static final intBuffer used for concatenating binary data that is either being encoded as base64 output, or decoded from base64 input.static final intBuffer used for reading byte-based input.static final intBuffer used for temporarily concatenating output; used for example when requesting output as byte array.static final intBuffer used for temporarily storing encoded content; used for example by UTF-8 encoding writerstatic final intBuffer used by generators; for byte-backed generators for buffering ofStringvalues to output (before encoding into UTF-8), and for char-backed generators as actual concatenation buffer.static final intFor parsers, temporary buffer into whichchar[]for names is copied when requested as such; forWriterBasedGeneratorused for buffering duringwriteString(Reader)operation (not commonly used).static final intUsed throughTextBuffer: directly by parsers (to concatenate String values) and indirectly viaSegmentedStringWriterwhen serializing (databind levelObjectMapperandObjectWriter).static final intBuffer used as input buffer for tokenization for character-based parsers. -
Constructor Summary
ConstructorsModifierConstructorDescriptionDefault constructor used for creating instances of this default implementation.protectedBufferRecycler(int bbCount, int cbCount) Alternate constructor to be used by sub-classes, to allow customization of number of low-level buffers in use. -
Method Summary
Modifier and TypeMethodDescriptionfinal byte[]allocByteBuffer(int ix) byte[]allocByteBuffer(int ix, int minSize) final char[]allocCharBuffer(int ix) char[]allocCharBuffer(int ix, int minSize) protected byte[]balloc(int size) protected intbyteBufferLength(int ix) protected char[]calloc(int size) protected intcharBufferLength(int ix) booleanvoidreleaseByteBuffer(int ix, byte[] buffer) voidreleaseCharBuffer(int ix, char[] buffer) voidMethod called when owner of this recycler no longer wishes use it; this should return it to pool passed viawithPool()(if any).withPool(RecyclerPool<BufferRecycler> pool) Method called by owner of this recycler instance, to provide reference toRecyclerPoolinto which instance is to be released (if any)
-
Field Details
-
BYTE_READ_IO_BUFFER
public static final int BYTE_READ_IO_BUFFERBuffer used for reading byte-based input.- See Also:
-
BYTE_WRITE_ENCODING_BUFFER
public static final int BYTE_WRITE_ENCODING_BUFFERBuffer used for temporarily storing encoded content; used for example by UTF-8 encoding writer- See Also:
-
BYTE_WRITE_CONCAT_BUFFER
public static final int BYTE_WRITE_CONCAT_BUFFERBuffer used for temporarily concatenating output; used for example when requesting output as byte array.- See Also:
-
BYTE_BASE64_CODEC_BUFFER
public static final int BYTE_BASE64_CODEC_BUFFERBuffer used for concatenating binary data that is either being encoded as base64 output, or decoded from base64 input.- See Also:
-
CHAR_TOKEN_BUFFER
public static final int CHAR_TOKEN_BUFFERBuffer used as input buffer for tokenization for character-based parsers.- See Also:
-
CHAR_CONCAT_BUFFER
public static final int CHAR_CONCAT_BUFFERBuffer used by generators; for byte-backed generators for buffering ofStringvalues to output (before encoding into UTF-8), and for char-backed generators as actual concatenation buffer.- See Also:
-
CHAR_TEXT_BUFFER
public static final int CHAR_TEXT_BUFFERUsed throughTextBuffer: directly by parsers (to concatenate String values) and indirectly viaSegmentedStringWriterwhen serializing (databind levelObjectMapperandObjectWriter). In both cases used as segments (and not for whole value), but may result in retention of larger chunks for big content (long text values during parsing; bigger output documents for generation).- See Also:
-
CHAR_NAME_COPY_BUFFER
public static final int CHAR_NAME_COPY_BUFFERFor parsers, temporary buffer into whichchar[]for names is copied when requested as such; forWriterBasedGeneratorused for buffering duringwriteString(Reader)operation (not commonly used).- See Also:
-
_byteBuffers
-
_charBuffers
-
-
Constructor Details
-
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 ofbyte[]buffers to allocatecbCount- Number ofchar[]buffers to allocate
-
-
Method Details
-
isLinkedWithPool
public boolean isLinkedWithPool()- Returns:
- True if this recycler is linked to pool and may be released
with
releaseToPool(); false if no linkage exists. - Since:
- 2.17
-
allocByteBuffer
public final byte[] allocByteBuffer(int ix) - Parameters:
ix- One ofREAD_IO_BUFFERconstants.- Returns:
- Buffer allocated (possibly recycled)
-
allocByteBuffer
public byte[] allocByteBuffer(int ix, int minSize) -
releaseByteBuffer
public void releaseByteBuffer(int ix, byte[] buffer) -
allocCharBuffer
public final char[] allocCharBuffer(int ix) -
allocCharBuffer
public char[] allocCharBuffer(int ix, int minSize) -
releaseCharBuffer
public void releaseCharBuffer(int ix, char[] buffer) -
byteBufferLength
protected int byteBufferLength(int ix) -
charBufferLength
protected int charBufferLength(int ix) -
balloc
protected byte[] balloc(int size) -
calloc
protected char[] calloc(int size) -
withPool
Method called by owner of this recycler instance, to provide reference toRecyclerPoolinto which instance is to be released (if any)- Specified by:
withPoolin interfaceRecyclerPool.WithPool<BufferRecycler>- Parameters:
pool- Pool that "owns" pooled item- Returns:
- This item (for call chaining)
- Since:
- 2.16
-
releaseToPool
public void releaseToPool()Method called when owner of this recycler no longer wishes use it; this should return it to pool passed viawithPool()(if any).- Specified by:
releaseToPoolin interfaceRecyclerPool.WithPool<BufferRecycler>- Since:
- 2.16
-