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 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 5 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
6 // for details. All rights reserved. Use of this source code is governed by a | 6 // for details. All rights reserved. Use of this source code is governed by a |
7 // BSD-style license that can be found in the LICENSE file. | 7 // BSD-style license that can be found in the LICENSE file. |
8 | 8 |
9 import "package:expect/expect.dart"; | 9 import "package:expect/expect.dart"; |
10 import 'dart:convert'; | 10 import 'dart:convert'; |
11 | 11 |
12 String decode(List<int> bytes, int chunkSize) { | 12 String decode(List<int> bytes, int chunkSize) { |
13 StringBuffer buffer = new StringBuffer(); | 13 StringBuffer buffer = new StringBuffer(); |
14 var stringSink = new StringConversionSink.fromStringSink(buffer); | 14 var stringSink = new StringConversionSink.fromStringSink(buffer); |
15 var byteSink = new Utf8Decoder().startChunkedConversion(stringSink); | 15 var byteSink = new Utf8Decoder().startChunkedConversion(stringSink); |
16 int i = 0; | 16 int i = 0; |
17 while (i < bytes.length) { | 17 while (i < bytes.length) { |
18 List nextChunk = []; | 18 var nextChunk = <int>[]; |
19 for (int j = 0; j < chunkSize; j++) { | 19 for (int j = 0; j < chunkSize; j++) { |
20 if (i < bytes.length) { | 20 if (i < bytes.length) { |
21 nextChunk.add(bytes[i]); | 21 nextChunk.add(bytes[i]); |
22 i++; | 22 i++; |
23 } | 23 } |
24 } | 24 } |
25 byteSink.add(nextChunk); | 25 byteSink.add(nextChunk); |
26 } | 26 } |
27 byteSink.close(); | 27 byteSink.close(); |
28 return buffer.toString(); | 28 return buffer.toString(); |
29 } | 29 } |
30 | 30 |
31 String decodeAllowMalformed(List<int> bytes, int chunkSize) { | 31 String decodeAllowMalformed(List<int> bytes, int chunkSize) { |
32 StringBuffer buffer = new StringBuffer(); | 32 StringBuffer buffer = new StringBuffer(); |
33 var stringSink = new StringConversionSink.fromStringSink(buffer); | 33 var stringSink = new StringConversionSink.fromStringSink(buffer); |
34 var decoder = new Utf8Decoder(allowMalformed: true); | 34 var decoder = new Utf8Decoder(allowMalformed: true); |
35 var byteSink = decoder.startChunkedConversion(stringSink); | 35 var byteSink = decoder.startChunkedConversion(stringSink); |
36 int i = 0; | 36 int i = 0; |
37 while (i < bytes.length) { | 37 while (i < bytes.length) { |
38 List nextChunk = []; | 38 var nextChunk = <int>[]; |
39 for (int j = 0; j < chunkSize; j++) { | 39 for (int j = 0; j < chunkSize; j++) { |
40 if (i < bytes.length) { | 40 if (i < bytes.length) { |
41 nextChunk.add(bytes[i]); | 41 nextChunk.add(bytes[i]); |
42 i++; | 42 i++; |
43 } | 43 } |
44 } | 44 } |
45 byteSink.add(nextChunk); | 45 byteSink.add(nextChunk); |
46 } | 46 } |
47 byteSink.close(); | 47 byteSink.close(); |
48 return buffer.toString(); | 48 return buffer.toString(); |
(...skipping 19 matching lines...) Expand all Loading... |
68 Expect.equals("", decodeAllowMalformed([0xEF, 0xBB, 0xBF], 4)); | 68 Expect.equals("", decodeAllowMalformed([0xEF, 0xBB, 0xBF], 4)); |
69 Expect.equals("a\u{FEFF}", decode([0x61, 0xEF, 0xBB, 0xBF], 1)); | 69 Expect.equals("a\u{FEFF}", decode([0x61, 0xEF, 0xBB, 0xBF], 1)); |
70 Expect.equals("a\u{FEFF}", decode([0x61, 0xEF, 0xBB, 0xBF], 2)); | 70 Expect.equals("a\u{FEFF}", decode([0x61, 0xEF, 0xBB, 0xBF], 2)); |
71 Expect.equals("a\u{FEFF}", decode([0x61, 0xEF, 0xBB, 0xBF], 3)); | 71 Expect.equals("a\u{FEFF}", decode([0x61, 0xEF, 0xBB, 0xBF], 3)); |
72 Expect.equals("a\u{FEFF}", decode([0x61, 0xEF, 0xBB, 0xBF], 4)); | 72 Expect.equals("a\u{FEFF}", decode([0x61, 0xEF, 0xBB, 0xBF], 4)); |
73 Expect.equals("a\u{FEFF}", decodeAllowMalformed([0x61, 0xEF, 0xBB, 0xBF], 1)); | 73 Expect.equals("a\u{FEFF}", decodeAllowMalformed([0x61, 0xEF, 0xBB, 0xBF], 1)); |
74 Expect.equals("a\u{FEFF}", decodeAllowMalformed([0x61, 0xEF, 0xBB, 0xBF], 2)); | 74 Expect.equals("a\u{FEFF}", decodeAllowMalformed([0x61, 0xEF, 0xBB, 0xBF], 2)); |
75 Expect.equals("a\u{FEFF}", decodeAllowMalformed([0x61, 0xEF, 0xBB, 0xBF], 3)); | 75 Expect.equals("a\u{FEFF}", decodeAllowMalformed([0x61, 0xEF, 0xBB, 0xBF], 3)); |
76 Expect.equals("a\u{FEFF}", decodeAllowMalformed([0x61, 0xEF, 0xBB, 0xBF], 4)); | 76 Expect.equals("a\u{FEFF}", decodeAllowMalformed([0x61, 0xEF, 0xBB, 0xBF], 4)); |
77 } | 77 } |
OLD | NEW |