Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 55543817 authored by Roman Kiryanov's avatar Roman Kiryanov Committed by Automerger Merge Worker
Browse files

Merge "Add wrappers around IO functions to check the return values" into...

Merge "Add wrappers around IO functions to check the return values" into tm-dev am: bce1d372 am: fa35f3ea

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/18283701



Change-Id: I0d89df80ab6f552b0b3d7c93a517e23d77b0357a
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 0d6a4a13 fa35f3ea
Loading
Loading
Loading
Loading
+35 −6
Original line number Original line Diff line number Diff line
@@ -25,6 +25,7 @@ import android.system.OsConstants;
import android.system.VmSocketAddress;
import android.system.VmSocketAddress;
import android.util.Slog;
import android.util.Slog;


import java.io.EOFException;
import java.io.FileDescriptor;
import java.io.FileDescriptor;
import java.io.InterruptedIOException;
import java.io.InterruptedIOException;
import java.net.SocketException;
import java.net.SocketException;
@@ -93,16 +94,16 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
        }
        }
    }
    }


    private byte[] receiveMessage() throws ErrnoException, InterruptedIOException {
    private byte[] receiveMessage() throws ErrnoException, InterruptedIOException, EOFException {
        final byte[] lengthBits = new byte[4];
        final byte[] lengthBits = new byte[4];
        Os.read(mPipe, lengthBits, 0, lengthBits.length);
        readFully(mPipe, lengthBits, 0, lengthBits.length);


        final ByteBuffer bb = ByteBuffer.wrap(lengthBits);
        final ByteBuffer bb = ByteBuffer.wrap(lengthBits);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        final int msgLen = bb.getInt();
        final int msgLen = bb.getInt();


        final byte[] msg = new byte[msgLen];
        final byte[] msg = new byte[msgLen];
        Os.read(mPipe, msg, 0, msg.length);
        readFully(mPipe, msg, 0, msg.length);


        return msg;
        return msg;
    }
    }
@@ -115,8 +116,8 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
        bb.order(ByteOrder.LITTLE_ENDIAN);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        bb.putInt(msg.length);
        bb.putInt(msg.length);


        Os.write(fd, lengthBits, 0, lengthBits.length);
        writeFully(fd, lengthBits, 0, lengthBits.length);
        Os.write(fd, msg, 0, msg.length);
        writeFully(fd, msg, 0, msg.length);
    }
    }


    EmulatorClipboardMonitor(final Consumer<ClipData> setAndroidClipboard) {
    EmulatorClipboardMonitor(final Consumer<ClipData> setAndroidClipboard) {
@@ -141,7 +142,7 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
                        Slog.i(TAG, "Setting the guest clipboard to '" + str + "'");
                        Slog.i(TAG, "Setting the guest clipboard to '" + str + "'");
                    }
                    }
                    setAndroidClipboard.accept(clip);
                    setAndroidClipboard.accept(clip);
                } catch (ErrnoException | InterruptedIOException e) {
                } catch (ErrnoException | EOFException | InterruptedIOException e) {
                    closePipe();
                    closePipe();
                } catch (InterruptedException | IllegalArgumentException e) {
                } catch (InterruptedException | IllegalArgumentException e) {
                }
                }
@@ -182,4 +183,32 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
            t.start();
            t.start();
        }
        }
    }
    }

    private static void readFully(final FileDescriptor fd,
                                  final byte[] buf, int offset, int size)
                                  throws ErrnoException, InterruptedIOException, EOFException {
        while (size > 0) {
            final int r = Os.read(fd, buf, offset, size);
            if (r > 0) {
                offset += r;
                size -= r;
            } else {
                throw new EOFException();
            }
        }
    }

    private static void writeFully(final FileDescriptor fd,
                                   final byte[] buf, int offset, int size)
                                   throws ErrnoException, InterruptedIOException {
        while (size > 0) {
            final int r = Os.write(fd, buf, offset, size);
            if (r > 0) {
                offset += r;
                size -= r;
            } else {
                throw new ErrnoException("write", OsConstants.EIO);
            }
        }
    }
}
}