TryCatch tail functions

This commit is contained in:
zontreck 2024-06-05 22:01:01 -07:00
parent 2a5774bd6d
commit d885485684
5 changed files with 22 additions and 192 deletions

View file

@ -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) {}
}