Index: lib/src/byte_accumulator_sink.dart |
diff --git a/lib/src/byte_accumulator_sink.dart b/lib/src/byte_accumulator_sink.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..985b71c01dae9e3edb77a4328b88f684573eddc4 |
--- /dev/null |
+++ b/lib/src/byte_accumulator_sink.dart |
@@ -0,0 +1,43 @@ |
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+import 'dart:convert'; |
+import 'dart:typed_data'; |
+ |
+import 'package:typed_data/typed_data.dart'; |
+ |
+/// A sink that provides synchronous access to the concatenated bytes passed to |
Lasse Reichstein Nielsen
2016/04/22 09:15:13
I don't think you need to say "synchronous" here.
nweiz
2016/04/22 21:19:56
Done.
|
+/// it. |
+class ByteAccumulatorSink extends ByteConversionSinkBase { |
+ /// The bytes accumulated so far. |
+ /// |
+ /// This should not be modified, although this restriction isn't enforced. |
Lasse Reichstein Nielsen
2016/04/22 09:15:13
More precisely:
The returned `Uint8List` is viewi
nweiz
2016/04/22 21:19:56
Done.
|
+ Uint8List get bytes => new Uint8List.view(_buffer.buffer, 0, _buffer.length); |
+ final _buffer = new Uint8Buffer(); |
Lasse Reichstein Nielsen
2016/04/22 09:15:13
Empty line before
nweiz
2016/04/22 21:19:56
Done.
|
+ |
+ /// Whether [close] has been called. |
+ bool get isClosed => _isClosed; |
Lasse Reichstein Nielsen
2016/04/22 09:15:13
As usual I prefer the fields before the getters. I
nweiz
2016/04/22 21:19:56
I think of fields as a special case of getters, so
|
+ var _isClosed = false; |
+ |
+ void add(List<int> bytes) { |
+ if (_isClosed) { |
+ throw new StateError("Can't add to a closed sink."); |
+ } |
+ |
+ _buffer.addAll(bytes); |
+ } |
+ |
+ void addSlice(List<int> chunk, int start, int end, bool isLast) { |
+ if (_isClosed) { |
+ throw new StateError("Can't add to a closed sink."); |
+ } |
+ |
+ _buffer.addAll(chunk, start, end); |
+ if (isLast) _isClosed = true; |
+ } |
+ |
+ void close() { |
+ _isClosed = true; |
+ } |
+} |