Skip to content

Instantly share code, notes, and snippets.

@bergwerf
Created September 28, 2018 04:30
Show Gist options
  • Save bergwerf/4be3c95fc3354f4110b8f0ed18c67ce9 to your computer and use it in GitHub Desktop.
Save bergwerf/4be3c95fc3354f4110b8f0ed18c67ce9 to your computer and use it in GitHub Desktop.
Dart buffered StringSink
/// Buffered stream sink that uses a string buffer.
class BufferedStringSink implements StringSink {
final buffer = new StringBuffer();
final int bufferThreshold;
final StringSink target;
BufferedStringSink(this.target, {this.bufferThreshold: 10000});
void flush() {
target.write(buffer);
buffer.clear();
}
void _check() {
if (buffer.length > bufferThreshold) {
flush();
}
}
@override
void write(obj) {
buffer.write(obj);
_check();
}
@override
void writeAll(objects, [separator]) {
buffer.writeAll(objects, separator);
_check();
}
@override
void writeCharCode(charCode) {
buffer.writeCharCode(charCode);
_check();
}
@override
void writeln([obj]) {
buffer.writeln(obj);
_check();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment