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