OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library console; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:io'; |
| 9 import 'dart:utf'; |
| 10 import 'package:polymer/src/file_system.dart'; |
| 11 |
| 12 /** File system implementation for console VM (i.e. no browser). */ |
| 13 class ConsoleFileSystem implements FileSystem { |
| 14 |
| 15 /** Pending futures for file write requests. */ |
| 16 final _pending = <String, Future>{}; |
| 17 |
| 18 Future flush() => Future.wait(_pending.values.toList()); |
| 19 |
| 20 void writeString(String path, String text) { |
| 21 if(!_pending.containsKey(path)) { |
| 22 _pending[path] = new File(path).open(mode: FileMode.WRITE) |
| 23 .then((file) => file.writeString(text)) |
| 24 .then((file) => file.close()) |
| 25 .whenComplete(() { _pending.remove(path); }); |
| 26 } |
| 27 } |
| 28 |
| 29 // TODO(jmesserly): even better would be to pass the RandomAccessFile directly |
| 30 // to html5lib. This will require a further restructuring of FileSystem. |
| 31 // Probably it just needs "readHtml" and "readText" methods. |
| 32 Future<List<int>> readTextOrBytes(String path) { |
| 33 return new File(path).open().then( |
| 34 (file) => file.length().then((length) { |
| 35 // TODO(jmesserly): is this guaranteed to read all of the bytes? |
| 36 var buffer = new List<int>(length); |
| 37 return file.readInto(buffer, 0, length) |
| 38 .then((_) => file.close()) |
| 39 .then((_) => buffer); |
| 40 })); |
| 41 } |
| 42 |
| 43 // TODO(jmesserly): do we support any encoding other than UTF-8 for Dart? |
| 44 Future<String> readText(String path) { |
| 45 return readTextOrBytes(path).then(decodeUtf8); |
| 46 } |
| 47 } |
OLD | NEW |