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> inputBytes) { | 8 String decode(List<int> inputBytes) { |
9 List<int> bytes; | 9 List<int> bytes; |
10 ChunkedConversionSink byteSink = | 10 var byteSink = |
11 new ByteConversionSink.withCallback((result) => bytes = result); | 11 new ByteConversionSink.withCallback((result) => bytes = result); |
12 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); | 12 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
13 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(false); | 13 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(false); |
14 inputByteSink.add(inputBytes); | 14 inputByteSink.add(inputBytes); |
15 inputByteSink.close(); | 15 inputByteSink.close(); |
16 return UTF8.decode(bytes); | 16 return UTF8.decode(bytes); |
17 } | 17 } |
18 | 18 |
19 String decode2(List<int> inputBytes) { | 19 String decode2(List<int> inputBytes) { |
20 List<int> bytes; | 20 List<int> bytes; |
21 ChunkedConversionSink byteSink = | 21 var byteSink = |
22 new ByteConversionSink.withCallback((result) => bytes = result); | 22 new ByteConversionSink.withCallback((result) => bytes = result); |
23 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); | 23 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
24 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(false); | 24 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(false); |
25 inputBytes.forEach((b) => inputByteSink.addSlice([0, b, 1], 1, 2, false)); | 25 inputBytes.forEach((b) => inputByteSink.addSlice([0, b, 1], 1, 2, false)); |
26 inputByteSink.close(); | 26 inputByteSink.close(); |
27 return UTF8.decode(bytes); | 27 return UTF8.decode(bytes); |
28 } | 28 } |
29 | 29 |
30 String decodeAllowMalformed(List<int> inputBytes) { | 30 String decodeAllowMalformed(List<int> inputBytes) { |
31 List<int> bytes; | 31 List<int> bytes; |
32 ChunkedConversionSink byteSink = | 32 var byteSink = |
33 new ByteConversionSink.withCallback((result) => bytes = result); | 33 new ByteConversionSink.withCallback((result) => bytes = result); |
34 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); | 34 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
35 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(true); | 35 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(true); |
36 inputByteSink.add(inputBytes); | 36 inputByteSink.add(inputBytes); |
37 inputByteSink.close(); | 37 inputByteSink.close(); |
38 return UTF8.decode(bytes); | 38 return UTF8.decode(bytes); |
39 } | 39 } |
40 | 40 |
41 String decodeAllowMalformed2(List<int> inputBytes) { | 41 String decodeAllowMalformed2(List<int> inputBytes) { |
42 List<int> bytes; | 42 List<int> bytes; |
43 ChunkedConversionSink byteSink = | 43 var byteSink = |
44 new ByteConversionSink.withCallback((result) => bytes = result); | 44 new ByteConversionSink.withCallback((result) => bytes = result); |
45 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); | 45 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
46 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(true); | 46 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(true); |
47 inputBytes.forEach((b) => inputByteSink.addSlice([0, b, 1], 1, 2, false)); | 47 inputBytes.forEach((b) => inputByteSink.addSlice([0, b, 1], 1, 2, false)); |
48 inputByteSink.close(); | 48 inputByteSink.close(); |
49 return UTF8.decode(bytes); | 49 return UTF8.decode(bytes); |
50 } | 50 } |
51 | 51 |
52 | 52 |
53 final TESTS = [ | 53 final TESTS = [ |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 for (var test in []..addAll(allTests)..addAll(allTests2)) { | 145 for (var test in []..addAll(allTests)..addAll(allTests2)) { |
146 List<int> bytes = test[0]; | 146 List<int> bytes = test[0]; |
147 Expect.throws(() => decode(bytes), (e) => e is FormatException); | 147 Expect.throws(() => decode(bytes), (e) => e is FormatException); |
148 Expect.throws(() => decode2(bytes), (e) => e is FormatException); | 148 Expect.throws(() => decode2(bytes), (e) => e is FormatException); |
149 | 149 |
150 String expected = test[1]; | 150 String expected = test[1]; |
151 Expect.equals(expected, decodeAllowMalformed(bytes)); | 151 Expect.equals(expected, decodeAllowMalformed(bytes)); |
152 Expect.equals(expected, decodeAllowMalformed2(bytes)); | 152 Expect.equals(expected, decodeAllowMalformed2(bytes)); |
153 } | 153 } |
154 } | 154 } |
OLD | NEW |