ensureCapacity method

LByteBuffer ensureCapacity(
  1. LByteBuffer buffer,
  2. int size
)

Implementation

LByteBuffer ensureCapacity(LByteBuffer buffer, int size) {
  // This sucks if you accidentally pass is a MemoryMappedBuffer of size
  // 80M
  // like I did while messing around, within moments I had 1 gig of
  // swap...
  // TODO check this
  // if (((Buffer) buffer).isReadOnly() ) {
  //     return buffer;
  // }

  int limit = buffer.limit;
  while (limit < size) {
    limit *= 2;
  }
  if (limit != buffer.limit) {
    // clean up the old buffer and allocate a new one
    buffer = LByteBuffer(limit, doSigned: true);
  }
  return buffer;
}