directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From blorit...@apache.org
Subject svn commit: r111867 - in incubator/directory/protocol-api/trunk/src: java/org/apache/protocol/bufferpool test/org/apache/protocol/bufferpool test/org/apache/protocol/bufferpool/test
Date Tue, 14 Dec 2004 20:07:46 GMT
Author: bloritsch
Date: Tue Dec 14 12:07:43 2004
New Revision: 111867

URL: http://svn.apache.org/viewcvs?view=rev&rev=111867
Log:
Add the BufferPool to the protocol API because protocols would need to use new buffers for
the encoding cycle.
Added:
   incubator/directory/protocol-api/trunk/src/java/org/apache/protocol/bufferpool/
   incubator/directory/protocol-api/trunk/src/java/org/apache/protocol/bufferpool/BufferPool.java
   incubator/directory/protocol-api/trunk/src/test/org/apache/protocol/bufferpool/
   incubator/directory/protocol-api/trunk/src/test/org/apache/protocol/bufferpool/test/
   incubator/directory/protocol-api/trunk/src/test/org/apache/protocol/bufferpool/test/TestBufferPool.java

Added: incubator/directory/protocol-api/trunk/src/java/org/apache/protocol/bufferpool/BufferPool.java
Url: http://svn.apache.org/viewcvs/incubator/directory/protocol-api/trunk/src/java/org/apache/protocol/bufferpool/BufferPool.java?view=auto&rev=111867
==============================================================================
--- (empty file)
+++ incubator/directory/protocol-api/trunk/src/java/org/apache/protocol/bufferpool/BufferPool.java
Tue Dec 14 12:07:43 2004
@@ -0,0 +1,86 @@
+/*
+ *   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.protocol.bufferpool;
+
+import java.nio.ByteBuffer;
+import java.util.LinkedList;
+import java.lang.ref.WeakReference;
+
+/**
+ * Provide a tested and working ByteBuffer pool that uses WeakReferences
+ * so that old buffers can automatically be cleaned up by garbage
+ * collection.
+ */
+public class BufferPool
+{
+    private static final int KILOBYTE = 1024;
+    private static int m_usedBuffers;
+    private static final LinkedList m_reserveBuffers = new LinkedList();
+
+    /**
+     * Get an unused ByteBuffer.
+     *
+     * @return an unused ByteBuffer.
+     */
+    public static ByteBuffer getBuffer()
+    {
+        ByteBuffer buffer = null;
+        while (null == buffer && m_reserveBuffers.size() > 0)
+        {
+            final WeakReference ref = (WeakReference)m_reserveBuffers.removeFirst();
+            buffer = (ByteBuffer)ref.get();
+        }
+
+        if ( buffer == null )
+        {
+            buffer = createBuffer();
+        }
+        
+        m_usedBuffers++;
+
+        return buffer;
+    }
+
+    private static ByteBuffer createBuffer()
+    {
+        return ByteBuffer.allocateDirect( KILOBYTE );
+    }
+
+    public static int buffersInUse()
+    {
+        return m_usedBuffers;
+    }
+
+    public static void putBuffer( final ByteBuffer buffer )
+    {
+        m_usedBuffers--;
+        assert buffer.isDirect() && KILOBYTE == buffer.capacity() : "The provided
buffer could not have been from this pool.";
+        buffer.clear();
+        m_reserveBuffers.add( new WeakReference(buffer) );
+    }
+
+    public static int buffersInReserve()
+    {
+        return m_reserveBuffers.size();
+    }
+
+    public static void reset()
+    {
+        m_usedBuffers = 0;
+        m_reserveBuffers.clear();
+    }
+}

Added: incubator/directory/protocol-api/trunk/src/test/org/apache/protocol/bufferpool/test/TestBufferPool.java
Url: http://svn.apache.org/viewcvs/incubator/directory/protocol-api/trunk/src/test/org/apache/protocol/bufferpool/test/TestBufferPool.java?view=auto&rev=111867
==============================================================================
--- (empty file)
+++ incubator/directory/protocol-api/trunk/src/test/org/apache/protocol/bufferpool/test/TestBufferPool.java
Tue Dec 14 12:07:43 2004
@@ -0,0 +1,127 @@
+/*
+ *   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.protocol.bufferpool.test;
+
+import junit.framework.TestCase;
+
+import java.nio.ByteBuffer;
+
+import org.apache.protocol.bufferpool.BufferPool;
+
+
+/**
+ * Created by IntelliJ IDEA. User: berin Date: Dec 9, 2004 Time: 9:40:14
+ * AM To change this template use File | Settings | File Templates.
+ */
+public class TestBufferPool extends TestCase
+{
+    private static final int KB = 1024;
+    private static final int TOO_SMALL = 256;
+
+    public TestBufferPool(final String name)
+    {
+        super(name);
+    }
+
+    public void tearDown() throws Exception
+    {
+        super.tearDown();
+
+        BufferPool.reset();
+    }
+
+    public void testGetBuffer()
+    {
+        final ByteBuffer buffer = BufferPool.getBuffer();
+
+        assertNotNull(buffer);
+        assertEquals(KB, buffer.capacity());
+        assertTrue( buffer.isDirect() );
+        assertEquals( 1, BufferPool.buffersInUse() );
+
+        BufferPool.putBuffer(buffer);
+    }
+
+    public void testPutBuffer()
+    {
+        final ByteBuffer buffer = BufferPool.getBuffer();
+        assertEquals( 1, BufferPool.buffersInUse() );
+        assertEquals( 0, BufferPool.buffersInReserve() );
+
+        BufferPool.putBuffer(buffer);
+        assertEquals( 0, BufferPool.buffersInUse() );
+        assertEquals( 1, BufferPool.buffersInReserve() );
+    }
+
+    public void testBuffersInReserve()
+    {
+        assertEquals( 0, BufferPool.buffersInReserve() );
+
+        final ByteBuffer buffer1 = BufferPool.getBuffer();
+        BufferPool.putBuffer( buffer1 );
+
+        assertEquals( 1, BufferPool.buffersInReserve() );
+
+        final ByteBuffer buffer2 = BufferPool.getBuffer();
+
+        assertEquals( 0, BufferPool.buffersInReserve() );
+
+        BufferPool.putBuffer( buffer2 );
+
+        assertEquals( 1, BufferPool.buffersInReserve() );
+    }
+
+    public void testPutBuffer_notDirect()
+    {
+        boolean assertionsEnabled = false;
+        assert assertionsEnabled = true;
+
+        if (assertionsEnabled)
+        {
+            try
+            {
+                BufferPool.putBuffer( ByteBuffer.allocate( KB ) );
+                fail("Did not throw AssertionError as expected.");
+            }
+            catch (Error e)
+            {
+                assertTrue("Caught error, but not the expected type" + e, e instanceof AssertionError);
+                if ( ! (e instanceof AssertionError ) ) throw e;
+            }
+        }
+    }
+
+    public void testPutBuffer_tooSmall()
+    {
+        boolean assertionsEnabled = false;
+        assert assertionsEnabled = true;
+
+        if (assertionsEnabled)
+        {
+            try
+            {
+                BufferPool.putBuffer( ByteBuffer.allocateDirect( TOO_SMALL ) );
+                fail("Did not throw AssertionError as expected.");
+            }
+            catch (Error e)
+            {
+                assertTrue("Caught error, but not the expected type" + e, e instanceof AssertionError);
+                if ( ! (e instanceof AssertionError ) ) throw e;
+            }
+        }
+    }
+}

Mime
View raw message