| OLD | NEW |
| 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 * [writeStream] 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 [writeStream]) 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, |
| 19 => new _IOSinkImpl(target); | 20 {Encoding encoding: Encoding.UTF_8}) |
| 21 => new _IOSinkImpl(target, encoding); |
| 22 |
| 23 /** |
| 24 * The [Encoding] used when writing strings. Depending on the |
| 25 * underlying consumer this property might be mutable. |
| 26 */ |
| 27 Encoding encoding; |
| 28 |
| 29 /** |
| 30 * Writes the bytes uninterpreted to the consumer. |
| 31 */ |
| 32 void writeBytes(List<int> data); |
| 20 | 33 |
| 21 /** | 34 /** |
| 22 * Provide functionality for piping to the [IOSink]. | 35 * Provide functionality for piping to the [IOSink]. |
| 23 */ | 36 */ |
| 24 Future<T> consume(Stream<List<int>> stream); | 37 Future<T> consume(Stream<List<int>> stream); |
| 25 | 38 |
| 26 /** | 39 /** |
| 27 * Like [consume], but will not close the target when done. | 40 * Like [consume], but will not close the target when done. |
| 28 */ | 41 */ |
| 29 Future<T> addStream(Stream<List<int>> stream); | 42 Future<T> writeStream(Stream<List<int>> stream); |
| 30 | |
| 31 /** | |
| 32 * Write a list of bytes to the target. | |
| 33 */ | |
| 34 void add(List<int> data); | |
| 35 | |
| 36 /** | |
| 37 * Write a String to the target. | |
| 38 */ | |
| 39 void addString(String string, [Encoding encoding = Encoding.UTF_8]); | |
| 40 | 43 |
| 41 /** | 44 /** |
| 42 * Close the target. | 45 * Close the target. |
| 43 */ | 46 */ |
| 44 void close(); | 47 void close(); |
| 45 | 48 |
| 46 /** | 49 /** |
| 47 * Get future that will complete when all data has been written to | 50 * Get future that will complete when all data has been written to |
| 48 * the IOSink and it has been closed. | 51 * the IOSink and it has been closed. |
| 49 */ | 52 */ |
| 50 Future<T> get done; | 53 Future<T> get done; |
| 51 } | 54 } |
| 52 | 55 |
| 53 | 56 |
| 54 class _IOSinkImpl<T> implements IOSink<T> { | 57 class _IOSinkImpl<T> implements IOSink<T> { |
| 55 final StreamConsumer<List<int>, T> _target; | 58 final StreamConsumer<List<int>, T> _target; |
| 56 | 59 |
| 57 Completer _writeStreamCompleter; | 60 Completer _writeStreamCompleter; |
| 58 StreamController<List<int>> _controllerInstance; | 61 StreamController<List<int>> _controllerInstance; |
| 59 Future<T> _pipeFuture; | 62 Future<T> _pipeFuture; |
| 60 StreamSubscription<List<int>> _bindSubscription; | 63 StreamSubscription<List<int>> _bindSubscription; |
| 61 bool _paused = true; | 64 bool _paused = true; |
| 65 bool _encodingMutable = true; |
| 62 | 66 |
| 63 _IOSinkImpl(StreamConsumer<List<int>, T> target) : _target = target; | 67 _IOSinkImpl(StreamConsumer<List<int>, T> this._target, this._encoding); |
| 68 |
| 69 Encoding _encoding; |
| 70 |
| 71 Encoding get encoding => _encoding; |
| 72 void set encoding(Encoding value) { |
| 73 if (!_encodingMutable) { |
| 74 throw new StateError("IOSink encoding is not mutable"); |
| 75 } |
| 76 _encoding = value; |
| 77 } |
| 78 |
| 79 void write(Object obj) { |
| 80 // This comment is copied from runtime/lib/string_buffer_patch.dart. |
| 81 // TODO(srdjan): The following four lines could be replaced by |
| 82 // '$obj', but apparently this is too slow on the Dart VM. |
| 83 String string; |
| 84 if (obj is String) { |
| 85 string = obj; |
| 86 } else { |
| 87 string = obj.toString(); |
| 88 if (string is! String) { |
| 89 throw new ArgumentError('toString() did not return a string'); |
| 90 } |
| 91 } |
| 92 if (string.isEmpty) return; |
| 93 writeBytes(_encodeString(string, _encoding)); |
| 94 } |
| 95 |
| 96 void writeAll(Iterable objects) { |
| 97 for (Object obj in objects) write(obj); |
| 98 } |
| 99 |
| 100 void writeln(Object obj) { |
| 101 write(obj); |
| 102 write("\n"); |
| 103 } |
| 104 |
| 105 void writeCharCode(int charCode) { |
| 106 write(new String.fromCharCode(charCode)); |
| 107 } |
| 108 |
| 109 void writeBytes(List<int> data) { |
| 110 if (_isBound) { |
| 111 throw new StateError("IOSink is already bound to a stream"); |
| 112 } |
| 113 _controller.add(data); |
| 114 } |
| 64 | 115 |
| 65 Future<T> consume(Stream<List<int>> stream) { | 116 Future<T> consume(Stream<List<int>> stream) { |
| 66 if (_isBound) { | 117 if (_isBound) { |
| 67 throw new StateError("IOSink is already bound to a stream"); | 118 throw new StateError("IOSink is already bound to a stream"); |
| 68 } | 119 } |
| 69 return _fillFromStream(stream); | 120 return _fillFromStream(stream); |
| 70 } | 121 } |
| 71 | 122 |
| 72 Future<T> addStream(Stream<List<int>> stream) { | 123 Future<T> writeStream(Stream<List<int>> stream) { |
| 73 if (_isBound) { | 124 if (_isBound) { |
| 74 throw new StateError("IOSink is already bound to a stream"); | 125 throw new StateError("IOSink is already bound to a stream"); |
| 75 } | 126 } |
| 76 return _fillFromStream(stream, unbind: true); | 127 return _fillFromStream(stream, unbind: true); |
| 77 } | 128 } |
| 78 | 129 |
| 79 void add(List<int> data) { | |
| 80 if (_isBound) { | |
| 81 throw new StateError("IOSink is already bound to a stream"); | |
| 82 } | |
| 83 _controller.add(data); | |
| 84 } | |
| 85 | |
| 86 void addString(String string, [Encoding encoding = Encoding.UTF_8]) { | |
| 87 add(_encodeString(string, encoding)); | |
| 88 } | |
| 89 | |
| 90 void close() { | 130 void close() { |
| 91 if (_isBound) { | 131 if (_isBound) { |
| 92 throw new StateError("IOSink is already bound to a stream"); | 132 throw new StateError("IOSink is already bound to a stream"); |
| 93 } | 133 } |
| 94 _controller.close(); | 134 _controller.close(); |
| 95 } | 135 } |
| 96 | 136 |
| 97 Future<T> get done { | 137 Future<T> get done { |
| 98 _controller; | 138 _controller; |
| 99 return _pipeFuture.then((_) => this); | 139 return _pipeFuture.then((_) => this); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 }, | 224 }, |
| 185 onError: _controller.signalError); | 225 onError: _controller.signalError); |
| 186 if (_paused) _pause(); | 226 if (_paused) _pause(); |
| 187 if (unbind) { | 227 if (unbind) { |
| 188 return _writeStreamCompleter.future; | 228 return _writeStreamCompleter.future; |
| 189 } else { | 229 } else { |
| 190 return _pipeFuture.then((_) => this); | 230 return _pipeFuture.then((_) => this); |
| 191 } | 231 } |
| 192 } | 232 } |
| 193 } | 233 } |
| OLD | NEW |