directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r161856 - in directory/network/trunk/src: java/org/apache/mina/common/ByteBuffer.java test/org/apache/mina/common/ByteBufferTest.java
Date Tue, 19 Apr 2005 08:29:29 GMT
Author: trustin
Date: Tue Apr 19 01:29:28 2005
New Revision: 161856

URL: http://svn.apache.org/viewcvs?view=rev&rev=161856
Log:
* Added ByteBuffer.isDirect() and isReadOnly();
* ByteBuffer.duplicate(), slice() and asReadOnly() returns MINA ByteBuffer now.

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=161855&r2=161856
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java Tue Apr 19 01:29:28
2005
@@ -177,7 +177,11 @@
      * Returns the underlying NIO buffer instance.
      */
     public abstract java.nio.ByteBuffer buf();
-
+    
+    public abstract boolean isDirect();
+    
+    public abstract boolean isReadOnly();
+    
     public abstract int capacity();
     
     public abstract int position();
@@ -202,11 +206,11 @@
 
     public abstract boolean hasRemaining();
 
-    public abstract java.nio.ByteBuffer slice();
+    public abstract ByteBuffer slice();
 
-    public abstract java.nio.ByteBuffer duplicate();
+    public abstract ByteBuffer duplicate();
 
-    public abstract java.nio.ByteBuffer asReadOnlyBuffer();
+    public abstract ByteBuffer asReadOnlyBuffer();
 
     public abstract byte get();
 
@@ -422,13 +426,11 @@
      */
     public abstract ByteBuffer fork( int newCapacity );
     
-    private static class DefaultByteBuffer extends ByteBuffer
+    private static abstract class BaseByteBuffer extends ByteBuffer
     {
         private final java.nio.ByteBuffer buf;
 
-        private int refCount = 1;
-        
-        private DefaultByteBuffer( java.nio.ByteBuffer buf )
+        protected BaseByteBuffer( java.nio.ByteBuffer buf )
         {
             if( buf == null )
             {
@@ -437,60 +439,19 @@
             this.buf = buf;
         }
 
-        private DefaultByteBuffer( int capacity, boolean direct )
+        public java.nio.ByteBuffer buf()
         {
-            if( direct )
-            {
-                buf = java.nio.ByteBuffer.allocateDirect( capacity );
-            }
-            else
-            {
-                buf = java.nio.ByteBuffer.allocate( capacity );
-            }
+            return buf;
         }
         
-        private synchronized void resetRefCount()
+        public boolean isDirect()
         {
-            refCount = 1;
+            return buf.isDirect();
         }
         
-        public synchronized void acquire()
+        public boolean isReadOnly()
         {
-            if( refCount <= 0 )
-            {
-                throw new IllegalStateException( "Already released buffer." );
-            }
-
-            refCount ++;
-        }
-
-        public synchronized void release()
-        {
-            if( refCount <= 0 )
-            {
-                refCount = 0;
-                throw new IllegalStateException(
-                        "Already released buffer.  You released the buffer too many times."
);
-            }
-
-            refCount --;
-            if( refCount > 0)
-            {
-                return;
-            }
-
-            Stack[] bufferStacks = buf.isDirect()? directBufferStacks : heapBufferStacks;
-            Stack stack = bufferStacks[ getBufferStackIndex( bufferStacks, buf.capacity()
) ];
-            synchronized( stack )
-            {
-                // push back
-                stack.push( this );
-            }
-        }
-
-        public java.nio.ByteBuffer buf()
-        {
-            return buf;
+            return buf.isReadOnly();
         }
 
         public int capacity()
@@ -560,19 +521,19 @@
             return buf.hasRemaining();
         }
 
-        public java.nio.ByteBuffer slice()
+        public ByteBuffer slice()
         {
-            return buf.slice();
+            return new DuplicateByteBuffer( this, buf.slice());
         }
 
-        public java.nio.ByteBuffer duplicate()
+        public ByteBuffer duplicate()
         {
-            return buf.duplicate();
+            return new DuplicateByteBuffer( this, buf.duplicate() );
         }
 
-        public java.nio.ByteBuffer asReadOnlyBuffer()
+        public ByteBuffer asReadOnlyBuffer()
         {
-            return buf.asReadOnlyBuffer();
+            return new DuplicateByteBuffer( this, buf.asReadOnlyBuffer() );
         }
 
         public byte get()
@@ -1144,6 +1105,80 @@
                 throw new IllegalArgumentException(
                         "fieldSize cannot be negative: " + fieldSize );
             }
+        }
+    }
+
+    private static class DefaultByteBuffer extends BaseByteBuffer
+    {
+        private int refCount = 1;
+        
+        protected DefaultByteBuffer( java.nio.ByteBuffer buf )
+        {
+            super( buf );
+        }
+
+        protected DefaultByteBuffer( int capacity, boolean direct )
+        {
+            super( direct? java.nio.ByteBuffer.allocateDirect( capacity ) :
+                           java.nio.ByteBuffer.allocate( capacity ) );
+        }
+        
+        private synchronized void resetRefCount()
+        {
+            refCount = 1;
+        }
+        
+        public synchronized void acquire()
+        {
+            if( refCount <= 0 )
+            {
+                throw new IllegalStateException( "Already released buffer." );
+            }
+
+            refCount ++;
+        }
+
+        public synchronized void release()
+        {
+            if( refCount <= 0 )
+            {
+                refCount = 0;
+                throw new IllegalStateException(
+                        "Already released buffer.  You released the buffer too many times."
);
+            }
+
+            refCount --;
+            if( refCount > 0)
+            {
+                return;
+            }
+
+            Stack[] bufferStacks = isDirect()? directBufferStacks : heapBufferStacks;
+            Stack stack = bufferStacks[ getBufferStackIndex( bufferStacks, capacity() ) ];
+            synchronized( stack )
+            {
+                // push back
+                stack.push( this );
+            }
+        }
+    }
+    
+    private static class DuplicateByteBuffer extends BaseByteBuffer
+    {
+        private ByteBuffer buf;
+
+        private DuplicateByteBuffer( ByteBuffer buf, java.nio.ByteBuffer duplicateBuf )
+        {
+            super( duplicateBuf );
+            this.buf = buf;
+        }
+
+        public void acquire() {
+            buf.acquire();
+        }
+
+        public void release() {
+            buf.release();
         }
     }
 }

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=161855&r2=161856
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java (original)
+++ directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java Tue Apr 19
01:29:28 2005
@@ -185,4 +185,16 @@
             Assert.assertEquals( buf.get( i ), newBuf.get( i ) );
         }
     }
+    
+    public void testDuplication()
+    {
+        ByteBuffer buf = ByteBuffer.allocate( 16 );
+        buf.fillAndReset( buf.remaining() );
+
+        ByteBuffer buf2 = buf.duplicate();
+        buf.putInt( 1234 );
+        Assert.assertEquals( 4, buf.position() );
+        Assert.assertEquals( 0, buf2.position() );
+        Assert.assertEquals( 1234, buf2.getInt() );
+    }
 }



Mime
View raw message