directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r151543 - in incubator/directory/network/trunk/mina: ./ src/java/org/apache/mina/common/ src/java/org/apache/mina/io/socket/ src/java/org/apache/mina/util/ src/test/org/apache/mina/common/
Date Sun, 06 Feb 2005 05:03:11 GMT
Author: trustin
Date: Sat Feb  5 21:03:07 2005
New Revision: 151543

URL: http://svn.apache.org/viewcvs?view=rev&rev=151543
Log:
 * Added Doug Lea's concurrent package to dependency.
 * Added util.SyncUtil class to acquire locks conveniently.

 * ByteBuffer can allocate buffers with arbitrary size now.
 * Added a test case for ByteBuffer

Added:
    incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/SyncUtil.java   (with
props)
    incubator/directory/network/trunk/mina/src/test/org/apache/mina/common/
    incubator/directory/network/trunk/mina/src/test/org/apache/mina/common/ByteBufferTest.java
  (with props)
Modified:
    incubator/directory/network/trunk/mina/project.xml
    incubator/directory/network/trunk/mina/src/java/org/apache/mina/common/ByteBuffer.java
    incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketSession.java
    incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/IoHandlerFilterManager.java
    incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ProtocolHandlerFilterManager.java

Modified: incubator/directory/network/trunk/mina/project.xml
URL: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/project.xml?view=diff&r1=151542&r2=151543
==============================================================================
--- incubator/directory/network/trunk/mina/project.xml (original)
+++ incubator/directory/network/trunk/mina/project.xml Sat Feb  5 21:03:07 2005
@@ -77,6 +77,13 @@
 
 	<dependencies>
 
