TryCatch tail functions
This commit is contained in:
parent
2a5774bd6d
commit
d885485684
5 changed files with 22 additions and 192 deletions
|
@ -85,10 +85,12 @@ Stream<List<int>> tail(final File file) async* {
|
|||
|
||||
Stream<Uint8List> _read() async* {
|
||||
while (pos < len) {
|
||||
final bytesRead = await randomAccess.readInto(buf);
|
||||
pos += bytesRead;
|
||||
try {
|
||||
final bytesRead = await randomAccess.readInto(buf);
|
||||
pos += bytesRead;
|
||||
|
||||
yield buf.sublist(0, bytesRead);
|
||||
yield buf.sublist(0, bytesRead);
|
||||
} catch (E) {}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -98,15 +100,22 @@ Stream<List<int>> tail(final File file) async* {
|
|||
// Step 2: wait for modify events and read more bytes from file
|
||||
await for (final event in file.watch(events: FileSystemEvent.modify)) {
|
||||
if ((event as FileSystemModifyEvent).contentChanged) {
|
||||
len = await (randomAccess.length());
|
||||
yield* _read();
|
||||
try {
|
||||
len = await (randomAccess.length());
|
||||
yield* _read();
|
||||
} catch (E) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void tailAndPrint(File file) {
|
||||
tail(file).transform(utf8.decoder).transform(LineSplitter()).forEach((line) {
|
||||
// Do something with the line that has been read, e.g. print it out...
|
||||
print(line);
|
||||
});
|
||||
try {
|
||||
tail(file)
|
||||
.transform(utf8.decoder)
|
||||
.transform(LineSplitter())
|
||||
.forEach((line) {
|
||||
// Do something with the line that has been read, e.g. print it out...
|
||||
print(line);
|
||||
});
|
||||
} catch (E) {}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue