Liveview: refactor the BT stream reading functionality
Read the expected number of bytes at each step instead of a single byte.
This commit is contained in:
parent
e53b8b6b32
commit
4eb56eb9ca
|
@ -166,38 +166,33 @@ public class LiveviewIoThread extends GBDeviceIoThread {
|
||||||
private byte[] parseIncoming() throws IOException {
|
private byte[] parseIncoming() throws IOException {
|
||||||
ByteArrayOutputStream msgStream = new ByteArrayOutputStream();
|
ByteArrayOutputStream msgStream = new ByteArrayOutputStream();
|
||||||
|
|
||||||
int needRead = 1;
|
boolean finished = false;
|
||||||
ReaderState state = ReaderState.ID;
|
ReaderState state = ReaderState.ID;
|
||||||
do {
|
byte[] incoming = new byte[1];
|
||||||
|
|
||||||
byte read = -1;
|
while (!finished) {
|
||||||
read = (byte) mInStream.read();
|
mInStream.read(incoming);
|
||||||
|
msgStream.write(incoming);
|
||||||
|
|
||||||
if (read == -1) {
|
|
||||||
LOG.error("Invalid message received (length=" + msgStream.size() + ")");
|
|
||||||
}
|
|
||||||
needRead--;
|
|
||||||
msgStream.write(read);
|
|
||||||
if (needRead == 0) {
|
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case ID:
|
case ID:
|
||||||
state = ReaderState.HEADER_LEN;
|
state = ReaderState.HEADER_LEN;
|
||||||
needRead = 1;
|
incoming = new byte[1];
|
||||||
break;
|
break;
|
||||||
case HEADER_LEN:
|
case HEADER_LEN:
|
||||||
state = ReaderState.HEADER;
|
state = ReaderState.HEADER;
|
||||||
needRead = read;
|
incoming = new byte[incoming[0]];
|
||||||
break;
|
break;
|
||||||
case HEADER:
|
case HEADER:
|
||||||
int payloadSize = getLastInt(msgStream);
|
int payloadSize = getLastInt(msgStream);
|
||||||
state = ReaderState.PAYLOAD;
|
state = ReaderState.PAYLOAD;
|
||||||
needRead = payloadSize;
|
incoming = new byte[payloadSize];
|
||||||
break;
|
break;
|
||||||
default:
|
case PAYLOAD: //read is blocking, if we are here we have all the data
|
||||||
|
finished = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (needRead > 0);
|
|
||||||
byte[] msgArray = msgStream.toByteArray();
|
byte[] msgArray = msgStream.toByteArray();
|
||||||
LOG.debug("received: " + GB.hexdump(msgArray, 0, msgArray.length));
|
LOG.debug("received: " + GB.hexdump(msgArray, 0, msgArray.length));
|
||||||
return msgArray;
|
return msgArray;
|
||||||
|
|
Loading…
Reference in New Issue