Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a845229

Browse files
committedOct 28, 2024
Add readToEnd
1 parent 8d348f7 commit a845229

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed
 

‎lib/src/random_access_source_base.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ abstract class RandomAccessSource {
1818
/// Sets the current position in the source.
1919
Future<void> setPosition(int position);
2020

21+
/// Reads all the remaining bytes from the source.
22+
Future<Uint8List> readToEnd();
23+
2124
/// Closes the source.
2225
Future<void> close();
2326
}
@@ -62,6 +65,13 @@ class BytesRASource extends RandomAccessSource {
6265
_position = position;
6366
}
6467

68+
@override
69+
Future<Uint8List> readToEnd() async {
70+
final result = Uint8List.sublistView(_bytes, _position);
71+
_position = _bytes.length;
72+
return result;
73+
}
74+
6575
@override
6676
Future<void> close() async {}
6777
}
@@ -101,6 +111,11 @@ class RandomAccessFileRASource extends RandomAccessSource {
101111
await _file.setPosition(position);
102112
}
103113

114+
@override
115+
Future<Uint8List> readToEnd() async {
116+
return _file.read(await _file.length());
117+
}
118+
104119
@override
105120
Future<void> close() async {
106121
await _file.close();

‎test/bytes_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,19 @@ void main() {
6060
expect(await src.readByte(), 3);
6161
await src.close();
6262
});
63+
64+
test('ReadToEnd', () async {
65+
final src = await _bytesSource();
66+
expect(await src.readToEnd(), Uint8List.fromList([1, 2, 3, 4, 5]));
67+
expect(await src.position(), 5);
68+
await src.close();
69+
});
70+
71+
test('ReadToEnd (halfway)', () async {
72+
final src = await _bytesSource();
73+
await src.setPosition(2);
74+
expect(await src.readToEnd(), Uint8List.fromList([3, 4, 5]));
75+
expect(await src.position(), 5);
76+
await src.close();
77+
});
6378
}

‎test/bytes_view_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,19 @@ void main() {
6161
expect(await src.readByte(), 3);
6262
await src.close();
6363
});
64+
65+
test('ReadToEnd', () async {
66+
final src = await _bytesSource();
67+
expect(await src.readToEnd(), Uint8List.fromList([1, 2, 3, 4, 5]));
68+
expect(await src.position(), 5);
69+
await src.close();
70+
});
71+
72+
test('ReadToEnd (halfway)', () async {
73+
final src = await _bytesSource();
74+
await src.setPosition(2);
75+
expect(await src.readToEnd(), Uint8List.fromList([3, 4, 5]));
76+
expect(await src.position(), 5);
77+
await src.close();
78+
});
6479
}

‎test/raf_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,19 @@ void main() {
6969
expect(await src.readByte(), 3);
7070
await src.close();
7171
});
72+
73+
test('ReadToEnd', () async {
74+
final src = await _rafSource();
75+
expect(await src.readToEnd(), Uint8List.fromList([1, 2, 3, 4, 5]));
76+
expect(await src.position(), 5);
77+
await src.close();
78+
});
79+
80+
test('ReadToEnd (halfway)', () async {
81+
final src = await _rafSource();
82+
await src.setPosition(2);
83+
expect(await src.readToEnd(), Uint8List.fromList([3, 4, 5]));
84+
expect(await src.position(), 5);
85+
await src.close();
86+
});
7287
}

0 commit comments

Comments
 (0)
Please sign in to comment.