readUntilTerminatingByte method

Uint8List readUntilTerminatingByte(
  1. int value, {
  2. bool? copy,
  3. bool include = false,
})

Reads the buffer until the terminating value is reached (e.g. null-terminated bytes).

When include is set, the returned Uint8List will also contain the terminating value, otherwise it is left out.

Throws StateError when value is not yet present in the buffer.

Implementation

Uint8List readUntilTerminatingByte(
  int value, {
  bool? copy,
  bool include = false,
}) {
  final bb = BytesBuffer(copy: copy ?? _copy);
  while (true) {
    _clearQueue();
    if (_queue.isEmpty) {
      if (bb.length > 0) {
        _queue.add(bb.toBytes());
      }
      throw StateError('Not enough bytes to read.');
    }
    final index = _queue.first.indexOf(value, _offset);
    if (index < 0) {
      _removeFirstQueueItemAddToBuffer(bb);
      continue;
    } else {
      final amount = (include ? index + 1 : index) - _offset;
      if (amount > 0) {
        _addNextBytesToBuffer(bb, amount);
      }
      break;
    }
  }
  if (!include) {
    _offset++;
  }
  return bb.toBytes();
}