Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(526)

Unified Diff: lib/src/string_accumulator_sink.dart

Issue 1911053002: Add sinks to provide synchronous access to events. (Closed) Base URL: git@github.com:dart-lang/convert@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/src/string_accumulator_sink.dart
diff --git a/lib/src/string_accumulator_sink.dart b/lib/src/string_accumulator_sink.dart
new file mode 100644
index 0000000000000000000000000000000000000000..6d8ad566e5b9d627a4d24f20a5fbebf07854a6d0
--- /dev/null
+++ b/lib/src/string_accumulator_sink.dart
@@ -0,0 +1,38 @@
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'dart:convert';
+
+/// A sink that provides synchronous access to the concatenated strings passed
Lasse Reichstein Nielsen 2016/04/22 09:15:14 Again, no need for "synchronous".
nweiz 2016/04/22 21:19:56 Done.
+/// to it.
+class StringAccumulatorSink extends StringConversionSinkBase {
+ /// The string accumulated so far.
+ String get string => _buffer.toString();
+ final _buffer = new StringBuffer();
+
+ /// Whether [close] has been called.
+ bool get isClosed => _isClosed;
+ var _isClosed = false;
+
+ void add(String chunk) {
+ if (_isClosed) {
+ throw new StateError("Can't add to a closed sink.");
+ }
+
+ _buffer.write(chunk);
+ }
+
+ void addSlice(String chunk, int start, int end, bool isLast) {
+ if (_isClosed) {
+ throw new StateError("Can't add to a closed sink.");
+ }
+
+ _buffer.write(chunk.substring(start, end));
+ if (isLast) _isClosed = true;
+ }
+
+ void close() {
+ _isClosed = true;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698