Skip to content

Commit

Permalink
Add readToEnd
Browse files Browse the repository at this point in the history
  • Loading branch information
mgenware committed Oct 28, 2024
1 parent 8d348f7 commit a845229
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/src/random_access_source_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ abstract class RandomAccessSource {
/// Sets the current position in the source.
Future<void> setPosition(int position);

/// Reads all the remaining bytes from the source.
Future<Uint8List> readToEnd();

/// Closes the source.
Future<void> close();
}
Expand Down Expand Up @@ -62,6 +65,13 @@ class BytesRASource extends RandomAccessSource {
_position = position;
}

@override
Future<Uint8List> readToEnd() async {
final result = Uint8List.sublistView(_bytes, _position);
_position = _bytes.length;
return result;
}

@override
Future<void> close() async {}
}
Expand Down Expand Up @@ -101,6 +111,11 @@ class RandomAccessFileRASource extends RandomAccessSource {
await _file.setPosition(position);
}

@override
Future<Uint8List> readToEnd() async {
return _file.read(await _file.length());
}

@override
Future<void> close() async {
await _file.close();
Expand Down
15 changes: 15 additions & 0 deletions test/bytes_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,19 @@ void main() {
expect(await src.readByte(), 3);
await src.close();
});

test('ReadToEnd', () async {
final src = await _bytesSource();
expect(await src.readToEnd(), Uint8List.fromList([1, 2, 3, 4, 5]));
expect(await src.position(), 5);
await src.close();
});

test('ReadToEnd (halfway)', () async {
final src = await _bytesSource();
await src.setPosition(2);
expect(await src.readToEnd(), Uint8List.fromList([3, 4, 5]));
expect(await src.position(), 5);
await src.close();
});
}
15 changes: 15 additions & 0 deletions test/bytes_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,19 @@ void main() {
expect(await src.readByte(), 3);
await src.close();
});

test('ReadToEnd', () async {
final src = await _bytesSource();
expect(await src.readToEnd(), Uint8List.fromList([1, 2, 3, 4, 5]));
expect(await src.position(), 5);
await src.close();
});

test('ReadToEnd (halfway)', () async {
final src = await _bytesSource();
await src.setPosition(2);
expect(await src.readToEnd(), Uint8List.fromList([3, 4, 5]));
expect(await src.position(), 5);
await src.close();
});
}
15 changes: 15 additions & 0 deletions test/raf_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,19 @@ void main() {
expect(await src.readByte(), 3);
await src.close();
});

test('ReadToEnd', () async {
final src = await _rafSource();
expect(await src.readToEnd(), Uint8List.fromList([1, 2, 3, 4, 5]));
expect(await src.position(), 5);
await src.close();
});

test('ReadToEnd (halfway)', () async {
final src = await _rafSource();
await src.setPosition(2);
expect(await src.readToEnd(), Uint8List.fromList([3, 4, 5]));
expect(await src.position(), 5);
await src.close();
});
}

0 comments on commit a845229

Please sign in to comment.