directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nik...@apache.org
Subject svn commit: r393303 - in /directory/trunks/mina/core/src: main/java/org/apache/mina/filter/codec/ main/java/org/apache/mina/filter/codec/demux/ main/java/org/apache/mina/transport/socket/nio/support/ test/java/org/apache/mina/filter/codec/
Date Tue, 11 Apr 2006 20:11:37 GMT
Author: niklas
Date: Tue Apr 11 13:11:35 2006
New Revision: 393303

URL: http://svn.apache.org/viewcvs?rev=393303&view=rev
Log:
Applied patches attached to DIRMINA-201 submitted by Robert Greig. 

Modified:
    directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
    directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/demux/DemuxingProtocolCodecFactory.java
    directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/SocketIoProcessor.java
    directory/trunks/mina/core/src/test/java/org/apache/mina/filter/codec/CumulativeProtocolDecoderTest.java

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java?rev=393303&r1=393302&r2=393303&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
(original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
Tue Apr 11 13:11:35 2006
@@ -59,31 +59,11 @@
     
     private static final String BUFFER = CumulativeProtocolDecoder.class.getName() + ".Buffer";
     
-    private final int initialCapacity;
-    
     /**
-     * Creates a new instance with the 16 bytes initial capacity of
-     * cumulative buffer.  Please note that the capacity increases
-     * automatically.
+     * Creates a new instance.
      */
     protected CumulativeProtocolDecoder()
     {
-        this( 16 );
-    }
-    
-    /**
-     * Creates a new instance with the specified initial capacity of
-     * cumulative buffer.  Please note that the capacity increases
-     * automatically.
-     */
-    protected CumulativeProtocolDecoder( int initialCapacity )
-    {
-        if( initialCapacity < 0 )
-        {
-            throw new IllegalArgumentException( "initialCapacity: " + initialCapacity );
-        }
-        
-        this.initialCapacity = initialCapacity;
     }
     
     /**
@@ -99,45 +79,52 @@
                         ProtocolDecoderOutput out ) throws Exception
     {
         ByteBuffer buf = ( ByteBuffer ) session.getAttribute( BUFFER );
-        if( buf == null )
+        // if we have a session buffer, append data to that otherwise
+        // use the buffer read from the network directly
+        if( buf != null )
         {
-            buf = ByteBuffer.allocate( initialCapacity );
-            buf.setAutoExpand( true );
-            session.setAttribute( BUFFER, buf );
+            buf.put( in );
+            buf.flip();
+        }
+        else
+        {
+            buf = in;
         }
         
-        buf.put( in );
-        buf.flip();
-
-        try
+        for( ;; )
         {
-            for( ;; )
+            int oldPos = buf.position();
+            boolean decoded = doDecode( session, buf, out );
+            if( decoded )
             {
-                int oldPos = buf.position();
-                boolean decoded = doDecode( session, buf, out );
-                if( decoded )
+                if( buf.position() == oldPos )
                 {
-                    if( buf.position() == oldPos )
-                    {
-                        throw new IllegalStateException(
-                                "doDecode() can't return true when buffer is not consumed."
);
-                    }
-                    
-                    if( !buf.hasRemaining() )
-                    {
-                        break;
-                    }
+                    throw new IllegalStateException(
+                            "doDecode() can't return true when buffer is not consumed." );
                 }
-                else
+                
+                if( !buf.hasRemaining() )
                 {
                     break;
                 }
             }
+            else
+            {
+                break;
+            }
         }
-        finally
+        
+        // if there is any data left that cannot be decoded, we store
+        // it in a buffer in the session and next time this decoder is
+        // invoked the session buffer gets appended to
+        if ( buf.hasRemaining() )
         {
-            buf.compact();
+            storeRemainingInSession( buf, session );
         }
+        else
+        {
+            removeSessionBuffer( session );
+        }        
     }
     
     /**
@@ -161,6 +148,11 @@
      */
     public void dispose( IoSession session ) throws Exception
     {
+        removeSessionBuffer( session );
+    }
+    
+    private void removeSessionBuffer(IoSession session)
+    {        
         ByteBuffer buf = ( ByteBuffer ) session.getAttribute( BUFFER );
         if( buf != null )
         {
@@ -168,4 +160,12 @@
             session.removeAttribute( BUFFER );
         }
     }
+    
+    private void storeRemainingInSession(ByteBuffer buf, IoSession session)
+    {
+        ByteBuffer remainingBuf = ByteBuffer.allocate( buf.remaining() );
+        remainingBuf.setAutoExpand( true );
+        remainingBuf.put( buf );
+        session.setAttribute( BUFFER, remainingBuf );
+    }    
 }

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/demux/DemuxingProtocolCodecFactory.java
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/demux/DemuxingProtocolCodecFactory.java?rev=393303&r1=393302&r2=393303&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/demux/DemuxingProtocolCodecFactory.java
(original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/demux/DemuxingProtocolCodecFactory.java
Tue Apr 11 13:11:35 2006
@@ -249,8 +249,6 @@
 
         protected ProtocolDecoderImpl() throws Exception
         {
-            super( 16 );
-            
             MessageDecoderFactory[] decoderFactories = DemuxingProtocolCodecFactory.this.decoderFactories;
             decoders = new MessageDecoder[ decoderFactories.length ];
             for( int i = decoderFactories.length - 1; i >= 0; i-- )

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/SocketIoProcessor.java
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/SocketIoProcessor.java?rev=393303&r1=393302&r2=393303&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/SocketIoProcessor.java
(original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/SocketIoProcessor.java
Tue Apr 11 13:11:35 2006
@@ -303,10 +303,8 @@
 
             if( readBytes > 0 )
             {
-                ByteBuffer newBuf = ByteBuffer.allocate( readBytes );
-                newBuf.put( buf );
-                newBuf.flip();
-                ( ( SocketFilterChain ) session.getFilterChain() ).messageReceived( session,
newBuf );
+                ( ( SocketFilterChain ) session.getFilterChain() ).messageReceived( session,
buf );
+                buf = null;
             }
             if( ret < 0 )
             {
@@ -321,7 +319,8 @@
         }
         finally
         {
-            buf.release();
+            if( buf != null )
+                buf.release();
         }
     }
 

Modified: directory/trunks/mina/core/src/test/java/org/apache/mina/filter/codec/CumulativeProtocolDecoderTest.java
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/core/src/test/java/org/apache/mina/filter/codec/CumulativeProtocolDecoderTest.java?rev=393303&r1=393302&r2=393303&view=diff
==============================================================================
--- directory/trunks/mina/core/src/test/java/org/apache/mina/filter/codec/CumulativeProtocolDecoderTest.java
(original)
+++ directory/trunks/mina/core/src/test/java/org/apache/mina/filter/codec/CumulativeProtocolDecoderTest.java
Tue Apr 11 13:11:35 2006
@@ -120,10 +120,6 @@
     
     private static class IntegerDecoder extends CumulativeProtocolDecoder
     {
-        protected IntegerDecoder()
-        {
-            super( 4 );
-        }
 
         protected boolean doDecode( IoSession session, ByteBuffer in,
                                     ProtocolDecoderOutput out ) throws Exception
@@ -164,10 +160,6 @@
     
     private static class WrongDecoder extends CumulativeProtocolDecoder
     {
-        public WrongDecoder()
-        {
-            super( 4 );
-        }
 
         protected boolean doDecode( IoSession session, ByteBuffer in,
                                     ProtocolDecoderOutput out ) throws Exception {



Mime
View raw message