directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r161900 - directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.java
Date Tue, 19 Apr 2005 14:45:32 GMT
Author: trustin
Date: Tue Apr 19 07:45:31 2005
New Revision: 161900

URL: http://svn.apache.org/viewcvs?view=rev&rev=161900
Log:
* Separated nio buffer pool and mina buffer pool
* Reformatted ByteBufferProxy

Modified:
    directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java
    directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.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=161899&r2=161900
==============================================================================
--- 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 07:45:31
2005
@@ -71,6 +71,8 @@
 {
     private static final int MINIMUM_CAPACITY = 1;
 
+    private static final Stack containerStack = new Stack();
+
     private static final Stack[] heapBufferStacks = new Stack[] {
             new Stack(), new Stack(), new Stack(), new Stack(),
             new Stack(), new Stack(), new Stack(), new Stack(),
@@ -122,32 +124,64 @@
      */
     public static ByteBuffer allocate( int capacity, boolean direct )
     {
+        java.nio.ByteBuffer nioBuffer = allocate0( capacity, direct );
+        DefaultByteBuffer buf;
+        
+        synchronized( containerStack )
+        {
+            buf = ( DefaultByteBuffer ) containerStack.pop();
+        }
+        
+        if( buf == null )
+        {
+            buf = new DefaultByteBuffer();
+        }
+
+        buf.init( nioBuffer );
+        return buf;
+    }
+    
+    private static java.nio.ByteBuffer allocate0( int capacity, boolean direct )
+    {
         Stack[] bufferStacks = direct? directBufferStacks : heapBufferStacks;
         int idx = getBufferStackIndex( bufferStacks, capacity );
         Stack stack = bufferStacks[ idx ];
 
-        DefaultByteBuffer buf;
+        java.nio.ByteBuffer buf;
         synchronized( stack )
         {
-            buf = ( DefaultByteBuffer ) stack.pop();
-            if( buf == null )
-            {
-                buf = new DefaultByteBuffer( MINIMUM_CAPACITY << idx, direct );
-            }
+            buf = ( java.nio.ByteBuffer ) stack.pop();
         }
 
+        if( buf == null )
+        {
+            buf = direct ? java.nio.ByteBuffer.allocateDirect( MINIMUM_CAPACITY <<
idx ) :
+                           java.nio.ByteBuffer.allocate( MINIMUM_CAPACITY << idx );
+        }
+        
         buf.clear();
-        buf.init();
-
         return buf;
     }
     
+    private static void release0( java.nio.ByteBuffer buf )
+    {
+        Stack[] bufferStacks = buf.isDirect()? directBufferStacks : heapBufferStacks;
+        Stack stack = bufferStacks[ getBufferStackIndex( bufferStacks, buf.capacity() ) ];
+        synchronized( stack )
+        {
+            // push back
+            stack.push( buf );
+        }
+    }
+    
     /**
      * Wraps the specified NIO {@link java.nio.ByteBuffer} into MINA buffer.
      */
     public static ByteBuffer wrap( java.nio.ByteBuffer nioBuffer )
     {
-        return new DefaultByteBuffer( nioBuffer );
+        DefaultByteBuffer buf = new DefaultByteBuffer();
+        buf.init( nioBuffer );
+        return buf;
     }
     
     private static int getBufferStackIndex( Stack[] bufferStacks, int size )
@@ -421,23 +455,13 @@
         private int refCount = 1;
         private boolean autoExpand;
 
-        protected DefaultByteBuffer( java.nio.ByteBuffer buf )
-        {
-            if( buf == null )
-            {
-                throw new NullPointerException( "buf" );
-            }
-            this.buf = buf;
-        }
-
-        protected DefaultByteBuffer( int capacity, boolean direct )
+        protected DefaultByteBuffer()
         {
-            this( direct? java.nio.ByteBuffer.allocateDirect( capacity ) :
-                          java.nio.ByteBuffer.allocate( capacity ) );
         }
 
-        private synchronized void init()
+        private synchronized void init( java.nio.ByteBuffer buf )
         {
+            this.buf = buf;
             autoExpand = false;
             refCount = 1;
         }
@@ -452,30 +476,31 @@
             refCount ++;
         }
 
-        public synchronized void release()
+        public void release()
         {
-            if( refCount <= 0 )
+            synchronized( this )
             {
-                refCount = 0;
-                throw new IllegalStateException(
-                        "Already released buffer.  You released the buffer too many times."
);
-            }
+                if( refCount <= 0 )
+                {
+                    refCount = 0;
+                    throw new IllegalStateException(
+                            "Already released buffer.  You released the buffer too many times."
);
+                }
 
-            refCount --;
-            if( refCount > 0)
-            {
-                return;
+                refCount --;
+                if( refCount > 0)
+                {
+                    return;
+                }
             }
 
-            Stack[] bufferStacks = isDirect()? directBufferStacks : heapBufferStacks;
-            Stack stack = bufferStacks[ getBufferStackIndex( bufferStacks, capacity() ) ];
-            synchronized( stack )
+            release0( buf );
+            synchronized( containerStack )
             {
-                // push back
-                stack.push( this );
+                containerStack.push( this );
             }
         }
-        
+
         public java.nio.ByteBuffer buf()
         {
             return buf;
@@ -1349,7 +1374,7 @@
             newBuf.limit( limit );
             newBuf.position( pos );
             this.buf = newBuf;
-            new DefaultByteBuffer( oldBuf ).release();
+            release0( oldBuf );
         }
         
         private static void checkFieldSize( int fieldSize )

Modified: directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.java?view=diff&r1=161899&r2=161900
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.java Tue Apr 19
07:45:31 2005
@@ -39,13 +39,14 @@
  * @author Trustin Lee (trustin@apache.org)
  * @version $Rev$, $Date$
  */
-public class ByteBufferProxy extends ByteBuffer {
+public class ByteBufferProxy extends ByteBuffer
+{
 
     /**
      * The buffer proxied by this proxy.
      */
     protected ByteBuffer buf;
-    
+
     /**
      * Create a new instance.
      * @param buf the buffer to be proxied
@@ -59,368 +60,449 @@
         this.buf = buf;
     }
 
-    public void acquire() {
+    public void acquire()
+    {
         buf.acquire();
     }
 
-    public void release() {
+    public void release()
+    {
         buf.release();
     }
-    
+
     public boolean isDirect()
     {
         return buf.isDirect();
     }
-    
-    public java.nio.ByteBuffer buf() {
+
+    public java.nio.ByteBuffer buf()
+    {
         return buf.buf();
     }
 
-    public int capacity() {
+    public int capacity()
+    {
         return buf.capacity();
     }
 
-    public int position() {
+    public int position()
+    {
         return buf.position();
     }
 
-    public ByteBuffer position(int newPosition) {
+    public ByteBuffer position( int newPosition )
+    {
         buf.position( newPosition );
         return this;
     }
 
-    public int limit() {
+    public int limit()
+    {
         return buf.limit();
     }
 
-    public ByteBuffer limit(int newLimit) {
+    public ByteBuffer limit( int newLimit )
+    {
         buf.limit( newLimit );
         return this;
     }
 
-    public ByteBuffer mark() {
+    public ByteBuffer mark()
+    {
         buf.mark();
         return this;
     }
 
-    public ByteBuffer reset() {
+    public ByteBuffer reset()
+    {
         buf.reset();
         return this;
     }
 
-    public ByteBuffer clear() {
+    public ByteBuffer clear()
+    {
         buf.clear();
         return this;
     }
 
-    public ByteBuffer flip() {
+    public ByteBuffer flip()
+    {
         buf.flip();
         return this;
     }
 
-    public ByteBuffer rewind() {
+    public ByteBuffer rewind()
+    {
         buf.rewind();
         return this;
     }
 
-    public int remaining() {
+    public int remaining()
+    {
         return buf.remaining();
     }
 
-    public boolean hasRemaining() {
+    public boolean hasRemaining()
+    {
         return buf.hasRemaining();
     }
 
-    public byte get() {
+    public byte get()
+    {
         return buf.get();
     }
 
-    public short getUnsigned() {
+    public short getUnsigned()
+    {
         return buf.getUnsigned();
     }
 
-    public ByteBuffer put(byte b) {
+    public ByteBuffer put( byte b )
+    {
         buf.put( b );
         return this;
     }
 
-    public byte get(int index) {
+    public byte get( int index )
+    {
         return buf.get( index );
     }
 
-    public short getUnsigned(int index) {
+    public short getUnsigned( int index )
+    {
         return buf.getUnsigned( index );
     }
 
-    public ByteBuffer put(int index, byte b) {
+    public ByteBuffer put( int index, byte b )
+    {
         buf.put( index, b );
         return this;
     }
 
-    public ByteBuffer get(byte[] dst, int offset,
-            int length) {
+    public ByteBuffer get( byte[] dst, int offset, int length )
+    {
         buf.get( dst, offset, length );
         return this;
     }
 
-    public ByteBuffer get(byte[] dst) {
+    public ByteBuffer get( byte[] dst )
+    {
         buf.get( dst );
         return this;
     }
 
-    public ByteBuffer put(ByteBuffer src) {
+    public ByteBuffer put( ByteBuffer src )
+    {
         buf.put( src );
         return this;
     }
 
-    public ByteBuffer put(java.nio.ByteBuffer src) {
+    public ByteBuffer put( java.nio.ByteBuffer src )
+    {
         buf.put( src );
         return this;
     }
 
-    public ByteBuffer put(byte[] src, int offset,
-            int length) {
+    public ByteBuffer put( byte[] src, int offset, int length )
+    {
         buf.put( src, offset, length );
         return this;
     }
 
-    public ByteBuffer put(byte[] src) {
+    public ByteBuffer put( byte[] src )
+    {
         buf.put( src );
         return this;
     }
 
-    public ByteBuffer compact() {
+    public ByteBuffer compact()
+    {
         buf.compact();
         return this;
     }
 
-    public String toString() {
+    public String toString()
+    {
         return buf.toString();
     }
 
-    public int hashCode() {
+    public int hashCode()
+    {
         return buf.hashCode();
     }
 
-    public boolean equals(Object ob) {
+    public boolean equals( Object ob )
+    {
         return buf.equals( ob );
     }
 
-    public int compareTo(ByteBuffer that) {
+    public int compareTo( ByteBuffer that )
+    {
         return buf.compareTo( that );
     }
 
-    public ByteOrder order() {
+    public ByteOrder order()
+    {
         return buf.order();
     }
 
-    public ByteBuffer order(ByteOrder bo) {
+    public ByteBuffer order( ByteOrder bo )
+    {
         buf.order( bo );
         return this;
     }
 
-    public char getChar() {
+    public char getChar()
+    {
         return buf.getChar();
     }
 
-    public ByteBuffer putChar(char value) {
+    public ByteBuffer putChar( char value )
+    {
         buf.putChar( value );
         return this;
     }
 
-    public char getChar(int index) {
+    public char getChar( int index )
+    {
         return buf.getChar( index );
     }
 
-    public ByteBuffer putChar(int index, char value) {
+    public ByteBuffer putChar( int index, char value )
+    {
         buf.putChar( index, value );
         return this;
     }
 
-    public CharBuffer asCharBuffer() {
+    public CharBuffer asCharBuffer()
+    {
         return buf.asCharBuffer();
     }
 
-    public short getShort() {
+    public short getShort()
+    {
         return buf.getShort();
     }
 
-    public int getUnsignedShort() {
+    public int getUnsignedShort()
+    {
         return buf.getUnsignedShort();
     }
 
-    public ByteBuffer putShort(short value) {
+    public ByteBuffer putShort( short value )
+    {
         buf.putShort( value );
         return this;
     }
 
-    public short getShort(int index) {
+    public short getShort( int index )
+    {
         return buf.getShort( index );
     }
 
-    public int getUnsignedShort(int index) {
+    public int getUnsignedShort( int index )
+    {
         return buf.getUnsignedShort( index );
     }
 
-    public ByteBuffer putShort(int index, short value) {
+    public ByteBuffer putShort( int index, short value )
+    {
         buf.putShort( index, value );
         return this;
     }
 
-    public ShortBuffer asShortBuffer() {
+    public ShortBuffer asShortBuffer()
+    {
         return buf.asShortBuffer();
     }
 
-    public int getInt() {
+    public int getInt()
+    {
         return buf.getInt();
     }
 
-    public long getUnsignedInt() {
+    public long getUnsignedInt()
+    {
         return buf.getUnsignedInt();
     }
 
-    public ByteBuffer putInt(int value) {
+    public ByteBuffer putInt( int value )
+    {
         buf.putInt( value );
         return this;
     }
 
-    public int getInt(int index) {
+    public int getInt( int index )
+    {
         return buf.getInt( index );
     }
 
-    public long getUnsignedInt(int index) {
+    public long getUnsignedInt( int index )
+    {
         return buf.getUnsignedInt( index );
     }
 
-    public ByteBuffer putInt(int index, int value) {
+    public ByteBuffer putInt( int index, int value )
+    {
         buf.putInt( index, value );
         return this;
     }
 
-    public IntBuffer asIntBuffer() {
+    public IntBuffer asIntBuffer()
+    {
         return buf.asIntBuffer();
     }
 
-    public long getLong() {
+    public long getLong()
+    {
         return buf.getLong();
     }
 
-    public ByteBuffer putLong(long value) {
+    public ByteBuffer putLong( long value )
+    {
         buf.putLong( value );
         return this;
     }
 
-    public long getLong(int index) {
+    public long getLong( int index )
+    {
         return buf.getLong( index );
     }
 
-    public ByteBuffer putLong(int index, long value) {
+    public ByteBuffer putLong( int index, long value )
+    {
         buf.putLong( index, value );
         return this;
     }
 
-    public LongBuffer asLongBuffer() {
+    public LongBuffer asLongBuffer()
+    {
         return buf.asLongBuffer();
     }
 
-    public float getFloat() {
+    public float getFloat()
+    {
         return buf.getFloat();
     }
 
-    public ByteBuffer putFloat(float value) {
+    public ByteBuffer putFloat( float value )
+    {
         buf.putFloat( value );
         return this;
     }
 
-    public float getFloat(int index) {
+    public float getFloat( int index )
+    {
         return buf.getFloat( index );
     }
 
-    public ByteBuffer putFloat(int index, float value) {
+    public ByteBuffer putFloat( int index, float value )
+    {
         buf.putFloat( index, value );
         return this;
     }
 
-    public FloatBuffer asFloatBuffer() {
+    public FloatBuffer asFloatBuffer()
+    {
         return buf.asFloatBuffer();
     }
 
-    public double getDouble() {
+    public double getDouble()
+    {
         return buf.getDouble();
     }
 
-    public ByteBuffer putDouble(double value) {
+    public ByteBuffer putDouble( double value )
+    {
         buf.putDouble( value );
         return this;
     }
 
-    public double getDouble(int index) {
+    public double getDouble( int index )
+    {
         return buf.getDouble( index );
     }
 
-    public ByteBuffer putDouble(int index, double value) {
+    public ByteBuffer putDouble( int index, double value )
+    {
         buf.putDouble( index, value );
         return this;
     }
 
-    public DoubleBuffer asDoubleBuffer() {
+    public DoubleBuffer asDoubleBuffer()
+    {
         return buf.asDoubleBuffer();
     }
 
-    public String getHexDump() {
+    public String getHexDump()
+    {
         return buf.getHexDump();
     }
 
-    public String getString(int fieldSize, CharsetDecoder decoder) throws CharacterCodingException
{
+    public String getString( int fieldSize, CharsetDecoder decoder )
+            throws CharacterCodingException
+    {
         return buf.getString( fieldSize, decoder );
     }
 
-    public String getString(CharsetDecoder decoder) throws CharacterCodingException {
+    public String getString( CharsetDecoder decoder )
+            throws CharacterCodingException
+    {
         return buf.getString( decoder );
     }
 
-    public ByteBuffer putString(
-            CharSequence in, int fieldSize, CharsetEncoder encoder) throws CharacterCodingException
{
+    public ByteBuffer putString( CharSequence in, int fieldSize,
+                                CharsetEncoder encoder )
+            throws CharacterCodingException
+    {
         buf.putString( in, fieldSize, encoder );
         return this;
     }
 
-    public ByteBuffer putString(
-            CharSequence in, CharsetEncoder encoder) throws CharacterCodingException {
+    public ByteBuffer putString( CharSequence in, CharsetEncoder encoder )
+            throws CharacterCodingException
+    {
         buf.putString( in, encoder );
         return this;
     }
 
-    public ByteBuffer skip(int size) {
+    public ByteBuffer skip( int size )
+    {
         buf.skip( size );
         return this;
     }
 
-    public ByteBuffer fill(byte value, int size) {
+    public ByteBuffer fill( byte value, int size )
+    {
         buf.fill( value, size );
         return this;
     }
 
-    public ByteBuffer fillAndReset(byte value, int size) {
+    public ByteBuffer fillAndReset( byte value, int size )
+    {
         buf.fillAndReset( value, size );
         return this;
     }
 
-    public ByteBuffer fill(int size) {
+    public ByteBuffer fill( int size )
+    {
         buf.fill( size );
         return this;
     }
 
-    public ByteBuffer fillAndReset(int size) {
+    public ByteBuffer fillAndReset( int size )
+    {
         buf.fillAndReset( size );
         return this;
     }
 
-    public boolean isAutoExpand() {
+    public boolean isAutoExpand()
+    {
         return buf.isAutoExpand();
     }
 
-    public ByteBuffer setAutoExpand(boolean autoExpand) {
+    public ByteBuffer setAutoExpand( boolean autoExpand )
+    {
         buf.setAutoExpand( autoExpand );
         return this;
     }



Mime
View raw message