From 9a4149e5dcbfe00dc26ff84eeea70eca94f97daf Mon Sep 17 00:00:00 2001 From: dahn Date: Thu, 29 Nov 2018 17:53:50 +0100 Subject: [PATCH] utils: cleanup Macaddresses utils (#2660) Cleanup parse code, fix java docs and remove unwanted comments. --- .../java/com/cloud/utils/net/MacAddress.java | 90 ++----------------- .../com/cloud/utils/net/MacAddressTest.java | 6 -- 2 files changed, 5 insertions(+), 91 deletions(-) diff --git a/utils/src/main/java/com/cloud/utils/net/MacAddress.java b/utils/src/main/java/com/cloud/utils/net/MacAddress.java index b9118cfcab0..d7ac9e39e7f 100644 --- a/utils/src/main/java/com/cloud/utils/net/MacAddress.java +++ b/utils/src/main/java/com/cloud/utils/net/MacAddress.java @@ -31,12 +31,10 @@ import java.util.Formatter; import org.apache.log4j.Logger; -import com.cloud.utils.NumbersUtil; - /** - * copied from the public domain utility from John Burkard. - * @author Johann Burkard - * @version 2.1.3 + * This class retrieves the (first) MAC address for the machine is it is loaded on and stores it statically for retrieval. + * It can also be used for formatting MAC addresses. + * copied fnd addpeted rom the public domain utility from John Burkard. **/ public class MacAddress { private static final Logger s_logger = Logger.getLogger(MacAddress.class); @@ -70,19 +68,6 @@ public class MacAddress { formatter.format("%02x%s%02x%s%02x%s%02x%s%02x%s%02x", _addr >> 40 & 0xff, separator, _addr >> 32 & 0xff, separator, _addr >> 24 & 0xff, separator, _addr >> 16 & 0xff, separator, _addr >> 8 & 0xff, separator, _addr & 0xff); return buff.toString(); - - /* - - String str = Long.toHexString(_addr); - - for (int i = str.length() - 1; i >= 0; i--) { - buff.append(str.charAt(i)); - if (separator != null && (str.length() - i) % 2 == 0) { - buff.append(separator); - } - } - return buff.reverse().toString(); - */ } @Override @@ -242,13 +227,6 @@ public class MacAddress { return null; } - public static void main(String[] args) { - MacAddress addr = MacAddress.getMacAddress(); - System.out.println("addr in integer is " + addr.toLong()); - System.out.println("addr in bytes is " + NumbersUtil.bytesToString(addr.toByteArray(), 0, addr.toByteArray().length)); - System.out.println("addr in char is " + addr.toString(":")); - } - /** * Parses a long from a hex encoded number. This method will skip * all characters that are not 0-9 and a-f (the String is lower cased first). @@ -258,7 +236,7 @@ public class MacAddress { * @return a long * @throws NullPointerException if the String is null */ - public static long parseLong(String s) throws NullPointerException { + private static long parseLong(String s) throws NullPointerException { s = s.toLowerCase(); long out = 0; byte shifts = 0; @@ -278,35 +256,6 @@ public class MacAddress { return out; } - /** - * Parses an int from a hex encoded number. This method will skip - * all characters that are not 0-9 and a-f (the String is lower cased first). - * Returns 0 if the String does not contain any interesting characters. - * - * @param s the String to extract an int from, may not be null - * @return an int - * @throws NullPointerException if the String is null - */ - public static int parseInt(String s) throws NullPointerException { - s = s.toLowerCase(); - int out = 0; - byte shifts = 0; - char c; - for (int i = 0; i < s.length() && shifts < 8; i++) { - c = s.charAt(i); - if ((c > 47) && (c < 58)) { - out <<= 4; - ++shifts; - out |= c - 48; - } else if ((c > 96) && (c < 103)) { - ++shifts; - out <<= 4; - out |= c - 87; - } - } - return out; - } - /** * Parses a short from a hex encoded number. This method will skip * all characters that are not 0-9 and a-f (the String is lower cased first). @@ -316,7 +265,7 @@ public class MacAddress { * @return a short * @throws NullPointerException if the String is null */ - public static short parseShort(String s) throws NullPointerException { + private static short parseShort(String s) throws NullPointerException { s = s.toLowerCase(); short out = 0; byte shifts = 0; @@ -335,33 +284,4 @@ public class MacAddress { } return out; } - - /** - * Parses a byte from a hex encoded number. This method will skip - * all characters that are not 0-9 and a-f (the String is lower cased first). - * Returns 0 if the String does not contain any interesting characters. - * - * @param s the String to extract a byte from, may not be null - * @return a byte - * @throws NullPointerException if the String is null - */ - public static byte parseByte(String s) throws NullPointerException { - s = s.toLowerCase(); - byte out = 0; - byte shifts = 0; - char c; - for (int i = 0; i < s.length() && shifts < 2; i++) { - c = s.charAt(i); - if ((c > 47) && (c < 58)) { - out <<= 4; - ++shifts; - out |= c - 48; - } else if ((c > 96) && (c < 103)) { - ++shifts; - out <<= 4; - out |= c - 87; - } - } - return out; - } } diff --git a/utils/src/test/java/com/cloud/utils/net/MacAddressTest.java b/utils/src/test/java/com/cloud/utils/net/MacAddressTest.java index 845a435eb54..42b03b70daf 100644 --- a/utils/src/test/java/com/cloud/utils/net/MacAddressTest.java +++ b/utils/src/test/java/com/cloud/utils/net/MacAddressTest.java @@ -51,10 +51,4 @@ public class MacAddressTest { // TODO public final void testToString() throws Exception { // TODO public final void testGetMacAddress() throws Exception { // TODO public final void testParse() throws Exception { - // TODO public final void testMain() throws Exception { - // TODO public final void testParseLong() throws Exception { - // TODO public final void testParseInt() throws Exception { - // TODO public final void testParseShort() throws Exception { - // TODO public final void testParseByte() throws Exception { - }