| 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 | 8 * Helper class to wrap a [StreamConsumer<List<int>>] and provide |
| 9 * utility functions for writing to the StreamConsumer directly. The | 9 * utility functions for writing to the StreamConsumer directly. The |
| 10 * [IOSink] buffers the input given by [write], [writeAll], [writeln], | 10 * [IOSink] buffers the input given by [write], [writeAll], [writeln], |
| 11 * [writeCharCode] and [add] and will delay a [consume] or | 11 * [writeCharCode] and [add] and will delay a [consume] or |
| 12 * [writeStream] until the buffer is flushed. | 12 * [writeStream] until the buffer is flushed. |
| 13 * | 13 * |
| 14 * When the [IOSink] is bound to a stream (through either [consume] | 14 * When the [IOSink] is bound to a stream (through either [consume] |
| 15 * or [writeStream]) any call to the [IOSink] will throw a | 15 * or [writeStream]) any call to the [IOSink] will throw a |
| 16 * [StateError]. | 16 * [StateError]. |
| 17 */ | 17 */ |
| 18 abstract class IOSink<T> | 18 abstract class IOSink<T> |
| 19 implements StreamConsumer<List<int>, T>, StringSink, EventSink<List<int>> { | 19 implements StreamConsumer<List<int>>, StringSink, EventSink<List<int>> { |
| 20 factory IOSink(StreamConsumer<List<int>, T> target, | 20 factory IOSink(StreamConsumer<List<int>> target, |
| 21 {Encoding encoding: Encoding.UTF_8}) | 21 {Encoding encoding: Encoding.UTF_8}) |
| 22 => new _IOSinkImpl(target, encoding); | 22 => new _IOSinkImpl(target, encoding); |
| 23 | 23 |
| 24 /** | 24 /** |
| 25 * The [Encoding] used when writing strings. Depending on the | 25 * The [Encoding] used when writing strings. Depending on the |
| 26 * underlying consumer this property might be mutable. | 26 * underlying consumer this property might be mutable. |
| 27 */ | 27 */ |
| 28 Encoding encoding; | 28 Encoding encoding; |
| 29 | 29 |
| 30 /** | 30 /** |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 | 63 |
| 64 /** | 64 /** |
| 65 * Get future that will complete when all data has been written to | 65 * Get future that will complete when all data has been written to |
| 66 * the IOSink and it has been closed. | 66 * the IOSink and it has been closed. |
| 67 */ | 67 */ |
| 68 Future<T> get done; | 68 Future<T> get done; |
| 69 } | 69 } |
| 70 | 70 |
| 71 | 71 |
| 72 class _IOSinkImpl<T> implements IOSink<T> { | 72 class _IOSinkImpl<T> implements IOSink<T> { |
| 73 final StreamConsumer<List<int>, T> _target; | 73 final StreamConsumer<List<int>> _target; |
| 74 | 74 |
| 75 Completer _writeStreamCompleter; | 75 Completer _writeStreamCompleter; |
| 76 StreamController<List<int>> _controllerInstance; | 76 StreamController<List<int>> _controllerInstance; |
| 77 Future<T> _pipeFuture; | 77 Future<T> _pipeFuture; |
| 78 StreamSubscription<List<int>> _bindSubscription; | 78 StreamSubscription<List<int>> _bindSubscription; |
| 79 bool _paused = true; | 79 bool _paused = true; |
| 80 bool _encodingMutable = true; | 80 bool _encodingMutable = true; |
| 81 | 81 |
| 82 _IOSinkImpl(StreamConsumer<List<int>, T> this._target, this._encoding); | 82 _IOSinkImpl(StreamConsumer<List<int>> this._target, this._encoding); |
| 83 | 83 |
| 84 Encoding _encoding; | 84 Encoding _encoding; |
| 85 | 85 |
| 86 Encoding get encoding => _encoding; | 86 Encoding get encoding => _encoding; |
| 87 | 87 |
| 88 void set encoding(Encoding value) { | 88 void set encoding(Encoding value) { |
| 89 if (!_encodingMutable) { | 89 if (!_encodingMutable) { |
| 90 throw new StateError("IOSink encoding is not mutable"); | 90 throw new StateError("IOSink encoding is not mutable"); |
| 91 } | 91 } |
| 92 _encoding = value; | 92 _encoding = value; |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 }, | 264 }, |
| 265 onError: _controller.addError); | 265 onError: _controller.addError); |
| 266 if (_paused) _pause(); | 266 if (_paused) _pause(); |
| 267 if (unbind) { | 267 if (unbind) { |
| 268 return _writeStreamCompleter.future; | 268 return _writeStreamCompleter.future; |
| 269 } else { | 269 } else { |
| 270 return _pipeFuture; | 270 return _pipeFuture; |
| 271 } | 271 } |
| 272 } | 272 } |
| 273 } | 273 } |
| OLD | NEW |