Search in sources :

Example 1 with MacAddressFormatException

use of org.netxms.base.MacAddressFormatException in project netxms by netxms.

the class MacAddress method parseMacAddress.

/**
 * Parse MAC address string representation. Supported representations are 6 groups of
 * two hex digits, separated by spaces, minuses, or colons; or 4 groups of three hex digits
 * separated by dots; or 12 non-separated digits; or 16 non-separated hex digits.
 * Examples of valid MAC address strings:
 * 00:10:FA:23:11:7A
 * 01 02 fa c4 10 dc
 * 00-90-0b-11-01-29
 * 009.00b.110.129
 * 0203fcd456c1
 * 0203FCD465C1DF56
 *
 * @param str MAC address string
 * @return MAC address object
 * @throws MacAddressFormatException if MAC address sting is invalid
 */
public static MacAddress parseMacAddress(String str) throws MacAddressFormatException {
    Pattern pattern = Pattern.compile("^([0-9a-fA-F]{2})[ :-]?([0-9a-fA-F]{2})[ .:-]?([0-9a-fA-F]{2})[ :-]?([0-9a-fA-F]{2})[ .:-]?([0-9a-fA-F]{2})[ :-]?([0-9a-fA-F]{2})[ .:-]?([0-9a-fA-F]{2})?[ :-]?([0-9a-fA-F]{2})?$");
    Matcher matcher = pattern.matcher(str.trim());
    if (matcher.matches()) {
        int count = matcher.groupCount();
        List<Byte> byteList = new ArrayList<Byte>();
        try {
            for (int i = 0; i < count; i++) {
                if (matcher.group(i + 1) != null)
                    byteList.add((byte) Integer.parseInt(matcher.group(i + 1), 16));
            }
        } catch (NumberFormatException e) {
            throw new MacAddressFormatException();
        }
        byte[] bytes = new byte[byteList.size()];
        for (int i = 0; i < byteList.size(); i++) bytes[i] = byteList.get(i).byteValue();
        return new MacAddress(bytes);
    } else {
        // Try xxx.xxx.xxx.xxx format (Brocade/Foundry)
        pattern = Pattern.compile("^([0-9a-fA-F]{3})\\.([0-9a-fA-F]{3})\\.([0-9a-fA-F]{3})\\.([0-9a-fA-F]{3})$");
        matcher = pattern.matcher(str.trim());
        if (matcher.matches()) {
            byte[] bytes = new byte[6];
            try {
                int j = 0;
                for (int i = 1; i <= 4; i += 2) {
                    int left = Integer.parseInt(matcher.group(i), 16);
                    int right = Integer.parseInt(matcher.group(i + 1), 16);
                    bytes[j++] = (byte) (left >> 4);
                    bytes[j++] = (byte) (((left & 0x00F) << 4) | (right >> 8));
                    bytes[j++] = (byte) (right & 0x0FF);
                }
            } catch (NumberFormatException e) {
                throw new MacAddressFormatException();
            }
            return new MacAddress(bytes);
        } else {
            throw new MacAddressFormatException();
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) MacAddressFormatException(org.netxms.base.MacAddressFormatException) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList)

Example 2 with MacAddressFormatException

use of org.netxms.base.MacAddressFormatException in project netxms by netxms.

the class MacAddressTest method testMacAddress.

public void testMacAddress() throws Exception {
    MacAddress a1 = new MacAddress();
    assertEquals("00:00:00:00:00:00", a1.toString());
    MacAddress a2 = new MacAddress(new byte[] { 0x02, 0x0C, (byte) 0xD4, 0x55, 0x22, 0x1A });
    assertEquals("02:0C:D4:55:22:1A", a2.toString());
    MacAddress a3 = MacAddress.parseMacAddress("02:0C:D4:55:22:1A");
    assertEquals(a2, a3);
    MacAddress a4 = MacAddress.parseMacAddress("020.CD4.552.21A");
    assertEquals(a2, a4);
    assertEquals("02:0C:D4:55:22:1A", a4.toString());
    try {
        MacAddress.parseMacAddress("bad mac string");
        assertTrue(false);
    } catch (MacAddressFormatException e) {
        System.out.println("Exception caught: " + e.getMessage());
    }
}
Also used : MacAddressFormatException(org.netxms.base.MacAddressFormatException) MacAddress(org.netxms.base.MacAddress)

Aggregations

MacAddressFormatException (org.netxms.base.MacAddressFormatException)2 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 MacAddress (org.netxms.base.MacAddress)1