Skip to content

Commit 28f5856

Browse files
committed
Update source file structure
1 parent 03ec659 commit 28f5856

File tree

5 files changed

+132
-124
lines changed

5 files changed

+132
-124
lines changed

lib/random_access_source.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/// Shared interfaces for random access data.
22
library;
33

4-
export 'src/random_access_source_base.dart';
4+
export 'src/random_access_source.dart';
5+
export 'src/bytes_ra_source.dart';
6+
export 'src/random_access_file_ra_source.dart';
57
export 'src/random_access_binary_reader.dart';

lib/src/bytes_ra_source.dart

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import 'dart:typed_data';
2+
3+
import '../random_access_source.dart';
4+
5+
class BytesRASource extends RandomAccessSource {
6+
final Uint8List _bytes;
7+
int _position = 0;
8+
9+
BytesRASource(this._bytes);
10+
11+
@override
12+
Future<int> length() async {
13+
return _bytes.length;
14+
}
15+
16+
@override
17+
Future<int> readByte() async {
18+
if (_position >= _bytes.length) {
19+
return -1;
20+
}
21+
return _bytes[_position++];
22+
}
23+
24+
@override
25+
Future<Uint8List> read(int length) async {
26+
if (_position >= _bytes.length) {
27+
return Uint8List(0);
28+
}
29+
final end = (_position + length).clamp(0, _bytes.length);
30+
final result = Uint8List.sublistView(_bytes, _position, end);
31+
_position = end;
32+
return result;
33+
}
34+
35+
@override
36+
Future<int> position() async {
37+
return _position;
38+
}
39+
40+
@override
41+
Future<void> setPosition(int position) async {
42+
_position = position;
43+
}
44+
45+
@override
46+
Future<Uint8List> readToEnd() async {
47+
final result = Uint8List.sublistView(_bytes, _position);
48+
_position = _bytes.length;
49+
return result;
50+
}
51+
52+
@override
53+
Future<void> close() async {}
54+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import 'dart:io';
2+
import 'dart:typed_data';
3+
4+
import '../random_access_source.dart';
5+
6+
class RandomAccessFileRASource extends RandomAccessSource {
7+
final RandomAccessFile _file;
8+
9+
RandomAccessFileRASource(this._file);
10+
11+
static Future<RandomAccessFileRASource> open(String path) async {
12+
final file = await File(path).open();
13+
return RandomAccessFileRASource(file);
14+
}
15+
16+
@override
17+
Future<int> length() async {
18+
return _file.length();
19+
}
20+
21+
@override
22+
Future<int> readByte() async {
23+
return _file.readByte();
24+
}
25+
26+
@override
27+
Future<Uint8List> read(int length) async {
28+
return _file.read(length);
29+
}
30+
31+
@override
32+
Future<int> position() async {
33+
return _file.position();
34+
}
35+
36+
@override
37+
Future<void> setPosition(int position) async {
38+
await _file.setPosition(position);
39+
}
40+
41+
@override
42+
Future<Uint8List> readToEnd() async {
43+
return _file.read(await _file.length());
44+
}
45+
46+
@override
47+
Future<void> close() async {
48+
await _file.close();
49+
}
50+
}

lib/src/random_access_source.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import 'dart:typed_data';
2+
3+
/// Base class for random access sources.
4+
abstract class RandomAccessSource {
5+
/// Gets the length of the source.
6+
Future<int> length();
7+
8+
/// Reads a byte from the source.
9+
Future<int> readByte();
10+
11+
/// Reads an array of bytes from the source.
12+
Future<Uint8List> read(int length);
13+
14+
/// Gets the current position in the source.
15+
Future<int> position();
16+
17+
/// Sets the current position in the source.
18+
Future<void> setPosition(int position);
19+
20+
/// Reads all the remaining bytes from the source.
21+
Future<Uint8List> readToEnd();
22+
23+
/// Closes the source.
24+
Future<void> close();
25+
}

lib/src/random_access_source_base.dart

Lines changed: 0 additions & 123 deletions
This file was deleted.

0 commit comments

Comments
 (0)