close method

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

Closes this input stream and releases any system resources associated with the stream.

The close method of InputStream does nothing. Subclasses should override this method to release resources.

Example

final input = FileInputStream('data.txt');
try {
  // Use the input stream
  final data = await input.readAll();
  processData(data);
} finally {
  await input.close(); // Always close in finally block
}

Throws IOException if an I/O error occurs.

Implementation

@override
Future<void> close() async {
  if (!isClosed) {
    try {
      await _randomAccessFile?.close();
    } catch (e) {
      throw IOException('Error closing file: ${_file.path}', cause: e);
    } finally {
      _randomAccessFile = null;
      await super.close();
    }
  }
}