package org.kne.cloud.network.klalb;

import java.util.concurrent.ConcurrentLinkedQueue;

public class ByteArrayRecycle {
	private  ConcurrentLinkedQueue<byte[]>rec=new ConcurrentLinkedQueue<>();
	private int capacity;
	private int length;
	public ByteArrayRecycle(int capacity, int length) {
		super();
		this.capacity = capacity;
		this.length = length;
	}
	public void recycle(byte[]b) {
		if(b.length!=length)
			throw new IllegalArgumentException("wrong length");
		if(rec.size()<capacity) {
			rec.add(b);
		}
	}
	public byte[] create() {
		byte[]b=rec.poll();
		if(b==null) {
			b=new byte[length];
		}
		return b;
	}
}
