res = getSliceBuffer( _allocator, _length );
}else{
ByteBuffer buff = null;
Integer reqVal = new Integer(_length);
//loop through the buffer pools to find a buffer big enough
Iterator it = buffersMap.keySet().iterator();
while (it.hasNext()) {
Integer keyVal = (Integer)it.next();
// check if the buffers in this pool are big enough
if ( reqVal.compareTo(keyVal) <= 0 ){
ArrayList bufferPool = (ArrayList)buffersMap.get(keyVal);
while( true ){
synchronized ( poolsLock ) {
// make sure we don't remove a buffer when running compaction
// if there are no free buffers in the pool, create a new one.
// otherwise use one from the pool
if ( bufferPool.isEmpty()){
buff = allocateNewBuffer(keyVal.intValue());
if ( buff == null ){
Debug.out( "allocateNewBuffer for " + _length + " returned null" );
}
break;
}else{
synchronized ( bufferPool ) {
buff = (ByteBuffer)bufferPool.remove(bufferPool.size() - 1);
}
if ( buff == null ){
Debug.out( "buffer pool for " + _length + " contained null entry" );
}else{
break;
}
}
}
}
break;
}
}
if ( buff == null ){
String str = "Unable to find an appropriate buffer pool for " + _length;
Debug.out( str );
throw( new RuntimeException( str ));
}
res = new DirectByteBuffer( _allocator, buff, this );
}
// clear doesn't actually zero the data, it just sets pos to 0 etc.
ByteBuffer buff = res.getBufferInternal();
buff.clear(); //scrub the buffer
buff.limit( _length );
bytesOut += buff.capacity();
if ( DEBUG_PRINT_MEM || DEBUG_TRACK_HANDEDOUT ){
synchronized( handed_out ){