skip method

Future<int> skip(
  1. int n
)

Skips characters.

This method will block until some characters are available, an I/O error occurs, or the end of the stream is reached.

Parameters

  • n: The number of characters to skip

Returns

The number of characters actually skipped.

Example

final reader = FileReader('data.txt');
try {
  // Skip the first 100 characters (e.g., header)
  final skipped = await reader.skip(100);
  print('Skipped $skipped characters');
  
  // Now read the actual content
  final content = await reader.readAll();
  processContent(content);
} finally {
  await reader.close();
}

Throws IOException if an I/O error occurs. Throws StreamClosedException if the reader has been closed.

Implementation

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

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

  int skipped = 0;
  final buffer = List<int>.filled(8192.clamp(1, n), 0);

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

  return skipped;
}