readViewFromStream method

  1. @override
Future<void> readViewFromStream(
  1. GpsPointsView<GpsPoint> view,
  2. StreamReaderState source,
  3. int version,
  4. ByteData metadata,
)
override

Overwrites the contents of view (if it is not read-only) with the information fom the source. version and metadata indicate the additional information that was read from the block header in the file, and may be used to e.g. convert old formats to new.

Implementation

@override
Future<void> readViewFromStream(GpsPointsView view, StreamReaderState source,
    int version, ByteData metadata) {
  return Future.sync(() async {
    final gpc = view as GpcEfficient;

    // Pre-allocate the capacity if possible.
    gpc.capacity = source.remainingStreamBytesHint ?? gpc.capacity;

    // Read data in roughly chunks, such that they're divisible by the
    // expected element size.
    final elementsPerChunk = chunkSize ~/ gpc.elementSizeInBytes;
    // Ensure it's robust for stupid situations where the element size is
    // huge: always read at least one element at a time.
    final chunkSizeBytes =
        gpc.elementSizeInBytes * max<int>(1, elementsPerChunk);

    do {
      final readData =
          await source.readByteData(chunkSizeBytes, gpc.elementSizeInBytes);

      if (readData.lengthInBytes == 0) {
        break;
      }
      gpc.addByteData(readData);
    } while (true);
  });
}