readStream method

  1. @override
Stream<List<int>> readStream(
  1. String key
)
override

Read as a byte stream. Primary read path.

Implementation

@override
Stream<List<int>> readStream(String key) async* {
  checkNotDisposed();
  final info = await _inner.head(key);
  if (info == null) throw FileNotFoundError(key);

  if (info.metadata[_metaEncrypted] != 'true') {
    yield* _inner.readStream(key);
    return;
  }

  final encKey = await _keyResolver?.resolveKey(key);
  if (encKey == null) throw EncryptionKeyMissingError(key);

  final g = await _resolveGeometry(key, info);
  if (g.chunkCount == 0 || g.originalSize == 0) return;

  // Stream decrypt chunk by chunk
  for (var i = 0; i < g.chunkCount; i++) {
    final isLastChunk = i == g.chunkCount - 1;
    final dataSize = isLastChunk
        ? g.originalSize - (i * g.chunkSize)
        : g.chunkSize;
    final chunkTotalSize = dataSize + _encryptor.chunkMacSize;
    final chunkStart =
        g.headerSize + (i * (g.chunkSize + _encryptor.chunkMacSize));

    final chunkData = await _inner.readRange(
      key,
      start: chunkStart,
      length: chunkTotalSize,
    );

    yield await _encryptor.decryptChunk(chunkData, encKey, g.nonce, i);
  }
}