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

Commit 2ae3a7e4 authored by Shai Barack's avatar Shai Barack
Browse files

Parcel: flag marshall/unmarshall with ByteBuffer APIs

Put these APIs behind a flag so we could add them to the SDK,
at some point.

Remove the unit test for these APIs.
Another change is moving these tests to CTS, where they belong.

Bug: 401362825
Flag: android.os.parcel_marshall_bytebuffer
Change-Id: I2edcb2a5de7b4e07a36986b38246d4410bde2f75
parent c3229d98
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -34264,6 +34264,7 @@ package android.os {
    method public boolean hasFileDescriptors();
    method public boolean hasFileDescriptors(int, int);
    method public byte[] marshall();
    method @FlaggedApi("android.os.parcel_marshall_bytebuffer") public void marshall(@NonNull java.nio.ByteBuffer);
    method @NonNull public static android.os.Parcel obtain();
    method @NonNull public static android.os.Parcel obtain(@NonNull android.os.IBinder);
    method @Deprecated @Nullable public Object[] readArray(@Nullable ClassLoader);
@@ -34333,6 +34334,7 @@ package android.os {
    method public void setDataSize(int);
    method public void setPropagateAllowBlocking();
    method public void unmarshall(@NonNull byte[], int, int);
    method @FlaggedApi("android.os.parcel_marshall_bytebuffer") public void unmarshall(@NonNull java.nio.ByteBuffer);
    method public void writeArray(@Nullable Object[]);
    method public void writeBinderArray(@Nullable android.os.IBinder[]);
    method public void writeBinderList(@Nullable java.util.List<android.os.IBinder>);
+4 −4
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import static com.android.internal.util.Preconditions.checkArgument;

import static java.util.Objects.requireNonNull;

import android.annotation.FlaggedApi;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
@@ -27,6 +28,7 @@ import android.annotation.SuppressLint;
import android.annotation.TestApi;
import android.app.AppOpsManager;
import android.compat.annotation.UnsupportedAppUsage;
import android.os.Flags;
import android.ravenwood.annotation.RavenwoodClassLoadHook;
import android.ravenwood.annotation.RavenwoodKeepWholeClass;
import android.ravenwood.annotation.RavenwoodReplace;
@@ -837,9 +839,8 @@ public final class Parcel {
     * @param buffer The ByteBuffer to write the data to.
     * @throws ReadOnlyBufferException if the buffer is read-only.
     * @throws BufferOverflowException if the buffer is too small.
     *
     * @hide
     */
    @FlaggedApi(Flags.FLAG_PARCEL_MARSHALL_BYTEBUFFER)
    public final void marshall(@NonNull ByteBuffer buffer) {
        if (buffer == null) {
            throw new NullPointerException();
@@ -875,9 +876,8 @@ public final class Parcel {
     * Fills the raw bytes of this Parcel with data from the supplied buffer.
     *
     * @param buffer will read buffer.remaining() bytes from the buffer.
     *
     * @hide
     */
    @FlaggedApi(Flags.FLAG_PARCEL_MARSHALL_BYTEBUFFER)
    public final void unmarshall(@NonNull ByteBuffer buffer) {
        if (buffer == null) {
            throw new NullPointerException();
+9 −0
Original line number Diff line number Diff line
@@ -352,6 +352,15 @@ flag {
     bug: "330345513"
}

flag {
     namespace: "system_performance"
     name: "parcel_marshall_bytebuffer"
     is_exported: true
     description: "Parcel marshal/unmarshall APIs that use ByteBuffer."
     is_fixed_read_only: true
     bug: "401362825"
}

flag {
     namespace: "system_performance"
     name: "perfetto_sdk_tracing"
+0 −61
Original line number Diff line number Diff line
@@ -29,8 +29,6 @@ import android.util.Log;

import androidx.test.ext.junit.runners.AndroidJUnit4;

import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -418,63 +416,4 @@ public class ParcelTest {
        int binderEndPos = pA.dataPosition();
        assertTrue(pA.hasBinders(binderStartPos, binderEndPos - binderStartPos));
    }

    private static final byte[] TEST_DATA = new byte[] {4, 8, 15, 16, 23, 42};

    // Allow for some Parcel overhead
    private static final int TEST_DATA_LENGTH = TEST_DATA.length + 100;

    @Test
    public void testMarshall_ByteBuffer_wrapped() {
        ByteBuffer bb = ByteBuffer.allocate(TEST_DATA_LENGTH);
        testMarshall_ByteBuffer(bb);
    }

    @Test
    public void testMarshall_DirectByteBuffer() {
        ByteBuffer bb = ByteBuffer.allocateDirect(TEST_DATA_LENGTH);
        testMarshall_ByteBuffer(bb);
    }

    private void testMarshall_ByteBuffer(ByteBuffer bb) {
        // Ensure that Parcel respects the starting offset by not starting at 0
        bb.position(1);
        bb.mark();

        // Parcel test data, then marshall into the ByteBuffer
        Parcel p1 = Parcel.obtain();
        p1.writeByteArray(TEST_DATA);
        p1.marshall(bb);
        p1.recycle();

        assertTrue(bb.position() > 1);
        bb.reset();

        // Unmarshall test data into a new Parcel
        Parcel p2 = Parcel.obtain();
        bb.reset();
        p2.unmarshall(bb);
        assertTrue(bb.position() > 1);
        p2.setDataPosition(0);
        byte[] marshalled = p2.marshall();

        bb.reset();
        for (int i = 0; i < TEST_DATA.length; i++) {
            assertEquals(bb.get(), marshalled[i]);
        }

        byte[] testDataCopy = new byte[TEST_DATA.length];
        p2.setDataPosition(0);
        p2.readByteArray(testDataCopy);
        for (int i = 0; i < TEST_DATA.length; i++) {
            assertEquals(TEST_DATA[i], testDataCopy[i]);
        }

        // Test that overflowing the buffer throws an exception
        bb.reset();
        // Leave certainly not enough room for the test data
        bb.limit(bb.position() + TEST_DATA.length - 1);
        assertThrows(BufferOverflowException.class, () -> p2.marshall(bb));
        p2.recycle();
    }
}