|
1 | 1 | package betaflight.configurator.plugin; |
2 | 2 |
|
| 3 | +import android.util.Base64; |
3 | 4 | import android.util.Log; |
4 | 5 | import com.getcapacitor.JSObject; |
5 | 6 | import com.getcapacitor.Plugin; |
|
13 | 14 | import java.net.InetSocketAddress; |
14 | 15 | import java.net.Socket; |
15 | 16 | import java.nio.charset.StandardCharsets; |
| 17 | +import java.util.Arrays; |
16 | 18 | import java.util.concurrent.atomic.AtomicReference; |
17 | 19 | import java.util.concurrent.locks.ReentrantLock; |
18 | 20 |
|
@@ -217,28 +219,19 @@ private void startReaderThread() { |
217 | 219 | readerThread = new Thread(() -> { |
218 | 220 | Log.d(TAG, "Reader thread started"); |
219 | 221 | try { |
220 | | - ByteArrayOutputStream lineBuf = new ByteArrayOutputStream(); |
| 222 | + byte[] buf = new byte[4096]; |
221 | 223 | 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) { |
224 | 226 | notifyDisconnectFromPeer(); |
225 | 227 | break; |
226 | 228 | } |
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); |
233 | 232 | JSObject payload = new JSObject(); |
234 | | - payload.put("data", line); |
| 233 | + payload.put("data", b64); |
235 | 234 | 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 | | - } |
242 | 235 | } |
243 | 236 | } |
244 | 237 | } catch (Exception e) { |
|
0 commit comments