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

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

Issue 14208007: Improve WebSocket interface by making it implement StreamSink. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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
« no previous file with comments | « no previous file | sdk/lib/io/websocket.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/io/io_sink.dart
diff --git a/sdk/lib/io/io_sink.dart b/sdk/lib/io/io_sink.dart
index 8001905509676c265355189d1ecebed38dac85ef..c6a82f00338e7fefc4fac78a7828f22c85471c41 100644
--- a/sdk/lib/io/io_sink.dart
+++ b/sdk/lib/io/io_sink.dart
@@ -52,74 +52,20 @@ abstract class IOSink implements StreamSink<List<int>>, StringSink {
Future get done;
}
-
-class _IOSinkImpl implements IOSink {
- final StreamConsumer<List<int>> _target;
+class _StreamSinkImpl<T> implements StreamSink<T> {
+ final StreamConsumer<T> _target;
Completer _doneCompleter = new Completer();
Future _doneFuture;
- StreamController<List<int>> _controllerInstance;
+ StreamController<T> _controllerInstance;
Completer _controllerCompleter;
- Encoding _encoding;
bool _isClosed = false;
bool _isBound = false;
- bool _encodingMutable = true;
- _IOSinkImpl(StreamConsumer<List<int>> this._target, this._encoding) {
+ _StreamSinkImpl(StreamConsumer<T> this._target) {
_doneFuture = _doneCompleter.future;
}
- 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;
- add(_encodeString(string, _encoding));
- }
-
- void writeAll(Iterable objects, [String separator = ""]) {
- Iterator iterator = objects.iterator;
- if (!iterator.moveNext()) return;
- if (separator.isEmpty) {
- do {
- write(iterator.current);
- } while (iterator.moveNext());
- } else {
- write(iterator.current);
- while (iterator.moveNext()) {
- write(separator);
- write(iterator.current);
- }
- }
- }
-
- void writeln([Object obj = ""]) {
- write(obj);
- write("\n");
- }
-
- void writeCharCode(int charCode) {
- write(new String.fromCharCode(charCode));
- }
-
- void add(List<int> data) {
+ void add(T data) {
_controller.add(data);
}
@@ -127,9 +73,9 @@ class _IOSinkImpl implements IOSink {
_controller.addError(error);
}
- Future addStream(Stream<List<int>> stream) {
+ Future addStream(Stream<T> stream) {
if (_isBound) {
- throw new StateError("IOSink is already bound to a stream");
+ throw new StateError("StreamSink is already bound to a stream");
}
_isBound = true;
// Wait for any sync operations to complete.
@@ -147,7 +93,7 @@ class _IOSinkImpl implements IOSink {
Future close() {
if (_isBound) {
- throw new StateError("IOSink is bound to a stream");
+ throw new StateError("StreamSink is bound to a stream");
}
if (!_isClosed) {
_isClosed = true;
@@ -179,15 +125,15 @@ class _IOSinkImpl implements IOSink {
}
}
- StreamController<List<int>> get _controller {
+ StreamController<T> get _controller {
if (_isBound) {
- throw new StateError("IOSink is bound to a stream");
+ throw new StateError("StreamSink is bound to a stream");
}
if (_isClosed) {
- throw new StateError("IOSink is closed");
+ throw new StateError("StreamSink is closed");
}
if (_controllerInstance == null) {
- _controllerInstance = new StreamController<List<int>>();
+ _controllerInstance = new StreamController<T>();
_controllerCompleter = new Completer();
_target.addStream(_controller.stream)
.then(
@@ -220,3 +166,63 @@ class _IOSinkImpl implements IOSink {
return _controllerInstance;
}
}
+
+
+class _IOSinkImpl extends _StreamSinkImpl<List<int>> implements IOSink {
+ Encoding _encoding;
+ bool _encodingMutable = true;
+
+ _IOSinkImpl(StreamConsumer<List<int>> target, this._encoding)
+ : super(target);
+
+ 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;
+ add(_encodeString(string, _encoding));
+ }
+
+ void writeAll(Iterable objects, [String separator = ""]) {
+ Iterator iterator = objects.iterator;
+ if (!iterator.moveNext()) return;
+ if (separator.isEmpty) {
+ do {
+ write(iterator.current);
+ } while (iterator.moveNext());
+ } else {
+ write(iterator.current);
+ while (iterator.moveNext()) {
+ write(separator);
+ write(iterator.current);
+ }
+ }
+ }
+
+ void writeln([Object obj = ""]) {
+ write(obj);
+ write("\n");
+ }
+
+ void writeCharCode(int charCode) {
+ write(new String.fromCharCode(charCode));
+ }
+}
« no previous file with comments | « no previous file | sdk/lib/io/websocket.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698