toLines static method
Transforms a stream of Uint8List into a stream of lines separated by delimiter.
Useful for text-based protocols like ELM327 (OBD-II).
Default delimiter is '\r' (0x0D).
Implementation
static StreamTransformer<Uint8List, String> toLines({int delimiter = 13}) {
return StreamTransformer<Uint8List, String>.fromHandlers(
handleData: (data, sink) {
// Simple implementation: convert to string and split.
// For production, a buffer-based approach handling split delimiters is better.
// Here we assume ASCII for simplicity as per common ELM327 usage.
// Note: This simple implementation might break if a delimiter is split across chunks.
// But for standard OBD responses which are short, this often works.
// A robust implementation would use a buffer.
// Let's use a robust buffer approach.
_buffer.addAll(data);
int index;
while ((index = _buffer.indexOf(delimiter)) != -1) {
final lineBytes = _buffer.sublist(0, index);
sink.add(String.fromCharCodes(lineBytes));
_buffer.removeRange(0, index + 1);
}
},
);
}