skip method

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

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

@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 reader
  if (n > 0) {
    final skippedFromReader = await _reader.skip(n);
    totalSkipped += skippedFromReader;
  }

  return totalSkipped;
}