Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.ning.compress;
  
Simple helper class to encapsulate details of basic buffer recycling scheme, which helps a lot (as per profiling) for smaller encoding cases.

Author(s):
Tatu Saloranta (tatu.saloranta@iki.fi)
 
 public final class BufferRecycler
 {
     private final static int MIN_ENCODING_BUFFER = 4000;
 
     private final static int MIN_OUTPUT_BUFFER = 8000;
    
    
This ThreadLocal contains a java.lang.ref.SoftReference to a BufferRecycler used to provide a low-cost buffer recycling for buffers we need for encoding, decoding.
 
      final protected static ThreadLocal<SoftReference<BufferRecycler>> _recyclerRef
          = new ThreadLocal<SoftReference<BufferRecycler>>();
    
 
     private byte[] _inputBuffer;
     private byte[] _outputBuffer;
 
     private byte[] _decodingBuffer;
     private byte[] _encodingBuffer;
 
     private int[] _encodingHash;
    
    
Accessor to get thread-local recycler instance
 
     public static BufferRecycler instance()
     {
         SoftReference<BufferRecyclerref = .get();
         BufferRecycler br = (ref == null) ? null : ref.get();
         if (br == null) {
             br = new BufferRecycler();
             .set(new SoftReference<BufferRecycler>(br));
         }
         return br;
     }
 
     /*
     ///////////////////////////////////////////////////////////////////////
     // Buffers for encoding (output)
     ///////////////////////////////////////////////////////////////////////
      */
     
     public byte[] allocEncodingBuffer(int minSize)
     {
         byte[] buf = ;
         if (buf == null || buf.length < minSize) {
             buf = new byte[Math.max(minSize)];
         } else {
              = null;
         }
         return buf;
     }
 
     public void releaseEncodeBuffer(byte[] buffer)
     {
         if ( == null || (buffer != null && buffer.length > .)) {
              = buffer;
         }
     }
     
     public byte[] allocOutputBuffer(int minSize)
     {
         byte[] buf = ;
         if (buf == null || buf.length < minSize) {
             buf = new byte[Math.max(minSize)];
         } else {
              = null;
         }
         return buf;
     }
 
     public void releaseOutputBuffer(byte[] buffer)
     {
         if ( == null || (buffer != null && buffer.length > .)) {
              = buffer;
         }
     }
 
     public int[] allocEncodingHash(int suggestedSize)
     {
         int[] buf = ;
         if (buf == null || buf.length < suggestedSize) {
             buf = new int[suggestedSize];
         } else {
              = null;
         }
         return buf;
    }
    public void releaseEncodingHash(int[] buffer)
    {
        if ( == null || (buffer != null && buffer.length > .)) {
             = buffer;
        }
    }
    /*
    ///////////////////////////////////////////////////////////////////////
    // Buffers for decoding (input)
    ///////////////////////////////////////////////////////////////////////
     */
    public byte[] allocInputBuffer(int minSize)
    {
        byte[] buf = ;
        if (buf == null || buf.length < minSize) {
            buf = new byte[Math.max(minSize)];
        } else {
             = null;
        }
        return buf;
    }
    public void releaseInputBuffer(byte[] buffer)
    {
        if ( == null || (buffer != null && buffer.length > .)) {
             = buffer;
        }
    }
    
    public byte[] allocDecodeBuffer(int size)
    {
        byte[] buf = ;
        if (buf == null || buf.length < size) {
            buf = new byte[size];
        } else {
             = null;
        }
        return buf;
    }
    public void releaseDecodeBuffer(byte[] buffer)
    {
        if ( == null || (buffer != null && buffer.length > .)) {
             = buffer;
        }
    }
    
New to GrepCode? Check out our FAQ X