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();
......
}
}