dev@grizzly.java.net

how to optimize the ByteBuffer memory ?

From: Survivant 00 <survivant00_at_gmail.com>
Date: Fri, 14 Nov 2008 08:40:45 -0500

I have to snippet of codes. I,m looking for a way to optimize the
ByteBuffer realocation if possible.


*Part1 in ProtocolParser*

// Check if buffer is full
                if (processingBuffer.remaining() ==
processingBuffer.capacity()) {
                    // If full - reallocate

                    // but check if the max length is attain
                    if(processingBuffer.capacity() +
processingBuffer.remaining()<LIMITBB){
                        ByteBuffer newBB = ByteBufferFactory.allocateView(
                                processingBuffer.capacity() * 2,
                                processingBuffer.isDirect());
                        newBB.put(processingBuffer);
                        processingBuffer = newBB;
                        WorkerThread workerThread = (WorkerThread)
Thread.currentThread();
                        workerThread.setByteBuffer(processingBuffer);
                    } else {

                        s_logger.info("BUFFER max length was reached");

                        processingBuffer.clear();

                        maxBufferReached = true;

                        return maxBufferReached;
                    }
                }

*Part 2 sending data to the clients*

public class Client ....

   public void sendToClient(StringBuffer sb) {

        ByteBuffer writeBuffer =
ByteBuffer.allocateDirect(sb.toString().getBytes().length);

        writeBuffer.put(sb.toString().getBytes());

        writeBuffer.flip();

        ......
    }

}