Uint8List remain = Uint8List(0);
String utf8Log = "";
void stream2utf8(Uint8List data) {
final findLF = data.lastIndexWhere((e) => e == 0xa);// find position of the last \n
final endLF = findLF + 1; // to include LF
final len = remain.length + (findLF < 0 ? data.length : endLF);
final resemble = Uint8List(len);
if (remain.isNotEmpty) resemble.setAll(0, remain);// copy remain to front of resemble
resemble.setRange(remain.length, len, data); // append sublist of data to resemble
if (findLF < 0) { // keep in memory
remain = resemble;
} else { // line feed found, time to flush out
remain = Uint8List.sublistView(data, endLF);
Uint8List afterESC = Uint8List.sublistView(resemble);
while (afterESC.isNotEmpty) { // until empty
final findESC = afterESC.indexOf(0x1b); // find ESC position
utf8Log += utf8.decode(Uint8List.sublistView(afterESC, 0,
findESC < 0 ? afterESC.length : findESC
));
if (findESC < 0) break;
var lenESC = 1; // sequence ^[ ... m seek
if (afterESC[findESC + lenESC] == 0x5b) { // found sequence start '['
do {
lenESC ++; // one byte advance
if (afterESC[findESC + lenESC] == 0x6d) { // found end of character 'm'
lenESC ++;// one byte advance
break;
}
} while (findESC + lenESC < afterESC.length);
if (findESC + lenESC >= afterESC.length) { // todo: other ^[ sequence not yet implement!
afterESC[findESC] = 0x5E; // change ESC from 0x1b to '^' to prevent infinit loop
continue;// backoff to show this sequence
}
} else { // todo: other ^ sequence not yet implement!
afterESC[findESC] = 0x5E; // change ESC from 0x1b to '^' to prevent infinit loop
continue;// backoff to show this sequence
}
afterESC = Uint8List.sublistView(afterESC, findESC + lenESC);// ok, go ahead
}
setState((){}); // update screen if necessary
}
沒有留言:
張貼留言