Created
September 28, 2018 04:30
-
-
Save bergwerf/4be3c95fc3354f4110b8f0ed18c67ce9 to your computer and use it in GitHub Desktop.
Dart buffered StringSink
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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