| 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 utility |
| 9 * functions for writing to the StreamConsumer directly. The [IOSink] | 9 * functions for writing to the StreamConsumer directly. The [IOSink] |
| 10 * buffers the input given by [add] and [addString] and will delay a [consume] | 10 * buffers the input given by [add] and [addString] and will delay a [consume] |
| 11 * or [addStream] until the buffer is flushed. | 11 * or [addStream] until the buffer is flushed. |
| 12 * | 12 * |
| 13 * When the [IOSink] is bound to a stream (through either [consume] | 13 * When the [IOSink] is bound to a stream (through either [consume] |
| 14 * or [addStream]) any call to the [IOSink] will throw a | 14 * or [addStream]) any call to the [IOSink] will throw a |
| 15 * [StateError]. | 15 * [StateError]. |
| 16 */ | 16 */ |
| 17 class IOSink<T> implements StreamConsumer<List<int>, T> { | 17 abstract class IOSink<T> implements StreamConsumer<List<int>, T> { |
| 18 factory IOSink(StreamConsumer<List<int>, T> target) |
| 19 => new _IOSinkImpl(target); |
| 20 |
| 21 /** |
| 22 * Provide functionality for piping to the [IOSink]. |
| 23 */ |
| 24 Future<T> consume(Stream<List<int>> stream); |
| 25 |
| 26 /** |
| 27 * Like [consume], but will not close the target when done. |
| 28 */ |
| 29 Future<T> addStream(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 |
| 41 /** |
| 42 * Close the target. |
| 43 */ |
| 44 void close(); |
| 45 |
| 46 /** |
| 47 * Get future that will complete when all data has been written to |
| 48 * the IOSink and it has been closed. |
| 49 */ |
| 50 Future<T> get done; |
| 51 } |
| 52 |
| 53 |
| 54 class _IOSinkImpl<T> implements IOSink<T> { |
| 18 final StreamConsumer<List<int>, T> _target; | 55 final StreamConsumer<List<int>, T> _target; |
| 19 | 56 |
| 20 StreamController<List<int>> _controllerInstance; | 57 StreamController<List<int>> _controllerInstance; |
| 21 Future<T> _pipeFuture; | 58 Future<T> _pipeFuture; |
| 22 StreamSubscription<List<int>> _bindSubscription; | 59 StreamSubscription<List<int>> _bindSubscription; |
| 23 bool _paused = true; | 60 bool _paused = true; |
| 24 | 61 |
| 25 IOSink(StreamConsumer<List<int>, T> target) : _target = target; | 62 _IOSinkImpl(StreamConsumer<List<int>, T> target) : _target = target; |
| 26 | 63 |
| 27 /** | |
| 28 * Provide functionality for piping to the [IOSink]. | |
| 29 */ | |
| 30 Future<T> consume(Stream<List<int>> stream) { | 64 Future<T> consume(Stream<List<int>> stream) { |
| 31 if (_isBound) { | 65 if (_isBound) { |
| 32 throw new StateError("IOSink is already bound to a stream"); | 66 throw new StateError("IOSink is already bound to a stream"); |
| 33 } | 67 } |
| 34 return _fillFromStream(stream); | 68 return _fillFromStream(stream); |
| 35 } | 69 } |
| 36 | 70 |
| 37 /** | |
| 38 * Like [consume], but will not close the target when done. | |
| 39 */ | |
| 40 Future<T> addStream(Stream<List<int>> stream) { | 71 Future<T> addStream(Stream<List<int>> stream) { |
| 41 if (_isBound) { | 72 if (_isBound) { |
| 42 throw new StateError("IOSink is already bound to a stream"); | 73 throw new StateError("IOSink is already bound to a stream"); |
| 43 } | 74 } |
| 44 return _fillFromStream(stream, unbind: true); | 75 return _fillFromStream(stream, unbind: true); |
| 45 } | 76 } |
| 46 | 77 |
| 47 /** | |
| 48 * Write a list of bytes to the target. | |
| 49 */ | |
| 50 void add(List<int> data) { | 78 void add(List<int> data) { |
| 51 if (_isBound) { | 79 if (_isBound) { |
| 52 throw new StateError("IOSink is already bound to a stream"); | 80 throw new StateError("IOSink is already bound to a stream"); |
| 53 } | 81 } |
| 54 _controller.add(data); | 82 _controller.add(data); |
| 55 } | 83 } |
| 56 | 84 |
| 57 /** | |
| 58 * Write a String to the target. | |
| 59 */ | |
| 60 void addString(String string, [Encoding encoding = Encoding.UTF_8]) { | 85 void addString(String string, [Encoding encoding = Encoding.UTF_8]) { |
| 61 add(_encodeString(string, encoding)); | 86 add(_encodeString(string, encoding)); |
| 62 } | 87 } |
| 63 | 88 |
| 64 /** | |
| 65 * Close the target. | |
| 66 */ | |
| 67 void close() { | 89 void close() { |
| 68 if (_isBound) { | 90 if (_isBound) { |
| 69 throw new StateError("IOSink is already bound to a stream"); | 91 throw new StateError("IOSink is already bound to a stream"); |
| 70 } | 92 } |
| 71 _controller.close(); | 93 _controller.close(); |
| 72 } | 94 } |
| 73 | 95 |
| 74 /** | |
| 75 * Get future that will complete when all data has been written to | |
| 76 * the IOSink and it has been closed. | |
| 77 */ | |
| 78 Future<T> get done { | 96 Future<T> get done { |
| 79 _controller; | 97 _controller; |
| 80 return _pipeFuture.then((_) => this); | 98 return _pipeFuture.then((_) => this); |
| 81 } | 99 } |
| 82 | 100 |
| 83 StreamController<List<int>> get _controller { | 101 StreamController<List<int>> get _controller { |
| 84 if (_controllerInstance == null) { | 102 if (_controllerInstance == null) { |
| 85 _controllerInstance = new StreamController<List<int>>( | 103 _controllerInstance = new StreamController<List<int>>( |
| 86 onPauseStateChange: _onPauseStateChange, | 104 onPauseStateChange: _onPauseStateChange, |
| 87 onSubscriptionStateChange: _onSubscriptionStateChange); | 105 onSubscriptionStateChange: _onSubscriptionStateChange); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 if (unbind) { | 182 if (unbind) { |
| 165 _pipeFuture | 183 _pipeFuture |
| 166 .then((_) => completeUnbind(), | 184 .then((_) => completeUnbind(), |
| 167 onError: (error) => completeUnbind(error)); | 185 onError: (error) => completeUnbind(error)); |
| 168 return unbindCompleter.future; | 186 return unbindCompleter.future; |
| 169 } else { | 187 } else { |
| 170 return _pipeFuture.then((_) => this); | 188 return _pipeFuture.then((_) => this); |
| 171 } | 189 } |
| 172 } | 190 } |
| 173 } | 191 } |
| OLD | NEW |