diff --git a/staticlibs/device/com/android/net/module/util/BpfMap.java b/staticlibs/device/com/android/net/module/util/BpfMap.java index 7dbe2c494c..a3f12c1bdd 100644 --- a/staticlibs/device/com/android/net/module/util/BpfMap.java +++ b/staticlibs/device/com/android/net/module/util/BpfMap.java @@ -28,8 +28,6 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; import java.util.NoSuchElementException; import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; @@ -221,9 +219,7 @@ public class BpfMap implements IBpfMap key == null ? null : key.writeToBytes(), rawKey)) return null; - final ByteBuffer buffer = ByteBuffer.wrap(rawKey); - buffer.order(ByteOrder.nativeOrder()); - return Struct.parse(mKeyClass, buffer); + return Struct.parse(mKeyClass, rawKey); } /** @@ -261,9 +257,7 @@ public class BpfMap implements IBpfMap byte[] rawValue = new byte[mValueSize]; if (!nativeFindMapEntry(mMapFd.getFd(), key.writeToBytes(), rawValue)) return null; - final ByteBuffer buffer = ByteBuffer.wrap(rawValue); - buffer.order(ByteOrder.nativeOrder()); - return Struct.parse(mValueClass, buffer); + return Struct.parse(mValueClass, rawValue); } /** Synchronize Kernel RCU */ diff --git a/staticlibs/device/com/android/net/module/util/Struct.java b/staticlibs/device/com/android/net/module/util/Struct.java index 006642b26f..ccf3957e88 100644 --- a/staticlibs/device/com/android/net/module/util/Struct.java +++ b/staticlibs/device/com/android/net/module/util/Struct.java @@ -654,6 +654,17 @@ public class Struct { } } + /** + * Parse raw data from a byte array according to the pre-defined annotation rule and return + * the type-variable object which is subclass of Struct class. + * This assumes the raw data has the same byte order as the native order. + */ + public static T parse(final Class clazz, final byte[] bytes) { + final ByteBuffer buf = ByteBuffer.wrap(bytes); + buf.order(ByteOrder.nativeOrder()); + return parse(clazz, buf); + } + private static int getSizeInternal(final FieldInfo[] fieldInfos) { int size = 0; for (FieldInfo fi : fieldInfos) {