| 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 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 } | 178 } |
| 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 controller = new StreamController(); | 189 var controller = new StreamController(); |
| 189 controller.stream | 190 controller.stream |
| 190 .transform(new StringDecoder(encoding)) | 191 .transform(new StringDecoder(encoding)) |
| 191 .listen((data) => string = data); | 192 .listen((data) => string = data, |
| 193 onError: (e) => error = e); |
| 192 controller.add(bytes); | 194 controller.add(bytes); |
| 193 controller.close(); | 195 controller.close(); |
| 196 if (error != null) throw error; |
| 194 assert(string != null); | 197 assert(string != null); |
| 195 return string; | 198 return string; |
| 196 } | 199 } |
| 200 |
| 201 |
| 202 // Utility function to synchronously decode a utf8-encoded list of bytes, |
| 203 // throwing on error. |
| 204 String _decodeUtf8Strict(List<int> bytes) { |
| 205 if (bytes.length == 0) return ""; |
| 206 var string; |
| 207 var error; |
| 208 var controller = new StreamController(); |
| 209 controller.stream |
| 210 .transform(new Utf8DecoderTransformer(null)) |
| 211 .listen((data) => string = data, |
| 212 onError: (e) => error = e); |
| 213 controller.add(bytes); |
| 214 controller.close(); |
| 215 if (error != null) throw error; |
| 216 assert(string != null); |
| 217 return string; |
| 218 } |
| 197 | 219 |
| 198 | 220 |
| 199 // Utility function to synchronously encode a String. | 221 // Utility function to synchronously encode a String. |
| 200 // Will throw an exception if the encoding is invalid. | 222 // Will throw an exception if the encoding is invalid. |
| 201 List<int> _encodeString(String string, [Encoding encoding = Encoding.UTF_8]) { | 223 List<int> _encodeString(String string, [Encoding encoding = Encoding.UTF_8]) { |
| 202 if (string.length == 0) return []; | 224 if (string.length == 0) return []; |
| 203 var bytes; | 225 var bytes; |
| 204 var controller = new StreamController(); | 226 var controller = new StreamController(); |
| 205 controller.stream | 227 controller.stream |
| 206 .transform(new StringEncoder(encoding)) | 228 .transform(new StringEncoder(encoding)) |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 | 385 |
| 364 // Utility class for decoding Windows current code page data delivered | 386 // Utility class for decoding Windows current code page data delivered |
| 365 // as a stream of bytes. | 387 // as a stream of bytes. |
| 366 class _WindowsCodePageDecoder extends StreamEventTransformer<List<int>, String>
{ | 388 class _WindowsCodePageDecoder extends StreamEventTransformer<List<int>, String>
{ |
| 367 void handleData(List<int> data, EventSink<String> sink) { | 389 void handleData(List<int> data, EventSink<String> sink) { |
| 368 sink.add(_decodeBytes(data)); | 390 sink.add(_decodeBytes(data)); |
| 369 } | 391 } |
| 370 | 392 |
| 371 external static String _decodeBytes(List<int> bytes); | 393 external static String _decodeBytes(List<int> bytes); |
| 372 } | 394 } |
| OLD | NEW |