discontinuedPackage fluttersdk_telescope has been discontinued and replaced by crumbs.

read method

Future<Map<String, Object>?> read(
  1. String name, {
  2. int offset = 0,
  3. int maxBytes = 65536,
})

Read whole lines of the file called name, from byte offset, after a flush.

Returns {lines, next}: lines the complete lines found within maxBytes bytes of offset (without their newline), next the offset to pass to continue. A line longer than maxBytes is returned whole rather than cut. At the end of the file lines is empty and next equals offset. An offset that is not 0 or a previous next may start mid-line.

An offset inside a multi-byte character decodes the cut bytes as U+FFFD rather than fail.

Returns null, a refusal, when name is not exactly the name of a timeline file files lists (a path, a .. segment, a link, or another file in the directory are all refused), when offset is negative, or when maxBytes is not positive.

Implementation

Future<Map<String, Object>?> read(
  String name, {
  int offset = 0,
  int maxBytes = 65536,
}) async {
  if (offset < 0 || maxBytes < 1) return null;
  await flush();

  return _serial(() async {
    // The listing is the traversal guard: [name] is only compared, never
    // joined into a path, and the file read is the entry the directory
    // listing returned.
    final File? file = await _find(name);
    if (file == null) return null;

    final Uint8List bytes = await _readLines(file, offset, maxBytes);
    final List<String> lines = bytes.isEmpty
        ? <String>[]
        : (utf8.decode(bytes, allowMalformed: true).split('\n')
          ..removeLast());

    return <String, Object>{
      'lines': lines,
      'next': offset + bytes.length,
    };
  });
}