OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 const sampleText = "Sample text file."; |
| 6 |
| 7 main() async { |
| 8 var uriEncoded = sampleText.replaceAll(' ', '%20'); |
| 9 await testUri("data:application/dart;charset=utf-8,$uriEncoded"); |
| 10 // TODO: Support other data: URI formats too. |
| 11 // See: https://github.com/dart-lang/sdk/issues/24030 |
| 12 // await testUri("data:text/plain;charset=utf-8,$uriEncoded"); |
| 13 var base64Encoded = "U2FtcGxlIHRleHQgZmlsZS4="; |
| 14 // await testUri("data:application/dart;charset=utf-8;base64,$base64Encoded"); |
| 15 // await testUri("data:text/plain;charset=utf-8;base64,$base64Encoded"); |
| 16 } |
| 17 |
| 18 testUri(uriText) async { |
| 19 var resource = new Resource(uriText); |
| 20 |
| 21 if (resource.uri != Uri.parse(uriText)) { |
| 22 throw "uriText: Incorrect URI: ${resource.uri}"; |
| 23 } |
| 24 |
| 25 var text = await resource.readAsString(); |
| 26 if (text != sampleText) { |
| 27 throw "uriText: Incorrect reading of text file: $text"; |
| 28 } |
| 29 |
| 30 var bytes = await resource.readAsBytes(); |
| 31 if (!compareBytes(bytes, sampleText.codeUnits)) { |
| 32 throw "uriText: Incorrect reading of bytes: $bytes"; |
| 33 } |
| 34 |
| 35 var streamBytes = []; |
| 36 await for (var byteSlice in resource.openRead()) { |
| 37 streamBytes.addAll(byteSlice); |
| 38 } |
| 39 if (!compareBytes(streamBytes, sampleText.codeUnits)) { |
| 40 throw "uriText: Incorrect reading of bytes: $bytes"; |
| 41 } |
| 42 } |
| 43 |
| 44 /// Checks that [bytes] and [expectedBytes] have the same contents. |
| 45 bool compareBytes(bytes, expectedBytes) { |
| 46 if (bytes.length != expectedBytes.length) return false; |
| 47 for (int i = 0; i < expectedBytes.length; i++) { |
| 48 if (bytes[i] != expectedBytes[i]) return false; |
| 49 } |
| 50 return true; |
| 51 } |
OLD | NEW |