Skip to content

Commit 527b17e

Browse files
committed
Data Format
1 parent a128d44 commit 527b17e

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

android/app/src/main/java/betaflight/configurator/plugin/SocketPlugin.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package betaflight.configurator.plugin;
22

3+
import android.util.Base64;
34
import android.util.Log;
45
import com.getcapacitor.JSObject;
56
import com.getcapacitor.Plugin;
@@ -13,6 +14,7 @@
1314
import java.net.InetSocketAddress;
1415
import java.net.Socket;
1516
import java.nio.charset.StandardCharsets;
17+
import java.util.Arrays;
1618
import java.util.concurrent.atomic.AtomicReference;
1719
import java.util.concurrent.locks.ReentrantLock;
1820

@@ -217,28 +219,19 @@ private void startReaderThread() {
217219
readerThread = new Thread(() -> {
218220
Log.d(TAG, "Reader thread started");
219221
try {
220-
ByteArrayOutputStream lineBuf = new ByteArrayOutputStream();
222+
byte[] buf = new byte[4096];
221223
while (readerRunning && state.get() == ConnectionState.CONNECTED && input != null) {
222-
int b = input.read();
223-
if (b == -1) {
224+
int read = input.read(buf);
225+
if (read == -1) {
224226
notifyDisconnectFromPeer();
225227
break;
226228
}
227-
if (b == '\n') {
228-
String line = new String(lineBuf.toByteArray(), StandardCharsets.UTF_8);
229-
lineBuf.reset();
230-
if (line.endsWith("\r")) {
231-
line = line.substring(0, line.length() - 1);
232-
}
229+
if (read > 0) {
230+
byte[] chunk = Arrays.copyOf(buf, read);
231+
String b64 = Base64.encodeToString(chunk, Base64.NO_WRAP);
233232
JSObject payload = new JSObject();
234-
payload.put("data", line);
233+
payload.put("data", b64);
235234
notifyListeners("dataReceived", payload);
236-
} else {
237-
lineBuf.write(b);
238-
if (lineBuf.size() > 1024 * 1024) { // safety cap
239-
lineBuf.reset();
240-
Log.w(TAG, "Dropped oversized line");
241-
}
242235
}
243236
}
244237
} catch (Exception e) {

0 commit comments

Comments
 (0)