Skip to content

Commit 8d646d2

Browse files
committed
Add skip
1 parent 893901e commit 8d646d2

File tree

7 files changed

+119
-8
lines changed

7 files changed

+119
-8
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,29 @@ abstract class RandomAccessSource {
1919
Future<int> readByte();
2020
2121
/// Reads an array of bytes from the source.
22-
Future<Uint8List> read(int length);
22+
Future<Uint8List> read(int count);
2323
2424
/// Gets the current position in the source.
2525
Future<int> position();
2626
2727
/// Sets the current position in the source.
2828
Future<void> seek(int position);
2929
30+
/// Reads all the remaining bytes from the source.
31+
Future<Uint8List> readToEnd();
32+
33+
/// Skips a number of bytes in the source.
34+
/// Returns the number of bytes actually skipped.
35+
Future<int> skip(int count);
36+
37+
/// Reads a specific number of bytes, ensuring that the exact number is read.
38+
/// Throws an exception if the number of bytes read is not equal to [length].
39+
Future<Uint8List> mustRead(int length);
40+
41+
/// Skips a specific number of bytes, ensuring that the exact number is skipped.
42+
/// Throws an exception if the number of bytes skipped is not equal to [length].
43+
Future<void> mustSkip(int length);
44+
3045
/// Closes the source.
3146
Future<void> close();
3247
}

lib/src/bytes_ra_source.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ class BytesRASource extends RandomAccessSource {
2222
}
2323

2424
@override
25-
Future<Uint8List> read(int length) async {
26-
return _syncSource.read(length);
25+
Future<Uint8List> read(int count) async {
26+
return _syncSource.read(count);
2727
}
2828

2929
@override

lib/src/file_ra_source.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class FileRASource extends RandomAccessSource {
2424
}
2525

2626
@override
27-
Future<Uint8List> read(int length) async {
28-
return _file.read(length);
27+
Future<Uint8List> read(int count) async {
28+
return _file.read(count);
2929
}
3030

3131
@override

lib/src/random_access_source.dart

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

test/bytes_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,28 @@ void main() {
7575
expect(await src.position(), 5);
7676
await src.close();
7777
});
78+
79+
test('Skip (less than available)', () async {
80+
final src = await _bytesSource();
81+
expect(await src.skip(2), 2);
82+
expect(await src.position(), 2);
83+
expect(await src.readByte(), 3);
84+
await src.close();
85+
});
86+
87+
test('Skip (more than available)', () async {
88+
final src = await _bytesSource();
89+
expect(await src.skip(10), 5);
90+
expect(await src.position(), 5);
91+
expect(await src.readByte(), -1);
92+
await src.close();
93+
});
94+
95+
test('Skip (exactly available)', () async {
96+
final src = await _bytesSource();
97+
expect(await src.skip(5), 5);
98+
expect(await src.position(), 5);
99+
expect(await src.readByte(), -1);
100+
await src.close();
101+
});
78102
}

test/bytes_view_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,28 @@ void main() {
7676
expect(await src.position(), 5);
7777
await src.close();
7878
});
79+
80+
test('Skip (less than available)', () async {
81+
final src = await _bytesSource();
82+
expect(await src.skip(2), 2);
83+
expect(await src.position(), 2);
84+
expect(await src.readByte(), 3);
85+
await src.close();
86+
});
87+
88+
test('Skip (more than available)', () async {
89+
final src = await _bytesSource();
90+
expect(await src.skip(10), 5);
91+
expect(await src.position(), 5);
92+
expect(await src.readByte(), -1);
93+
await src.close();
94+
});
95+
96+
test('Skip (exactly available)', () async {
97+
final src = await _bytesSource();
98+
expect(await src.skip(5), 5);
99+
expect(await src.position(), 5);
100+
expect(await src.readByte(), -1);
101+
await src.close();
102+
});
79103
}

test/raf_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,28 @@ void main() {
8484
expect(await src.position(), 5);
8585
await src.close();
8686
});
87+
88+
test('Skip (less than available)', () async {
89+
final src = await _rafSource();
90+
expect(await src.skip(2), 2);
91+
expect(await src.position(), 2);
92+
expect(await src.readByte(), 3);
93+
await src.close();
94+
});
95+
96+
test('Skip (more than available)', () async {
97+
final src = await _rafSource();
98+
expect(await src.skip(10), 5);
99+
expect(await src.position(), 5);
100+
expect(await src.readByte(), -1);
101+
await src.close();
102+
});
103+
104+
test('Skip (exactly available)', () async {
105+
final src = await _rafSource();
106+
expect(await src.skip(5), 5);
107+
expect(await src.position(), 5);
108+
expect(await src.readByte(), -1);
109+
await src.close();
110+
});
87111
}

0 commit comments

Comments
 (0)