Hi All,

        There's currently a couple of bugs in FixedSizeBuffer. The current code
        can't correctly ascertain the size of the buffer so it allows one to
        fill it indefinitely overwriting occupied cells. It also creates a
        buffer array 1 element too big (combined with the previous
        problem allowing the buffer to hold in memory one object too many).

        Attached is a patch to fix both problems.

        Cheers,

        Marcus

        BTW - is FixedSizeBuffer allowed to hold null elements, as this
        case seems to be broken as well ? (remove() won't remove null
        items, but add() will store them).

-- 
        .....
     ,,$$$$$$$$$,      Marcus Crafter
    ;$'      '$$$$:    Computer Systems Engineer
    $:         $$$$:   ManageSoft GmbH
     $       o_)$$$:   82-84 Mainzer Landstrasse
     ;$,    _/\ &&:'   60327 Frankfurt Germany
       '     /( &&&
           \_&&&&'
          &&&&.
    &&&&&&&:
Index: FixedSizeBuffer.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/collections/FixedSizeBuffer.java,v
retrieving revision 1.1
diff -u -r1.1 FixedSizeBuffer.java
--- FixedSizeBuffer.java        20 Dec 2001 18:28:33 -0000      1.1
+++ FixedSizeBuffer.java        25 Feb 2002 17:42:52 -0000
@@ -18,10 +18,11 @@
     private final Object[] m_elements;
     private       int       m_start = 0;
     private       int       m_end = 0;
+    private       boolean   m_full = false;
 
     public FixedSizeBuffer( int size )
     {
-        m_elements = new Object[ size + 1 ];
+        m_elements = new Object[ size ];
     }
 
     public FixedSizeBuffer()
@@ -37,7 +38,11 @@
         {
             size = m_elements.length - m_start + m_end;
         }
-        else
+        else if ( m_end == m_start )
+        {
+            size = ( m_full ? m_elements.length : 0 );
+        }
+       else
         {
             size = m_end - m_start;
         }
@@ -52,19 +57,23 @@
 
     public final void add( Object element )
     {
-        if ( size() > m_elements.length )
+        if ( m_full )
         {
             throw new BufferOverflowException( "The buffer cannot hold more than "
                                                + m_elements.length + " objects." );
         }
 
-        m_elements[ m_end ] = element;
+        m_elements[ m_end++ ] = element;
 
-        m_end++;
         if ( m_end >= m_elements.length )
         {
             m_end = 0;
         }
+
+        if ( m_end == m_start )
+        {
+            m_full = true;
+        }
     }
 
     public final Object remove()
@@ -78,15 +87,16 @@
 
         if ( null != element )
         {
-            m_elements[ m_start ] = null;
+            m_elements[ m_start++ ] = null;
 
-            m_start++;
             if ( m_start >= m_elements.length )
             {
                 m_start = 0;
             }
+
+            m_full = false;
         }
 
         return element;
     }
-}
\ No newline at end of file
+}

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to