OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 import 'dart:convert'; | 6 import 'dart:convert'; |
7 | 7 |
8 String decode(List<int> bytes) { | 8 String decode(List<int> bytes) { |
9 StringBuffer buffer = new StringBuffer(); | 9 StringBuffer buffer = new StringBuffer(); |
10 ChunkedConversionSink stringSink = | 10 var stringSink = |
11 new StringConversionSink.fromStringSink(buffer); | 11 new StringConversionSink.fromStringSink(buffer); |
12 var byteSink = new Utf8Decoder().startChunkedConversion(stringSink); | 12 var byteSink = new Utf8Decoder().startChunkedConversion(stringSink); |
13 bytes.forEach((byte) { byteSink.add([byte]); }); | 13 bytes.forEach((byte) { byteSink.add([byte]); }); |
14 byteSink.close(); | 14 byteSink.close(); |
15 return buffer.toString(); | 15 return buffer.toString(); |
16 } | 16 } |
17 | 17 |
18 String decodeAllowMalformed(List<int> bytes) { | 18 String decodeAllowMalformed(List<int> bytes) { |
19 StringBuffer buffer = new StringBuffer(); | 19 StringBuffer buffer = new StringBuffer(); |
20 ChunkedConversionSink stringSink = | 20 var stringSink = |
21 new StringConversionSink.fromStringSink(buffer); | 21 new StringConversionSink.fromStringSink(buffer); |
22 var decoder = new Utf8Decoder(allowMalformed: true); | 22 var decoder = new Utf8Decoder(allowMalformed: true); |
23 var byteSink = decoder.startChunkedConversion(stringSink); | 23 var byteSink = decoder.startChunkedConversion(stringSink); |
24 bytes.forEach((byte) { byteSink.add([byte]); }); | 24 bytes.forEach((byte) { byteSink.add([byte]); }); |
25 byteSink.close(); | 25 byteSink.close(); |
26 return buffer.toString(); | 26 return buffer.toString(); |
27 } | 27 } |
28 | 28 |
29 | 29 |
30 final TESTS = [ | 30 final TESTS = [ |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 }); | 120 }); |
121 | 121 |
122 for (var test in []..addAll(allTests)..addAll(allTests2)) { | 122 for (var test in []..addAll(allTests)..addAll(allTests2)) { |
123 List<int> bytes = test[0]; | 123 List<int> bytes = test[0]; |
124 Expect.throws(() => decode(bytes), (e) => e is FormatException); | 124 Expect.throws(() => decode(bytes), (e) => e is FormatException); |
125 | 125 |
126 String expected = test[1]; | 126 String expected = test[1]; |
127 Expect.equals(expected, decodeAllowMalformed(bytes)); | 127 Expect.equals(expected, decodeAllowMalformed(bytes)); |
128 } | 128 } |
129 } | 129 } |
OLD | NEW |