From e724ff43490661d9b1c7f92632303a4f08768f03 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Tue, 1 Oct 2024 22:50:34 +0200 Subject: [PATCH] Simplify IO.writeFully() for Android >= 6 Do not handle buffer properties manually for Android >= 6 (where it is already handled by Os.write()). Refs --- .../java/com/genymobile/scrcpy/util/IO.java | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) 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 8ef1500d..d9247a98 100644 --- a/server/src/main/java/com/genymobile/scrcpy/util/IO.java +++ b/server/src/main/java/com/genymobile/scrcpy/util/IO.java @@ -2,6 +2,7 @@ package com.genymobile.scrcpy.util; import com.genymobile.scrcpy.BuildConfig; +import android.os.Build; import android.system.ErrnoException; import android.system.Os; import android.system.OsConstants; @@ -30,20 +31,26 @@ 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 - // 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); - 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 + ")"); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + while (from.hasRemaining()) { + write(fd, from); + } + } else { + // ByteBuffer position is not updated as expected by Os.write() on old Android versions, so + // 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); + 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; + position += w; + from.position(position); } - remaining -= w; - position += w; - from.position(position); } }