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