diff --git a/packages/pmtiles/lib/src/archive.dart b/packages/pmtiles/lib/src/archive.dart index 21da829..52045d1 100644 --- a/packages/pmtiles/lib/src/archive.dart +++ b/packages/pmtiles/lib/src/archive.dart @@ -349,6 +349,10 @@ class PmTilesArchive { return fromReadAt(FileAt(f)); } + static Future fromBytes(List bytes) async { + return fromReadAt(MemoryAt(bytes)); + } + Future close() async { return _f.close(); } diff --git a/packages/pmtiles/lib/src/io.dart b/packages/pmtiles/lib/src/io.dart index c3dccc1..0ed20cd 100644 --- a/packages/pmtiles/lib/src/io.dart +++ b/packages/pmtiles/lib/src/io.dart @@ -1,4 +1,5 @@ import 'dart:collection'; +import 'dart:math' as math; import 'package:http/http.dart'; // Browsers don't support the File APIs @@ -85,3 +86,26 @@ class CordBuffer { .toList(growable: growable); } } + +/// In memory implementation of ReadAt. +class MemoryAt implements ReadAt { + final List bytes; + + MemoryAt(this.bytes); + + @override + Future readAt(int offset, int length) async { + if (offset >= bytes.length) { + return ByteStream.fromBytes([]); + } + + return ByteStream.fromBytes( + bytes.sublist(offset, math.min(bytes.length, offset + length)), + ); + } + + @override + Future close() async { + // Does nothing. + } +}