From 4eb56eb9ca1e7bcfb74f146a9e763737d6affe55 Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Mon, 5 Dec 2016 17:45:03 +0100 Subject: [PATCH] Liveview: refactor the BT stream reading functionality Read the expected number of bytes at each step instead of a single byte. --- .../devices/liveview/LiveviewIoThread.java | 51 +++++++++---------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/liveview/LiveviewIoThread.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/liveview/LiveviewIoThread.java index f257cf84..d9ea5a6a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/liveview/LiveviewIoThread.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/liveview/LiveviewIoThread.java @@ -166,38 +166,33 @@ public class LiveviewIoThread extends GBDeviceIoThread { private byte[] parseIncoming() throws IOException { ByteArrayOutputStream msgStream = new ByteArrayOutputStream(); - int needRead = 1; + boolean finished = false; ReaderState state = ReaderState.ID; - do { + byte[] incoming = new byte[1]; - byte read = -1; - read = (byte) mInStream.read(); + while (!finished) { + mInStream.read(incoming); + msgStream.write(incoming); - if (read == -1) { - LOG.error("Invalid message received (length=" + msgStream.size() + ")"); + switch (state) { + case ID: + state = ReaderState.HEADER_LEN; + incoming = new byte[1]; + break; + case HEADER_LEN: + state = ReaderState.HEADER; + incoming = new byte[incoming[0]]; + break; + case HEADER: + int payloadSize = getLastInt(msgStream); + state = ReaderState.PAYLOAD; + incoming = new byte[payloadSize]; + break; + case PAYLOAD: //read is blocking, if we are here we have all the data + finished = true; + break; } - needRead--; - msgStream.write(read); - if (needRead == 0) { - switch (state) { - case ID: - state = ReaderState.HEADER_LEN; - needRead = 1; - break; - case HEADER_LEN: - state = ReaderState.HEADER; - needRead = read; - break; - case HEADER: - int payloadSize = getLastInt(msgStream); - state = ReaderState.PAYLOAD; - needRead = payloadSize; - break; - default: - break; - } - } - } while (needRead > 0); + } byte[] msgArray = msgStream.toByteArray(); LOG.debug("received: " + GB.hexdump(msgArray, 0, msgArray.length)); return msgArray;