nextBytes method

  1. @override
List<int> nextBytes(
  1. int length
)
override

Returns length bytes from the RNG

Implementation

@override
List<int> nextBytes(int length) {
  final out = List<int>.filled(length, 0);
  int written = 0;

  // Internally, always consume a multiple of 4 bytes
  final fil =
      length + ((4 - (length % 4)) % 4); // round up to nearest multiple of 4

  while (written < fil) {
    if (_bufferOffset >= _buffer.length) {
      _refill();
    }

    final remainingInBuffer = _buffer.length - _bufferOffset;
    final take = remainingInBuffer.clamp(0, fil - written);

    // Only copy bytes that fit into output array
    final toCopy = (written + take <= length) ? take : (length - written);
    if (toCopy > 0) {
      out.setRange(written, written + toCopy, _buffer, _bufferOffset);
    }

    _bufferOffset += take;
    written += take;
  }

  return out;
}