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 <d9f7e57f5d%5E%21/>
This commit is contained in:
Romain Vimont 2024-10-01 22:49:55 +02:00
parent c0a6432967
commit 79014143b9

View File

@ -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 <https://github.com/Genymobile/scrcpy/issues/291>.
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);
}
}