Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  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; }
     }
 
     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; }
     }
 
     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)
     {
         int ix = type.ordinal();
         byte[] buffer = [ix];
         if (buffer == null) {
             buffer = balloc(type.size);
         } else {
             [ix] = null;
         }
         return buffer;
     }
 
     public final void releaseByteBuffer(ByteBufferType typebyte[] buffer)
     {
         [type.ordinal()] = buffer;
     }
 
     public final char[] allocCharBuffer(CharBufferType type)
     {
         return allocCharBuffer(type, 0);
     }
 
     public final char[] allocCharBuffer(CharBufferType typeint minSize)
     {
         if (type.size > minSize) {
             minSize = type.size;
         }
         int ix = type.ordinal();
         char[] buffer = [ix];
         if (buffer == null || buffer.length < minSize) {
             buffer = calloc(minSize);
         } else {
             [ix] = null;
         }
         return buffer;
     }
 
     public final void releaseCharBuffer(CharBufferType typechar[] buffer)
     {
         [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];
    }
New to GrepCode? Check out our FAQ X