Chromium Code Reviews| 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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 * Stream transformer that can decode a stream of bytes into a stream of | 84 * Stream transformer that can decode a stream of bytes into a stream of |
| 85 * strings using [encoding]. | 85 * strings using [encoding]. |
| 86 * | 86 * |
| 87 * Invalid or forbidden byte-sequences will not produce errors, but will instead | 87 * Invalid or forbidden byte-sequences will not produce errors, but will instead |
| 88 * insert [replacementChar] in the decoded strings. | 88 * insert [replacementChar] in the decoded strings. |
| 89 */ | 89 */ |
| 90 class StringDecoder implements StreamTransformer<List<int>, String> { | 90 class StringDecoder implements StreamTransformer<List<int>, String> { |
| 91 var _decoder; | 91 var _decoder; |
| 92 | 92 |
| 93 /** | 93 /** |
| 94 * Decodes a stream of bytes into a `String` with an optional | |
| 95 * [encoding] and [replacementChar]. | |
| 96 * | |
| 97 * The default value for [encoding] is [Encoding.UTF_8]. | |
| 98 * | |
| 99 * The default value for [replacementChar] is code point U+FFFD. | |
| 100 * | |
| 101 * Completes with the decoded `String` when the stream is done. | |
| 102 */ | |
| 103 static Future<String> decode(Stream<List<int>> stream, | |
| 104 [Encoding encoding = Encoding.UTF_8, | |
| 105 int replacementChar]) { | |
|
Anders Johnsen
2013/06/24 12:57:16
Type in replacement char?
Søren Gjesse
2013/06/24 13:18:16
Done.
| |
| 106 return stream | |
| 107 .transform(new StringDecoder(encoding, replacementChar )) | |
|
Anders Johnsen
2013/06/24 12:57:16
Extra space at end.
Søren Gjesse
2013/06/24 13:18:16
Done.
| |
| 108 .fold( | |
|
Anders Johnsen
2013/06/24 12:57:16
Why not join, and an optional separator?
Søren Gjesse
2013/06/24 13:18:16
There is no join on streams.
| |
| 109 new StringBuffer(), | |
| 110 (prev, data) => prev..write(data)) | |
| 111 .then((sb) => sb.toString()); | |
| 112 } | |
| 113 | |
| 114 /** | |
| 94 * Create a new [StringDecoder] with an optional [encoding] and | 115 * Create a new [StringDecoder] with an optional [encoding] and |
| 95 * [replacementChar]. | 116 * [replacementChar]. |
| 117 * | |
| 118 * The default value for [encoding] is [Encoding.UTF_8]. | |
| 119 * | |
| 120 * The default value for [replacementChar] is code point U+FFFD. | |
| 96 */ | 121 */ |
| 97 StringDecoder([Encoding encoding = Encoding.UTF_8, int replacementChar]) { | 122 StringDecoder([Encoding encoding = Encoding.UTF_8, int replacementChar]) { |
| 98 switch (encoding) { | 123 switch (encoding) { |
| 99 case Encoding.UTF_8: | 124 case Encoding.UTF_8: |
| 100 if (replacementChar == null) { | 125 if (replacementChar == null) { |
| 101 replacementChar = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT; | 126 replacementChar = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT; |
| 102 } | 127 } |
| 103 _decoder = new Utf8DecoderTransformer(replacementChar); | 128 _decoder = new Utf8DecoderTransformer(replacementChar); |
| 104 break; | 129 break; |
| 105 case Encoding.ASCII: | 130 case Encoding.ASCII: |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 385 | 410 |
| 386 // Utility class for decoding Windows current code page data delivered | 411 // Utility class for decoding Windows current code page data delivered |
| 387 // as a stream of bytes. | 412 // as a stream of bytes. |
| 388 class _WindowsCodePageDecoder extends StreamEventTransformer<List<int>, String> { | 413 class _WindowsCodePageDecoder extends StreamEventTransformer<List<int>, String> { |
| 389 void handleData(List<int> data, EventSink<String> sink) { | 414 void handleData(List<int> data, EventSink<String> sink) { |
| 390 sink.add(_decodeBytes(data)); | 415 sink.add(_decodeBytes(data)); |
| 391 } | 416 } |
| 392 | 417 |
| 393 external static String _decodeBytes(List<int> bytes); | 418 external static String _decodeBytes(List<int> bytes); |
| 394 } | 419 } |
| OLD | NEW |