FileSystemWritableByteSink class

A RandomAccessByteSink that writes to a browser file through a FileSystemWritableFileStream, so an export streamed into it with e.g. exportSvsRegionAsSvsToSink goes to disk instead of memory, however large the output.

Get the stream from a FileSystemFileHandle's createWritable(): a handle from showSaveFilePicker() (Chromium browsers) writes a file the user chose, one from the origin private file system (navigator.storage.getDirectory(), every current browser) a file the app can then hand to the user as a download. The browser writes to a temporary file and only replaces the target on close, so call abort instead if the export fails.

import 'dart:js_interop';

import 'package:svs/svs.dart';
import 'package:svs/svs_web.dart';
import 'package:web/web.dart' as web;

Future<void> exportTo(web.FileSystemFileHandle handle, SvsFile svs) async {
  final sink = FileSystemWritableByteSink(
    await handle.createWritable().toDart,
  );
  try {
    await rebuildSvsPyramidToSink(svs, sink: sink);
  } catch (_) {
    await sink.abort();
    rethrow;
  }
  await sink.close();
}

Contiguous writes are coalesced into chunks of up to 4 MB before they reach the stream, so a pyramid's many small tile writes cost a few large ones.

Implemented types

Constructors

FileSystemWritableByteSink(JSObject writableFileStream)
Wraps writableFileStream, which must be a JS FileSystemWritableFileStream — throws ArgumentError otherwise. Takes a plain JSObject so any binding works, e.g. package:web's.

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

abort() → Future<void>
Closes the stream without committing: the target file is left as it was before this sink was created. Does nothing after close.
close() → Future<void>
Writes out anything still pending and closes the stream, which is what replaces the target file with everything written. Idempotent.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
position() → Future<int>
The current write cursor position.
override
setPosition(int position) → Future<void>
Moves the write cursor to position, without writing anything.
override
toString() → String
A string representation of this object.
inherited
writeFrom(List<int> bytes) → Future<void>
Writes bytes starting at the current position, advancing it by bytes.length.
override

Operators

operator ==(Object other) → bool
The equality operator.
inherited