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

Commit 8c37737c authored by Tim Murray's avatar Tim Murray
Browse files

FileBridge: use a ByteBuffer for the temp buffer

Using a direct ByteBuffer allows the buffer to be passed to/from JNI
without intermediate copies.

Test: FileBridgeTests, apps get installed without allocation churn
Bug: 192020559
Change-Id: Ic5965c94ba14de6599af67d2e3bba33cd9996046
parent 9b03c1c4
Loading
Loading
Loading
Loading
+9 −3
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import libcore.io.Streams;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
@@ -95,9 +96,11 @@ public class FileBridge extends Thread {

    @Override
    public void run() {
        final byte[] temp = new byte[8192];
        final ByteBuffer tempBuffer = ByteBuffer.allocateDirect(8192);
        final byte[] temp = tempBuffer.hasArray() ? tempBuffer.array() : new byte[8192];
        try {
            while (IoBridge.read(mServer.getFileDescriptor(), temp, 0, MSG_LENGTH) == MSG_LENGTH) {
            while (IoBridge.read(mServer.getFileDescriptor(), temp,
                                 0, MSG_LENGTH) == MSG_LENGTH) {
                final int cmd = Memory.peekInt(temp, 0, ByteOrder.BIG_ENDIAN);
                if (cmd == CMD_WRITE) {
                    // Shuttle data into local file
@@ -138,7 +141,10 @@ public class FileBridge extends Thread {
    public static class FileBridgeOutputStream extends OutputStream {
        private final ParcelFileDescriptor mClientPfd;
        private final FileDescriptor mClient;
        private final byte[] mTemp = new byte[MSG_LENGTH];
        private final ByteBuffer mTempBuffer = ByteBuffer.allocateDirect(MSG_LENGTH);
        private final byte[] mTemp = mTempBuffer.hasArray()
                                     ? mTempBuffer.array()
                                     : new byte[MSG_LENGTH];

        public FileBridgeOutputStream(ParcelFileDescriptor clientPfd) {
            mClientPfd = clientPfd;