ensureCapacity method

  1. @override
void ensureCapacity(
  1. int needed
)
override

Ensures that capacity can store the needed bytes.

Implementation

@override
void ensureCapacity(int needed) {
  if (_capacity < needed) {
    final prevCapacity = _capacity;
    final newCapacity = math.max(prevCapacity * 2, needed);

    var extra = newCapacity - prevCapacity;

    if (extra <= 1024 * 32) {
      final bsZero = Uint8List(extra);
      _ioSetPosition(prevCapacity);
      _ioWriteBytes(bsZero, 0, extra);
    } else {
      _io.truncateSync(newCapacity);

      final bsSz = 1024 * 32;
      final bsZero = Uint8List(bsSz);

      _ioSetPosition(prevCapacity);

      for (var p = prevCapacity; p < newCapacity;) {
        var lng = newCapacity - p;
        if (lng > bsSz) lng = bsSz;
        _ioWriteBytes(bsZero, 0, lng);
        p += lng;
      }
    }

    _ioSyncPosition();

    _ioCapacity = _capacity = newCapacity;
    assert(_ioCapacity == _io.lengthSync());
  }
}