Author: trustin
Date: Sun Jan 21 22:06:12 2007
New Revision: 498539

URL: http://svn.apache.org/viewvc?view=rev&rev=498539
Log:
Fixed issue: DIRMINA-328 (CumulativeProtocolDecoder uses an inefficient 
accumulation strategy)
* Applied Steven's patch (Needed some adjustment for 1.0.x and 1.1-SNAPSHOT)

and..

* Fixed a potential memory leak reported in ReadThrottleFilterBuilder.java


Modified:
    
mina/branches/1.0/core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
    
mina/branches/1.1/core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
    
mina/trunk/core/src/main/java/org/apache/mina/filter/ReadThrottleFilterBuilder.java
    
mina/trunk/core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java

Modified: 
mina/branches/1.0/core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.0/core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java?view=diff&rev=498539&r1=498538&r2=498539
==============================================================================
--- 
mina/branches/1.0/core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
 (original)
+++ 
mina/branches/1.0/core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
 Sun Jan 21 22:06:12 2007
@@ -116,9 +116,10 @@
     public void decode( IoSession session, ByteBuffer in,
                         ProtocolDecoderOutput out ) throws Exception
     {
+        boolean usingSessionBuffer = true;
         ByteBuffer buf = ( ByteBuffer ) session.getAttribute( BUFFER );
-        // if we have a session buffer, append data to that otherwise
-        // use the buffer read from the network directly
+        // If we have a session buffer, append data to that; otherwise
+        // use the buffer read from the network directly.
         if( buf != null )
         {
             buf.put( in );
@@ -127,6 +128,7 @@
         else
         {
             buf = in;
+            usingSessionBuffer = false;
         }
         
         for( ;; )
@@ -157,12 +159,16 @@
         // invoked the session buffer gets appended to
         if ( buf.hasRemaining() )
         {
-            storeRemainingInSession( buf, session );
+            if ( usingSessionBuffer )
+                buf.compact();
+            else
+                storeRemainingInSession( buf, session );
         }
         else
         {
-            removeSessionBuffer( session );
-        }        
+            if ( usingSessionBuffer )
+                removeSessionBuffer( session );
+        }
     }
     
     /**
@@ -189,19 +195,18 @@
         removeSessionBuffer( session );
     }
     
-    private void removeSessionBuffer(IoSession session)
+    private void removeSessionBuffer( IoSession session )
     {        
-        ByteBuffer buf = ( ByteBuffer ) session.getAttribute( BUFFER );
+        ByteBuffer buf = ( ByteBuffer ) session.removeAttribute( BUFFER );
         if( buf != null )
         {
             buf.release();
-            session.removeAttribute( BUFFER );
         }
     }
     
-    private void storeRemainingInSession(ByteBuffer buf, IoSession session)
+    private void storeRemainingInSession( ByteBuffer buf, IoSession session )
     {
-        ByteBuffer remainingBuf = ByteBuffer.allocate( buf.remaining() );
+        ByteBuffer remainingBuf = ByteBuffer.allocate( buf.capacity() );
         remainingBuf.setAutoExpand( true );
         remainingBuf.put( buf );
         session.setAttribute( BUFFER, remainingBuf );

Modified: 
mina/branches/1.1/core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.1/core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java?view=diff&rev=498539&r1=498538&r2=498539
==============================================================================
--- 
mina/branches/1.1/core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
 (original)
+++ 
mina/branches/1.1/core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
 Sun Jan 21 22:06:12 2007
@@ -116,9 +116,10 @@
     public void decode( IoSession session, ByteBuffer in,
                         ProtocolDecoderOutput out ) throws Exception
     {
+        boolean usingSessionBuffer = true;
         ByteBuffer buf = ( ByteBuffer ) session.getAttribute( BUFFER );
-        // if we have a session buffer, append data to that otherwise
-        // use the buffer read from the network directly
+        // If we have a session buffer, append data to that; otherwise
+        // use the buffer read from the network directly.
         if( buf != null )
         {
             buf.put( in );
@@ -127,6 +128,7 @@
         else
         {
             buf = in;
+            usingSessionBuffer = false;
         }
         
         for( ;; )
@@ -157,12 +159,16 @@
         // invoked the session buffer gets appended to
         if ( buf.hasRemaining() )
         {
-            storeRemainingInSession( buf, session );
+            if ( usingSessionBuffer )
+                buf.compact();
+            else
+                storeRemainingInSession( buf, session );
         }
         else
         {
-            removeSessionBuffer( session );
-        }        
+            if ( usingSessionBuffer )
+                removeSessionBuffer( session );
+        }
     }
     
     /**
@@ -189,19 +195,18 @@
         removeSessionBuffer( session );
     }
     
-    private void removeSessionBuffer(IoSession session)
+    private void removeSessionBuffer( IoSession session )
     {        
-        ByteBuffer buf = ( ByteBuffer ) session.getAttribute( BUFFER );
+        ByteBuffer buf = ( ByteBuffer ) session.removeAttribute( BUFFER );
         if( buf != null )
         {
             buf.release();
-            session.removeAttribute( BUFFER );
         }
     }
     
-    private void storeRemainingInSession(ByteBuffer buf, IoSession session)
+    private void storeRemainingInSession( ByteBuffer buf, IoSession session )
     {
-        ByteBuffer remainingBuf = ByteBuffer.allocate( buf.remaining() );
+        ByteBuffer remainingBuf = ByteBuffer.allocate( buf.capacity() );
         remainingBuf.setAutoExpand( true );
         remainingBuf.put( buf );
         session.setAttribute( BUFFER, remainingBuf );

Modified: 
mina/trunk/core/src/main/java/org/apache/mina/filter/ReadThrottleFilterBuilder.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/filter/ReadThrottleFilterBuilder.java?view=diff&rev=498539&r1=498538&r2=498539
==============================================================================
--- 
mina/trunk/core/src/main/java/org/apache/mina/filter/ReadThrottleFilterBuilder.java
 (original)
+++ 
mina/trunk/core/src/main/java/org/apache/mina/filter/ReadThrottleFilterBuilder.java
 Sun Jan 21 22:06:12 2007
@@ -187,7 +187,7 @@
         {
             if( message instanceof ByteBuffer )
             {
-                release( session, ( (ByteBuffer)message ).capacity() );
+                release( session, ( (ByteBuffer)message ).remaining() );
             }
 
             nextFilter.messageReceived( session, message );

Modified: 
mina/trunk/core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java?view=diff&rev=498539&r1=498538&r2=498539
==============================================================================
--- 
mina/trunk/core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
 (original)
+++ 
mina/trunk/core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
 Sun Jan 21 22:06:12 2007
@@ -116,9 +116,10 @@
     public void decode( IoSession session, ByteBuffer in,
                         ProtocolDecoderOutput out ) throws Exception
     {
+        boolean usingSessionBuffer = true;
         ByteBuffer buf = ( ByteBuffer ) session.getAttribute( BUFFER );
-        // if we have a session buffer, append data to that otherwise
-        // use the buffer read from the network directly
+        // If we have a session buffer, append data to that; otherwise
+        // use the buffer read from the network directly.
         if( buf != null )
         {
             buf.put( in );
@@ -127,6 +128,7 @@
         else
         {
             buf = in;
+            usingSessionBuffer = false;
         }
         
         for( ;; )
@@ -157,12 +159,16 @@
         // invoked the session buffer gets appended to
         if ( buf.hasRemaining() )
         {
-            storeRemainingInSession( buf, session );
+            if ( usingSessionBuffer )
+                buf.compact();
+            else
+                storeRemainingInSession( buf, session );
         }
         else
         {
-            removeSessionBuffer( session );
-        }        
+            if ( usingSessionBuffer )
+                removeSessionBuffer( session );
+        }
     }
     
     /**
@@ -191,18 +197,14 @@
     
     private void removeSessionBuffer(IoSession session)
     {        
-        ByteBuffer buf = ( ByteBuffer ) session.getAttribute( BUFFER );
-        if( buf != null )
-        {
-            session.removeAttribute( BUFFER );
-        }
+       session.removeAttribute( BUFFER );
     }
     
     private void storeRemainingInSession(ByteBuffer buf, IoSession session)
     {
-        ByteBuffer remainingBuf = ByteBuffer.allocate( buf.remaining() );
+        final ByteBuffer remainingBuf = ByteBuffer.allocate( buf.capacity() );
         remainingBuf.setAutoExpand( true );
         remainingBuf.put( buf );
         session.setAttribute( BUFFER, remainingBuf );
-    }    
+    }
 }


Reply via email to