Files
KLALB/src/org/kne/cloud/network/klalb/ByteBufferPool.java
T
2024-08-18 17:47:11 +08:00

46 lines
1.1 KiB
Java

package org.kne.cloud.network.klalb;
import java.nio.ByteBuffer;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
public class ByteBufferPool {
private ArrayBlockingQueue<ByteBuffer>rec;
private int maxcount;
private int length;
private boolean direct;
public ByteBufferPool(int maxcount, int length,boolean direct) {
super();
this.maxcount = maxcount;
this.length = length;
this.direct=direct;
rec=new ArrayBlockingQueue<ByteBuffer>(length);
}
public ByteBufferPool(int maxcount, int length) {
this(maxcount, length, true);
}
public void back(ByteBuffer b) {
if(b.capacity()!=length)
throw new IllegalArgumentException("wrong length");
b.clear();
rec.offer(b);
}
public ByteBuffer borrow() {
ByteBuffer b=rec.poll();
if(b==null) {
if(direct)
b=ByteBuffer.allocateDirect(length);
else
b=ByteBuffer.allocate(length);
}
return b;
}
public int getMaxCount() {
return maxcount;
}
public int getLength() {
return length;
}
}