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

Unified Diff: sdk/lib/convert/chunked_conversion.dart

Issue 19883003: Add chunked conversion to converters. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 7 years, 5 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
« no previous file with comments | « sdk/lib/convert/byte_conversion.dart ('k') | sdk/lib/convert/convert.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/convert/chunked_conversion.dart
diff --git a/sdk/lib/convert/chunked_conversion.dart b/sdk/lib/convert/chunked_conversion.dart
new file mode 100644
index 0000000000000000000000000000000000000000..f8f049188acd62090aaced9796dd8af6f43e6b61
--- /dev/null
+++ b/sdk/lib/convert/chunked_conversion.dart
@@ -0,0 +1,48 @@
+// Copyright (c) 2013, 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.
+
+part of dart.convert;
+
+typedef void _ChunkedConversionCallback<T>(T accumulated);
+
+/**
+ * A [ChunkedConversionSink] is used to transmit data more efficiently between
+ * two converters during chunked conversions.
+ */
+abstract class ChunkedConversionSink<T> {
+ ChunkedConversionSink();
+ factory ChunkedConversionSink.withCallback(
+ void callback(List<T> accumulated)) = _SimpleCallbackSink;
+
+ /**
+ * Adds chunked data to this sink.
+ *
+ * This method is also used when converters are used as [StreamTransformer]s.
+ */
+ void add(T chunk);
+
+ /**
+ * Closes the sink.
+ *
+ * This signals the end of the chunked conversion. This method is called
+ * when converters are used as [StreamTransformer]'s.
+ */
+ void close();
+}
+
+/**
+ * This class accumulates all chunks and invokes a callback with a list of
+ * the chunks when the sink is closed.
+ *
+ * This class can be used to terminate a chunked conversion.
+ */
+class _SimpleCallbackSink<T> extends ChunkedConversionSink<T> {
+ final _ChunkedConversionCallback<List<T>> _callback;
+ final List<T> _accumulated = <T>[];
+
+ _SimpleCallbackSink(this._callback);
+
+ void add(T chunk) { _accumulated.add(chunk); }
+ void close() { _callback(_accumulated); }
+}
« no previous file with comments | « sdk/lib/convert/byte_conversion.dart ('k') | sdk/lib/convert/convert.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698