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 import 'dart:typed_data'; |
| 7 |
| 8 import 'package:typed_data/typed_data.dart'; |
| 9 |
| 10 /// A sink that provides access to the concatenated bytes passed to it. |
| 11 /// |
| 12 /// See also [ByteConversionSink.withCallback]. |
| 13 class ByteAccumulatorSink extends ByteConversionSinkBase { |
| 14 /// The bytes accumulated so far. |
| 15 /// |
| 16 /// The returned [Uint8List] is viewing a shared buffer, so it should not be |
| 17 /// changed and any bytes outside the view should not be accessed. |
| 18 Uint8List get bytes => new Uint8List.view(_buffer.buffer, 0, _buffer.length); |
| 19 |
| 20 final _buffer = new Uint8Buffer(); |
| 21 |
| 22 /// Whether [close] has been called. |
| 23 bool get isClosed => _isClosed; |
| 24 var _isClosed = false; |
| 25 |
| 26 /// Removes all bytes from [bytes]. |
| 27 /// |
| 28 /// This can be used to avoid double-processing data. |
| 29 void clear() { |
| 30 _buffer.clear(); |
| 31 } |
| 32 |
| 33 void add(List<int> bytes) { |
| 34 if (_isClosed) { |
| 35 throw new StateError("Can't add to a closed sink."); |
| 36 } |
| 37 |
| 38 _buffer.addAll(bytes); |
| 39 } |
| 40 |
| 41 void addSlice(List<int> chunk, int start, int end, bool isLast) { |
| 42 if (_isClosed) { |
| 43 throw new StateError("Can't add to a closed sink."); |
| 44 } |
| 45 |
| 46 _buffer.addAll(chunk, start, end); |
| 47 if (isLast) _isClosed = true; |
| 48 } |
| 49 |
| 50 void close() { |
| 51 _isClosed = true; |
| 52 } |
| 53 } |
OLD | NEW |