// 
// Decompiled by Procyon v0.6.0
// 

package com.github.luben.zstd;

import java.nio.ByteBuffer;
import java.lang.ref.SoftReference;
import java.util.concurrent.ConcurrentLinkedQueue;

public class RecyclingBufferPool implements BufferPool
{
    public static final BufferPool INSTANCE;
    private static final int buffSize;
    private final ConcurrentLinkedQueue<SoftReference<ByteBuffer>> pool;
    
    private RecyclingBufferPool() {
        this.pool = new ConcurrentLinkedQueue<SoftReference<ByteBuffer>>();
    }
    
    @Override
    public ByteBuffer get(final int i) {
        if (i > RecyclingBufferPool.buffSize) {
            throw new RuntimeException("Unsupported buffer size: " + i + ". Supported buffer sizes: " + RecyclingBufferPool.buffSize + " or smaller.");
        }
        while (true) {
            final SoftReference softReference = this.pool.poll();
            if (softReference == null) {
                return ByteBuffer.allocate(RecyclingBufferPool.buffSize);
            }
            final ByteBuffer byteBuffer = (ByteBuffer)softReference.get();
            if (byteBuffer != null) {
                return byteBuffer;
            }
        }
    }
    
    @Override
    public void release(final ByteBuffer referent) {
        if (referent.capacity() >= RecyclingBufferPool.buffSize) {
            referent.clear();
            this.pool.add(new SoftReference<ByteBuffer>(referent));
        }
    }
    
    static {
        INSTANCE = new RecyclingBufferPool();
        buffSize = Math.max(Math.max((int)ZstdOutputStreamNoFinalizer.recommendedCOutSize(), (int)ZstdInputStreamNoFinalizer.recommendedDInSize()), (int)ZstdInputStreamNoFinalizer.recommendedDOutSize());
    }
}
