Skip to content

Commit

Permalink
Work around Os.write() not updating position
Browse files Browse the repository at this point in the history
ByteBuffer position is not updated as expected by Os.write() on old
Android versions. Count the remaining bytes manually.

Fixes <Genymobile#291>.
  • Loading branch information
rom1v committed Oct 9, 2018
1 parent 8875955 commit b882322
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions server/src/main/java/com/genymobile/scrcpy/IO.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@ private 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 <https://github.com/Genymobile/scrcpy/issues/291>.
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);
Expand Down

0 comments on commit b882322

Please sign in to comment.