skip method

Future<int> skip(
  1. int n
)

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

Future<int> skip(int n) async {
  checkClosed();

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

  int skipped = 0;
  final buffer = Uint8List(8192.clamp(1, n)); // Use reasonable buffer size

  while (skipped < n) {
    final toRead = (n - skipped).clamp(1, buffer.length);
    final bytesRead = await read(buffer, 0, toRead);
    if (bytesRead == -1) {
      break;
    }
    skipped += bytesRead;
  }

  return skipped;
}