skip method

  1. @override
Future<int> skip(
  1. int n
)
override

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 {
  await _ensureOpen();

  if (n <= 0) {
    return 0;
  }

  try {
    final currentPosition = await _randomAccessFile!.position();
    final fileLength = await _randomAccessFile!.length();
    final maxSkip = fileLength - currentPosition;
    final actualSkip = n.clamp(0, maxSkip.toInt());

    if (actualSkip > 0) {
      await _randomAccessFile!.setPosition(currentPosition + actualSkip);
      _position += actualSkip;
    }

    return actualSkip;
  } catch (e) {
    throw IOException('Error skipping in file: ${_file.path}', cause: e);
  }
}