| 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.io; | 5 part of dart.io; |
| 6 | 6 |
| 7 /// The current system encoding. | 7 /// The current system encoding. |
| 8 /// | 8 /// |
| 9 /// This us used for converting from bytes to/from String when | 9 /// This us used for converting from bytes to/from String when |
| 10 /// communicating on stdin, stdout and stderr. | 10 /// communicating on stdin, stdout and stderr. |
| 11 /// | 11 /// |
| 12 /// On Windows this will use the currently active code page for the | 12 /// On Windows this will use the currently active code page for the |
| 13 /// conversion. On all other systems it will always use UTF-8. | 13 /// conversion. On all other systems it will always use UTF-8. |
| 14 const SystemEncoding SYSTEM_ENCODING = const SystemEncoding(); | 14 const SystemEncoding SYSTEM_ENCODING = const SystemEncoding(); |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * The system encoding is the current code page on Windows and UTF-8 on | 17 * The system encoding is the current code page on Windows and UTF-8 on |
| 18 * Linux and Mac. | 18 * Linux and Mac. |
| 19 */ | 19 */ |
| 20 class SystemEncoding extends Encoding { | 20 class SystemEncoding extends Encoding { |
| 21 const SystemEncoding(); | 21 const SystemEncoding(); |
| 22 | 22 |
| 23 String get name => 'system'; | 23 String get name => 'system'; |
| 24 | 24 |
| 25 List<int> encode(String input) => encoder.convert(input); | 25 List<int> encode(String input) => encoder.convert(input); |
| 26 String decode(List<int> encoded) => decoder.convert(encoded); | 26 String decode(List<int> encoded) => decoder.convert(encoded); |
| 27 | 27 |
| 28 ChunkedConverter<String, List<int>, String, List<int>> get encoder { | 28 Converter<String, List<int>> get encoder { |
| 29 if (Platform.operatingSystem == "windows") { | 29 if (Platform.operatingSystem == "windows") { |
| 30 return const _WindowsCodePageEncoder(); | 30 return const _WindowsCodePageEncoder(); |
| 31 } else { | 31 } else { |
| 32 return const Utf8Encoder(); | 32 return const Utf8Encoder(); |
| 33 } | 33 } |
| 34 } | 34 } |
| 35 | 35 |
| 36 ChunkedConverter<List<int>, String, List<int>, String> get decoder { | 36 Converter<List<int>, String> get decoder { |
| 37 if (Platform.operatingSystem == "windows") { | 37 if (Platform.operatingSystem == "windows") { |
| 38 return const _WindowsCodePageDecoder(); | 38 return const _WindowsCodePageDecoder(); |
| 39 } else { | 39 } else { |
| 40 return const Utf8Decoder(); | 40 return const Utf8Decoder(); |
| 41 } | 41 } |
| 42 } | 42 } |
| 43 } | 43 } |
| 44 | 44 |
| 45 class _WindowsCodePageEncoder | 45 class _WindowsCodePageEncoder extends Converter<String, List<int>> { |
| 46 extends ChunkedConverter<String, List<int>, String, List<int>> { | |
| 47 | 46 |
| 48 const _WindowsCodePageEncoder(); | 47 const _WindowsCodePageEncoder(); |
| 49 | 48 |
| 50 List<int> convert(String input) { | 49 List<int> convert(String input) { |
| 51 List<int> encoded = _encodeString(input); | 50 List<int> encoded = _encodeString(input); |
| 52 if (encoded == null) { | 51 if (encoded == null) { |
| 53 throw new FormatException("Invalid character for encoding"); | 52 throw new FormatException("Invalid character for encoding"); |
| 54 } | 53 } |
| 55 return encoded; | 54 return encoded; |
| 56 } | 55 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 void addSlice(String source, int start, int end, bool isLast) { | 90 void addSlice(String source, int start, int end, bool isLast) { |
| 92 if (start != 0 || end != source.length) { | 91 if (start != 0 || end != source.length) { |
| 93 source = source.substring(start, end); | 92 source = source.substring(start, end); |
| 94 } | 93 } |
| 95 add(source); | 94 add(source); |
| 96 if (isLast) close(); | 95 if (isLast) close(); |
| 97 } | 96 } |
| 98 } | 97 } |
| 99 | 98 |
| 100 | 99 |
| 101 class _WindowsCodePageDecoder | 100 class _WindowsCodePageDecoder extends Converter<List<int>, String> { |
| 102 extends ChunkedConverter<List<int>, String, List<int>, String> { | |
| 103 | 101 |
| 104 const _WindowsCodePageDecoder(); | 102 const _WindowsCodePageDecoder(); |
| 105 | 103 |
| 106 String convert(List<int> input) { | 104 String convert(List<int> input) { |
| 107 return _decodeBytes(input); | 105 return _decodeBytes(input); |
| 108 } | 106 } |
| 109 | 107 |
| 110 /** | 108 /** |
| 111 * Starts a chunked conversion. | 109 * Starts a chunked conversion. |
| 112 */ | 110 */ |
| (...skipping 16 matching lines...) Expand all Loading... |
| 129 _WindowsCodePageDecoderSink(this._sink); | 127 _WindowsCodePageDecoderSink(this._sink); |
| 130 | 128 |
| 131 void close() { | 129 void close() { |
| 132 _sink.close(); | 130 _sink.close(); |
| 133 } | 131 } |
| 134 | 132 |
| 135 void add(List<int> bytes) { | 133 void add(List<int> bytes) { |
| 136 _sink.add(_WindowsCodePageDecoder._decodeBytes(bytes)); | 134 _sink.add(_WindowsCodePageDecoder._decodeBytes(bytes)); |
| 137 } | 135 } |
| 138 } | 136 } |
| OLD | NEW |