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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of dart.io; 5 part of dart.io;
6 6
7 /** 7 /**
8 * Helper class to wrap a [StreamConsumer<List<int>, T>] and provide utility 8 * Helper class to wrap a [StreamConsumer<List<int>, T>] and provide
9 * functions for writing to the StreamConsumer directly. The [IOSink] 9 * utility functions for writing to the StreamConsumer directly. The
10 * buffers the input given by [add] and [addString] and will delay a [consume] 10 * [IOSink] buffers the input given by [write], [writeAll], [writeln],
11 * or [addStream] until the buffer is flushed. 11 * [writeCharCode] and [writeBytes] and will delay a [consume] or
12 * [addStream] until the buffer is flushed.
12 * 13 *
13 * When the [IOSink] is bound to a stream (through either [consume] 14 * When the [IOSink] is bound to a stream (through either [consume]
14 * or [addStream]) any call to the [IOSink] will throw a 15 * or [addStream]) any call to the [IOSink] will throw a
15 * [StateError]. 16 * [StateError].
16 */ 17 */
17 abstract class IOSink<T> implements StreamConsumer<List<int>, T> { 18 abstract class IOSink<T> implements StreamConsumer<List<int>, T>, StringSink {
18 factory IOSink(StreamConsumer<List<int>, T> target) 19 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
19 => new _IOSinkImpl(target); 20 => new _IOSinkImpl(target, encoding);
21
22 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
23
24 void writeBytes(List<int> data);
25
26 Future<T> writeStream(Stream<List<int>> stream) => addStream(stream);
20 27
21 /** 28 /**
22 * Provide functionality for piping to the [IOSink]. 29 * Provide functionality for piping to the [IOSink].
23 */ 30 */
24 Future<T> consume(Stream<List<int>> stream); 31 Future<T> consume(Stream<List<int>> stream);
25 32
26 /** 33 /**
27 * Like [consume], but will not close the target when done. 34 * Like [consume], but will not close the target when done.
28 */ 35 */
29 Future<T> addStream(Stream<List<int>> stream); 36 Future<T> addStream(Stream<List<int>> stream);
30 37
31 /** 38 /**
32 * Write a list of bytes to the target. 39 * Write a list of bytes to the target.
40 *
41 * *Deprecated*. Use [writeBytes] instead.
33 */ 42 */
43 @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.
34 void add(List<int> data); 44 void add(List<int> data);
35 45
36 /** 46 /**
37 * Write a String to the target. 47 * Write a String to the target.
48 *
49 * *Deprecated*. Use [write] instead.
38 */ 50 */
51 @deprecated
39 void addString(String string, [Encoding encoding = Encoding.UTF_8]); 52 void addString(String string, [Encoding encoding = Encoding.UTF_8]);
40 53
41 /** 54 /**
42 * Close the target. 55 * Close the target.
43 */ 56 */
44 void close(); 57 void close();
45 58
46 /** 59 /**
47 * Get future that will complete when all data has been written to 60 * Get future that will complete when all data has been written to
48 * the IOSink and it has been closed. 61 * the IOSink and it has been closed.
49 */ 62 */
50 Future<T> get done; 63 Future<T> get done;
51 } 64 }
52 65
53 66
54 class _IOSinkImpl<T> implements IOSink<T> { 67 class _IOSinkImpl<T> implements IOSink<T> {
55 final StreamConsumer<List<int>, T> _target; 68 final StreamConsumer<List<int>, T> _target;
56 69
57 StreamController<List<int>> _controllerInstance; 70 StreamController<List<int>> _controllerInstance;
58 Future<T> _pipeFuture; 71 Future<T> _pipeFuture;
59 StreamSubscription<List<int>> _bindSubscription; 72 StreamSubscription<List<int>> _bindSubscription;
60 bool _paused = true; 73 bool _paused = true;
74 bool _encodingMutable = true;
61 75
62 _IOSinkImpl(StreamConsumer<List<int>, T> target) : _target = target; 76 _IOSinkImpl(StreamConsumer<List<int>, T> this._target, Encoding encoding)
77 : _encoding = encoding;
78
79 Encoding _encoding;
80
81 Encoding get encoding => _encoding;
82 void set encoding(Encoding value) {
83 if (!_encodingMutable) {
84 throw new StateError("IOSink encoding is not mutable");
85 }
86 _encoding = value;
87 }
88
89 void write(Object obj) {
90 // This comment is copied from runtime/lib/string_buffer_patch.dart.
91 // TODO(srdjan): The following four lines could be replaced by
92 // '$obj', but apparently this is too slow on the Dart VM.
93 String string;
94 if (obj is String) {
95 string = obj;
96 } else {
97 string = obj.toString();
98 if (string is! String) {
99 throw new ArgumentError('toString() did not return a string');
100 }
101 }
102 if (string.isEmpty) return;
103 writeBytes(_encodeString(string, _encoding));
104 }
105
106 void writeAll(Iterable objects) {
107 for (Object obj in objects) write(obj);
108 }
109
110 void writeln(Object obj) {
111 write(obj);
112 write("\n");
113 }
114
115 void writeCharCode(int charCode) {
116 write(new String.fromCharCode(charCode));
117 }
118
119 void writeBytes(List<int> data) {
120 if (_isBound) {
121 throw new StateError("IOSink is already bound to a stream");
122 }
123 _controller.add(data);
124 }
63 125
64 Future<T> consume(Stream<List<int>> stream) { 126 Future<T> consume(Stream<List<int>> stream) {
65 if (_isBound) { 127 if (_isBound) {
66 throw new StateError("IOSink is already bound to a stream"); 128 throw new StateError("IOSink is already bound to a stream");
67 } 129 }
68 return _fillFromStream(stream); 130 return _fillFromStream(stream);
69 } 131 }
70 132
71 Future<T> addStream(Stream<List<int>> stream) { 133 Future<T> addStream(Stream<List<int>> stream) {
72 if (_isBound) { 134 if (_isBound) {
73 throw new StateError("IOSink is already bound to a stream"); 135 throw new StateError("IOSink is already bound to a stream");
74 } 136 }
75 return _fillFromStream(stream, unbind: true); 137 return _fillFromStream(stream, unbind: true);
76 } 138 }
77 139
78 void add(List<int> data) { 140 void add(List<int> data) => writeBytes(data);
79 if (_isBound) {
80 throw new StateError("IOSink is already bound to a stream");
81 }
82 _controller.add(data);
83 }
84 141
85 void addString(String string, [Encoding encoding = Encoding.UTF_8]) { 142 void addString(String string, [Encoding encoding = Encoding.UTF_8]) {
86 add(_encodeString(string, encoding)); 143 writeBytes(_encodeString(string, encoding));
87 } 144 }
88 145
89 void close() { 146 void close() {
90 if (_isBound) { 147 if (_isBound) {
91 throw new StateError("IOSink is already bound to a stream"); 148 throw new StateError("IOSink is already bound to a stream");
92 } 149 }
93 _controller.close(); 150 _controller.close();
94 } 151 }
95 152
96 Future<T> get done { 153 Future<T> get done {
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 if (unbind) { 239 if (unbind) {
183 _pipeFuture 240 _pipeFuture
184 .then((_) => completeUnbind(), 241 .then((_) => completeUnbind(),
185 onError: (error) => completeUnbind(error)); 242 onError: (error) => completeUnbind(error));
186 return unbindCompleter.future; 243 return unbindCompleter.future;
187 } else { 244 } else {
188 return _pipeFuture.then((_) => this); 245 return _pipeFuture.then((_) => this);
189 } 246 }
190 } 247 }
191 } 248 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698