@@ -9,7 +9,7 @@ abstract class RandomAccessSource {
99 Future <int > readByte ();
1010
1111 /// Reads an array of bytes from the source.
12- Future <Uint8List > read (int length );
12+ Future <Uint8List > read (int count );
1313
1414 /// Gets the current position in the source.
1515 Future <int > position ();
@@ -20,8 +20,20 @@ abstract class RandomAccessSource {
2020 /// Reads all the remaining bytes from the source.
2121 Future <Uint8List > readToEnd ();
2222
23- /// Closes the source.
24- Future <void > close ();
23+ /// Skips a number of bytes in the source.
24+ /// Returns the number of bytes actually skipped.
25+ Future <int > skip (int count) async {
26+ final currentPosition = await position ();
27+ final fileLength = await length ();
28+
29+ // Calculate the target position clamped within the file boundaries
30+ final targetPosition = (currentPosition + count).clamp (0 , fileLength);
31+ await seek (targetPosition);
32+
33+ // Calculate the actual bytes skipped
34+ final actualSkipped = targetPosition - currentPosition;
35+ return actualSkipped;
36+ }
2537
2638 /// Reads a specific number of bytes, ensuring that the exact number is read.
2739 /// Throws an exception if the number of bytes read is not equal to [length] .
@@ -32,4 +44,16 @@ abstract class RandomAccessSource {
3244 }
3345 return bytes;
3446 }
47+
48+ /// Skips a specific number of bytes, ensuring that the exact number is skipped.
49+ /// Throws an exception if the number of bytes skipped is not equal to [length] .
50+ Future <void > mustSkip (int length) async {
51+ final count = await skip (length);
52+ if (count != length) {
53+ throw Exception ('Failed to skip $length bytes, skipped $count bytes' );
54+ }
55+ }
56+
57+ /// Closes the source.
58+ Future <void > close ();
3559}
0 commit comments