You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by tr...@apache.org on 2005/04/15 12:24:50 UTC

svn commit: r161433 - in directory/network/trunk/src: java/org/apache/mina/common/ByteBuffer.java test/org/apache/mina/common/ByteBufferTest.java

Author: trustin
Date: Fri Apr 15 03:24:49 2005
New Revision: 161433

URL: http://svn.apache.org/viewcvs?view=rev&rev=161433
Log:
Added ByteBuffer.fork() that could be used to duplicate or resize buffers.

Modified:
    directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java
    directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java

Modified: directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java?view=diff&r1=161432&r2=161433
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java Fri Apr 15 03:24:49 2005
@@ -75,13 +75,25 @@
             new Stack(), new Stack(), new Stack(), new Stack(), };
     
     /**
-     * Returns the direct buffer which is capable of the specified size.
+     * Returns the direct or heap buffer which is capable of the specified
+     * size.  This method tries to allocate direct buffer first, and then
+     * tries heap buffer if direct buffer memory is exhausted.  Please use
+     * {@link #allocate(int, boolean)} to allocate buffers of specific type.
      * 
      * @param capacity the capacity of the buffer
      */
     public static ByteBuffer allocate( int capacity )
     {
-        return allocate( capacity, true );
+        try
+        {
+            // first try to allocate direct buffer
+            return allocate( capacity, true );
+        }
+        catch( OutOfMemoryError e )
+        {
+            // if failed, try heap
+            return allocate( capacity, false );
+        }
     }
     
     /**
@@ -227,7 +239,7 @@
     {
         return buf.capacity();
     }
-
+    
     public int position()
     {
         return buf.position();
@@ -914,6 +926,38 @@
         }
 
         return this;
+    }
+    
+    /**
+     * Allocates and returns a new {@link ByteBuffer} whose content, position,
+     * limit, and capacity is identical.  
+     */
+    public ByteBuffer fork()
+    {
+        return fork( this.capacity() );
+    }
+    
+    /**
+     * Allocates and returns a new {@link ByteBuffer} whose content, position,
+     * and limit except capacity is identical.  New capacity can be both greater
+     * and less than original capacity.  If limit or position is less than
+     * new capacity, they will become same with new capacity.
+     */
+    public ByteBuffer fork( int newCapacity )
+    {
+        ByteBuffer buf = allocate( newCapacity );
+        int pos = this.position();
+        int limit = this.limit();
+        this.position( 0 );
+        this.limit( newCapacity < this.capacity()? newCapacity : this.capacity() );
+        buf.put( this );
+        buf.position( pos < newCapacity? pos : newCapacity );
+        buf.limit( limit < newCapacity? limit : newCapacity );
+        this.limit( this.capacity() );
+        this.position( pos );
+        this.limit( limit );
+        
+        return buf;
     }
 
     private static void checkFieldSize( int fieldSize )

Modified: directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java?view=diff&r1=161432&r2=161433
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java (original)
+++ directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java Fri Apr 15 03:24:49 2005
@@ -102,4 +102,87 @@
         {
         }
     }
+    
+    public void testFork() throws Exception
+    {
+        ByteBuffer buf = ByteBuffer.allocate( 16 );
+        ByteBuffer newBuf;
+        
+        // initialize buf
+        for( int i = 0; i < 16; i ++ )
+        {
+            buf.put( i, (byte) i );
+        }
+
+        // without capacity
+        buf.position( 4 );
+        buf.limit( 8 );
+        newBuf = buf.fork();
+        Assert.assertEquals( 4, buf.position() );
+        Assert.assertEquals( 8, buf.limit() );
+        Assert.assertEquals( buf.position(), newBuf.position() );
+        Assert.assertEquals( buf.limit(), newBuf.limit() );
+        buf.limit( buf.capacity() );
+        newBuf.limit( newBuf.capacity() );
+        
+        // with larger capacity
+        buf.position( 4 );
+        buf.limit( 8 );
+        newBuf = buf.fork( 18 );
+        Assert.assertEquals( 4, buf.position() );
+        Assert.assertEquals( 8, buf.limit() );
+        Assert.assertEquals( buf.position(), newBuf.position() );
+        Assert.assertEquals( buf.limit(), newBuf.limit() );
+        buf.limit( buf.capacity() );
+        newBuf.limit( newBuf.capacity() );
+        for( int i = 0; i < 16; i ++ )
+        {
+            Assert.assertEquals( buf.get( i ), newBuf.get( i ) );
+        }
+        
+        // with smaller capacity
+        buf.position( 4 );
+        buf.limit( 8 );
+        newBuf = buf.fork( 12 );
+        Assert.assertEquals( 4, buf.position() );
+        Assert.assertEquals( 8, buf.limit() );
+        Assert.assertEquals( buf.position(), newBuf.position() );
+        Assert.assertEquals( buf.limit(), newBuf.limit() );
+        buf.limit( buf.capacity() );
+        newBuf.limit( newBuf.capacity() );
+        for( int i = 0; i < 12; i ++ )
+        {
+            Assert.assertEquals( buf.get( i ), newBuf.get( i ) );
+        }
+        
+        // with more smaller capacity
+        buf.position( 4 );
+        buf.limit( 8 );
+        newBuf = buf.fork( 6 );
+        Assert.assertEquals( 4, buf.position() );
+        Assert.assertEquals( 8, buf.limit() );
+        Assert.assertEquals( buf.position(), newBuf.position() );
+        Assert.assertEquals( 6, newBuf.limit() );
+        buf.limit( buf.capacity() );
+        newBuf.limit( newBuf.capacity() );
+        for( int i = 0; i < 6; i ++ )
+        {
+            Assert.assertEquals( buf.get( i ), newBuf.get( i ) );
+        }
+        
+        // with smallest capacity
+        buf.position( 4 );
+        buf.limit( 8 );
+        newBuf = buf.fork( 2 );
+        Assert.assertEquals( 4, buf.position() );
+        Assert.assertEquals( 8, buf.limit() );
+        Assert.assertEquals( 2, newBuf.position() );
+        Assert.assertEquals( 2, newBuf.limit() );
+        buf.limit( buf.capacity() );
+        newBuf.limit( newBuf.capacity() );
+        for( int i = 0; i < 2; i ++ )
+        {
+            Assert.assertEquals( buf.get( i ), newBuf.get( i ) );
+        }
+    }
 }