| 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 /** | 7 /** |
| 8 * String encodings. | 8 * String encodings. |
| 9 */ | 9 */ |
| 10 class Encoding { | 10 class Encoding { |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 | 179 |
| 180 Stream<List<int>> bind(Stream<String> stream) => _encoder.bind(stream); | 180 Stream<List<int>> bind(Stream<String> stream) => _encoder.bind(stream); |
| 181 } | 181 } |
| 182 | 182 |
| 183 | 183 |
| 184 // Utility function to synchronously decode a list of bytes. | 184 // Utility function to synchronously decode a list of bytes. |
| 185 String _decodeString(List<int> bytes, [Encoding encoding = Encoding.UTF_8]) { | 185 String _decodeString(List<int> bytes, [Encoding encoding = Encoding.UTF_8]) { |
| 186 if (bytes.length == 0) return ""; | 186 if (bytes.length == 0) return ""; |
| 187 var string; | 187 var string; |
| 188 var error; | 188 var error; |
| 189 var controller = new StreamController(); | 189 var controller = new StreamController(sync: true); |
| 190 controller.stream | 190 controller.stream |
| 191 .transform(new StringDecoder(encoding)) | 191 .transform(new StringDecoder(encoding)) |
| 192 .listen((data) => string = data, | 192 .listen((data) => string = data, |
| 193 onError: (e) => error = e); | 193 onError: (e) => error = e); |
| 194 controller.add(bytes); | 194 controller.add(bytes); |
| 195 controller.close(); | 195 controller.close(); |
| 196 if (error != null) throw error; | 196 if (error != null) throw error; |
| 197 assert(string != null); | 197 assert(string != null); |
| 198 return string; | 198 return string; |
| 199 } | 199 } |
| 200 | 200 |
| 201 | 201 |
| 202 // Utility function to synchronously decode a utf8-encoded list of bytes, | 202 // Utility function to synchronously decode a utf8-encoded list of bytes, |
| 203 // throwing on error. | 203 // throwing on error. |
| 204 String _decodeUtf8Strict(List<int> bytes) { | 204 String _decodeUtf8Strict(List<int> bytes) { |
| 205 if (bytes.length == 0) return ""; | 205 if (bytes.length == 0) return ""; |
| 206 var string; | 206 var string; |
| 207 var error; | 207 var error; |
| 208 var controller = new StreamController(); | 208 var controller = new StreamController(sync: true); |
| 209 controller.stream | 209 controller.stream |
| 210 .transform(new Utf8DecoderTransformer(null)) | 210 .transform(new Utf8DecoderTransformer(null)) |
| 211 .listen((data) => string = data, | 211 .listen((data) => string = data, |
| 212 onError: (e) => error = e); | 212 onError: (e) => error = e); |
| 213 controller.add(bytes); | 213 controller.add(bytes); |
| 214 controller.close(); | 214 controller.close(); |
| 215 if (error != null) throw error; | 215 if (error != null) throw error; |
| 216 assert(string != null); | 216 assert(string != null); |
| 217 return string; | 217 return string; |
| 218 } | 218 } |
| 219 | 219 |
| 220 | 220 |
| 221 // Utility function to synchronously encode a String. | 221 // Utility function to synchronously encode a String. |
| 222 // Will throw an exception if the encoding is invalid. | 222 // Will throw an exception if the encoding is invalid. |
| 223 List<int> _encodeString(String string, [Encoding encoding = Encoding.UTF_8]) { | 223 List<int> _encodeString(String string, [Encoding encoding = Encoding.UTF_8]) { |
| 224 if (string.length == 0) return []; | 224 if (string.length == 0) return []; |
| 225 var bytes; | 225 var bytes; |
| 226 var controller = new StreamController(); | 226 var controller = new StreamController(sync: true); |
| 227 controller.stream | 227 controller.stream |
| 228 .transform(new StringEncoder(encoding)) | 228 .transform(new StringEncoder(encoding)) |
| 229 .listen((data) => bytes = data); | 229 .listen((data) => bytes = data); |
| 230 controller.add(string); | 230 controller.add(string); |
| 231 controller.close(); | 231 controller.close(); |
| 232 assert(bytes != null); | 232 assert(bytes != null); |
| 233 return bytes; | 233 return bytes; |
| 234 } | 234 } |
| 235 | 235 |
| 236 | 236 |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 | 385 |
| 386 // Utility class for decoding Windows current code page data delivered | 386 // Utility class for decoding Windows current code page data delivered |
| 387 // as a stream of bytes. | 387 // as a stream of bytes. |
| 388 class _WindowsCodePageDecoder extends StreamEventTransformer<List<int>, String>
{ | 388 class _WindowsCodePageDecoder extends StreamEventTransformer<List<int>, String>
{ |
| 389 void handleData(List<int> data, EventSink<String> sink) { | 389 void handleData(List<int> data, EventSink<String> sink) { |
| 390 sink.add(_decodeBytes(data)); | 390 sink.add(_decodeBytes(data)); |
| 391 } | 391 } |
| 392 | 392 |
| 393 external static String _decodeBytes(List<int> bytes); | 393 external static String _decodeBytes(List<int> bytes); |
| 394 } | 394 } |
| OLD | NEW |