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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 // Encoder | 63 // Encoder |
64 // ------------------------------------------------------------------------ | 64 // ------------------------------------------------------------------------ |
65 | 65 |
66 /** | 66 /** |
67 * Base64 and base64url encoding converter. | 67 * Base64 and base64url encoding converter. |
68 * | 68 * |
69 * Encodes lists of bytes using base64 or base64url encoding. | 69 * Encodes lists of bytes using base64 or base64url encoding. |
70 * | 70 * |
71 * The results are ASCII strings using a restricted alphabet. | 71 * The results are ASCII strings using a restricted alphabet. |
72 */ | 72 */ |
73 class Base64Encoder extends Converter<List<int>, String> { | 73 class Base64Encoder extends |
| 74 ChunkedConverter<List<int>, String, List<int>, String> { |
74 final bool _urlSafe; | 75 final bool _urlSafe; |
75 | 76 |
76 const Base64Encoder() : _urlSafe = false; | 77 const Base64Encoder() : _urlSafe = false; |
77 const Base64Encoder.urlSafe() : _urlSafe = true; | 78 const Base64Encoder.urlSafe() : _urlSafe = true; |
78 | 79 |
79 String convert(List<int> input) { | 80 String convert(List<int> input) { |
80 if (input.isEmpty) return ""; | 81 if (input.isEmpty) return ""; |
81 var encoder = new _Base64Encoder(_urlSafe); | 82 var encoder = new _Base64Encoder(_urlSafe); |
82 Uint8List buffer = encoder.encode(input, 0, input.length, true); | 83 Uint8List buffer = encoder.encode(input, 0, input.length, true); |
83 return new String.fromCharCodes(buffer); | 84 return new String.fromCharCodes(buffer); |
84 } | 85 } |
85 | 86 |
86 ByteConversionSink startChunkedConversion(Sink<String> sink) { | 87 ByteConversionSink startChunkedConversion(Sink<String> sink) { |
87 if (sink is StringConversionSink) { | 88 if (sink is StringConversionSink) { |
88 return new _Utf8Base64EncoderSink(sink.asUtf8Sink(false), _urlSafe); | 89 return new _Utf8Base64EncoderSink(sink.asUtf8Sink(false), _urlSafe); |
89 } | 90 } |
90 return new _AsciiBase64EncoderSink(sink, _urlSafe); | 91 return new _AsciiBase64EncoderSink(sink, _urlSafe); |
91 } | 92 } |
92 | |
93 Stream<String> bind(Stream<List<int>> stream) { | |
94 return new Stream<String>.eventTransformed( | |
95 stream, | |
96 (EventSink sink) => | |
97 new _ConverterStreamEventSink<List<int>, String>( | |
98 this, sink)); | |
99 } | |
100 } | 93 } |
101 | 94 |
102 /** | 95 /** |
103 * Helper class for encoding bytes to base64. | 96 * Helper class for encoding bytes to base64. |
104 */ | 97 */ |
105 class _Base64Encoder { | 98 class _Base64Encoder { |
106 /** The RFC 4648 base64 encoding alphabet. */ | 99 /** The RFC 4648 base64 encoding alphabet. */ |
107 static const String _base64Alphabet = | 100 static const String _base64Alphabet = |
108 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | 101 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
109 | 102 |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 // Decoder | 334 // Decoder |
342 // ------------------------------------------------------------------------ | 335 // ------------------------------------------------------------------------ |
343 | 336 |
344 /** | 337 /** |
345 * Decoder for base64 encoded data. | 338 * Decoder for base64 encoded data. |
346 * | 339 * |
347 * This decoder accepts both base64 and base64url ("url-safe") encodings. | 340 * This decoder accepts both base64 and base64url ("url-safe") encodings. |
348 * | 341 * |
349 * The encoding is required to be properly padded. | 342 * The encoding is required to be properly padded. |
350 */ | 343 */ |
351 class Base64Decoder extends Converter<String, List<int>> { | 344 class Base64Decoder extends |
| 345 ChunkedConverter<String, List<int>, String, List<int>> { |
| 346 |
352 const Base64Decoder(); | 347 const Base64Decoder(); |
353 | 348 |
354 List<int> convert(String input, [int start = 0, int end]) { | 349 List<int> convert(String input, [int start = 0, int end]) { |
355 end = RangeError.checkValidRange(start, end, input.length); | 350 end = RangeError.checkValidRange(start, end, input.length); |
356 if (start == end) return new Uint8List(0); | 351 if (start == end) return new Uint8List(0); |
357 var decoder = new _Base64Decoder(); | 352 var decoder = new _Base64Decoder(); |
358 Uint8List buffer = decoder.decode(input, start, end); | 353 Uint8List buffer = decoder.decode(input, start, end); |
359 decoder.close(input, end); | 354 decoder.close(input, end); |
360 return buffer; | 355 return buffer; |
361 } | 356 } |
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
748 end = RangeError.checkValidRange(start, end, string.length); | 743 end = RangeError.checkValidRange(start, end, string.length); |
749 if (start == end) return; | 744 if (start == end) return; |
750 Uint8List buffer = _decoder.decode(string, start, end); | 745 Uint8List buffer = _decoder.decode(string, start, end); |
751 if (buffer != null) _sink.add(buffer); | 746 if (buffer != null) _sink.add(buffer); |
752 if (isLast) { | 747 if (isLast) { |
753 _decoder.close(string, end); | 748 _decoder.close(string, end); |
754 _sink.close(); | 749 _sink.close(); |
755 } | 750 } |
756 } | 751 } |
757 } | 752 } |
OLD | NEW |