close method

  1. @override
Future<void> close()
override

Finishes the sink and releases any associated resources.

Implementation

@override
Future<void> close() => _synchronized(() async {
  if (_state == _FileSinkState.committed) return;
  if (_state != _FileSinkState.open) {
    throw const ByteSinkClosedException();
  }

  try {
    await _handle.flush();
    _stagingSnapshot = await _captureStagingSnapshot();
    await _testHooks?.beforeStagingCommit?.call(_staging);
    if (_overwrite) {
      await _closeHandle();
      await _stagingOwnership.verifyOwned(_stagingSnapshot!);
      await _staging.rename(_target.path);
      await _stagingOwnership.deleteMarkerIfOwned();
    } else {
      await _commitWithoutOverwrite();
    }
    _state = _FileSinkState.committed;
  } on Object catch (error) {
    _state = _FileSinkState.failed;
    Object? cleanupError;
    try {
      await _cleanupAfterFailedCommit();
    } on Object catch (caught) {
      cleanupError = caught;
    }
    throw ByteSinkIoException(
      operation: 'commit staged bytes',
      cause: cleanupError == null
          ? error
          : _CommitAndCleanupException(error, cleanupError),
      location: _target.path,
    );
  }
}, allowClosed: true);