repositionSource method

Future<void> repositionSource(
  1. Source source,
  2. int position
)

Change the position of source in the file to position. The argument source must be either a source produced by this file handle, or a BufferedSource that directly wraps such a source. If the parameter is a BufferedSource, it will skip or clear buffered bytes.

Implementation

Future<void> repositionSource(Source source, int position) async {
  if (source is RealBufferedSource) {
    var fileHandleSource = source._source;
    checkArgument(
      fileHandleSource is FileHandleSource &&
          identical(fileHandleSource.fileHandle, this),
      'source was not created by this FileHandle',
    );
    fileHandleSource = fileHandleSource as FileHandleSource;
    checkState(!fileHandleSource._closed, 'closed');

    final bufferSize = source.buffer._length;
    final toSkip = position - (fileHandleSource._position - bufferSize);
    if (0 <= toSkip && toSkip < bufferSize) {
      // The new position requires only a buffer change.
      source.buffer.skip(toSkip);
    } else {
      // The new position doesn't share data with the current buffer.
      source.buffer.clear();
      fileHandleSource._position = position;
    }
  } else {
    checkArgument(
      source is FileHandleSource && identical(source.fileHandle, this),
      'source was not created by this FileHandle',
    );
    source = source as FileHandleSource;
    checkState(!source._closed, 'closed');
    source._position = position;
  }
}