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

Unified Diff: sdk/lib/io/io_sink.dart

Issue 12504006: Make IOSink implement StringSink (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Correct rebase Created 7 years, 9 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: sdk/lib/io/io_sink.dart
diff --git a/sdk/lib/io/io_sink.dart b/sdk/lib/io/io_sink.dart
index ace6f92f696f5e1e56ae1c01f18cf24d8269aed7..b5b1b1683ab485a3722a4bdaf01f0d82a4493ddc 100644
--- a/sdk/lib/io/io_sink.dart
+++ b/sdk/lib/io/io_sink.dart
@@ -5,18 +5,25 @@
part of dart.io;
/**
- * Helper class to wrap a [StreamConsumer<List<int>, T>] and provide utility
- * functions for writing to the StreamConsumer directly. The [IOSink]
- * buffers the input given by [add] and [addString] and will delay a [consume]
- * or [addStream] until the buffer is flushed.
+ * Helper class to wrap a [StreamConsumer<List<int>, T>] and provide
+ * utility functions for writing to the StreamConsumer directly. The
+ * [IOSink] buffers the input given by [write], [writeAll], [writeln],
+ * [writeCharCode] and [writeBytes] and will delay a [consume] or
+ * [addStream] until the buffer is flushed.
*
* When the [IOSink] is bound to a stream (through either [consume]
* or [addStream]) any call to the [IOSink] will throw a
* [StateError].
*/
-abstract class IOSink<T> implements StreamConsumer<List<int>, T> {
- factory IOSink(StreamConsumer<List<int>, T> target)
- => new _IOSinkImpl(target);
+abstract class IOSink<T> implements StreamConsumer<List<int>, T>, StringSink {
+ factory IOSink(StreamConsumer<List<int>, T> target, Encoding encoding)
nweiz 2013/03/06 20:31:51 It looks like you're almost always initializing IO
Søren Gjesse 2013/03/07 16:28:47 Changed encoding to be optional with a default val
+ => new _IOSinkImpl(target, encoding);
+
+ Encoding encoding;
nweiz 2013/03/06 20:31:51 Either mention here or in the http documentation t
Søren Gjesse 2013/03/07 16:28:47 Added documentation to IOSink, HttpClientRequest a
+
+ void writeBytes(List<int> data);
+
+ Future<T> writeStream(Stream<List<int>> stream) => addStream(stream);
/**
* Provide functionality for piping to the [IOSink].
@@ -30,12 +37,18 @@ abstract class IOSink<T> implements StreamConsumer<List<int>, T> {
/**
* Write a list of bytes to the target.
+ *
+ * *Deprecated*. Use [writeBytes] instead.
*/
+ @deprecated
Anders Johnsen 2013/03/07 08:48:44 Can't we just remote them now? They ain't part of
Søren Gjesse 2013/03/07 16:28:47 Done.
void add(List<int> data);
/**
* Write a String to the target.
+ *
+ * *Deprecated*. Use [write] instead.
*/
+ @deprecated
void addString(String string, [Encoding encoding = Encoding.UTF_8]);
/**
@@ -58,32 +71,76 @@ class _IOSinkImpl<T> implements IOSink<T> {
Future<T> _pipeFuture;
StreamSubscription<List<int>> _bindSubscription;
bool _paused = true;
+ bool _encodingMutable = true;
- _IOSinkImpl(StreamConsumer<List<int>, T> target) : _target = target;
+ _IOSinkImpl(StreamConsumer<List<int>, T> this._target, Encoding encoding)
+ : _encoding = encoding;
- Future<T> consume(Stream<List<int>> stream) {
+ Encoding _encoding;
+
+ Encoding get encoding => _encoding;
+ void set encoding(Encoding value) {
+ if (!_encodingMutable) {
+ throw new StateError("IOSink encoding is not mutable");
+ }
+ _encoding = value;
+ }
+
+ void write(Object obj) {
+ // This comment is copied from runtime/lib/string_buffer_patch.dart.
+ // TODO(srdjan): The following four lines could be replaced by
+ // '$obj', but apparently this is too slow on the Dart VM.
+ String string;
+ if (obj is String) {
+ string = obj;
+ } else {
+ string = obj.toString();
+ if (string is! String) {
+ throw new ArgumentError('toString() did not return a string');
+ }
+ }
+ if (string.isEmpty) return;
+ writeBytes(_encodeString(string, _encoding));
+ }
+
+ void writeAll(Iterable objects) {
+ for (Object obj in objects) write(obj);
+ }
+
+ void writeln(Object obj) {
+ write(obj);
+ write("\n");
+ }
+
+ void writeCharCode(int charCode) {
+ write(new String.fromCharCode(charCode));
+ }
+
+ void writeBytes(List<int> data) {
if (_isBound) {
throw new StateError("IOSink is already bound to a stream");
}
- return _fillFromStream(stream);
+ _controller.add(data);
}
- Future<T> addStream(Stream<List<int>> stream) {
+ Future<T> consume(Stream<List<int>> stream) {
if (_isBound) {
throw new StateError("IOSink is already bound to a stream");
}
- return _fillFromStream(stream, unbind: true);
+ return _fillFromStream(stream);
}
- void add(List<int> data) {
+ Future<T> addStream(Stream<List<int>> stream) {
if (_isBound) {
throw new StateError("IOSink is already bound to a stream");
}
- _controller.add(data);
+ return _fillFromStream(stream, unbind: true);
}
+ void add(List<int> data) => writeBytes(data);
+
void addString(String string, [Encoding encoding = Encoding.UTF_8]) {
- add(_encodeString(string, encoding));
+ writeBytes(_encodeString(string, encoding));
}
void close() {

Powered by Google App Engine
This is Rietveld 408576698