Skip to content

Commit 90a88c3

Browse files
committed
Add readInto
1 parent a868ca5 commit 90a88c3

File tree

7 files changed

+94
-0
lines changed

7 files changed

+94
-0
lines changed

lib/src/bytes_ra_source.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class BytesRASource extends RandomAccessSource {
2121
@override
2222
Future<Uint8List> read(int count) async => _syncSource.read(count);
2323

24+
@override
25+
Future<int> readInto(List<int> buffer, int offset, int count) async =>
26+
_syncSource.readInto(buffer, offset, count);
27+
2428
@override
2529
Future<int> position() async => _syncSource.position();
2630

@@ -61,6 +65,17 @@ class SyncBytesRASource {
6165
return result;
6266
}
6367

68+
int readInto(List<int> buffer, int offset, int count) {
69+
if (_position >= _bytes.length) {
70+
return 0;
71+
}
72+
final end = (_position + count).clamp(0, _bytes.length);
73+
final bytesToRead = end - _position;
74+
buffer.setRange(offset, offset + bytesToRead, _bytes, _position);
75+
_position = end;
76+
return bytesToRead;
77+
}
78+
6479
int position() {
6580
return _position;
6681
}

lib/src/file_ra_source_vm.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class FileRASource extends base.FileRASource {
2525
@override
2626
Future<Uint8List> read(int count) => _file.read(count);
2727

28+
@override
29+
Future<int> readInto(List<int> buffer, int offset, int count) =>
30+
_file.readInto(buffer, offset, offset + count);
31+
2832
@override
2933
Future<int> position() => _file.position();
3034

lib/src/file_ra_source_web.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class FileRASource extends base.FileRASource {
3636
@override
3737
Future<Uint8List> read(int count) => _bytes.read(count);
3838

39+
@override
40+
Future<int> readInto(List<int> buffer, int offset, int count) =>
41+
_bytes.readInto(buffer, offset, count);
42+
3943
@override
4044
Future<int> position() => _bytes.position();
4145

lib/src/random_access_source.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ abstract class RandomAccessSource {
1111
/// Reads an array of bytes from the source.
1212
Future<Uint8List> read(int count);
1313

14+
/// Reads bytes into the provided buffer.
15+
Future<int> readInto(List<int> buffer, int offset, int count);
16+
1417
/// Gets the current position in the source.
1518
Future<int> position();
1619

test/bytes_sync_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ void main() {
4949
expect(src.position(), 5);
5050
});
5151

52+
test('ReadInto', () async {
53+
final src = _bytesSource();
54+
final buffer = Uint8List(4);
55+
56+
var bytesRead = src.readInto(buffer, 0, 2);
57+
expect(bytesRead, 2);
58+
expect(buffer.sublist(0, 2), Uint8List.fromList([1, 2]));
59+
expect(src.position(), 2);
60+
61+
bytesRead = src.readInto(buffer, 2, 2);
62+
expect(bytesRead, 2);
63+
expect(buffer.sublist(2, 4), Uint8List.fromList([3, 4]));
64+
expect(src.position(), 4);
65+
66+
bytesRead = src.readInto(buffer, 0, 4);
67+
expect(bytesRead, 1);
68+
expect(buffer.sublist(0, 1), Uint8List.fromList([5]));
69+
expect(src.position(), 5);
70+
71+
bytesRead = src.readInto(buffer, 0, 4);
72+
expect(bytesRead, 0);
73+
expect(src.position(), 5);
74+
});
75+
5276
test('Position', () async {
5377
final src = _bytesSource();
5478
expect(src.position(), 0);

test/bytes_test.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,31 @@ void main() {
5252
await src.close();
5353
});
5454

55+
test('ReadInto', () async {
56+
final src = await _bytesSource();
57+
final buffer = Uint8List(4);
58+
59+
var bytesRead = await src.readInto(buffer, 0, 2);
60+
expect(bytesRead, 2);
61+
expect(buffer.sublist(0, 2), Uint8List.fromList([1, 2]));
62+
expect(await src.position(), 2);
63+
64+
bytesRead = await src.readInto(buffer, 2, 2);
65+
expect(bytesRead, 2);
66+
expect(buffer.sublist(2, 4), Uint8List.fromList([3, 4]));
67+
expect(await src.position(), 4);
68+
69+
bytesRead = await src.readInto(buffer, 0, 4);
70+
expect(bytesRead, 1);
71+
expect(buffer.sublist(0, 1), Uint8List.fromList([5]));
72+
expect(await src.position(), 5);
73+
74+
bytesRead = await src.readInto(buffer, 0, 4);
75+
expect(bytesRead, 0);
76+
expect(await src.position(), 5);
77+
await src.close();
78+
});
79+
5580
test('Position', () async {
5681
final src = await _bytesSource();
5782
expect(await src.position(), 0);

test/raf_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:typed_data';
2+
13
import 'package:test/test.dart';
24

35
import 'assets/flutter_icon_96x96.dart';
@@ -43,6 +45,23 @@ void main() {
4345
await src.close();
4446
});
4547

48+
test('ReadInto', () async {
49+
final src = await rafSource();
50+
final buffer = Uint8List(4);
51+
52+
var bytesRead = await src.readInto(buffer, 0, 2);
53+
expect(bytesRead, 2);
54+
expect(buffer.sublist(0, 2), flutterIcon.sublist(0, 2));
55+
expect(await src.position(), 2);
56+
57+
bytesRead = await src.readInto(buffer, 2, 2);
58+
expect(bytesRead, 2);
59+
expect(buffer.sublist(2, 4), flutterIcon.sublist(2, 4));
60+
expect(await src.position(), 4);
61+
62+
await src.close();
63+
});
64+
4665
test('Position', () async {
4766
final src = await rafSource();
4867
expect(await src.position(), 0);

0 commit comments

Comments
 (0)