OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.convert; | 5 part of dart.convert; |
6 | 6 |
7 /** | 7 /** |
8 * A [base64](https://tools.ietf.org/html/rfc4648) encoder and decoder. | 8 * A [base64](https://tools.ietf.org/html/rfc4648) encoder and decoder. |
9 * | 9 * |
10 * It encodes using the default base64 alphabet, | 10 * It encodes using the default base64 alphabet, |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 // Encoder | 197 // Encoder |
198 // ------------------------------------------------------------------------ | 198 // ------------------------------------------------------------------------ |
199 | 199 |
200 /** | 200 /** |
201 * Base64 and base64url encoding converter. | 201 * Base64 and base64url encoding converter. |
202 * | 202 * |
203 * Encodes lists of bytes using base64 or base64url encoding. | 203 * Encodes lists of bytes using base64 or base64url encoding. |
204 * | 204 * |
205 * The results are ASCII strings using a restricted alphabet. | 205 * The results are ASCII strings using a restricted alphabet. |
206 */ | 206 */ |
207 class Base64Encoder extends Converter<List<int>, String> { | 207 class Base64Encoder extends Converter<List<int>, String> |
| 208 implements ChunkedConverter<List<int>, String, List<int>, String> { |
208 | 209 |
209 final bool _urlSafe; | 210 final bool _urlSafe; |
210 | 211 |
211 const Base64Encoder() : _urlSafe = false; | 212 const Base64Encoder() : _urlSafe = false; |
212 const Base64Encoder.urlSafe() : _urlSafe = true; | 213 const Base64Encoder.urlSafe() : _urlSafe = true; |
213 | 214 |
214 String convert(List<int> input) { | 215 String convert(List<int> input) { |
215 if (input.isEmpty) return ""; | 216 if (input.isEmpty) return ""; |
216 var encoder = new _Base64Encoder(_urlSafe); | 217 var encoder = new _Base64Encoder(_urlSafe); |
217 Uint8List buffer = encoder.encode(input, 0, input.length, true); | 218 Uint8List buffer = encoder.encode(input, 0, input.length, true); |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 // Decoder | 469 // Decoder |
469 // ------------------------------------------------------------------------ | 470 // ------------------------------------------------------------------------ |
470 | 471 |
471 /** | 472 /** |
472 * Decoder for base64 encoded data. | 473 * Decoder for base64 encoded data. |
473 * | 474 * |
474 * This decoder accepts both base64 and base64url ("url-safe") encodings. | 475 * This decoder accepts both base64 and base64url ("url-safe") encodings. |
475 * | 476 * |
476 * The encoding is required to be properly padded. | 477 * The encoding is required to be properly padded. |
477 */ | 478 */ |
478 class Base64Decoder extends Converter<String, List<int>> { | 479 class Base64Decoder extends Converter<String, List<int>> |
| 480 implements ChunkedConverter<String, List<int>, String, List<int>> { |
479 | 481 |
480 const Base64Decoder(); | 482 const Base64Decoder(); |
481 | 483 |
482 List<int> convert(String input, [int start = 0, int end]) { | 484 List<int> convert(String input, [int start = 0, int end]) { |
483 end = RangeError.checkValidRange(start, end, input.length); | 485 end = RangeError.checkValidRange(start, end, input.length); |
484 if (start == end) return new Uint8List(0); | 486 if (start == end) return new Uint8List(0); |
485 var decoder = new _Base64Decoder(); | 487 var decoder = new _Base64Decoder(); |
486 Uint8List buffer = decoder.decode(input, start, end); | 488 Uint8List buffer = decoder.decode(input, start, end); |
487 decoder.close(input, end); | 489 decoder.close(input, end); |
488 return buffer; | 490 return buffer; |
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
876 end = RangeError.checkValidRange(start, end, string.length); | 878 end = RangeError.checkValidRange(start, end, string.length); |
877 if (start == end) return; | 879 if (start == end) return; |
878 Uint8List buffer = _decoder.decode(string, start, end); | 880 Uint8List buffer = _decoder.decode(string, start, end); |
879 if (buffer != null) _sink.add(buffer); | 881 if (buffer != null) _sink.add(buffer); |
880 if (isLast) { | 882 if (isLast) { |
881 _decoder.close(string, end); | 883 _decoder.close(string, end); |
882 _sink.close(); | 884 _sink.close(); |
883 } | 885 } |
884 } | 886 } |
885 } | 887 } |
OLD | NEW |