We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
readInto
1 parent a868ca5 commit 90a88c3Copy full SHA for 90a88c3
lib/src/bytes_ra_source.dart
@@ -21,6 +21,10 @@ class BytesRASource extends RandomAccessSource {
21
@override
22
Future<Uint8List> read(int count) async => _syncSource.read(count);
23
24
+ @override
25
+ Future<int> readInto(List<int> buffer, int offset, int count) async =>
26
+ _syncSource.readInto(buffer, offset, count);
27
+
28
29
Future<int> position() async => _syncSource.position();
30
@@ -61,6 +65,17 @@ class SyncBytesRASource {
61
65
return result;
62
66
}
63
67
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
64
79
int position() {
80
return _position;
81
lib/src/file_ra_source_vm.dart
@@ -25,6 +25,10 @@ class FileRASource extends base.FileRASource {
Future<Uint8List> read(int count) => _file.read(count);
+ Future<int> readInto(List<int> buffer, int offset, int count) =>
+ _file.readInto(buffer, offset, offset + count);
31
32
33
Future<int> position() => _file.position();
34
lib/src/file_ra_source_web.dart
@@ -36,6 +36,10 @@ class FileRASource extends base.FileRASource {
36
37
Future<Uint8List> read(int count) => _bytes.read(count);
38
39
40
41
+ _bytes.readInto(buffer, offset, count);
42
43
44
Future<int> position() => _bytes.position();
45
lib/src/random_access_source.dart
@@ -11,6 +11,9 @@ abstract class RandomAccessSource {
11
/// Reads an array of bytes from the source.
12
Future<Uint8List> read(int count);
13
14
+ /// Reads bytes into the provided buffer.
15
+ Future<int> readInto(List<int> buffer, int offset, int count);
16
17
/// Gets the current position in the source.
18
Future<int> position();
19
test/bytes_sync_test.dart
@@ -49,6 +49,30 @@ void main() {
49
expect(src.position(), 5);
50
});
51
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
+ bytesRead = src.readInto(buffer, 2, 2);
+ expect(buffer.sublist(2, 4), Uint8List.fromList([3, 4]));
+ expect(src.position(), 4);
+ bytesRead = src.readInto(buffer, 0, 4);
+ expect(bytesRead, 1);
+ expect(buffer.sublist(0, 1), Uint8List.fromList([5]));
+ expect(src.position(), 5);
+ expect(bytesRead, 0);
+ });
test('Position', () async {
final src = _bytesSource();
expect(src.position(), 0);
test/bytes_test.dart
@@ -52,6 +52,31 @@ void main() {
await src.close();
+ final src = await _bytesSource();
+ var bytesRead = await src.readInto(buffer, 0, 2);
+ expect(await src.position(), 2);
+ bytesRead = await src.readInto(buffer, 2, 2);
+ expect(await src.position(), 4);
+ bytesRead = await src.readInto(buffer, 0, 4);
+ expect(await src.position(), 5);
+ await src.close();
final src = await _bytesSource();
82
expect(await src.position(), 0);
test/raf_test.dart
@@ -1,3 +1,5 @@
1
+import 'dart:typed_data';
2
3
import 'package:test/test.dart';
4
5
import 'assets/flutter_icon_96x96.dart';
@@ -43,6 +45,23 @@ void main() {
46
47
48
+ final src = await rafSource();
+ expect(buffer.sublist(0, 2), flutterIcon.sublist(0, 2));
+ expect(buffer.sublist(2, 4), flutterIcon.sublist(2, 4));
final src = await rafSource();
0 commit comments