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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « lib/src/base64/encoder.dart ('k') | lib/src/crypto_utils.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2015, 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 library crypto.base64.encoder_sink;
6
7 import 'dart:convert';
8
9 import 'encoder.dart';
10
11 /// A [ChunkedConversionSink] for encoding chunks of data to Base64.
12 class Base64EncoderSink extends ChunkedConversionSink<List<int>> {
13 /// The encoder used to encode each chunk.
14 final Base64Encoder _encoder;
15
16 /// The underlying sink to which to emit the encoded strings.
17 final ChunkedConversionSink<String> _outSink;
18
19 /// The buffer of as-yet-unconverted bytes.
20 ///
21 /// This is used to ensure that we don't generate interstitial padding
22 /// characters.
23 final _buffer = new List<int>();
24
25 /// The length of [_buffer]; that is, the number of unconverted bytes.
26 var _bufferCount = 0;
27
28 Base64EncoderSink(this._outSink, urlSafe, addLineSeparator)
29 : _encoder = new Base64Encoder(
30 urlSafe: urlSafe, addLineSeparator: addLineSeparator);
31
32 void add(List<int> chunk) {
33 var nextBufferCount = (chunk.length + _bufferCount) % 3;
34 var decodableLength = _bufferCount + chunk.length - nextBufferCount;
35
36 if (_bufferCount + chunk.length > _buffer.length) {
37 _buffer.replaceRange(_bufferCount, _buffer.length,
38 chunk.sublist(0, _buffer.length - _bufferCount));
39 _buffer.addAll(chunk.sublist(_buffer.length - _bufferCount));
40 } else {
41 _buffer.replaceRange(_bufferCount, _bufferCount + chunk.length, chunk);
42 }
43
44 _outSink.add(_encoder.convert(_buffer, 0, decodableLength));
45 _buffer.removeRange(0, decodableLength);
46 _bufferCount = nextBufferCount;
47 }
48
49 void close() {
50 if (_bufferCount > 0) {
51 _outSink.add(_encoder.convert(_buffer.sublist(0, _bufferCount)));
52 }
53 _outSink.close();
54 }
55 }
56
OLDNEW
« 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