You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.1 KiB
38 lines
1.1 KiB
// A source of the bytes of a capsule read in pieces, for the tests of the
|
|
// inspection and of the opening from a ByteSource. It reads no file.
|
|
library;
|
|
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:datekeys/datekeys.dart';
|
|
|
|
/// A source that gives at most [step] bytes per read, so that every range
|
|
/// is read in several pieces, and counts what it reads.
|
|
final class ChunkySource implements ByteSource {
|
|
ChunkySource(this._b, [this.step = 4093]);
|
|
|
|
final Uint8List _b;
|
|
final int step;
|
|
int reads = 0;
|
|
int bytesRead = 0;
|
|
int largest = 0;
|
|
|
|
/// The end of the farthest range read.
|
|
int farthest = 0;
|
|
|
|
@override
|
|
int get length => _b.length;
|
|
|
|
@override
|
|
Future<Uint8List> read(int offset, int n) async {
|
|
reads++;
|
|
final k = n < step ? n : step;
|
|
final end = offset + k < _b.length ? offset + k : _b.length;
|
|
bytesRead += end - offset;
|
|
if (end - offset > largest) largest = end - offset;
|
|
if (end > farthest) farthest = end;
|
|
// A copy: the opening owns nothing that the source keeps.
|
|
return Uint8List.fromList(Uint8List.sublistView(_b, offset, end));
|
|
}
|
|
}
|