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 Converter<String, List<int>> get encoder { | 28 ChunkedConverter<String, List<int>, 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 Converter<List<int>, String> get decoder { | 36 ChunkedConverter<List<int>, String, 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 extends Converter<String, List<int>> { | 45 class _WindowsCodePageEncoder |
| 46 extends ChunkedConverter<String, List<int>, String, List<int>> { |
46 | 47 |
47 const _WindowsCodePageEncoder(); | 48 const _WindowsCodePageEncoder(); |
48 | 49 |
49 List<int> convert(String input) { | 50 List<int> convert(String input) { |
50 List<int> encoded = _encodeString(input); | 51 List<int> encoded = _encodeString(input); |
51 if (encoded == null) { | 52 if (encoded == null) { |
52 throw new FormatException("Invalid character for encoding"); | 53 throw new FormatException("Invalid character for encoding"); |
53 } | 54 } |
54 return encoded; | 55 return encoded; |
55 } | 56 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 void addSlice(String source, int start, int end, bool isLast) { | 91 void addSlice(String source, int start, int end, bool isLast) { |
91 if (start != 0 || end != source.length) { | 92 if (start != 0 || end != source.length) { |
92 source = source.substring(start, end); | 93 source = source.substring(start, end); |
93 } | 94 } |
94 add(source); | 95 add(source); |
95 if (isLast) close(); | 96 if (isLast) close(); |
96 } | 97 } |
97 } | 98 } |
98 | 99 |
99 | 100 |
100 class _WindowsCodePageDecoder extends Converter<List<int>, String> { | 101 class _WindowsCodePageDecoder |
| 102 extends ChunkedConverter<List<int>, String, List<int>, String> { |
101 | 103 |
102 const _WindowsCodePageDecoder(); | 104 const _WindowsCodePageDecoder(); |
103 | 105 |
104 String convert(List<int> input) { | 106 String convert(List<int> input) { |
105 return _decodeBytes(input); | 107 return _decodeBytes(input); |
106 } | 108 } |
107 | 109 |
108 /** | 110 /** |
109 * Starts a chunked conversion. | 111 * Starts a chunked conversion. |
110 */ | 112 */ |
(...skipping 16 matching lines...) Expand all Loading... |
127 _WindowsCodePageDecoderSink(this._sink); | 129 _WindowsCodePageDecoderSink(this._sink); |
128 | 130 |
129 void close() { | 131 void close() { |
130 _sink.close(); | 132 _sink.close(); |
131 } | 133 } |
132 | 134 |
133 void add(List<int> bytes) { | 135 void add(List<int> bytes) { |
134 _sink.add(_WindowsCodePageDecoder._decodeBytes(bytes)); | 136 _sink.add(_WindowsCodePageDecoder._decodeBytes(bytes)); |
135 } | 137 } |
136 } | 138 } |
OLD | NEW |