Adds tail functions
This commit is contained in:
parent
0b3458a0b6
commit
74fe37d7d8
2 changed files with 38 additions and 1 deletions
|
@ -1,4 +1,6 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
class PathHelper {
|
||||
String pth = "";
|
||||
|
@ -73,3 +75,38 @@ class PathHelper {
|
|||
return pth;
|
||||
}
|
||||
}
|
||||
|
||||
Stream<List<int>> tail(final File file) async* {
|
||||
final randomAccess = await file.open(mode: FileMode.read);
|
||||
var pos = await randomAccess.position();
|
||||
var len = await randomAccess.length();
|
||||
// Increase/decrease buffer size as needed.
|
||||
var buf = Uint8List(8192);
|
||||
|
||||
Stream<Uint8List> _read() async* {
|
||||
while (pos < len) {
|
||||
final bytesRead = await randomAccess.readInto(buf);
|
||||
pos += bytesRead;
|
||||
|
||||
yield buf.sublist(0, bytesRead);
|
||||
}
|
||||
}
|
||||
|
||||
// Step 1: read whole file
|
||||
yield* _read();
|
||||
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
name: libac_dart
|
||||
description: "Aria's Creations code library"
|
||||
version: 1.0.25
|
||||
version: 1.0.26
|
||||
homepage: "https://zontreck.com"
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue