Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Unified Diff: lib/src/base64/encoder_sink.dart

Issue 1349373002: Improve the style of code in lib/. (Closed) Base URL: git@github.com:dart-lang/crypto.git@master
Patch Set: Code review changes Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/src/base64/encoder.dart ('k') | lib/src/crypto_utils.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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();
+ }
+}
+
« no previous file with comments | « lib/src/base64/encoder.dart ('k') | lib/src/crypto_utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698