| 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 * An instance of [Base64Codec]. | 8 * An instance of [Base64Codec]. |
| 9 * | 9 * |
| 10 * This instance provides a convenient access to | 10 * This instance provides a convenient access to |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 // ------------------------------------------------------------------------ | 46 // ------------------------------------------------------------------------ |
| 47 // Encoder | 47 // Encoder |
| 48 // ------------------------------------------------------------------------ | 48 // ------------------------------------------------------------------------ |
| 49 | 49 |
| 50 /** | 50 /** |
| 51 * Base-64 encoding converter. | 51 * Base-64 encoding converter. |
| 52 * | 52 * |
| 53 * Encodes lists of bytes using base64 encoding. | 53 * Encodes lists of bytes using base64 encoding. |
| 54 * The result are ASCII strings using a restricted alphabet. | 54 * The result are ASCII strings using a restricted alphabet. |
| 55 */ | 55 */ |
| 56 class Base64Encoder extends Converter<List<int>, String> { | 56 class Base64Encoder extends |
| 57 ChunkedConverter<List<int>, String, List<int>, String> { |
| 58 |
| 57 const Base64Encoder(); | 59 const Base64Encoder(); |
| 58 | 60 |
| 59 String convert(List<int> input) { | 61 String convert(List<int> input) { |
| 60 if (input.isEmpty) return ""; | 62 if (input.isEmpty) return ""; |
| 61 var encoder = new _Base64Encoder(); | 63 var encoder = new _Base64Encoder(); |
| 62 Uint8List buffer = encoder.encode(input, 0, input.length, true); | 64 Uint8List buffer = encoder.encode(input, 0, input.length, true); |
| 63 return new String.fromCharCodes(buffer); | 65 return new String.fromCharCodes(buffer); |
| 64 } | 66 } |
| 65 | 67 |
| 66 ByteConversionSink startChunkedConversion(Sink<String> sink) { | 68 ByteConversionSink startChunkedConversion(Sink<String> sink) { |
| 67 if (sink is StringConversionSink) { | 69 if (sink is StringConversionSink) { |
| 68 return new _Utf8Base64EncoderSink(sink.asUtf8Sink(false)); | 70 return new _Utf8Base64EncoderSink(sink.asUtf8Sink(false)); |
| 69 } | 71 } |
| 70 return new _AsciiBase64EncoderSink(sink); | 72 return new _AsciiBase64EncoderSink(sink); |
| 71 } | 73 } |
| 72 | |
| 73 Stream<String> bind(Stream<List<int>> stream) { | |
| 74 return new Stream<String>.eventTransformed( | |
| 75 stream, | |
| 76 (EventSink sink) => | |
| 77 new _ConverterStreamEventSink<List<int>, String>(this, sink)); | |
| 78 } | |
| 79 } | 74 } |
| 80 | 75 |
| 81 /** | 76 /** |
| 82 * Helper class for encoding bytes to BASE-64. | 77 * Helper class for encoding bytes to BASE-64. |
| 83 */ | 78 */ |
| 84 class _Base64Encoder { | 79 class _Base64Encoder { |
| 85 /** The RFC 4648 base64 encoding alphabet. */ | 80 /** The RFC 4648 base64 encoding alphabet. */ |
| 86 static const String _base64Alphabet = | 81 static const String _base64Alphabet = |
| 87 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | 82 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 88 | 83 |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 if (buffer != null) { | 292 if (buffer != null) { |
| 298 _sink.addSlice(buffer, 0, buffer.length, isLast); | 293 _sink.addSlice(buffer, 0, buffer.length, isLast); |
| 299 } | 294 } |
| 300 } | 295 } |
| 301 } | 296 } |
| 302 | 297 |
| 303 // ------------------------------------------------------------------------ | 298 // ------------------------------------------------------------------------ |
| 304 // Decoder | 299 // Decoder |
| 305 // ------------------------------------------------------------------------ | 300 // ------------------------------------------------------------------------ |
| 306 | 301 |
| 307 class Base64Decoder extends Converter<String, List<int>> { | 302 class Base64Decoder extends |
| 303 ChunkedConverter<String, List<int>, String, List<int>> { |
| 304 |
| 308 const Base64Decoder(); | 305 const Base64Decoder(); |
| 309 | 306 |
| 310 List<int> convert(String input, [int start = 0, int end]) { | 307 List<int> convert(String input, [int start = 0, int end]) { |
| 311 end = RangeError.checkValidRange(start, end, input.length); | 308 end = RangeError.checkValidRange(start, end, input.length); |
| 312 if (start == end) return new Uint8List(0); | 309 if (start == end) return new Uint8List(0); |
| 313 var decoder = new _Base64Decoder(); | 310 var decoder = new _Base64Decoder(); |
| 314 Uint8List buffer = decoder.decode(input, start, end); | 311 Uint8List buffer = decoder.decode(input, start, end); |
| 315 decoder.close(input, end); | 312 decoder.close(input, end); |
| 316 return buffer; | 313 return buffer; |
| 317 } | 314 } |
| (...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 704 end = RangeError.checkValidRange(start, end, string.length); | 701 end = RangeError.checkValidRange(start, end, string.length); |
| 705 if (start == end) return; | 702 if (start == end) return; |
| 706 Uint8List buffer = _decoder.decode(string, start, end); | 703 Uint8List buffer = _decoder.decode(string, start, end); |
| 707 if (buffer != null) _sink.add(buffer); | 704 if (buffer != null) _sink.add(buffer); |
| 708 if (isLast) { | 705 if (isLast) { |
| 709 _decoder.close(string, end); | 706 _decoder.close(string, end); |
| 710 _sink.close(); | 707 _sink.close(); |
| 711 } | 708 } |
| 712 } | 709 } |
| 713 } | 710 } |
| OLD | NEW |