com.fasterxml.jackson.core.util.BufferRecycler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ehcache Show documentation
Show all versions of ehcache Show documentation
Ehcache is an open source, standards-based cache used to boost performance,
offload the database and simplify scalability. Ehcache is robust, proven and full-featured and
this has made it the most widely-used Java-based cache.
package com.fasterxml.jackson.core.util;
/**
* 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.
*/
public class BufferRecycler
{
public final static int DEFAULT_WRITE_CONCAT_BUFFER_LEN = 2000;
public enum ByteBufferType {
READ_IO_BUFFER(4000)
/**
* Buffer used for temporarily storing encoded content; used
* for example by UTF-8 encoding writer
*/
,WRITE_ENCODING_BUFFER(4000)
/**
* Buffer used for temporarily concatenating output; used for
* example when requesting output as byte array.
*/
,WRITE_CONCAT_BUFFER(2000)
/**
* Buffer used for concatenating binary data that is either being
* encoded as base64 output, or decoded from base64 input.
*
* @since 2.1
*/
,BASE64_CODEC_BUFFER(2000)
;
protected final int size;
ByteBufferType(int size) { this.size = size; }
}
public enum CharBufferType {
TOKEN_BUFFER(2000) // Tokenizable input
,CONCAT_BUFFER(2000) // concatenated output
,TEXT_BUFFER(200) // Text content from input
,NAME_COPY_BUFFER(200) // Temporary buffer for getting name characters
;
protected final int size;
CharBufferType(int size) { this.size = size; }
}
final protected byte[][] _byteBuffers = new byte[ByteBufferType.values().length][];
final protected char[][] _charBuffers = new char[CharBufferType.values().length][];
public BufferRecycler() { }
public final byte[] allocByteBuffer(ByteBufferType type) {
return allocByteBuffer(type, 0);
}
public final byte[] allocByteBuffer(ByteBufferType type, int minSize)
{
final int ix = type.ordinal();
final int DEF_SIZE = type.size;
if (minSize < DEF_SIZE) {
minSize = DEF_SIZE;
}
byte[] buffer = _byteBuffers[ix];
if (buffer == null || buffer.length < minSize) {
buffer = balloc(minSize);
} else {
_byteBuffers[ix] = null;
}
return buffer;
}
public final void releaseByteBuffer(ByteBufferType type, byte[] buffer)
{
_byteBuffers[type.ordinal()] = buffer;
}
public final char[] allocCharBuffer(CharBufferType type)
{
return allocCharBuffer(type, 0);
}
public final char[] allocCharBuffer(CharBufferType type, int minSize)
{
if (type.size > minSize) {
minSize = type.size;
}
int ix = type.ordinal();
char[] buffer = _charBuffers[ix];
if (buffer == null || buffer.length < minSize) {
buffer = calloc(minSize);
} else {
_charBuffers[ix] = null;
}
return buffer;
}
public final void releaseCharBuffer(CharBufferType type, char[] buffer)
{
_charBuffers[type.ordinal()] = buffer;
}
/*
/**********************************************************
/* Actual allocations separated for easier debugging/profiling
/**********************************************************
*/
private byte[] balloc(int size)
{
return new byte[size];
}
private char[] calloc(int size)
{
return new char[size];
}
}