+		<dependency>
+			<groupId>concurrent</groupId>
+			<artifactId>concurrent</artifactId>
+			<version>1.3.4</version>
+			<url>http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html</url>
+		</dependency>
+
 		<!-- ================================================================== -->
 		<!-- Compatibility dependencies for ProtocolEncoder and ProtocolDecoder -->
 		<!-- ================================================================== -->

Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/common/ByteBuffer.java
URL: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/common/ByteBuffer.java?view=diff&r1=151542&r2=151543
==============================================================================
--- incubator/directory/network/trunk/mina/src/java/org/apache/mina/common/ByteBuffer.java
(original)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/common/ByteBuffer.java
Sat Feb  5 21:03:07 2005
@@ -48,16 +48,6 @@
  */
 public final class ByteBuffer
 {
-    /**
-     * The maximum capacity of allocatable buffer (8192).
-     */
-    public static final int MAXIMUM_CAPACITY = 8192;
-
-    private static final int[] bufferStackSizes = new int[] { 16, 32, 64, 128,
-                                                             256, 512, 1024,
-                                                             2048, 3072, 4096,
-                                                             6144, 8192, };
-
     private static final Stack[] bufferStacks = new Stack[] { new Stack(),
                                                              new Stack(),
                                                              new Stack(),
@@ -69,6 +59,23 @@
                                                              new Stack(),
                                                              new Stack(),
                                                              new Stack(),
+                                                             new Stack(),
+                                                             new Stack(),
+                                                             new Stack(),
+                                                             new Stack(),
+                                                             new Stack(),
+                                                             new Stack(),
+                                                             new Stack(),
+                                                             new Stack(),
+                                                             new Stack(),
+                                                             new Stack(),
+                                                             new Stack(),
+                                                             new Stack(),
+                                                             new Stack(),
+                                                             new Stack(),
+                                                             new Stack(),
+                                                             new Stack(),
+                                                             new Stack(),
                                                              new Stack(), };
 
     /**
@@ -86,21 +93,21 @@
             if( buf == null )
             {
                 buf = new ByteBuffer( java.nio.ByteBuffer
-                        .allocateDirect( bufferStackSizes[ idx ] ) );
+                        .allocateDirect( 16 << idx ) );
             }
         }
 
         buf.clear();
         // Check leaked or dangling ByteBuffer.
-    	if (buf.inUse)
-    	{
-    		throw new IllegalStateException(
-    				"Already allocated buffer. Did you release the buffer more than once?");
-    	}
-    	else
-    	{
-    		buf.inUse = true;
-    	}
+        if( buf.inUse )
+        {
+            throw new IllegalStateException(
+                    "Already allocated buffer. Did you release the buffer more than once?"
);
+        }
+        else
+        {
+            buf.inUse = true;
+        }
 
         return buf;
     }
@@ -114,15 +121,15 @@
         synchronized( stack )
         {
             // Check leaked or dangling ByteBuffer.
-        	if (!buf.inUse)
-        	{
-        		throw new IllegalStateException(
-        				"Already released buffer.  Did you release the buffer more than once?");
-        	}
-        	else
-        	{
-        		buf.inUse = false; // clear the flag
-        	}
+            if( !buf.inUse )
+            {
+                throw new IllegalStateException(
+                        "Already released buffer.  Did you release the buffer more than once?"
);
+            }
+            else
+            {
+                buf.inUse = false; // clear the flag
+            }
 
             stack.push( buf );
         }
@@ -130,36 +137,24 @@
 
     private static int getBufferStackIndex( int size )
     {
-        if( size <= 16 )
-            return 0;
-        if( size <= 32 )
-            return 1;
-        if( size <= 64 )
-            return 2;
-        if( size <= 128 )
-            return 3;
-        if( size <= 256 )
-            return 4;
-        if( size <= 512 )
-            return 5;
-        if( size <= 1024 )
-            return 6;
-        if( size <= 2048 )
-            return 7;
-        if( size <= 3072 )
-            return 8;
-        if( size <= 4096 )
-            return 9;
-        if( size <= 6144 )
-            return 10;
-        if( size <= 8192 )
-            return 11;
+        int targetSize = 16;
+        int stackIdx = 0;
+        while( size > targetSize )
+        {
+            targetSize <<= 1;
+            stackIdx ++ ;
+            if( stackIdx >= bufferStacks.length )
+            {
+                throw new IllegalArgumentException(
+                        "Buffer size is too big: " + size );
+            }
+        }
 
-        throw new IllegalArgumentException( "Buffer size cannot exceed 8192: "
-                                            + size );
+        return stackIdx;
     }
 
     private final java.nio.ByteBuffer buf;
+
     private boolean inUse;
 
     private ByteBuffer( java.nio.ByteBuffer buf )
@@ -343,7 +338,7 @@
 
     public boolean equals( Object ob )
     {
-        if( ! ( ob instanceof ByteBuffer ) )
+        if( !( ob instanceof ByteBuffer ) )
             return false;
 
         ByteBuffer that = ( ByteBuffer ) ob;
@@ -594,7 +589,7 @@
 
         if( !utf16 )
         {
-            for( i = 0; i < fieldSize; i++ )
+            for( i = 0; i < fieldSize; i ++ )
             {
                 if( buf.get() == 0 )
                 {
@@ -740,9 +735,9 @@
      */
     public ByteBuffer skip( int size )
     {
-        return position(position() + size);
+        return position( position() + size );
     }
-    
+
     /**
      * Fills this buffer with the specified value.
      * This method moves buffer position forward.
@@ -760,7 +755,7 @@
             longValue <<= 32;
             longValue |= intValue;
 
-            for( int i = q; i > 0; i-- )
+            for( int i = q; i > 0; i -- )
             {
                 buf.putLong( longValue );
             }
@@ -820,7 +815,7 @@
         int q = size >>> 3;
         int r = size & 7;
 
-        for( int i = q; i > 0; i-- )
+        for( int i = q; i > 0; i -- )
         {
             buf.putLong( 0L );
         }
@@ -873,8 +868,7 @@
         if( fieldSize < 0 )
         {
             throw new IllegalArgumentException(
-                                                "fieldSize cannot be negative: "
-                                                                                        
                                                                                         
             + fieldSize );
+                    "fieldSize cannot be negative: " + fieldSize );
         }
     }
-}
\ No newline at end of file
+}

Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketSession.java
URL: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketSession.java?view=diff&r1=151542&r2=151543
==============================================================================
--- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketSession.java
(original)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketSession.java
Sat Feb  5 21:03:07 2005
@@ -40,6 +40,8 @@
  */
 class SocketSession implements IoSession
 {
+    private static final int READ_BUFFER_SIZE = 8192;
+
     private final IoHandlerFilterManager filterManager;
 
     private final SocketChannel ch;
@@ -87,8 +89,7 @@
         this.filterManager = filterManager;
         this.ch = ch;
         this.config = new SocketSessionConfig( ch );
-        this.readBuf = ByteBuffer.allocate( ByteBuffer.MAXIMUM_CAPACITY )
-                .limit( 0 );
+        this.readBuf = ByteBuffer.allocate( READ_BUFFER_SIZE ).limit( 0 );
         this.writeBufferQueue = new Queue();
         this.writeMarkerQueue = new Queue();
         this.handler = defaultHandler;
@@ -263,4 +264,4 @@
             SocketIoProcessor.getInstance().flushSession( SocketSession.this );
         }
     }
