Author: trustin
Date: Sun Nov 11 06:27:29 2007
New Revision: 593886
URL: http://svn.apache.org/viewvc?rev=593886&view=rev
Log:
More optimization for AbstractIoBuffer.compact()
Modified:
mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoBuffer.java
Modified:
mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoBuffer.java
URL:
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoBuffer.java?rev=593886&r1=593885&r2=593886&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoBuffer.java
(original)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoBuffer.java
Sun Nov 11 06:27:29 2007
@@ -303,11 +303,12 @@
public IoBuffer compact() {
int remaining = remaining();
int capacity = capacity();
+
if (capacity == 0) {
return this;
}
- if (isAutoShrink() && remaining <= capacity >>> 2) {
+ if (isAutoShrink() && remaining <= capacity >>> 2 && capacity >
initialCapacity) {
int newCapacity = capacity;
int minCapacity = Math.max(initialCapacity, remaining << 1);
for (;;) {
@@ -317,32 +318,28 @@
newCapacity >>>= 1;
}
- if (newCapacity < initialCapacity && initialCapacity ==
capacity()) {
- buf().compact();
- } else {
- newCapacity = Math.max(initialCapacity, newCapacity);
+ newCapacity = Math.max(initialCapacity, newCapacity);
- // Shrink and compact:
- //// Save the state.
- ByteOrder bo = order();
-
- //// Sanity check.
- if (remaining > newCapacity) {
- throw new IllegalStateException(
- "The amount of the remaining bytes is greater than
" +
- "the new capacity.");
- }
-
- //// Reallocate.
- ByteBuffer oldBuf = buf();
- ByteBuffer newBuf =
- allocator.allocateNioBuffer(newCapacity, isDirect());
- newBuf.put(oldBuf);
- buf(newBuf);
-
- //// Restore the state.
- buf().order(bo);
+ // Shrink and compact:
+ //// Save the state.
+ ByteOrder bo = order();
+
+ //// Sanity check.
+ if (remaining > newCapacity) {
+ throw new IllegalStateException(
+ "The amount of the remaining bytes is greater than " +
+ "the new capacity.");
}
+
+ //// Reallocate.
+ ByteBuffer oldBuf = buf();
+ ByteBuffer newBuf =
+ allocator.allocateNioBuffer(newCapacity, isDirect());
+ newBuf.put(oldBuf);
+ buf(newBuf);
+
+ //// Restore the state.
+ buf().order(bo);
} else {
buf().compact();
}