directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r430683 - in /directory/trunks/mina/core/src: main/java/org/apache/mina/common/ main/java/org/apache/mina/common/support/ test/java/org/apache/mina/common/
Date Fri, 11 Aug 2006 05:45:58 GMT
Author: trustin
Date: Thu Aug 10 22:45:57 2006
New Revision: 430683

URL: http://svn.apache.org/viewvc?rev=430683&view=rev
Log:
Fixed issue: DIRMINA-243 (mark() and autoexpand)
* Made sure mark is preserved after expansion
* Refactored ByteBuffer implementation
  * Most base implementation has been extracted into BaseByteBuffer to remove code duplication
* Added ByteBuffer.capacity()

Added:
    directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/BaseByteBuffer.java
  (with props)
Modified:
    directory/trunks/mina/core/src/main/java/org/apache/mina/common/ByteBuffer.java
    directory/trunks/mina/core/src/main/java/org/apache/mina/common/ByteBufferProxy.java
    directory/trunks/mina/core/src/main/java/org/apache/mina/common/PooledByteBufferAllocator.java
    directory/trunks/mina/core/src/main/java/org/apache/mina/common/SimpleByteBufferAllocator.java
    directory/trunks/mina/core/src/test/java/org/apache/mina/common/ByteBufferTest.java

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/common/ByteBuffer.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/core/src/main/java/org/apache/mina/common/ByteBuffer.java?rev=430683&r1=430682&r2=430683&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/common/ByteBuffer.java (original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/common/ByteBuffer.java Thu Aug
10 22:45:57 2006
@@ -292,12 +292,22 @@
      * @see java.nio.ByteBuffer#isDirect()
      */
     public abstract boolean isDirect();
+    
+    /**
+     * @see java.nio.ByteBuffer#isReadOnly()
+     */
+    public abstract boolean isReadOnly();
 
     /**
      * @see java.nio.ByteBuffer#capacity()
      */
     public abstract int capacity();
-
+    
+    /**
+     * Changes the capacity of this buffer.
+     */
+    public abstract ByteBuffer capacity( int newCapacity );
+    
     /**
      * Returns <tt>true</tt> if and only if <tt>autoExpand</tt> is
turned on.
      */
@@ -314,8 +324,11 @@
      * This method works even if you didn't set <tt>autoExpand</tt> to
      * <tt>true</tt>.
      */
-    public abstract ByteBuffer expand( int expectedRemaining );
-
+    public ByteBuffer expand( int expectedRemaining )
+    {
+        return expand( position(), expectedRemaining );
+    }
+    
     /**
      * Changes the capacity and limit of this buffer so this buffer get
      * the specified <tt>expectedRemaining</tt> room from the specified
@@ -355,7 +368,7 @@
      * @see java.nio.Buffer#position(int)
      */
     public abstract ByteBuffer position( int newPosition );
-
+    
     /**
      * @see java.nio.Buffer#limit()
      */
@@ -370,17 +383,23 @@
      * @see java.nio.Buffer#mark()
      */
     public abstract ByteBuffer mark();
+    
+    /**
+     * Returns the position of the current mark.  This method returns <tt>-1</tt>
if no
+     * mark is set.
+     */
+    public abstract int markValue();
 
     /**
      * @see java.nio.Buffer#reset()
      */
     public abstract ByteBuffer reset();
-
+    
     /**
      * @see java.nio.Buffer#clear()
      */
     public abstract ByteBuffer clear();
-
+    
     /**
      * Clears this buffer and fills its content with <tt>NUL</tt>.
      * The position is set to zero, the limit is set to the capacity,
@@ -416,7 +435,10 @@
     /**
      * @see java.nio.Buffer#remaining()
      */
-    public abstract int remaining();
+    public int remaining()
+    {
+	return limit() - position();
+    }
 
     /**
      * @see java.nio.Buffer#hasRemaining()

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/common/ByteBufferProxy.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/core/src/main/java/org/apache/mina/common/ByteBufferProxy.java?rev=430683&r1=430682&r2=430683&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/common/ByteBufferProxy.java (original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/common/ByteBufferProxy.java Thu
Aug 10 22:45:57 2006
@@ -632,4 +632,20 @@
     {
         return buf.arrayOffset();
     }
+
+    public ByteBuffer capacity( int newCapacity )
+    {
+	buf.capacity( newCapacity );
+	return this;
+    }
+
+    public boolean isReadOnly()
+    {
+	return buf.isReadOnly();
+    }
+
+    public int markValue()
+    {
+	return buf.markValue();
+    }
 }

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/common/PooledByteBufferAllocator.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/core/src/main/java/org/apache/mina/common/PooledByteBufferAllocator.java?rev=430683&r1=430682&r2=430683&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/common/PooledByteBufferAllocator.java
(original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/common/PooledByteBufferAllocator.java
Thu Aug 10 22:45:57 2006
@@ -18,15 +18,10 @@
  */
 package org.apache.mina.common;
 
-import org.apache.mina.util.ExpiringStack;
-
 import java.nio.ByteOrder;
-import java.nio.CharBuffer;
-import java.nio.DoubleBuffer;
-import java.nio.FloatBuffer;
-import java.nio.IntBuffer;
-import java.nio.LongBuffer;
-import java.nio.ShortBuffer;
+
+import org.apache.mina.common.support.BaseByteBuffer;
+import org.apache.mina.util.ExpiringStack;
 
 /**
  * A {@link ByteBufferAllocator} which pools allocated buffers. <p> All buffers are
allocated with the size of power of
@@ -316,11 +311,10 @@
         }
     }
 
-    private class PooledByteBuffer extends ByteBuffer
+    private class PooledByteBuffer extends BaseByteBuffer
     {
         private UnexpandableByteBuffer buf;
         private int refCount = 1;
-        private boolean autoExpand;
 
         protected PooledByteBuffer()
         {
@@ -334,7 +328,7 @@
                 buf.buf().clear();
             }
             buf.buf().order( ByteOrder.BIG_ENDIAN );
-            autoExpand = false;
+            setAutoExpand( false );
             refCount = 1;
         }
 
@@ -380,27 +374,6 @@
             return buf.buf();
         }
 
-        public boolean isDirect()
-        {
-            return buf.buf().isDirect();
-        }
-
-        public boolean isReadOnly()
-        {
-            return buf.buf().isReadOnly();
-        }
-
-        public boolean isAutoExpand()
-        {
-            return autoExpand;
-        }
-
-        public ByteBuffer setAutoExpand( boolean autoExpand )
-        {
-            this.autoExpand = autoExpand;
-            return this;
-        }
-
         public boolean isPooled()
         {
             return buf.isPooled();
@@ -411,75 +384,11 @@
             buf.setPooled( pooled );
         }
 
-        public int capacity()
-        {
-            return buf.buf().capacity();
-        }
-
-        public int position()
-        {
-            return buf.buf().position();
-        }
-
-        public ByteBuffer position( int newPosition )
-        {
-            autoExpand( newPosition, 0 );
-            buf.buf().position( newPosition );
-            return this;
-        }
-
-        public int limit()
-        {
-            return buf.buf().limit();
-        }
-
-        public ByteBuffer limit( int newLimit )
-        {
-            autoExpand( newLimit, 0 );
-            buf.buf().limit( newLimit );
-            return this;
-        }
-
-        public ByteBuffer mark()
-        {
-            buf.buf().mark();
-            return this;
-        }
-
-        public ByteBuffer reset()
-        {
-            buf.buf().reset();
-            return this;
-        }
-
-        public ByteBuffer clear()
-        {
-            buf.buf().clear();
-            return this;
-        }
-
-        public ByteBuffer flip()
-        {
-            buf.buf().flip();
-            return this;
-        }
-
-        public ByteBuffer rewind()
-        {
-            buf.buf().rewind();
-            return this;
-        }
-
-        public int remaining()
-        {
-            return buf.buf().remaining();
-        }
-
         public ByteBuffer duplicate()
         {
             PooledByteBuffer newBuf = allocateContainer();
             newBuf.init(
-                new UnexpandableByteBuffer( buf.buf().duplicate(), buf ), false );
+                new UnexpandableByteBuffer( buf().duplicate(), buf ), false );
             return newBuf;
         }
 
@@ -487,7 +396,7 @@
         {
             PooledByteBuffer newBuf = allocateContainer();
             newBuf.init(
-                new UnexpandableByteBuffer( buf.buf().slice(), buf ), false );
+                new UnexpandableByteBuffer( buf().slice(), buf ), false );
             return newBuf;
         }
 
@@ -495,298 +404,22 @@
         {
             PooledByteBuffer newBuf = allocateContainer();
             newBuf.init(
-                new UnexpandableByteBuffer( buf.buf().asReadOnlyBuffer(), buf ), false );
+                new UnexpandableByteBuffer( buf().asReadOnlyBuffer(), buf ), false );
             return newBuf;
         }
         
         public byte[] array()
         {
-            return buf.buf().array();
+            return buf().array();
         }
         
         public int arrayOffset()
         {
-            return buf.buf().arrayOffset();
-        }
-
-        public byte get()
-        {
-            return buf.buf().get();
-        }
-
-        public ByteBuffer put( byte b )
-        {
-            autoExpand( 1 );
-            buf.buf().put( b );
-            return this;
-        }
-
-        public byte get( int index )
-        {
-            return buf.buf().get( index );
-        }
-
-        public ByteBuffer put( int index, byte b )
-        {
-            autoExpand( index, 1 );
-            buf.buf().put( index, b );
-            return this;
-        }
-
-        public ByteBuffer get( byte[] dst, int offset, int length )
-        {
-            buf.buf().get( dst, offset, length );
-            return this;
-        }
-
-        public ByteBuffer put( java.nio.ByteBuffer src )
-        {
-            autoExpand( src.remaining() );
-            buf.buf().put( src );
-            return this;
-        }
-
-        public ByteBuffer put( byte[] src, int offset, int length )
-        {
-            autoExpand( length );
-            buf.buf().put( src, offset, length );
-            return this;
-        }
-
-        public ByteBuffer compact()
-        {
-            buf.buf().compact();
-            return this;
-        }
-
-        public int compareTo( ByteBuffer that )
-        {
-            return this.buf.buf().compareTo( that.buf() );
-        }
-
-        public ByteOrder order()
-        {
-            return buf.buf().order();
-        }
-
-        public ByteBuffer order( ByteOrder bo )
-        {
-            buf.buf().order( bo );
-            return this;
-        }
-
-        public char getChar()
-        {
-            return buf.buf().getChar();
-        }
-
-        public ByteBuffer putChar( char value )
-        {
-            autoExpand( 2 );
-            buf.buf().putChar( value );
-            return this;
-        }
-
-        public char getChar( int index )
-        {
-            return buf.buf().getChar( index );
-        }
-
-        public ByteBuffer putChar( int index, char value )
-        {
-            autoExpand( index, 2 );
-            buf.buf().putChar( index, value );
-            return this;
+            return buf().arrayOffset();
         }
 
-        public CharBuffer asCharBuffer()
+        protected void capacity0( int requestedCapacity )
         {
-            return buf.buf().asCharBuffer();
-        }
-
-        public short getShort()
-        {
-            return buf.buf().getShort();
-        }
-
-        public ByteBuffer putShort( short value )
-        {
-            autoExpand( 2 );
-            buf.buf().putShort( value );
-            return this;
-        }
-
-        public short getShort( int index )
-        {
-            return buf.buf().getShort( index );
-        }
-
-        public ByteBuffer putShort( int index, short value )
-        {
-            autoExpand( index, 2 );
-            buf.buf().putShort( index, value );
-            return this;
-        }
-
-        public ShortBuffer asShortBuffer()
-        {
-            return buf.buf().asShortBuffer();
-        }
-
-        public int getInt()
-        {
-            return buf.buf().getInt();
-        }
-
-        public ByteBuffer putInt( int value )
-        {
-            autoExpand( 4 );
-            buf.buf().putInt( value );
-            return this;
-        }
-
-        public int getInt( int index )
-        {
-            return buf.buf().getInt( index );
-        }
-
-        public ByteBuffer putInt( int index, int value )
-        {
-            autoExpand( index, 4 );
-            buf.buf().putInt( index, value );
-            return this;
-        }
-
-        public IntBuffer asIntBuffer()
-        {
-            return buf.buf().asIntBuffer();
-        }
-
-        public long getLong()
-        {
-            return buf.buf().getLong();
-        }
-
-        public ByteBuffer putLong( long value )
-        {
-            autoExpand( 8 );
-            buf.buf().putLong( value );
-            return this;
-        }
-
-        public long getLong( int index )
-        {
-            return buf.buf().getLong( index );
-        }
-
-        public ByteBuffer putLong( int index, long value )
-        {
-            autoExpand( index, 8 );
-            buf.buf().putLong( index, value );
-            return this;
-        }
-
-        public LongBuffer asLongBuffer()
-        {
-            return buf.buf().asLongBuffer();
-        }
-
-        public float getFloat()
-        {
-            return buf.buf().getFloat();
-        }
-
-        public ByteBuffer putFloat( float value )
-        {
-            autoExpand( 4 );
-            buf.buf().putFloat( value );
-            return this;
-        }
-
-        public float getFloat( int index )
-        {
-            return buf.buf().getFloat( index );
-        }
-
-        public ByteBuffer putFloat( int index, float value )
-        {
-            autoExpand( index, 4 );
-            buf.buf().putFloat( index, value );
-            return this;
-        }
-
-        public FloatBuffer asFloatBuffer()
-        {
-            return buf.buf().asFloatBuffer();
-        }
-
-        public double getDouble()
-        {
-            return buf.buf().getDouble();
-        }
-
-        public ByteBuffer putDouble( double value )
-        {
-            autoExpand( 8 );
-            buf.buf().putDouble( value );
-            return this;
-        }
-
-        public double getDouble( int index )
-        {
-            return buf.buf().getDouble( index );
-        }
-
-        public ByteBuffer putDouble( int index, double value )
-        {
-            autoExpand( index, 8 );
-            buf.buf().putDouble( index, value );
-            return this;
-        }
-
-        public DoubleBuffer asDoubleBuffer()
-        {
-            return buf.buf().asDoubleBuffer();
-        }
-
-        public ByteBuffer expand( int expectedRemaining )
-        {
-            if( autoExpand )
-            {
-                int pos = buf.buf().position();
-                int limit = buf.buf().limit();
-                int end = pos + expectedRemaining;
-                if( end > limit )
-                {
-                    ensureCapacity( end );
-                    buf.buf().limit( end );
-                }
-            }
-            return this;
-        }
-
-        public ByteBuffer expand( int pos, int expectedRemaining )
-        {
-            if( autoExpand )
-            {
-                int limit = buf.buf().limit();
-                int end = pos + expectedRemaining;
-                if( end > limit )
-                {
-                    ensureCapacity( end );
-                    buf.buf().limit( end );
-                }
-            }
-            return this;
-        }
-
-        private void ensureCapacity( int requestedCapacity )
-        {
-            if( requestedCapacity <= buf.buf().capacity() )
-            {
-                return;
-            }
-
             if( buf.isDerived() )
             {
                 throw new IllegalStateException( "Derived buffers cannot be expanded." );
@@ -819,15 +452,8 @@
             }
 
             newBuf.buf().clear();
-            newBuf.buf().order( oldBuf.buf().order() );
-
-            int pos = oldBuf.buf().position();
-            int limit = oldBuf.buf().limit();
             oldBuf.buf().clear();
             newBuf.buf().put( oldBuf.buf() );
-            newBuf.buf().position( 0 );
-            newBuf.buf().limit( limit );
-            newBuf.buf().position( pos );
             this.buf = newBuf;
             oldBuf.release();
         }

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/common/SimpleByteBufferAllocator.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/core/src/main/java/org/apache/mina/common/SimpleByteBufferAllocator.java?rev=430683&r1=430682&r2=430683&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/common/SimpleByteBufferAllocator.java
(original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/common/SimpleByteBufferAllocator.java
Thu Aug 10 22:45:57 2006
@@ -19,12 +19,8 @@
 package org.apache.mina.common;
 
 import java.nio.ByteOrder;
-import java.nio.CharBuffer;
-import java.nio.DoubleBuffer;
-import java.nio.FloatBuffer;
-import java.nio.IntBuffer;
-import java.nio.LongBuffer;
-import java.nio.ShortBuffer;
+
+import org.apache.mina.common.support.BaseByteBuffer;
 
 /**
  * A simplistic {@link ByteBufferAllocator} which simply allocates a new
@@ -64,17 +60,15 @@
     {
     }
 
-    private static class SimpleByteBuffer extends ByteBuffer
+    private static class SimpleByteBuffer extends BaseByteBuffer
     {
         private java.nio.ByteBuffer buf;
         private int refCount = 1;
-        private boolean autoExpand;
 
         protected SimpleByteBuffer( java.nio.ByteBuffer buf )
         {
             this.buf = buf;
             buf.order( ByteOrder.BIG_ENDIAN );
-            autoExpand = false;
             refCount = 1;
         }
 
@@ -112,27 +106,6 @@
             return buf;
         }
         
-        public boolean isDirect()
-        {
-            return buf.isDirect();
-        }
-        
-        public boolean isReadOnly()
-        {
-            return buf.isReadOnly();
-        }
-        
-        public boolean isAutoExpand()
-        {
-            return autoExpand;
-        }
-        
-        public ByteBuffer setAutoExpand( boolean autoExpand )
-        {
-            this.autoExpand = autoExpand;
-            return this;
-        }
-        
         public boolean isPooled()
         {
             return false;
@@ -142,346 +115,8 @@
         {
         }
 
-        public int capacity()
-        {
-            return buf.capacity();
-        }
-        
-        public int position()
-        {
-            return buf.position();
-        }
-
-        public ByteBuffer position( int newPosition )
-        {
-            autoExpand( newPosition, 0 );
-            buf.position( newPosition );
-            return this;
-        }
-
-        public int limit()
+        protected void capacity0( int requestedCapacity )
         {
-            return buf.limit();
-        }
-
-        public ByteBuffer limit( int newLimit )
-        {
-            autoExpand( newLimit, 0 );
-            buf.limit( newLimit );
-            return this;
-        }
-
-        public ByteBuffer mark()
-        {
-            buf.mark();
-            return this;
-        }
-
-        public ByteBuffer reset()
-        {
-            buf.reset();
-            return this;
-        }
-
-        public ByteBuffer clear()
-        {
-            buf.clear();
-            return this;
-        }
-
-        public ByteBuffer flip()
-        {
-            buf.flip();
-            return this;
-        }
-
-        public ByteBuffer rewind()
-        {
-            buf.rewind();
-            return this;
-        }
-
-        public int remaining()
-        {
-            return buf.remaining();
-        }
-
-        public byte get()
-        {
-            return buf.get();
-        }
-
-        public ByteBuffer put( byte b )
-        {
-            autoExpand( 1 );
-            buf.put( b );
-            return this;
-        }
-
-        public byte get( int index )
-        {
-            return buf.get( index );
-        }
-
-        public ByteBuffer put( int index, byte b )
-        {
-            autoExpand( index, 1 );
-            buf.put( index, b );
-            return this;
-        }
-
-        public ByteBuffer get( byte[] dst, int offset, int length )
-        {
-            buf.get( dst, offset, length );
-            return this;
-        }
-
-        public ByteBuffer put( java.nio.ByteBuffer src )
-        {
-            autoExpand( src.remaining() );
-            buf.put( src );
-            return this;
-        }
-
-        public ByteBuffer put( byte[] src, int offset, int length )
-        {
-            autoExpand( length );
-            buf.put( src, offset, length );
-            return this;
-        }
-
-        public ByteBuffer compact()
-        {
-            buf.compact();
-            return this;
-        }
-
-        public int compareTo( ByteBuffer that )
-        {
-            return this.buf.compareTo( that.buf() );
-        }
-
-        public ByteOrder order()
-        {
-            return buf.order();
-        }
-
-        public ByteBuffer order( ByteOrder bo )
-        {
-            buf.order( bo );
-            return this;
-        }
-
-        public char getChar()
-        {
-            return buf.getChar();
-        }
-
-        public ByteBuffer putChar( char value )
-        {
-            autoExpand( 2 );
-            buf.putChar( value );
-            return this;
-        }
-
-        public char getChar( int index )
-        {
-            return buf.getChar( index );
-        }
-
-        public ByteBuffer putChar( int index, char value )
-        {
-            autoExpand( index, 2 );
-            buf.putChar( index, value );
-            return this;
-        }
-
-        public CharBuffer asCharBuffer()
-        {
-            return buf.asCharBuffer();
-        }
-
-        public short getShort()
-        {
-            return buf.getShort();
-        }
-
-        public ByteBuffer putShort( short value )
-        {
-            autoExpand( 2 );
-            buf.putShort( value );
-            return this;
-        }
-
-        public short getShort( int index )
-        {
-            return buf.getShort( index );
-        }
-
-        public ByteBuffer putShort( int index, short value )
-        {
-            autoExpand( index, 2 );
-            buf.putShort( index, value );
-            return this;
-        }
-
-        public ShortBuffer asShortBuffer()
-        {
-            return buf.asShortBuffer();
-        }
-
-        public int getInt()
-        {
-            return buf.getInt();
-        }
-
-        public ByteBuffer putInt( int value )
-        {
-            autoExpand( 4 );
-            buf.putInt( value );
-            return this;
-        }
-
-        public int getInt( int index )
-        {
-            return buf.getInt( index );
-        }
-
-        public ByteBuffer putInt( int index, int value )
-        {
-            autoExpand( index, 4 );
-            buf.putInt( index, value );
-            return this;
-        }
-
-        public IntBuffer asIntBuffer()
-        {
-            return buf.asIntBuffer();
-        }
-
-        public long getLong()
-        {
-            return buf.getLong();
-        }
-
-        public ByteBuffer putLong( long value )
-        {
-            autoExpand( 8 );
-            buf.putLong( value );
-            return this;
-        }
-
-        public long getLong( int index )
-        {
-            return buf.getLong( index );
-        }
-
-        public ByteBuffer putLong( int index, long value )
-        {
-            autoExpand( index, 8 );
-            buf.putLong( index, value );
-            return this;
-        }
-
-        public LongBuffer asLongBuffer()
-        {
-            return buf.asLongBuffer();
-        }
-
-        public float getFloat()
-        {
-            return buf.getFloat();
-        }
-
-        public ByteBuffer putFloat( float value )
-        {
-            autoExpand( 4 );
-            buf.putFloat( value );
-            return this;
-        }
-
-        public float getFloat( int index )
-        {
-            return buf.getFloat( index );
-        }
-
-        public ByteBuffer putFloat( int index, float value )
-        {
-            autoExpand( index, 4 );
-            buf.putFloat( index, value );
-            return this;
-        }
-
-        public FloatBuffer asFloatBuffer()
-        {
-            return buf.asFloatBuffer();
-        }
-
-        public double getDouble()
-        {
-            return buf.getDouble();
-        }
-
-        public ByteBuffer putDouble( double value )
-        {
-            autoExpand( 8 );
-            buf.putDouble( value );
-            return this;
-        }
-
-        public double getDouble( int index )
-        {
-            return buf.getDouble( index );
-        }
-
-        public ByteBuffer putDouble( int index, double value )
-        {
-            autoExpand( index, 8 );
-            buf.putDouble( index, value );
-            return this;
-        }
-
-        public DoubleBuffer asDoubleBuffer()
-        {
-            return buf.asDoubleBuffer();
-        }
-
-        public ByteBuffer expand( int expectedRemaining )
-        {
-            if( autoExpand )
-            {
-                int pos = buf.position();
-                int limit = buf.limit();
-                int end = pos + expectedRemaining;
-                if( end > limit ) {
-                    ensureCapacity( end );
-                    buf.limit( end );
-                }
-            }
-            return this;
-        }
-        
-        public ByteBuffer expand( int pos, int expectedRemaining )
-        {
-            if( autoExpand )
-            {
-                int limit = buf.limit();
-                int end = pos + expectedRemaining;
-                if( end > limit ) {
-                    ensureCapacity( end );
-                    buf.limit( end );
-                }
-            }
-            return this;
-        }
-        
-        private void ensureCapacity( int requestedCapacity )
-        {
-            if( requestedCapacity <= buf.capacity() )
-            {
-                return;
-            }
-            
             int newCapacity = MINIMUM_CAPACITY;
             while( newCapacity < requestedCapacity )
             {
@@ -500,15 +135,8 @@
             }
 
             newBuf.clear();
-            newBuf.order( oldBuf.order() );
-
-            int pos = oldBuf.position();
-            int limit = oldBuf.limit();
             oldBuf.clear();
             newBuf.put( oldBuf );
-            newBuf.position( 0 );
-            newBuf.limit( limit );
-            newBuf.position( pos );
             this.buf = newBuf;
         }
 

Added: directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/BaseByteBuffer.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/BaseByteBuffer.java?rev=430683&view=auto
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/BaseByteBuffer.java
(added)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/BaseByteBuffer.java
Thu Aug 10 22:45:57 2006
@@ -0,0 +1,435 @@
+/*
+ *   @(#) $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.common.support;
+
+import java.nio.ByteOrder;
+import java.nio.CharBuffer;
+import java.nio.DoubleBuffer;
+import java.nio.FloatBuffer;
+import java.nio.IntBuffer;
+import java.nio.LongBuffer;
+import java.nio.ShortBuffer;
+
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.common.ByteBufferAllocator;
+
+/**
+ * A base implementation of {@link ByteBuffer}.  This implementation
+ * assumes that {@link ByteBuffer#buf()} always returns a correct NIO
+ * {@link java.nio.ByteBuffer} instance.  Most implementations could
+ * extend this class and implement their own buffer management mechanism.
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ * @noinspection StaticNonFinalField
+ * @see ByteBufferAllocator
+ */
+public abstract class BaseByteBuffer extends ByteBuffer
+{
+    private boolean autoExpand;
+    
+    /**
+     * We don't have any access to Buffer.markValue(), so we need to track it down,
+     * which will cause small extra overhead.
+     */
+    private int mark = -1;
+
+    protected BaseByteBuffer()
+    {
+    }
+
+    public boolean isDirect()
+    {
+	return buf().isDirect();
+    }
+    
+    public boolean isReadOnly()
+    {
+	return buf().isReadOnly();
+    }
+
+    public int capacity()
+    {
+	return buf().capacity();
+    }
+    
+    public ByteBuffer capacity( int newCapacity )
+    {
+	if( newCapacity > capacity() )
+	{
+	    // Allocate a new buffer and transfer all settings to it.
+	    int pos = position();
+	    int limit = limit();
+	    ByteOrder bo = order();
+
+	    capacity0( newCapacity );
+	    buf().limit( limit );
+	    if( mark >= 0 )
+	    {
+		buf().position( mark );
+		buf().mark();
+	    }
+	    buf().position( pos );
+	    buf().order( bo );
+	}
+	
+	return this;
+    }
+    
+    /**
+     * Implement this method to increase the capacity of this buffer.
+     * <tt>newCapacity</tt> is always greater than the current capacity.
+     */
+    protected abstract void capacity0( int newCapacity );
+
+    public boolean isAutoExpand()
+    {
+	return autoExpand;
+    }
+
+    public ByteBuffer setAutoExpand( boolean autoExpand )
+    {
+	this.autoExpand = autoExpand;
+	return this;
+    }
+
+    public ByteBuffer expand( int pos, int expectedRemaining )
+    {
+	int end = pos + expectedRemaining;
+	if( end > capacity() )
+	{
+	    // The buffer needs expansion.
+	    capacity( end );
+	}
+	
+	if( end > limit() )
+	{
+	    // We call limit() directly to prevent StackOverflowError
+	    buf().limit( end );
+	}
+	return this;
+    }
+
+    public int position()
+    {
+	return buf().position();
+    }
+
+    public ByteBuffer position( int newPosition )
+    {
+        autoExpand( newPosition, 0 );
+	buf().position( newPosition );
+        if( mark > newPosition )
+        {
+            mark = -1;
+        }
+        return this;
+    }
+    
+    public int limit()
+    {
+	return buf().limit();
+    }
+
+    public ByteBuffer limit( int newLimit )
+    {
+        autoExpand( newLimit, 0 );
+	buf().limit( newLimit );
+        if( mark > newLimit )
+        {
+            mark = -1;
+        }
+        return this;
+    }
+    
+    public ByteBuffer mark()
+    {
+	buf().mark();
+        mark = position();
+        return this;
+    }
+    
+    public int markValue()
+    {
+	return mark;
+    }
+
+    public ByteBuffer reset()
+    {
+	buf().reset();
+	return this;
+    }
+    
+    public ByteBuffer clear()
+    {
+	buf().clear();
+	mark = -1;
+	return this;
+    }
+    
+    public ByteBuffer flip()
+    {
+	buf().flip();
+	mark = -1;
+	return this;
+    }
+    
+    public ByteBuffer rewind()
+    {
+	buf().rewind();
+	mark = -1;
+	return this;
+    }
+    
+    public byte get()
+    {
+	return buf().get();
+    }
+
+    public ByteBuffer put( byte b )
+    {
+        autoExpand( 1 );
+	buf().put( b );
+	return this;
+    }
+
+    public byte get( int index )
+    {
+	return buf().get( index );
+    }
+
+    public ByteBuffer put( int index, byte b )
+    {
+        autoExpand( index, 1 );
+	buf().put( index, b );
+	return this;
+    }
+
+    public ByteBuffer get( byte[] dst, int offset, int length )
+    {
+	buf().get( dst, offset, length );
+	return this;
+    }
+
+    public ByteBuffer put( java.nio.ByteBuffer src )
+    {
+        autoExpand( src.remaining() );
+	buf().put( src );
+	return this;
+    }
+
+    public ByteBuffer put( byte[] src, int offset, int length )
+    {
+        autoExpand( length );
+	buf().put( src, offset, length );
+	return this;
+    }
+
+    public ByteBuffer compact()
+    {
+	buf().compact();
+	mark = -1;
+	return this;
+    }
+
+    public ByteOrder order()
+    {
+	return buf().order();
+    }
+
+    public ByteBuffer order( ByteOrder bo )
+    {
+	buf().order( bo );
+	return this;
+    }
+
+    public char getChar()
+    {
+	return buf().getChar();
+    }
+
+    public ByteBuffer putChar( char value )
+    {
+        autoExpand( 2 );
+	buf().putChar( value );
+	return this;
+    }
+
+    public char getChar( int index )
+    {
+	return buf().getChar( index );
+    }
+
+    public ByteBuffer putChar( int index, char value )
+    {
+        autoExpand( index, 2 );
+	buf().putChar( index, value );
+	return this;
+    }
+
+    public CharBuffer asCharBuffer()
+    {
+	return buf().asCharBuffer();
+    }
+
+    public short getShort()
+    {
+	return buf().getShort();
+    }
+
+    public ByteBuffer putShort( short value )
+    {
+        autoExpand( 2 );
+	buf().putShort( value );
+	return this;
+    }
+
+    public short getShort( int index )
+    {
+	return buf().getShort( index );
+    }
+
+    public ByteBuffer putShort( int index, short value )
+    {
+        autoExpand( index, 2 );
+	buf().putShort( index, value );
+	return this;
+    }
+
+    public ShortBuffer asShortBuffer()
+    {
+	return buf().asShortBuffer();
+    }
+
+    public int getInt()
+    {
+	return buf().getInt();
+    }
+
+    public ByteBuffer putInt( int value )
+    {
+        autoExpand( 4 );
+	buf().putInt( value );
+	return this;
+    }
+
+    public int getInt( int index )
+    {
+	return buf().getInt( index );
+    }
+
+    public ByteBuffer putInt( int index, int value )
+    {
+        autoExpand( index, 4 );
+	buf().putInt( index, value );
+	return this;
+    }
+
+    public IntBuffer asIntBuffer()
+    {
+	return buf().asIntBuffer();
+    }
+
+    public long getLong()
+    {
+	return buf().getLong();
+    }
+
+    public ByteBuffer putLong( long value )
+    {
+        autoExpand( 8 );
+	buf().putLong( value );
+	return this;
+    }
+
+    public long getLong( int index )
+    {
+	return buf().getLong( index );
+    }
+
+    public ByteBuffer putLong( int index, long value )
+    {
+        autoExpand( index, 8 );
+	buf().putLong( index, value );
+	return this;
+    }
+
+    public LongBuffer asLongBuffer()
+    {
+	return buf().asLongBuffer();
+    }
+
+    public float getFloat()
+    {
+	return buf().getFloat();
+    }
+
+    public ByteBuffer putFloat( float value )
+    {
+        autoExpand( 4 );
+	buf().putFloat( value );
+	return this;
+    }
+
+    public float getFloat( int index )
+    {
+	return buf().getFloat( index );
+    }
+
+    public ByteBuffer putFloat( int index, float value )
+    {
+        autoExpand( index, 4 );
+	buf().putFloat( index, value );
+	return this;
+    }
+
+    public FloatBuffer asFloatBuffer()
+    {
+	return buf().asFloatBuffer();
+    }
+
+    public double getDouble()
+    {
+	return buf().getDouble();
+    }
+
+    public ByteBuffer putDouble( double value )
+    {
+        autoExpand( 8 );
+	buf().putDouble( value );
+	return this;
+    }
+
+    public double getDouble( int index )
+    {
+	return buf().getDouble( index );
+    }
+
+    public ByteBuffer putDouble( int index, double value )
+    {
+        autoExpand( index, 8 );
+	buf().putDouble( index, value );
+	return this;
+    }
+
+    public DoubleBuffer asDoubleBuffer()
+    {
+	return buf().asDoubleBuffer();
+    }
+}

Propchange: directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/BaseByteBuffer.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Modified: directory/trunks/mina/core/src/test/java/org/apache/mina/common/ByteBufferTest.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/core/src/test/java/org/apache/mina/common/ByteBufferTest.java?rev=430683&r1=430682&r2=430683&view=diff
==============================================================================
--- directory/trunks/mina/core/src/test/java/org/apache/mina/common/ByteBufferTest.java (original)
+++ directory/trunks/mina/core/src/test/java/org/apache/mina/common/ByteBufferTest.java Thu
Aug 10 22:45:57 2006
@@ -153,6 +153,26 @@
         Assert.assertEquals( 4, buf.capacity() );
     }
 
+    public void testAutoExpandMark() throws Exception
+    {
+    	ByteBuffer buf = ByteBuffer.allocate( 4 ).setAutoExpand( true );
+
+    	buf.put( ( byte ) 0 );
+    	buf.put( ( byte ) 0 );
+    	buf.put( ( byte ) 0 );
+
+    	// Position should be 3 when we reset this buffer.
+    	buf.mark();
+
+    	// Overflow it
+    	buf.put( ( byte ) 0 );
+    	buf.put( ( byte ) 0 );
+    	
+    	Assert.assertEquals( 5, buf.position() );
+    	buf.reset();
+    	Assert.assertEquals( 3, buf.position() );
+    }
+    
     public void testPooledProperty() throws Exception
     {
         ByteBuffer buf = ByteBuffer.allocate( 16 );
@@ -510,7 +530,7 @@
 
         // Test writing an object.
         buf.putObject( o );
-
+        
         // Test reading an object.
         buf.clear();
         Object o2 = buf.getObject();



Mime
View raw message