skip method
Skips over and discards n
bytes of data from this input stream.
The skip method may, for a variety of reasons, end up skipping over
some smaller number of bytes, possibly 0
. This may result from any of
a number of conditions; reaching end of file before n
bytes have been
skipped is only one possibility.
Parameters
n
: The number of bytes to be skipped
Returns
The actual number of bytes skipped.
Example
final input = FileInputStream('data.bin');
try {
// Skip the first 100 bytes (e.g., header)
final skipped = await input.skip(100);
print('Skipped $skipped bytes');
// Now read the actual data
final data = await input.readAll();
processData(data);
} finally {
await input.close();
}
Throws IOException if an I/O error occurs. Throws StreamClosedException if the stream has been closed.
Implementation
@override
Future<int> skip(int n) async {
checkClosed();
if (n <= 0) {
return 0;
}
int totalSkipped = 0;
// First, skip from buffer if available
if (_position < _count) {
final availableInBuffer = _count - _position;
final toSkipFromBuffer = n.clamp(0, availableInBuffer);
_position += toSkipFromBuffer;
totalSkipped += toSkipFromBuffer;
n -= toSkipFromBuffer;
}
// If we need to skip more, delegate to underlying stream
if (n > 0) {
final skippedFromStream = await _input.skip(n);
totalSkipped += skippedFromStream;
}
return totalSkipped;
}