-}
\ No newline at end of file
+}

Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/IoHandlerFilterManager.java
URL: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/IoHandlerFilterManager.java?view=diff&r1=151542&r2=151543
==============================================================================
--- incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/IoHandlerFilterManager.java
(original)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/IoHandlerFilterManager.java
Sat Feb  5 21:03:07 2005
@@ -296,7 +296,7 @@
         return list;
     }
 
-    private static class Entry
+    private class Entry
     {
         private Entry prevEntry;
 
@@ -331,7 +331,7 @@
                     }
                     catch( Throwable e )
                     {
-                        exceptionCaught( session, e );
+                        IoHandlerFilterManager.this.fireExceptionCaught( session, e );
                     }
                 }
 
@@ -346,7 +346,7 @@
                     }
                     catch( Throwable e )
                     {
-                        exceptionCaught( session, e );
+                        IoHandlerFilterManager.this.fireExceptionCaught( session, e );
                     }
                 }
 
@@ -361,7 +361,7 @@
                     }
                     catch( Throwable e )
                     {
-                        exceptionCaught( session, e );
+                        IoHandlerFilterManager.this.fireExceptionCaught( session, e );
                     }
                 }
 
@@ -390,7 +390,7 @@
                     }
                     catch( Throwable e )
                     {
-                        exceptionCaught( session, e );
+                        IoHandlerFilterManager.this.fireExceptionCaught( session, e );
                     }
                 }
 
@@ -405,7 +405,7 @@
                     }
                     catch( Throwable e )
                     {
-                        exceptionCaught( session, e );
+                        IoHandlerFilterManager.this.fireExceptionCaught( session, e );
                     }
                 }
             };

Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ProtocolHandlerFilterManager.java
URL: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ProtocolHandlerFilterManager.java?view=diff&r1=151542&r2=151543
==============================================================================
--- incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ProtocolHandlerFilterManager.java
(original)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ProtocolHandlerFilterManager.java
Sat Feb  5 21:03:07 2005
@@ -282,7 +282,7 @@
         return list;
     }
 
-    private static class Entry
+    private class Entry
     {
         private Entry prevEntry;
 
@@ -317,7 +317,7 @@
                     }
                     catch( Throwable e )
                     {
-                        exceptionCaught( session, e );
+                        ProtocolHandlerFilterManager.this.fireExceptionCaught( session, e
);
                     }
                 }
 
@@ -332,7 +332,7 @@
                     }
                     catch( Throwable e )
                     {
-                        exceptionCaught( session, e );
+                        ProtocolHandlerFilterManager.this.fireExceptionCaught( session, e
);
                     }
                 }
 
@@ -348,7 +348,7 @@
                     }
                     catch( Throwable e )
                     {
-                        exceptionCaught( session, e );
+                        ProtocolHandlerFilterManager.this.fireExceptionCaught( session, e
);
                     }
                 }
 
@@ -380,7 +380,7 @@
                     }
                     catch( Throwable e )
                     {
-                        exceptionCaught( session, e );
+                        ProtocolHandlerFilterManager.this.fireExceptionCaught( session, e
);
                     }
                 }
 
