read method

  1. @override
Future<Uint8List> read(
  1. int offset,
  2. int length
)
override

Reads exactly length bytes starting at byte offset.

Throws UnexpectedEofException if the range extends past the end of the source and ArchiveClosedException if the source is closed.

Implementation

@override
Future<Uint8List> read(int offset, int length) {
  checkByteSourceRange(this, offset, length);
  return _synchronized(() async {
    if (_closed) {
      throw ArchiveClosedException('read($offset, $length) after close()');
    }
    await _file.setPosition(offset);
    final result = Uint8List(length);
    var filled = 0;
    while (filled < length) {
      final got = await _file.readInto(result, filled, length);
      if (got <= 0) {
        // The file shrank underneath us after open().
        throw UnexpectedEofException(
          'file ended at ${offset + filled} while reading '
          '$length byte(s) at offset $offset',
          offset: offset + filled,
        );
      }
      filled += got;
    }
    return result;
  });
}