Chromium Code Reviews| 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 import "dart:io"; | |
| 6 | |
| 7 const sampleText = "Sample text file"; | |
| 8 | |
| 9 main() async { | |
| 10 var file = await createFile(); | |
| 11 var uri = new Uri.file(file.path); | |
| 12 | |
| 13 var resource = new Resource(uri.toString()); | |
| 14 | |
| 15 if (resource.uri != uri) { | |
| 16 throw "Incorrect URI: ${resource.uri}"; | |
| 17 } | |
| 18 | |
| 19 var text = await resource.readAsString(); | |
| 20 if (text != sampleText) { | |
| 21 throw "Incorrect reading of text file: $text"; | |
| 22 } | |
| 23 | |
| 24 var bytes = await resource.readAsBytes(); | |
| 25 if (!compareBytes(bytes, sampleText.codeUnits)) { | |
| 26 throw "Incorrect reading of bytes: $bytes"; | |
| 27 } | |
| 28 | |
| 29 var streamBytes = []; | |
| 30 await for (var byteSlice in resource.openRead()) { | |
| 31 streamBytes.addAll(byteSlice); | |
| 32 } | |
| 33 if (!compareBytes(streamBytes, sampleText.codeUnits)) { | |
| 34 throw "Incorrect reading of bytes: $bytes"; | |
| 35 } | |
| 36 if (!compareBytes(streamBytes, bytes)) { | |
|
Søren Gjesse
2015/08/07 13:51:16
ditto.
| |
| 37 throw "Inconsistent reading of bytes: $bytes / $streamBytes"; | |
| 38 } | |
| 39 | |
| 40 await deleteFile(file); | |
| 41 } | |
| 42 | |
| 43 /// Checks that [bytes] and [expectedBytes] have the same contents. | |
| 44 bool compareBytes(bytes, expectedBytes) { | |
| 45 if (bytes.length != expectedBytes.length) return false; | |
| 46 for (int i = 0; i < expectedBytes.length; i++) { | |
| 47 if (bytes[i] != expectedBytes[i]) return false; | |
| 48 } | |
| 49 return true; | |
| 50 } | |
| 51 | |
| 52 createFile() async { | |
| 53 var tempDir = await Directory.systemTemp.createTemp("sample"); | |
| 54 var filePath = tempDir.path + Platform.pathSeparator + "sample.txt"; | |
| 55 var file = new File(filePath); | |
| 56 await file.create(); | |
| 57 await file.writeAsString(sampleText); | |
| 58 return file; | |
| 59 } | |
| 60 | |
| 61 deleteFile(File file) async { | |
| 62 // Removes the file and the temporary directory it's in. | |
| 63 var parentDir = new Directory(file.path.substring(0, | |
| 64 file.path.lastIndexOf(Platform.pathSeparator))); | |
| 65 await parentDir.delete(recursive: true); | |
| 66 } | |
| OLD | NEW |