{
return null;
}
else
{
throw new BufferOverflowException();
}
}
// Try to allocate the given size
final ByteBuffer byteBuffer = slab.allocate( size );
// If allocation succeed, return the buffer
if ( byteBuffer != null )
{
return byteBuffer;
}
// Otherwise we have the option to allow in a bigger slab.
if ( !allowAllocationToBiggerSlab )
{
if ( returnNullWhenNoBufferAvailable )
{
return null;
}
else
{
throw new BufferOverflowException();
}
}
else
{
// We can try to allocate to a bigger slab.
// size + 1 here because getSlabThatMatchTheSize do a size -1 and thus will return the same slab
final int biggerSize = slab.getSliceSize() + 1;
final FixedSizeByteBufferAllocatorImpl biggerSlab = getSlabThatMatchTheSize( biggerSize );
if ( biggerSlab == null )
{
// We were already trying to allocate in the biggest slab
if ( returnNullWhenNoBufferAvailable )
{
return null;
}
else
{
throw new BufferOverflowException();
}
}
final ByteBuffer secondByteBuffer = biggerSlab.allocate( size );
if ( secondByteBuffer == null )
{
if ( returnNullWhenNoBufferAvailable )
{
return null;
}
else
{
throw new BufferOverflowException();
}
}
else
{
return secondByteBuffer;