Skip to content

Commit 94e9b12

Browse files
committed
Add RandomAccessBinaryReader
1 parent 3ed169c commit 94e9b12

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

lib/random_access_source.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
library;
33

44
export 'src/random_access_source_base.dart';
5+
export 'src/random_access_binary_reader.dart';
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import 'dart:typed_data';
2+
3+
import '../random_access_source.dart';
4+
5+
/// A class that reads binary data from a [RandomAccessSource].
6+
class RandomAccessBinaryReader {
7+
final RandomAccessSource source;
8+
Endian endian = Endian.big;
9+
10+
RandomAccessBinaryReader(this.source);
11+
12+
Future<Uint8List> readBytes(int count) async {
13+
return source.read(count);
14+
}
15+
16+
Future<Uint8List> mustReadBytes(int count) async {
17+
final data = await source.read(count);
18+
if (data.length != count) {
19+
throw Exception(
20+
'Unexpected end of file, expected $count bytes, but got ${data.length} bytes');
21+
}
22+
return data;
23+
}
24+
25+
Future<double> readFloat32([Endian? endian]) async {
26+
final ed = endian ?? this.endian;
27+
final data = await mustReadBytes(4);
28+
return ByteData.sublistView(data).getFloat32(0, ed);
29+
}
30+
31+
Future<double> readFloat64([Endian? endian]) async {
32+
final ed = endian ?? this.endian;
33+
final data = await mustReadBytes(8);
34+
return ByteData.sublistView(data).getFloat64(0, ed);
35+
}
36+
37+
Future<int> readInt8() async {
38+
final data = await mustReadBytes(1);
39+
return ByteData.sublistView(data).getInt8(0);
40+
}
41+
42+
Future<int> readInt16([Endian? endian]) async {
43+
final ed = endian ?? this.endian;
44+
final data = await mustReadBytes(2);
45+
return ByteData.sublistView(data).getInt16(0, ed);
46+
}
47+
48+
Future<int> readInt32([Endian? endian]) async {
49+
final ed = endian ?? this.endian;
50+
final data = await mustReadBytes(4);
51+
return ByteData.sublistView(data).getInt32(0, ed);
52+
}
53+
54+
Future<int> readInt64([Endian? endian]) async {
55+
final ed = endian ?? this.endian;
56+
final data = await mustReadBytes(8);
57+
return ByteData.sublistView(data).getInt64(0, ed);
58+
}
59+
60+
Future<int> readUint8() async {
61+
final data = await mustReadBytes(1);
62+
return ByteData.sublistView(data).getUint8(0);
63+
}
64+
65+
Future<int> readUint16([Endian? endian]) async {
66+
final ed = endian ?? this.endian;
67+
final data = await mustReadBytes(2);
68+
return ByteData.sublistView(data).getUint16(0, ed);
69+
}
70+
71+
Future<int> readUint32([Endian? endian]) async {
72+
final ed = endian ?? this.endian;
73+
final data = await mustReadBytes(4);
74+
return ByteData.sublistView(data).getUint32(0, ed);
75+
}
76+
77+
Future<int> readUint64([Endian? endian]) async {
78+
final ed = endian ?? this.endian;
79+
final data = await mustReadBytes(8);
80+
return ByteData.sublistView(data).getUint64(0, ed);
81+
}
82+
}

0 commit comments

Comments
 (0)