From 83ca56d80dc43d1f6eb6f6e961fa22f68c0710b4 Mon Sep 17 00:00:00 2001 From: mayong Date: Fri, 24 Nov 2017 09:59:01 +0800 Subject: [PATCH] avoid call NetworkInterface.getByInetAddress in getHardwareAddress getHardwareAddress will be called frequently. And in Android platform, it will call getNetworkInterfacesList, and small array objects are created which will casue GC. --- .../android/AndroidNetworkAddressFactory.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java b/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java index 74d8bd479..d4dcb7b63 100644 --- a/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java +++ b/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java @@ -23,6 +23,7 @@ import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; +import java.util.Enumeration; import java.util.logging.Level; import java.util.logging.Logger; @@ -110,4 +111,23 @@ protected void discoverNetworkInterfaces() throws InitializationException { super.discoverNetworkInterfaces(); } } + + public byte[] getHardwareAddress(InetAddress inetAddress) { + try { + if (inetAddress == null) { + throw new NullPointerException("inetAddress == null"); + } + for (NetworkInterface networkInterface : networkInterfaces) { + Enumeration e = networkInterface.getInetAddresses(); + while(e.hasMoreElements()) { + e.nextElement().equals(inetAddress); + return networkInterface.getHardwareAddress(); + } + } + return null; + } catch (Throwable ex) { + log.log(Level.WARNING, "Cannot get hardware address for: " + inetAddress, ex); + return null; + } + } }