From b882322f7371b16acd53677c4a3adbaaed0aef77 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Mon, 8 Oct 2018 18:49:48 +0200 Subject: [PATCH] Work around Os.write() not updating position ByteBuffer position is not updated as expected by Os.write() on old Android versions. Count the remaining bytes manually. Fixes . --- server/src/main/java/com/genymobile/scrcpy/IO.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/IO.java b/server/src/main/java/com/genymobile/scrcpy/IO.java index bfd48be2..f1f05105 100644 --- a/server/src/main/java/com/genymobile/scrcpy/IO.java +++ b/server/src/main/java/com/genymobile/scrcpy/IO.java @@ -14,9 +14,18 @@ public class IO { } public static void writeFully(FileDescriptor fd, ByteBuffer from) throws IOException { - while (from.hasRemaining()) { + // ByteBuffer position is not updated as expected by Os.write() on old Android versions, so + // count the remaining bytes manually. + // See . + int remaining = from.remaining(); + while (remaining > 0) { try { - Os.write(fd, from); + int w = Os.write(fd, from); + if (BuildConfig.DEBUG && w < 0) { + // w should not be negative, since an exception is thrown on error + throw new AssertionError("Os.write() returned a negative value (" + w + ")"); + } + remaining -= w; } catch (ErrnoException e) { if (e.errno != OsConstants.EINTR) { throw new IOException(e);