Author: trustin
Date: Mon Nov 12 18:49:43 2007
New Revision: 594400

URL: http://svn.apache.org/viewvc?rev=594400&view=rev
Log:
Moved normalizeCapacity method to IoBuffer

Modified:
    
mina/trunk/core/src/main/java/org/apache/mina/common/CachedBufferAllocator.java
    mina/trunk/core/src/main/java/org/apache/mina/common/IoBuffer.java

Modified: 
mina/trunk/core/src/main/java/org/apache/mina/common/CachedBufferAllocator.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/CachedBufferAllocator.java?rev=594400&r1=594399&r2=594400&view=diff
==============================================================================
--- 
mina/trunk/core/src/main/java/org/apache/mina/common/CachedBufferAllocator.java 
(original)
+++ 
mina/trunk/core/src/main/java/org/apache/mina/common/CachedBufferAllocator.java 
Mon Nov 12 18:49:43 2007
@@ -142,7 +142,7 @@
     }
 
     public IoBuffer allocate(int requestedCapacity, boolean direct) {
-        int actualCapacity = normalizeCapacity(requestedCapacity);
+        int actualCapacity = IoBuffer.normalizeCapacity(requestedCapacity);
         IoBuffer buf ;
         if (maxCachedBufferSize != 0 && actualCapacity > maxCachedBufferSize) {
             if (direct) {
@@ -188,29 +188,6 @@
     public void dispose() {
     }
     
-    private static int normalizeCapacity(int requestedCapacity) {
-        switch (requestedCapacity) {
-        case 0:
-        case 1 <<  0: case 1 <<  1: case 1 <<  2: case 1 <<  3: case 1 <<  4:
-        case 1 <<  5: case 1 <<  6: case 1 <<  7: case 1 <<  8: case 1 <<  9:
-        case 1 << 10: case 1 << 11: case 1 << 12: case 1 << 13: case 1 << 14:
-        case 1 << 15: case 1 << 16: case 1 << 17: case 1 << 18: case 1 << 19:
-        case 1 << 21: case 1 << 22: case 1 << 23: case 1 << 24: case 1 << 25:
-        case 1 << 26: case 1 << 27: case 1 << 28: case 1 << 29: case 1 << 30:
-        case Integer.MAX_VALUE:
-            return requestedCapacity;
-        }
-        
-        int newCapacity = 1;
-        while (newCapacity < requestedCapacity) {
-            newCapacity <<= 1;
-            if (newCapacity < 0) {
-                return Integer.MAX_VALUE;
-            }
-        }
-        return newCapacity;
-    }
-
     private class CachedBuffer extends AbstractIoBuffer {
         private final Thread ownerThread;
         private ByteBuffer buf;

Modified: mina/trunk/core/src/main/java/org/apache/mina/common/IoBuffer.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/IoBuffer.java?rev=594400&r1=594399&r2=594400&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/IoBuffer.java 
(original)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/IoBuffer.java Mon Nov 
12 18:49:43 2007
@@ -226,7 +226,7 @@
         if (capacity < 0) {
             throw new IllegalArgumentException("capacity: " + capacity);
         }
-
+        
         return allocator.allocate(capacity, direct);
     }
 
@@ -250,6 +250,36 @@
     public static IoBuffer wrap(byte[] byteArray, int offset, int length) {
         return wrap(ByteBuffer.wrap(byteArray, offset, length));
     }
+    
+    /**
+     * Normalizes the specified capacity of the buffer to power of 2,
+     * which is often helpful for optimal memory usage and performance. 
+     * If it is greater than or equal to [EMAIL PROTECTED] Integer#MAX_VALUE}, 
it
+     * returns [EMAIL PROTECTED] Integer#MAX_VALUE}.  If it is zero, it 
returns zero.
+     */
+    protected static int normalizeCapacity(int requestedCapacity) {
+        switch (requestedCapacity) {
+        case 0:
+        case 1 <<  0: case 1 <<  1: case 1 <<  2: case 1 <<  3: case 1 <<  4:
+        case 1 <<  5: case 1 <<  6: case 1 <<  7: case 1 <<  8: case 1 <<  9:
+        case 1 << 10: case 1 << 11: case 1 << 12: case 1 << 13: case 1 << 14:
+        case 1 << 15: case 1 << 16: case 1 << 17: case 1 << 18: case 1 << 19:
+        case 1 << 21: case 1 << 22: case 1 << 23: case 1 << 24: case 1 << 25:
+        case 1 << 26: case 1 << 27: case 1 << 28: case 1 << 29: case 1 << 30:
+        case Integer.MAX_VALUE:
+            return requestedCapacity;
+        }
+        
+        int newCapacity = 1;
+        while (newCapacity < requestedCapacity) {
+            newCapacity <<= 1;
+            if (newCapacity < 0) {
+                return Integer.MAX_VALUE;
+            }
+        }
+        return newCapacity;
+    }
+
 
     /**
      * Creates a new instance.  This is an empty constructor.


Reply via email to