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 library file_system_vm; | 5 library file_system_vm; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 import 'dart:utf'; | 8 import 'dart:utf'; |
9 import 'file_system.dart'; | 9 import 'file_system.dart'; |
10 | 10 |
11 /** File system implementation using the vm api's. */ | 11 /** File system implementation using the vm api's. */ |
12 class VMFileSystem implements FileSystem { | 12 class VMFileSystem implements FileSystem { |
| 13 |
| 14 /** Pending futures for file write requests. */ |
| 15 List<Future> _pending = <Future>[]; |
| 16 |
| 17 Future flush() { |
| 18 return Futures.wait(_pending).transform((_) { |
| 19 // Some new work might be pending that was only queued up after the call |
| 20 // to flush so we cannot simply clear the future list. |
| 21 _pending = _pending.filter((f) => !f.hasValue); |
| 22 return null; |
| 23 }); |
| 24 } |
| 25 |
13 void writeString(String path, String text) { | 26 void writeString(String path, String text) { |
| 27 // TODO(jacobr): the following async code mysterously leads to sporadic |
| 28 // data corruption in the Dart VM. We need to create a reliable repro and |
| 29 // file a bug with the VM team. |
| 30 /* |
| 31 _pending.add(new File(path).open(FileMode.WRITE).chain( |
| 32 (file) => file.writeString(text).chain((_) => file.close()))); |
| 33 */ |
14 var file = new File(path).openSync(FileMode.WRITE); | 34 var file = new File(path).openSync(FileMode.WRITE); |
15 file.writeStringSync(text); | 35 file.writeStringSync(text); |
16 file.closeSync(); | 36 file.closeSync(); |
17 } | 37 } |
18 | 38 |
19 String readAll(String filename) { | 39 Future<String> readAll(String filename) { |
20 var file = (new File(filename)).openSync(); | 40 return new File(filename).open().chain((file) => |
21 var length = file.lengthSync(); | 41 file.length().chain((int length) { |
22 var buffer = new List<int>(length); | 42 var buffer = new List<int>(length); |
23 var bytes = file.readListSync(buffer, 0, length); | 43 |
24 file.closeSync(); | 44 return file.readList(buffer, 0, length).transform((length) { |
25 return new String.fromCharCodes(new Utf8Decoder(buffer).decodeRest()); | 45 file.close(); |
| 46 // TODO(jmesserly): support all html5 encodings not just UTF8. |
| 47 return new String.fromCharCodes(new Utf8Decoder(buffer).decodeRest()); |
| 48 }); |
| 49 })); |
26 } | 50 } |
27 | 51 |
28 void createDirectory(String path, [bool recursive = false]) { | 52 void createDirectory(String path, [bool recursive = false]) { |
29 // TODO(rnystrom): Implement. | 53 // TODO(rnystrom): Implement. |
30 throw 'createDirectory() is not implemented by VMFileSystem yet.'; | 54 throw 'createDirectory() is not implemented by VMFileSystem yet.'; |
31 } | 55 } |
32 | 56 |
33 void removeDirectory(String path, [bool recursive = false]) { | 57 void removeDirectory(String path, [bool recursive = false]) { |
34 // TODO(rnystrom): Implement. | 58 // TODO(rnystrom): Implement. |
35 throw 'removeDirectory() is not implemented by VMFileSystem yet.'; | 59 throw 'removeDirectory() is not implemented by VMFileSystem yet.'; |
36 } | 60 } |
37 } | 61 } |
OLD | NEW |