@@ -396,7 +396,7 @@
                     }
                     catch( Throwable e )
                     {
-                        exceptionCaught( session, e );
+                        ProtocolHandlerFilterManager.this.fireExceptionCaught( session, e
);
                     }
                 }
             };

Added: incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/SyncUtil.java
URL: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/SyncUtil.java?view=auto&rev=151543
==============================================================================
--- incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/SyncUtil.java (added)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/SyncUtil.java Sat
Feb  5 21:03:07 2005
@@ -0,0 +1,53 @@
+/*
+ *   @(#) $Id$
+ * 
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.mina.util;
+
+import EDU.oswego.cs.dl.util.concurrent.Sync;
+
+
+/**
+ * Utility class that acquires lock from Doug Lea's <code>Sync</code> not
+ * throwing {@link InterruptedException}.
+ *
+ * @author Trustin Lee (trustin@gmail.com)
+ * @version $Rev$, $Date$
+ */
+public class SyncUtil {
+    private SyncUtil() {
+    }
+
+    public static final void acquire(Sync sync) {
+        boolean wasInterrupted = Thread.interrupted(); // record and clear
+
+        for (;;) {
+            try {
+                sync.acquire(); // or any other method throwing
+
+                // InterruptedException
+                break;
+            } catch (InterruptedException ex) { // re-interrupted; try again
+                wasInterrupted = true;
+            }
+        }
+
+        if (wasInterrupted) { // re-establish interrupted state
+            Thread.currentThread().interrupt();
+        }
+    }
+}

Propchange: incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/SyncUtil.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: incubator/directory/network/trunk/mina/src/test/org/apache/mina/common/ByteBufferTest.java
URL: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/test/org/apache/mina/common/ByteBufferTest.java?view=auto&rev=151543
==============================================================================
--- incubator/directory/network/trunk/mina/src/test/org/apache/mina/common/ByteBufferTest.java
(added)
+++ incubator/directory/network/trunk/mina/src/test/org/apache/mina/common/ByteBufferTest.java
Sat Feb  5 21:03:07 2005
@@ -0,0 +1,70 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.common;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+/**
+ * TODO Document me.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$, 
+ */
+public class ByteBufferTest extends TestCase
+{
+
+    public static void main( String[] args )
+    {
+        junit.textui.TestRunner.run( ByteBufferTest.class );
+    }
+
+    protected void setUp() throws Exception
+    {
+    }
+
+    protected void tearDown() throws Exception
+    {
+    }
+
+    public void testAllocate() throws Exception
+    {
+        for( int i = 10; i < 1048576 * 2; i = i * 11 / 10 ) // increase by 10%
+        {
+            ByteBuffer buf = ByteBuffer.allocate( i );
+            Assert.assertEquals( 0, buf.position() );
+            Assert.assertEquals( buf.capacity(), buf.remaining() );
+            Assert.assertTrue( buf.capacity() >= i );
+            Assert.assertTrue( buf.capacity() < i * 2 );
+        }
+    }
+
+    public void testRelease() throws Exception
+    {
+        for( int i = 10; i < 1048576 * 2; i = i * 11 / 10 ) // increase by 10%
+        {
+            ByteBuffer buf = ByteBuffer.allocate( i );
+            Assert.assertEquals( 0, buf.position() );
+            Assert.assertEquals( buf.capacity(), buf.remaining() );
+            Assert.assertTrue( buf.capacity() >= i );
+            Assert.assertTrue( buf.capacity() < i * 2 );
+            ByteBuffer.release( buf );
+        }
+    }
+
+    public void testLeakageDetection() throws Exception
+    {
+        ByteBuffer buf = ByteBuffer.allocate( 1024 );
+        ByteBuffer.release( buf );
+        try
+        {
+            ByteBuffer.release( buf );
+            Assert.fail( "Releasing a buffer twice should fail." );
+        }
+        catch( IllegalStateException e )
+        {
+
+        }
+    }
+}

Propchange: incubator/directory/network/trunk/mina/src/test/org/apache/mina/common/ByteBufferTest.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision



Mime
View raw message