OLD | NEW |
(Empty) | |
| 1 /** |
| 2 * This library adds `dart:io` support to the HTML5 parser. Call |
| 3 * [initDartIOSupport] before calling the [parse] methods and they will accept |
| 4 * a [RandomAccessFile] as input, in addition to the other input types. |
| 5 */ |
| 6 library parser_console; |
| 7 |
| 8 import 'dart:io'; |
| 9 import 'parser.dart'; |
| 10 import 'src/inputstream.dart' as inputstream; |
| 11 |
| 12 /** |
| 13 * Adds support to the [HtmlParser] for running on a console VM. In particular |
| 14 * this means it will be able to handle `dart:io` and [RandomAccessFile]s as |
| 15 * input to the various [parse] methods. |
| 16 */ |
| 17 void useConsole() { |
| 18 inputstream.consoleSupport = new _ConsoleSupport(); |
| 19 } |
| 20 |
| 21 class _ConsoleSupport extends inputstream.ConsoleSupport { |
| 22 List<int> bytesFromFile(source) { |
| 23 if (source is! RandomAccessFile) return null; |
| 24 return readAllBytesFromFile(source); |
| 25 } |
| 26 } |
| 27 |
| 28 // TODO(jmesserly): this should be `RandomAccessFile.readAllBytes`. |
| 29 /** Synchronously reads all bytes from the [file]. */ |
| 30 List<int> readAllBytesFromFile(RandomAccessFile file) { |
| 31 int length = file.lengthSync(); |
| 32 var bytes = new List<int>(length); |
| 33 |
| 34 int bytesRead = 0; |
| 35 while (bytesRead < length) { |
| 36 int read = file.readIntoSync(bytes, bytesRead, length - bytesRead); |
| 37 if (read <= 0) { |
| 38 // This could happen if, for example, the file was resized while |
| 39 // we're reading. Just shrink the bytes array and move on. |
| 40 bytes = bytes.sublist(0, bytesRead); |
| 41 break; |
| 42 } |
| 43 bytesRead += read; |
| 44 } |
| 45 return bytes; |
| 46 } |
OLD | NEW |