Index: lib/src/base64/encoder_sink.dart |
diff --git a/lib/src/base64/encoder_sink.dart b/lib/src/base64/encoder_sink.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6e726e785877256c876429d6bece60e9c5f9f72b |
--- /dev/null |
+++ b/lib/src/base64/encoder_sink.dart |
@@ -0,0 +1,56 @@ |
+// Copyright (c) 2015, 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. |
+ |
+library crypto.base64.encoder_sink; |
+ |
+import 'dart:convert'; |
+ |
+import 'encoder.dart'; |
+ |
+/// A [ChunkedConversionSink] for encoding chunks of data to Base64. |
+class Base64EncoderSink extends ChunkedConversionSink<List<int>> { |
+ /// The encoder used to encode each chunk. |
+ final Base64Encoder _encoder; |
+ |
+ /// The underlying sink to which to emit the encoded strings. |
+ final ChunkedConversionSink<String> _outSink; |
+ |
+ /// The buffer of as-yet-unconverted bytes. |
+ /// |
+ /// This is used to ensure that we don't generate interstitial padding |
+ /// characters. |
+ final _buffer = new List<int>(); |
+ |
+ /// The length of [_buffer]; that is, the number of unconverted bytes. |
+ var _bufferCount = 0; |
+ |
+ Base64EncoderSink(this._outSink, urlSafe, addLineSeparator) |
+ : _encoder = new Base64Encoder( |
+ urlSafe: urlSafe, addLineSeparator: addLineSeparator); |
+ |
+ void add(List<int> chunk) { |
+ var nextBufferCount = (chunk.length + _bufferCount) % 3; |
+ var decodableLength = _bufferCount + chunk.length - nextBufferCount; |
+ |
+ if (_bufferCount + chunk.length > _buffer.length) { |
+ _buffer.replaceRange(_bufferCount, _buffer.length, |
+ chunk.sublist(0, _buffer.length - _bufferCount)); |
+ _buffer.addAll(chunk.sublist(_buffer.length - _bufferCount)); |
+ } else { |
+ _buffer.replaceRange(_bufferCount, _bufferCount + chunk.length, chunk); |
+ } |
+ |
+ _outSink.add(_encoder.convert(_buffer, 0, decodableLength)); |
+ _buffer.removeRange(0, decodableLength); |
+ _bufferCount = nextBufferCount; |
+ } |
+ |
+ void close() { |
+ if (_bufferCount > 0) { |
+ _outSink.add(_encoder.convert(_buffer.sublist(0, _bufferCount))); |
+ } |
+ _outSink.close(); |
+ } |
+} |
+ |