readBytesUtilTerminator method

SubBytes readBytesUtilTerminator(
  1. List<int> bytes, {
  2. List<int>? terminator,
})

Implementation

SubBytes readBytesUtilTerminator(List<int> bytes, {List<int>? terminator}) {
  if (bytes.isEmpty) return SubBytes.o(bytes);
  // ignore: no_leading_underscores_for_local_identifiers
  List<int> _terminator;
  if (terminator == null || terminator.isEmpty == true) {
    if (codecType == ByteCodecType.UTF16 ||
    codecType == ByteCodecType.UTF16BE ||
    codecType == ByteCodecType.UTF16LE) {
      _terminator = [0x00, 0x00];
    } else {
      _terminator = [0x00];
    }
  } else {
    _terminator = terminator;
  }
  if (_terminator.isEmpty || _terminator.length > bytes.length) return SubBytes.o(bytes);
  int findTerminatorIndex = 0;
  for (int i = 0; i < bytes.length; i = i + _terminator.length) {
    final sub = bytes.sublist(i, i + _terminator.length);
    bool match = true;
    for (var j = 0; j < sub.length; j++) {
      if (sub[j] != _terminator[j]) {
        match = false;
        break;
      }
    }
    if (match) {
      findTerminatorIndex = i + _terminator.length;
      break;
    }
  }
  final subBytes = findTerminatorIndex > 0 ? bytes.sublist(0, findTerminatorIndex - _terminator.length) : bytes;
  if (findTerminatorIndex > 0) {
    return SubBytes(bytes: subBytes, terminator: _terminator);
  } else {
    return SubBytes.o(bytes);
  }
}