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