readByteData method

Future<ByteData> readByteData(
  1. int maxBytes,
  2. int bytesMultiple
)

Reads ByteData of at most maxBytes length (since the stream may not contain that many bytes), but such at the amount read is always a multiple of bytesMultiple.

Implementation

Future<ByteData> readByteData(int maxBytes, int bytesMultiple) async {
  // Try to get enough data in the cache.
  final cachedBytes = await _ensureEnoughBytesInCache(maxBytes);

  // Always read a correct multiple of bytes.
  var nrBytesToRead = min(maxBytes, cachedBytes);
  nrBytesToRead = (nrBytesToRead ~/ bytesMultiple) * bytesMultiple;

  final bytes = await readBytes(nrBytesToRead);

  final result = ByteData(nrBytesToRead);
  if (bytes != null) {
    result.buffer.asUint8List().setRange(0, nrBytesToRead, bytes);
  }

  return result;
}