OLD | NEW |
| (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 import 'dart:convert'; | |
6 | |
7 import 'decoder.dart'; | |
8 | |
9 /// This is deprecated. | |
10 /// | |
11 /// Use the `Base64Decoder` class in `dart:convert` instead. | |
12 @Deprecated("Will be removed in crypto 1.0.0.") | |
13 class Base64DecoderSink extends ChunkedConversionSink<String> { | |
14 /// The encoder used to decode each chunk. | |
15 final Base64Decoder _decoder = new Base64Decoder(); | |
16 | |
17 /// The underlying sink to which to emit the decoded strings. | |
18 final ChunkedConversionSink<List<int>> _outSink; | |
19 | |
20 /// The as-yet-unconverted text. | |
21 /// | |
22 /// This is used to handle text stopping partway through a four-character | |
23 /// 32-bit chunk. | |
24 String _unconverted = ""; | |
25 | |
26 Base64DecoderSink(this._outSink); | |
27 | |
28 void add(String chunk) { | |
29 if (chunk.isEmpty) return; | |
30 if (_unconverted.isNotEmpty) chunk = _unconverted + chunk; | |
31 chunk = chunk.replaceAll("%3D", "="); | |
32 | |
33 // The decodable length is the length of the initial substring comprising | |
34 // full four-character 32-bit chunks. Any leftovers are handled when [add] | |
35 // or [close] are next called. | |
36 var decodableLength = chunk.length; | |
37 if (chunk.length > 3 && chunk.contains("%", chunk.length - 2)) { | |
38 decodableLength = chunk.lastIndexOf("%"); | |
39 } | |
40 decodableLength -= decodableLength % 4; | |
41 | |
42 _unconverted = chunk.substring(decodableLength); | |
43 if (decodableLength > 0) { | |
44 _outSink.add(_decoder.convert(chunk.substring(0, decodableLength))); | |
45 } | |
46 } | |
47 | |
48 void close() { | |
49 if (_unconverted.isNotEmpty) _outSink.add(_decoder.convert(_unconverted)); | |
50 _outSink.close(); | |
51 } | |
52 } | |
OLD | NEW |