directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r329220 - in /directory/network/trunk/src: examples/org/apache/mina/examples/reverser/ java/org/apache/mina/filter/codec/ test/org/apache/mina/filter/codec/
Date Fri, 28 Oct 2005 15:07:27 GMT
Author: trustin
Date: Fri Oct 28 08:07:17 2005
New Revision: 329220

URL: http://svn.apache.org/viewcvs?rev=329220&view=rev
Log:
Resolved issue: DIRMINA-109 - CumulativeDecoder doesn't release allocated buffer.
* Implemented CumulativeDecoder.dispose() method.
* DemuxingProtocolCodecFactory.ProtocolDecoderImpl now calls super.dispose().
* CumulativeDecoder is now can be a singleton.

Modified:
    directory/network/trunk/src/examples/org/apache/mina/examples/reverser/ReverseProtocolHandler.java
    directory/network/trunk/src/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
    directory/network/trunk/src/java/org/apache/mina/filter/codec/DemuxingProtocolCodecFactory.java
    directory/network/trunk/src/java/org/apache/mina/filter/codec/ProtocolCodecFactory.java
    directory/network/trunk/src/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java
    directory/network/trunk/src/test/org/apache/mina/filter/codec/CumulativeProtocolDecoderTest.java

Modified: directory/network/trunk/src/examples/org/apache/mina/examples/reverser/ReverseProtocolHandler.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/examples/org/apache/mina/examples/reverser/ReverseProtocolHandler.java?rev=329220&r1=329219&r2=329220&view=diff
==============================================================================
--- directory/network/trunk/src/examples/org/apache/mina/examples/reverser/ReverseProtocolHandler.java
(original)
+++ directory/network/trunk/src/examples/org/apache/mina/examples/reverser/ReverseProtocolHandler.java
Fri Oct 28 08:07:17 2005
@@ -40,13 +40,13 @@
     
     private static ProtocolCodecFactory CODEC_FACTORY = new ProtocolCodecFactory()
     {
-        public ProtocolEncoder newEncoder()
+        public ProtocolEncoder getEncoder()
         {
             // Create a new encoder.
             return new TextLineEncoder();
         }
 
-        public ProtocolDecoder newDecoder()
+        public ProtocolDecoder getDecoder()
         {
             // Create a new decoder.
             return new TextLineDecoder();

Modified: directory/network/trunk/src/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java?rev=329220&r1=329219&r2=329220&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
(original)
+++ directory/network/trunk/src/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
Fri Oct 28 08:07:17 2005
@@ -57,18 +57,33 @@
  */
 public abstract class CumulativeProtocolDecoder extends ProtocolDecoderAdapter {
     
-    /** Cumulation buffer */
-    private ByteBuffer buf;
+    private static final String BUFFER = CumulativeProtocolDecoder.class.getName() + ".Buffer";
+    
+    private final int initialCapacity;
     
     /**
-     * Creates a new instance with the specified default capacity of
+     * Creates a new instance with the 16 bytes initial capacity of
      * cumulative buffer.  Please note that the capacity increases
      * automatically.
      */
-    protected CumulativeProtocolDecoder( int defaultCapacity )
+    protected CumulativeProtocolDecoder()
     {
-        buf = ByteBuffer.allocate( defaultCapacity );
-        buf.setAutoExpand( true );
+        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;
     }
     
     /**
@@ -83,13 +98,14 @@
     public void decode( IoSession session, ByteBuffer in,
                         ProtocolDecoderOutput out ) throws Exception
     {
-        if( session.getTransportType().isConnectionless() )
+        ByteBuffer buf = ( ByteBuffer ) session.getAttribute( BUFFER );
+        if( buf == null )
         {
-            throw new IllegalStateException(
-                    "This decoder doesn't work for connectionless transport types." );
+            buf = ByteBuffer.allocate( initialCapacity );
+            buf.setAutoExpand( true );
+            session.setAttribute( BUFFER, buf );
         }
-
-        ByteBuffer buf = this.buf;
+        
         buf.put( in );
         buf.flip();
 
@@ -137,4 +153,19 @@
      */
     protected abstract boolean doDecode( IoSession session, ByteBuffer in,
                                          ProtocolDecoderOutput out ) throws Exception;
+
+    /**
+     * Releases the cumulative buffer used by the specified <tt>session</tt>.
+     * Please don't forget to call <tt>super.dispose( session )</tt> when
+     * you override this method.
+     */
+    public void dispose( IoSession session ) throws Exception
+    {
+        ByteBuffer buf = ( ByteBuffer ) session.getAttribute( BUFFER );
+        if( buf != null )
+        {
+            buf.release();
+            session.removeAttribute( BUFFER );
+        }
+    }
 }

Modified: directory/network/trunk/src/java/org/apache/mina/filter/codec/DemuxingProtocolCodecFactory.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/filter/codec/DemuxingProtocolCodecFactory.java?rev=329220&r1=329219&r2=329220&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/filter/codec/DemuxingProtocolCodecFactory.java
(original)
+++ directory/network/trunk/src/java/org/apache/mina/filter/codec/DemuxingProtocolCodecFactory.java
Fri Oct 28 08:07:17 2005
@@ -122,11 +122,11 @@
         this.decoderFactories = newDecoderFactories;
     }
     
-    public ProtocolEncoder newEncoder() {
+    public ProtocolEncoder getEncoder() {
         return new ProtocolEncoderImpl();
     }
 
-    public ProtocolDecoder newDecoder() {
+    public ProtocolDecoder getDecoder() {
         return new ProtocolDecoderImpl();
     }
     
@@ -313,7 +313,10 @@
 
         public void dispose( IoSession session ) throws Exception
         {
-            // ProtocolEncoder.dispose() already called disposeCodec().
+            super.dispose( session );
+            
+            // ProtocolEncoder.dispose() already called disposeCodec(),
+            // so there's nothing more we need to do.
         }
     }
     

Modified: directory/network/trunk/src/java/org/apache/mina/filter/codec/ProtocolCodecFactory.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/filter/codec/ProtocolCodecFactory.java?rev=329220&r1=329219&r2=329220&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/filter/codec/ProtocolCodecFactory.java
(original)
+++ directory/network/trunk/src/java/org/apache/mina/filter/codec/ProtocolCodecFactory.java
Fri Oct 28 08:07:17 2005
@@ -32,14 +32,14 @@
 public interface ProtocolCodecFactory
 {
     /**
-     * Creates a new instance of {@link ProtocolEncoder} which encodes message
-     * objects into binary or protocol-specific data.
+     * Returns a new (or reusable) instance of {@link ProtocolEncoder} which
+     * encodes message objects into binary or protocol-specific data.
      */
-    ProtocolEncoder newEncoder();
+    ProtocolEncoder getEncoder();
 
     /**
-     * Creates a new instance of {@link ProtocolDecoder} which decodes binary or
-     * protocol-specific data into message objects.
+     * Returns a new (or reusable) instance of {@link ProtocolDecoder} which
+     * decodes binary or protocol-specific data into message objects.
      */
-    ProtocolDecoder newDecoder();
+    ProtocolDecoder getDecoder();
 }

Modified: directory/network/trunk/src/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java?rev=329220&r1=329219&r2=329220&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java
(original)
+++ directory/network/trunk/src/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java
Fri Oct 28 08:07:17 2005
@@ -152,7 +152,7 @@
         ProtocolEncoder encoder = ( ProtocolEncoder ) session.getAttribute( ENCODER );
         if( encoder == null )
         {
-            encoder = factory.newEncoder();
+            encoder = factory.getEncoder();
             session.setAttribute( ENCODER, encoder );
         }
         return encoder;
@@ -174,7 +174,7 @@
         ProtocolDecoder decoder = ( ProtocolDecoder ) session.getAttribute( DECODER );
         if( decoder == null )
         {
-            decoder = factory.newDecoder();
+            decoder = factory.getDecoder();
             session.setAttribute( DECODER, decoder );
         }
         return decoder;

Modified: directory/network/trunk/src/test/org/apache/mina/filter/codec/CumulativeProtocolDecoderTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/filter/codec/CumulativeProtocolDecoderTest.java?rev=329220&r1=329219&r2=329220&view=diff
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/filter/codec/CumulativeProtocolDecoderTest.java
(original)
+++ directory/network/trunk/src/test/org/apache/mina/filter/codec/CumulativeProtocolDecoderTest.java
Fri Oct 28 08:07:17 2005
@@ -61,6 +61,7 @@
 
     protected void tearDown() throws Exception
     {
+        decoder.dispose( session );
     }
     
     public void testCumulation() throws Exception
@@ -102,7 +103,6 @@
             expected.add( new Integer( i ) );
         }
         Assert.assertEquals( expected, output.getValues() );
-        
     }
     
     public void testWrongImplementationDetection() throws Exception {



Mime
View raw message