You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by tr...@apache.org on 2008/03/17 04:18:44 UTC

svn commit: r637706 - in /mina: branches/1.0/core/src/main/java/org/apache/mina/common/ branches/1.1/core/src/main/java/org/apache/mina/common/ trunk/core/src/main/java/org/apache/mina/common/ trunk/core/src/test/java/org/apache/mina/common/

Author: trustin
Date: Sun Mar 16 20:18:43 2008
New Revision: 637706

URL: http://svn.apache.org/viewvc?rev=637706&view=rev
Log:
Resolved issue: DIRMiNA-551 - IoBuffer doesn't double up on auto-expansion.
* Fixed AbstractIoBuffer to double up on auto-expansion
* Fixing 1.x costs more than 2.x, so I ended up fixing documentation.


Modified:
    mina/branches/1.0/core/src/main/java/org/apache/mina/common/ByteBuffer.java
    mina/branches/1.1/core/src/main/java/org/apache/mina/common/ByteBuffer.java
    mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoBuffer.java
    mina/trunk/core/src/main/java/org/apache/mina/common/IoBuffer.java
    mina/trunk/core/src/test/java/org/apache/mina/common/IoBufferTest.java

Modified: mina/branches/1.0/core/src/main/java/org/apache/mina/common/ByteBuffer.java
URL: http://svn.apache.org/viewvc/mina/branches/1.0/core/src/main/java/org/apache/mina/common/ByteBuffer.java?rev=637706&r1=637705&r2=637706&view=diff
==============================================================================
--- mina/branches/1.0/core/src/main/java/org/apache/mina/common/ByteBuffer.java (original)
+++ mina/branches/1.0/core/src/main/java/org/apache/mina/common/ByteBuffer.java Sun Mar 16 20:18:43 2008
@@ -124,9 +124,8 @@
  * buf.putString( greeting, utf8encoder );
  * </pre>
  * NIO <tt>ByteBuffer</tt> is reallocated by MINA <tt>ByteBuffer</tt> behind
- * the scene if the encoded data is larger than 16 bytes.  Its capacity will
- * increase by two times, and its limit will increase to the last position
- * the string is written.
+ * the scene if the encoded data is larger than 16 bytes.  Its capacity and
+ * its limit will increase to the last position the string is written.
  * </p>
  *
  * <h2>Derived Buffers</h2>

Modified: mina/branches/1.1/core/src/main/java/org/apache/mina/common/ByteBuffer.java
URL: http://svn.apache.org/viewvc/mina/branches/1.1/core/src/main/java/org/apache/mina/common/ByteBuffer.java?rev=637706&r1=637705&r2=637706&view=diff
==============================================================================
--- mina/branches/1.1/core/src/main/java/org/apache/mina/common/ByteBuffer.java (original)
+++ mina/branches/1.1/core/src/main/java/org/apache/mina/common/ByteBuffer.java Sun Mar 16 20:18:43 2008
@@ -124,9 +124,8 @@
  * buf.putString( greeting, utf8encoder );
  * </pre>
  * NIO <tt>ByteBuffer</tt> is reallocated by MINA <tt>ByteBuffer</tt> behind
- * the scene if the encoded data is larger than 16 bytes.  Its capacity will
- * increase by two times, and its limit will increase to the last position
- * the string is written.
+ * the scene if the encoded data is larger than 16 bytes.  Its capacity and
+ * its limit will increase to the last position the string is written.
  * </p>
  *
  * <h2>Derived Buffers</h2>

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=637706&r1=637705&r2=637706&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 Mar 16 20:18:43 2008
@@ -199,20 +199,34 @@
 
     @Override
     public final IoBuffer expand(int expectedRemaining) {
-        return expand(position(), expectedRemaining);
+        return expand(position(), expectedRemaining, false);
+    }
+    
+    private IoBuffer expand(int expectedRemaining, boolean autoExpand) {
+        return expand(position(), expectedRemaining, autoExpand);
     }
 
     @Override
     public final IoBuffer expand(int pos, int expectedRemaining) {
+        return expand(pos, expectedRemaining, false);
+    }
+    
+    private IoBuffer expand(int pos, int expectedRemaining, boolean autoExpand) {
         if (!recapacityAllowed) {
             throw new IllegalStateException(
                     "Derived buffers and their parent can't be expanded.");
         }
 
         int end = pos + expectedRemaining;
-        if (end > capacity()) {
+        int newCapacity;
+        if (autoExpand) {
+            newCapacity = IoBuffer.normalizeCapacity(end);
+        } else {
+            newCapacity = end;
+        }
+        if (newCapacity > capacity()) {
             // The buffer needs expansion.
-            capacity(end);
+            capacity(newCapacity);
         }
 
         if (end > limit()) {
@@ -2028,7 +2042,7 @@
      */
     private IoBuffer autoExpand(int expectedRemaining) {
         if (isAutoExpand()) {
-            expand(expectedRemaining);
+            expand(expectedRemaining, true);
         }
         return this;
     }
@@ -2039,7 +2053,7 @@
      */
     private IoBuffer autoExpand(int pos, int expectedRemaining) {
         if (isAutoExpand()) {
-            expand(pos, expectedRemaining);
+            expand(pos, expectedRemaining, true);
         }
         return this;
     }

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=637706&r1=637705&r2=637706&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 Sun Mar 16 20:18:43 2008
@@ -464,7 +464,7 @@
      */
     public abstract IoBuffer sweep();
 
-    /**
+    /**double
      * Clears this buffer and fills its content with <tt>value</tt>.
      * The position is set to zero, the limit is set to the capacity,
      * and the mark is discarded.

Modified: mina/trunk/core/src/test/java/org/apache/mina/common/IoBufferTest.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/test/java/org/apache/mina/common/IoBufferTest.java?rev=637706&r1=637705&r2=637706&view=diff
==============================================================================
--- mina/trunk/core/src/test/java/org/apache/mina/common/IoBufferTest.java (original)
+++ mina/trunk/core/src/test/java/org/apache/mina/common/IoBufferTest.java Sun Mar 16 20:18:43 2008
@@ -96,6 +96,17 @@
         Assert.assertEquals(2, buf.position());
         Assert.assertEquals(4, buf.limit());
         Assert.assertEquals(4, buf.capacity());
+
+        // Make sure the buffer is doubled up.
+        buf = IoBuffer.allocate(1).setAutoExpand(true);
+        int lastCapacity = buf.capacity();
+        for (int i = 0; i < 1048576; i ++) {
+            buf.put((byte) 0);
+            if (lastCapacity != buf.capacity()) {
+                Assert.assertEquals(lastCapacity * 2, buf.capacity());
+                lastCapacity = buf.capacity();
+            }
+        }
     }
 
     public void testAutoExpandMark() throws Exception {