Java tutorial
//package com.java2s; //License from project: Apache License import java.net.InetAddress; public class Main { /** * @return positive - current is better, 0 - cannot tell from MAC addr, negative - candidate is better. */ private static int compareAddresses(byte[] current, byte[] candidate) { if (candidate == null) { return 1; } // Must be EUI-48 or longer. if (candidate.length < 6) { return 1; } // Must not be filled with only 0 and 1. boolean onlyZeroAndOne = true; for (byte b : candidate) { if (b != 0 && b != 1) { onlyZeroAndOne = false; break; } } if (onlyZeroAndOne) { return 1; } // Must not be a multicast address if ((candidate[0] & 1) != 0) { return 1; } // Prefer globally unique address. if ((current[0] & 2) == 0) { if ((candidate[0] & 2) == 0) { // Both current and candidate are globally unique addresses. return 0; } else { // Only current is globally unique. return 1; } } else { if ((candidate[0] & 2) == 0) { // Only candidate is globally unique. return -1; } else { // Both current and candidate are non-unique. return 0; } } } /** * @return positive - current is better, 0 - cannot tell, negative - candidate is better */ private static int compareAddresses(InetAddress current, InetAddress candidate) { return scoreAddress(current) - scoreAddress(candidate); } private static int scoreAddress(InetAddress addr) { if (addr.isAnyLocalAddress()) { return 0; } if (addr.isMulticastAddress()) { return 1; } if (addr.isLinkLocalAddress()) { return 2; } if (addr.isSiteLocalAddress()) { return 3; } return 4; } }