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,24 +38,28 @@ final class SocketReceiveBufferAllocator {
ByteBuffer get(int size) {
if (buf == null) {
buf = ByteBuffer.allocateDirect(normalizeCapacity(size));
buf = newBuffer(size);
} else if (buf.capacity() < size) {
buf = newBuffer(size);
} else if (((buf.capacity() / 100) * percentual) > size) {
if (++exceedCount == maxExceedCount) {
buf = newBuffer(size);
} else {
buf.clear();
}
} else {
exceedCount = 0;
buf.clear();
}
return buf;
}
private ByteBuffer newBuffer(int size) {
if (buf != null) {
exceedCount = 0;
ByteBufferUtil.destroy(buf);
buf = ByteBuffer.allocateDirect(normalizeCapacity(size));
} else if (buf.capacity() > size) {
if (((buf.capacity() / 100) * percentual) > size) {
if (++exceedCount == maxExceedCount) {
exceedCount = 0;
ByteBufferUtil.destroy(buf);
buf = ByteBuffer.allocateDirect(normalizeCapacity(size));
}
} else {
exceedCount = 0;
}
}
buf.clear();
return buf;
return ByteBuffer.allocateDirect(normalizeCapacity(size));
}
private static int normalizeCapacity(int capacity) {