Simplify IO.writeFully() for Android >= 6

Do not handle buffer properties manually for Android >= 6 (where it is
already handled by Os.write()).

Refs <d9f7e57f5d%5E%21/>
This commit is contained in:
Romain Vimont 2024-10-01 22:50:34 +02:00
parent 79014143b9
commit e724ff4349

View File

@ -2,6 +2,7 @@ package com.genymobile.scrcpy.util;
import com.genymobile.scrcpy.BuildConfig; import com.genymobile.scrcpy.BuildConfig;
import android.os.Build;
import android.system.ErrnoException; import android.system.ErrnoException;
import android.system.Os; import android.system.Os;
import android.system.OsConstants; import android.system.OsConstants;
@ -30,20 +31,26 @@ public final class IO {
} }
public static void writeFully(FileDescriptor fd, ByteBuffer from) throws IOException { 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 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// handle the position and the remaining bytes manually. while (from.hasRemaining()) {
// See <https://github.com/Genymobile/scrcpy/issues/291>. write(fd, from);
int position = from.position(); }
int remaining = from.remaining(); } else {
while (remaining > 0) { // ByteBuffer position is not updated as expected by Os.write() on old Android versions, so
int w = write(fd, from); // handle the position and the remaining bytes manually.
if (BuildConfig.DEBUG && w < 0) { // See <https://github.com/Genymobile/scrcpy/issues/291>.
// w should not be negative, since an exception is thrown on error int position = from.position();
throw new AssertionError("Os.write() returned a negative value (" + w + ")"); 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);
} }
} }