MacAddressUtil { public static byte[] parseMAC(String value) { final byte[] machineId; final char separator; switch (value.length()) { case 17: separator = value.charAt(2); validateMacSeparator(separator); machineId = new byte[EUI48_MAC_ADDRESS_LENGTH]; break; case 23: separator = value.charAt(2); validateMacSeparator(separator); machineId = new byte[EUI64_MAC_ADDRESS_LENGTH]; break; default: throw new IllegalArgumentException("value is not supported [MAC-48, EUI-48, EUI-64]"); } final int end = machineId.length - 1; int j = 0; for (int i = 0; i < end; ++i, j += 3) { final int sIndex = j + 2; machineId[i] = StringUtil.decodeHexByte(value, j); if (value.charAt(sIndex) != separator) { throw new IllegalArgumentException("expected separator '" + separator + " but got '" + value.charAt(sIndex) + "' at index: " + sIndex); } } machineId[end] = StringUtil.decodeHexByte(value, j); return machineId; } private  MacAddressUtil(); static byte[] bestAvailableMac(); static byte[] defaultMachineId(); static byte[] parseMAC(String value); static String formatAddress(byte[] addr);  }
@Test public void testParseMacEUI48() { assertArrayEquals(new byte[]{0, (byte) 0xaa, 0x11, (byte) 0xbb, 0x22, (byte) 0xcc}, parseMAC("00-AA-11-BB-22-CC")); assertArrayEquals(new byte[]{0, (byte) 0xaa, 0x11, (byte) 0xbb, 0x22, (byte) 0xcc}, parseMAC("00:AA:11:BB:22:CC")); }
@Test public void testParseMacMAC48ToEUI64() { assertArrayEquals(new byte[]{0, (byte) 0xaa, 0x11, (byte) 0xff, (byte) 0xff, (byte) 0xbb, 0x22, (byte) 0xcc}, parseMAC("00-AA-11-FF-FF-BB-22-CC")); assertArrayEquals(new byte[]{0, (byte) 0xaa, 0x11, (byte) 0xff, (byte) 0xff, (byte) 0xbb, 0x22, (byte) 0xcc}, parseMAC("00:AA:11:FF:FF:BB:22:CC")); }
@Test public void testParseMacEUI48ToEUI64() { assertArrayEquals(new byte[]{0, (byte) 0xaa, 0x11, (byte) 0xff, (byte) 0xfe, (byte) 0xbb, 0x22, (byte) 0xcc}, parseMAC("00-AA-11-FF-FE-BB-22-CC")); assertArrayEquals(new byte[]{0, (byte) 0xaa, 0x11, (byte) 0xff, (byte) 0xfe, (byte) 0xbb, 0x22, (byte) 0xcc}, parseMAC("00:AA:11:FF:FE:BB:22:CC")); }
@Test(expected = IllegalArgumentException.class) public void testParseMacInvalid7HexGroupsA() { parseMAC("00-AA-11-BB-22-CC-FF"); }
@Test(expected = IllegalArgumentException.class) public void testParseMacInvalid7HexGroupsB() { parseMAC("00:AA:11:BB:22:CC:FF"); }
@Test(expected = IllegalArgumentException.class) public void testParseMacInvalidEUI48MixedSeparatorA() { parseMAC("00-AA:11-BB-22-CC"); }
@Test(expected = IllegalArgumentException.class) public void testParseMacInvalidEUI48MixedSeparatorB() { parseMAC("00:AA-11:BB:22:CC"); }
@Test(expected = IllegalArgumentException.class) public void testParseMacInvalidEUI64MixedSeparatorA() { parseMAC("00-AA-11-FF-FE-BB-22:CC"); }
@Test(expected = IllegalArgumentException.class) public void testParseMacInvalidEUI64MixedSeparatorB() { parseMAC("00:AA:11:FF:FE:BB:22-CC"); }
@Test(expected = IllegalArgumentException.class) public void testParseMacInvalidEUI48TrailingSeparatorA() { parseMAC("00-AA-11-BB-22-CC-"); }
@Test(expected = IllegalArgumentException.class) public void testParseMacInvalidEUI48TrailingSeparatorB() { parseMAC("00:AA:11:BB:22:CC:"); }
@Test(expected = IllegalArgumentException.class) public void testParseMacInvalidEUI64TrailingSeparatorA() { parseMAC("00-AA-11-FF-FE-BB-22-CC-"); }
@Test(expected = IllegalArgumentException.class) public void testParseMacInvalidEUI64TrailingSeparatorB() { parseMAC("00:AA:11:FF:FE:BB:22:CC:"); }