From 79014143b9cc958ed4b36b8e9a49676243ca68b7 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Tue, 1 Oct 2024 22:49:55 +0200 Subject: [PATCH] Fix IO.writeFully() on Android 5 Os.write() did not update the ByteBuffer position before Android 6. A workaround was added by commit b882322f7371b16acd53677c4a3adbaaed0aef77, which fixed part of the problem, but the position was still not updated across calls, causing the wrong chunk to be written. Refs --- server/src/main/java/com/genymobile/scrcpy/util/IO.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/util/IO.java b/server/src/main/java/com/genymobile/scrcpy/util/IO.java index 5c558c1b..8ef1500d 100644 --- a/server/src/main/java/com/genymobile/scrcpy/util/IO.java +++ b/server/src/main/java/com/genymobile/scrcpy/util/IO.java @@ -31,8 +31,9 @@ public final class IO { public static void writeFully(FileDescriptor fd, ByteBuffer from) throws IOException { // ByteBuffer position is not updated as expected by Os.write() on old Android versions, so - // count the remaining bytes manually. + // handle the position and the remaining bytes manually. // See . + int position = from.position(); int remaining = from.remaining(); while (remaining > 0) { int w = write(fd, from); @@ -41,6 +42,8 @@ public final class IO { throw new AssertionError("Os.write() returned a negative value (" + w + ")"); } remaining -= w; + position += w; + from.position(position); } }