Skip to content

Commit

Permalink
Added a new MemoryReadAt for reading pmtiles from byte arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
bramp committed Feb 1, 2024
1 parent fec17b5 commit 9f7d933
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/pmtiles/lib/src/archive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,10 @@ class PmTilesArchive {
return fromReadAt(FileAt(f));
}

static Future<PmTilesArchive> fromBytes(List<int> bytes) async {
return fromReadAt(MemoryAt(bytes));
}

Future<void> close() async {
return _f.close();
}
Expand Down
24 changes: 24 additions & 0 deletions packages/pmtiles/lib/src/io.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:collection';
import 'dart:math' as math;
import 'package:http/http.dart';

// Browsers don't support the File APIs
Expand Down Expand Up @@ -85,3 +86,26 @@ class CordBuffer {
.toList(growable: growable);
}
}

/// In memory implementation of ReadAt.
class MemoryAt implements ReadAt {
final List<int> bytes;

MemoryAt(this.bytes);

@override
Future<ByteStream> 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<void> close() async {
// Does nothing.
}
}

0 comments on commit 9f7d933

Please sign in to comment.