package org.kne.cloud.network;

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;

public class ByteArrayPool {
	private  Queue<byte[]> rec;
	private int maxcount;
	private int length;
	public ByteArrayPool(int maxcount, int length) {
		super();
		this.maxcount = maxcount;
		this.length = length;
		rec=new ConcurrentLinkedQueue<byte[]>();
	}
	public void back(byte[]b) {
		if(b.length!=length)
			throw new IllegalArgumentException("wrong length");
		rec.offer(b);
	}
	public byte[] borrow() {
		byte[]b=rec.poll();
		if(b==null) {
			b=new byte[length];
		}
		return b;
	}
	public int getMaxCount() {
		return maxcount;
	}
	public int getLength() {
		return length;
	}
	
}
