Make the code more compact. See #480

This commit is contained in:
norman 2012-07-30 14:28:28 +02:00
parent f129ee1e28
commit 947a12555a

View File

@ -38,26 +38,30 @@ final class SocketReceiveBufferAllocator {
ByteBuffer get(int size) { ByteBuffer get(int size) {
if (buf == null) { if (buf == null) {
buf = ByteBuffer.allocateDirect(normalizeCapacity(size)); buf = newBuffer(size);
} else if (buf.capacity() < size) { } else if (buf.capacity() < size) {
exceedCount = 0; buf = newBuffer(size);
ByteBufferUtil.destroy(buf); } else if (((buf.capacity() / 100) * percentual) > size) {
buf = ByteBuffer.allocateDirect(normalizeCapacity(size));
} else if (buf.capacity() > size) {
if (((buf.capacity() / 100) * percentual) > size) {
if (++exceedCount == maxExceedCount) { if (++exceedCount == maxExceedCount) {
exceedCount = 0; buf = newBuffer(size);
ByteBufferUtil.destroy(buf); } else {
buf = ByteBuffer.allocateDirect(normalizeCapacity(size)); buf.clear();
} }
} else { } else {
exceedCount = 0; exceedCount = 0;
}
}
buf.clear(); buf.clear();
}
return buf; return buf;
} }
private ByteBuffer newBuffer(int size) {
if (buf != null) {
exceedCount = 0;
ByteBufferUtil.destroy(buf);
}
return ByteBuffer.allocateDirect(normalizeCapacity(size));
}
private static int normalizeCapacity(int capacity) { private static int normalizeCapacity(int capacity) {
// Normalize to multiple of 1024 // Normalize to multiple of 1024
int q = capacity >>> 10; int q = capacity >>> 10;