| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 * Open-ended Encoding enum. | 8 * Open-ended Encoding enum. |
| 9 */ | 9 */ |
| 10 abstract class Encoding extends Codec<String, List<int>> { | 10 abstract class Encoding extends Codec<String, List<int>> { |
| 11 const Encoding(); | 11 const Encoding(); |
| 12 | 12 |
| 13 Converter<String, List<int>> get encoder; | 13 Converter<String, List<int>> get encoder; |
| 14 Converter<List<int>, String> get decoder; | 14 Converter<List<int>, String> get decoder; |
| 15 | 15 |
| 16 Future<String> decodeStream(Stream<List<int>> byteStream) { | 16 Future<String> decodeStream(Stream<List<int>> byteStream) { |
| 17 return byteStream | 17 return byteStream |
| 18 .transform(decoder) | 18 .transform(decoder) |
| 19 .fold(new StringBuffer(), (buffer, string) => buffer..write(string)) | 19 .fold(new StringBuffer(), (buffer, string) => buffer..write(string)) |
| 20 .then((buffer) => buffer.toString()); | 20 .then((buffer) => buffer.toString()); |
| 21 } | 21 } |
| 22 | 22 |
| 23 /** | 23 /** |
| 24 * Name of the encoding. | 24 * Name of the encoding. |
| 25 * | 25 * |
| 26 * If the encoding is standardized, this is the lower-case version of one of | 26 * If the encoding is standardized, this is the lower-case version of one of |
| 27 * the IANA official names for the character set (see | 27 * the IANA official names for the character set (see |
| 28 * http://www.iana.org/assignments/character-sets/character-sets.xml) | 28 * http://www.iana.org/assignments/character-sets/character-sets.xml) |
| 29 */ | 29 */ |
| 30 String get name; | 30 String get name; |
| 31 | 31 |
| 32 // All aliases (in lowercase) of supported encoding from | 32 // All aliases (in lowercase) of supported encoding from |
| 33 // http://www.iana.org/assignments/character-sets/character-sets.xml. | 33 // http://www.iana.org/assignments/character-sets/character-sets.xml. |
| 34 static Map<String, Encoding> _nameToEncoding = <String, Encoding> { | 34 static Map<String, Encoding> _nameToEncoding = <String, Encoding>{ |
| 35 // ISO_8859-1:1987. | 35 // ISO_8859-1:1987. |
| 36 "iso_8859-1:1987": LATIN1, | 36 "iso_8859-1:1987": LATIN1, |
| 37 "iso-ir-100": LATIN1, | 37 "iso-ir-100": LATIN1, |
| 38 "iso_8859-1": LATIN1, | 38 "iso_8859-1": LATIN1, |
| 39 "iso-8859-1": LATIN1, | 39 "iso-8859-1": LATIN1, |
| 40 "latin1": LATIN1, | 40 "latin1": LATIN1, |
| 41 "l1": LATIN1, | 41 "l1": LATIN1, |
| 42 "ibm819": LATIN1, | 42 "ibm819": LATIN1, |
| 43 "cp819": LATIN1, | 43 "cp819": LATIN1, |
| 44 "csisolatin1": LATIN1, | 44 "csisolatin1": LATIN1, |
| 45 | 45 |
| 46 // US-ASCII. | 46 // US-ASCII. |
| 47 "iso-ir-6": ASCII, | 47 "iso-ir-6": ASCII, |
| 48 "ansi_x3.4-1968": ASCII, | 48 "ansi_x3.4-1968": ASCII, |
| 49 "ansi_x3.4-1986": ASCII, | 49 "ansi_x3.4-1986": ASCII, |
| 50 "iso_646.irv:1991": ASCII, | 50 "iso_646.irv:1991": ASCII, |
| 51 "iso646-us": ASCII, | 51 "iso646-us": ASCII, |
| 52 "us-ascii": ASCII, | 52 "us-ascii": ASCII, |
| 53 "us": ASCII, | 53 "us": ASCII, |
| 54 "ibm367": ASCII, | 54 "ibm367": ASCII, |
| 55 "cp367": ASCII, | 55 "cp367": ASCII, |
| 56 "csascii": ASCII, | 56 "csascii": ASCII, |
| 57 "ascii": ASCII, // This is not in the IANA official names. | 57 "ascii": ASCII, // This is not in the IANA official names. |
| 58 | 58 |
| 59 // UTF-8. | 59 // UTF-8. |
| 60 "csutf8": UTF8, | 60 "csutf8": UTF8, |
| 61 "utf-8": UTF8 | 61 "utf-8": UTF8 |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 /** | 64 /** |
| 65 * Gets an [Encoding] object from the name of the character set | 65 * Gets an [Encoding] object from the name of the character set |
| 66 * name. The names used are the IANA official names for the | 66 * name. The names used are the IANA official names for the |
| 67 * character set (see | 67 * character set (see |
| 68 * http://www.iana.org/assignments/character-sets/character-sets.xml). | 68 * http://www.iana.org/assignments/character-sets/character-sets.xml). |
| 69 * | 69 * |
| 70 * The [name] passed is case insensitive. | 70 * The [name] passed is case insensitive. |
| 71 * | 71 * |
| 72 * If character set is not supported [:null:] is returned. | 72 * If character set is not supported [:null:] is returned. |
| 73 */ | 73 */ |
| 74 static Encoding getByName(String name) { | 74 static Encoding getByName(String name) { |
| 75 if (name == null) return null; | 75 if (name == null) return null; |
| 76 name = name.toLowerCase(); | 76 name = name.toLowerCase(); |
| 77 return _nameToEncoding[name]; | 77 return _nameToEncoding[name]; |
| 78 } | 78 } |
| 79 } | 79 } |
| OLD | NEW |