Author: proyal Date: Fri Apr 28 13:57:56 2006 New Revision: 398004 URL: http://svn.apache.org/viewcvs?rev=398004&view=rev Log: Allow user control over whether or not direct buffers are used Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/common/ByteBuffer.java Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/common/ByteBuffer.java URL: http://svn.apache.org/viewcvs/directory/trunks/mina/core/src/main/java/org/apache/mina/common/ByteBuffer.java?rev=398004&r1=398003&r2=398004&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 Fri Apr 28 13:57:56 2006 @@ -150,11 +150,14 @@ * @version $Rev$, $Date$ * * @see ByteBufferAllocator + * @noinspection StaticNonFinalField */ public abstract class ByteBuffer implements Comparable { private static ByteBufferAllocator allocator = new PooledByteBufferAllocator(); + private static boolean useDirectBuffers = true; + /** * Returns the current allocator which manages the allocated buffers. */ @@ -183,6 +186,16 @@ } } + public static boolean isUseDirectBuffers() + { + return useDirectBuffers; + } + + public static void setUseDirectBuffers( boolean useDirectBuffers ) + { + ByteBuffer.useDirectBuffers = useDirectBuffers; + } + /** * Returns the direct or heap buffer which is capable of the specified * size. This method tries to allocate direct buffer first, and then @@ -193,16 +206,20 @@ */ public static ByteBuffer allocate( int capacity ) { - try + if( useDirectBuffers ) { - // first try to allocate direct buffer - return allocate( capacity, true ); - } - catch( OutOfMemoryError e ) - { - // if failed, try heap - return allocate( capacity, false ); + try + { + // first try to allocate direct buffer + return allocate( capacity, true ); + } + catch( OutOfMemoryError e ) + { + // fall through to heap buffer + } } + + return allocate( capacity, false ); } /**