OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 part of dart.io; | |
6 | |
7 /// The current system encoding. | |
8 /// | |
9 /// This us used for converting from bytes to/from String when | |
10 /// communicating on stdin, stdout and stderr. | |
11 /// | |
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. | |
14 const SystemEncoding SYSTEM_ENCODING = const SystemEncoding(); | |
15 | |
16 /** | |
17 * The system encoding is the current code page on Windows and UTF-8 on | |
18 * Linux and Mac. | |
19 */ | |
20 class SystemEncoding extends Encoding { | |
21 const SystemEncoding(); | |
22 | |
23 String get name => 'system'; | |
24 | |
25 List<int> encode(String input) => encoder.convert(input); | |
26 String decode(List<int> encoded) => decoder.convert(encoded); | |
27 | |
28 Converter<String, List<int>> get encoder { | |
29 if (Platform.operatingSystem == "windows") { | |
30 return const _WindowsCodePageEncoder(); | |
31 } else { | |
32 return const Utf8Encoder(); | |
33 } | |
34 } | |
35 | |
36 Converter<List<int>, String> get decoder { | |
37 if (Platform.operatingSystem == "windows") { | |
38 return const _WindowsCodePageDecoder(); | |
39 } else { | |
40 return const Utf8Decoder(); | |
41 } | |
42 } | |
43 } | |
44 | |
45 class _WindowsCodePageEncoder extends Converter<String, List<int>> { | |
46 | |
47 const _WindowsCodePageEncoder(); | |
48 | |
49 List<int> convert(String input) { | |
50 List<int> encoded = _encodeString(input); | |
51 if (encoded == null) { | |
52 throw new FormatException("Invalid character for encoding"); | |
53 } | |
54 return encoded; | |
55 } | |
56 | |
57 /** | |
58 * Starts a chunked conversion. | |
59 */ | |
60 StringConversionSink startChunkedConversion(Sink<List<int>> sink) { | |
61 return new _WindowsCodePageEncoderSink(sink); | |
62 } | |
63 | |
64 external static List<int> _encodeString(String string); | |
65 } | |
66 | |
67 class _WindowsCodePageEncoderSink extends StringConversionSinkBase { | |
68 // TODO(floitsch): provide more efficient conversions when the input is | |
69 // not a String. | |
70 | |
71 final Sink<List<int>> _sink; | |
72 | |
73 _WindowsCodePageEncoderSink(this._sink); | |
74 | |
75 void close() { | |
76 _sink.close(); | |
77 } | |
78 | |
79 void add(String string) { | |
80 List<int> encoded = _WindowsCodePageEncoder._encodeString(string); | |
81 if (encoded == null) { | |
82 throw new FormatException("Invalid character for encoding"); | |
83 } | |
84 _sink.add(encoded); | |
85 } | |
86 | |
87 void addSlice(String source, int start, int end, bool isLast) { | |
88 if (start != 0 || end != source.length) { | |
89 source = source.substring(start, end); | |
90 } | |
91 add(source); | |
92 if (isLast) close(); | |
93 } | |
94 } | |
95 | |
96 | |
97 class _WindowsCodePageDecoder extends Converter<List<int>, String> { | |
98 | |
99 const _WindowsCodePageDecoder(); | |
100 | |
101 String convert(List<int> input) { | |
102 return _decodeBytes(input); | |
103 } | |
104 | |
105 /** | |
106 * Starts a chunked conversion. | |
107 */ | |
108 ByteConversionSink startChunkedConversion(Sink<String> sink) { | |
109 return new _WindowsCodePageDecoderSink(sink); | |
110 } | |
111 | |
112 external static String _decodeBytes(List<int> bytes); | |
113 } | |
114 | |
115 class _WindowsCodePageDecoderSink extends ByteConversionSinkBase { | |
116 // TODO(floitsch): provide more efficient conversions when the input is | |
117 // a slice. | |
118 | |
119 final Sink<String> _sink; | |
120 | |
121 _WindowsCodePageDecoderSink(this._sink); | |
122 | |
123 void close() { | |
124 _sink.close(); | |
125 } | |
126 | |
127 void add(List<int> bytes) { | |
128 _sink.add(_WindowsCodePageDecoder._decodeBytes(bytes)); | |
129 } | |
130 } | |
OLD | NEW |