OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 import 'dart:convert'; |
| 6 |
| 7 /// A sink that provides access to the concatenated strings passed to it. |
| 8 /// |
| 9 /// See also [StringConversionSink.withCallback]. |
| 10 class StringAccumulatorSink extends StringConversionSinkBase { |
| 11 /// The string accumulated so far. |
| 12 String get string => _buffer.toString(); |
| 13 final _buffer = new StringBuffer(); |
| 14 |
| 15 /// Whether [close] has been called. |
| 16 bool get isClosed => _isClosed; |
| 17 var _isClosed = false; |
| 18 |
| 19 /// Empties [string]. |
| 20 /// |
| 21 /// This can be used to avoid double-processing data. |
| 22 void clear() { |
| 23 _buffer.clear(); |
| 24 } |
| 25 |
| 26 void add(String chunk) { |
| 27 if (_isClosed) { |
| 28 throw new StateError("Can't add to a closed sink."); |
| 29 } |
| 30 |
| 31 _buffer.write(chunk); |
| 32 } |
| 33 |
| 34 void addSlice(String chunk, int start, int end, bool isLast) { |
| 35 if (_isClosed) { |
| 36 throw new StateError("Can't add to a closed sink."); |
| 37 } |
| 38 |
| 39 _buffer.write(chunk.substring(start, end)); |
| 40 if (isLast) _isClosed = true; |
| 41 } |
| 42 |
| 43 void close() { |
| 44 _isClosed = true; |
| 45 } |
| 46 } |
OLD | NEW |