Skip to content

Commit

Permalink
fix(geobase): improved byte writer chunk flushing #115
Browse files Browse the repository at this point in the history
  • Loading branch information
navispatial committed Jul 2, 2022
1 parent fe04bab commit 2ecc2c4
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions dart/geobase/lib/src/utils/byte_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//
// Docs: https://github.com/navibyte/geospatial

import 'dart:math' as math;

import 'dart:typed_data';

/// A writer (integer and floating point values) writing a sequence of bytes.
Expand All @@ -13,6 +15,7 @@ class ByteWriter {
final _ChunkedByteBuffer _buffer;
ByteData _chunk;
int _offset = 0;
bool _needNewChunk = false;

/// Endianness to be used when writing a sequence of bytes.
final Endian endian;
Expand All @@ -33,27 +36,30 @@ class ByteWriter {
_chunk = ByteData(bufferSize);

void _reserve(int len) {
if (_offset + len > bufferSize) {
if (_flush()) {
_chunk = ByteData(bufferSize);
if (_needNewChunk) {
_chunk = ByteData(math.max(bufferSize, len));
_offset = 0;
_needNewChunk = false;
} else {
if (_offset + len > bufferSize) {
_flush();
_chunk = ByteData(math.max(bufferSize, len));
_offset = 0;
_needNewChunk = false;
}
}
}

bool _flush() {
void _flush() {
if (_offset > 0) {
_buffer.addBytes(_chunk.buffer.asUint8List(0, _offset));
_offset = 0;
return true;
} else {
_offset = 0;
return false;
_needNewChunk = true;
}
}

/// Collects the data written to a sequence of bytes in a Uint8List.
Uint8List toBytes() {
_flush();
_flush();
return _buffer.toBytes();
}

Expand Down Expand Up @@ -109,7 +115,6 @@ class ByteWriter {
_reserve(8);
_chunk.setInt64(_offset, value, endian);
_offset += 8;

}

/// Writes [value] as a single byte (unsigned binary representation).
Expand Down

0 comments on commit 2ecc2c4

Please sign in to comment.