Skip to content

Commit 0dbc3a7

Browse files
committed
Add BytesRASource
1 parent be0b6a2 commit 0dbc3a7

File tree

1 file changed

+49
-15
lines changed

1 file changed

+49
-15
lines changed

lib/src/bytes_ra_source.dart

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,66 @@ import 'dart:typed_data';
33
import '../random_access_source.dart';
44

55
class BytesRASource extends RandomAccessSource {
6-
final Uint8List _bytes;
7-
int _position = 0;
6+
late final SyncBytesRASource _syncSource;
7+
8+
Uint8List get bytes => _syncSource._bytes;
89

9-
BytesRASource(this._bytes);
10+
BytesRASource(Uint8List bytes) {
11+
_syncSource = SyncBytesRASource(bytes);
12+
}
1013

1114
@override
1215
Future<int> length() async {
13-
return _bytes.length;
16+
return bytes.length;
1417
}
1518

1619
@override
1720
Future<int> readByte() async {
21+
return _syncSource.readByte();
22+
}
23+
24+
@override
25+
Future<Uint8List> read(int length) async {
26+
return _syncSource.read(length);
27+
}
28+
29+
@override
30+
Future<int> position() async {
31+
return _syncSource.position();
32+
}
33+
34+
@override
35+
Future<void> setPosition(int position) async {
36+
_syncSource.setPosition(position);
37+
}
38+
39+
@override
40+
Future<Uint8List> readToEnd() async {
41+
return _syncSource.readToEnd();
42+
}
43+
44+
@override
45+
Future<void> close() async {}
46+
}
47+
48+
class SyncBytesRASource {
49+
final Uint8List _bytes;
50+
int _position = 0;
51+
52+
SyncBytesRASource(this._bytes);
53+
54+
int length() {
55+
return _bytes.length;
56+
}
57+
58+
int readByte() {
1859
if (_position >= _bytes.length) {
1960
return -1;
2061
}
2162
return _bytes[_position++];
2263
}
2364

24-
@override
25-
Future<Uint8List> read(int length) async {
65+
Uint8List read(int length) {
2666
if (_position >= _bytes.length) {
2767
return Uint8List(0);
2868
}
@@ -32,23 +72,17 @@ class BytesRASource extends RandomAccessSource {
3272
return result;
3373
}
3474

35-
@override
36-
Future<int> position() async {
75+
int position() {
3776
return _position;
3877
}
3978

40-
@override
41-
Future<void> setPosition(int position) async {
79+
void setPosition(int position) {
4280
_position = position;
4381
}
4482

45-
@override
46-
Future<Uint8List> readToEnd() async {
83+
Uint8List readToEnd() {
4784
final result = Uint8List.sublistView(_bytes, _position);
4885
_position = _bytes.length;
4986
return result;
5087
}
51-
52-
@override
53-
Future<void> close() async {}
5488
}

0 commit comments

Comments
 (0)