All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.java.org.codehaus.jackson.util.BufferRecycler Maven / Gradle / Ivy

Go to download

Jackson is a high-performance JSON processor (parser, generator)

The newest version!
package org.codehaus.jackson.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 final class BufferRecycler
{
    public enum ByteBufferType {
        READ_IO_BUFFER(4000)
            ,WRITE_IO_BUFFER(4000);
            
        private 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
            ;
        
        private final int size;

        CharBufferType(int size) { this.size = size; }
    }

    final protected byte[][] mByteBuffers = new byte[ByteBufferType.values().length][];
    final protected char[][] mCharBuffers = new char[CharBufferType.values().length][];

    public BufferRecycler() { }

    public byte[] allocByteBuffer(ByteBufferType type)
    {
        int ix = type.ordinal();
        byte[] buffer = mByteBuffers[ix];
        if (buffer == null) {
            buffer = balloc(type.size);
        } else {
            mByteBuffers[ix] = null;
        }
        return buffer;
    }

    public void releaseByteBuffer(ByteBufferType type, byte[] buffer)
    {
        mByteBuffers[type.ordinal()] = buffer;
    }

    public char[] allocCharBuffer(CharBufferType type)
    {
        return allocCharBuffer(type, 0);
    }

    public char[] allocCharBuffer(CharBufferType type, int minSize)
    {
        if (type.size > minSize) {
            minSize = type.size;
        }
        int ix = type.ordinal();
        char[] buffer = mCharBuffers[ix];
        if (buffer == null || buffer.length < minSize) {
            buffer = calloc(minSize);
        } else {
            mCharBuffers[ix] = null;
        }
        return buffer;
    }

    public void releaseCharBuffer(CharBufferType type, char[] buffer)
    {
        mCharBuffers[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];
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy