NetUtils { public static int getSubnetMaskLength(byte[] subnetMask) { int maskLength = 0; if (subnetMask != null && (subnetMask.length == 4 || subnetMask.length == 16)) { int index = 0; while (index < subnetMask.length && subnetMask[index] == (byte) 0xFF) { maskLength += NetUtils.NUM_BITS_IN_A_BYTE; index++; } if (index != subnetMask.length) { int bits = NetUtils.NUM_BITS_IN_A_BYTE - 1; while (bits >= 0 && (subnetMask[index] & 1 << bits) != 0) { bits--; maskLength++; } } } return maskLength; } private  NetUtils(); static int byteArray4ToInt(byte[] ba); static long byteArray6ToLong(byte[] ba); static byte[] longToByteArray6(long addr); static byte[] intToByteArray4(int value); static InetAddress getInetAddress(int address); static InetAddress getInetNetworkMask(int prefixMaskLength, boolean isV6); static int getSubnetMaskLength(byte[] subnetMask); static int getSubnetMaskLength(InetAddress subnetMask); static InetAddress getSubnetPrefix(InetAddress ip, int maskLen); static boolean inetAddressConflict(InetAddress testAddress, InetAddress filterAddress, InetAddress testMask,
            InetAddress filterMask); static boolean isZeroMAC(byte[] mac); static boolean isBroadcastMACAddr(byte[] macAddress); static boolean isUnicastMACAddr(byte[] macAddress); static boolean isMulticastMACAddr(byte[] macAddress); static boolean isAny(InetAddress ip); static boolean fieldsConflict(int field1, int field2); static InetAddress parseInetAddress(String addressString); static boolean isIPv4AddressValid(String cidr); static boolean isIPv6AddressValid(String cidr); static boolean isIPAddressValid(String cidr); static int getUnsignedByte(byte value); static int getUnsignedShort(short value); static InetAddress gethighestIP(boolean v6); static byte[] getBroadcastMACAddr(); static final int NUM_BITS_IN_A_BYTE; static final int MAC_ADDR_LENGTH_IN_BYTES; static final int MAC_ADDR_LENGTH_IN_WORDS; }
@Test public void testGetSubnetLen() { byte[] address = { (byte) 128, (byte) 0, (byte) 0, 0 }; assertTrue(NetUtils.getSubnetMaskLength(address) == 1); byte[] address1 = { (byte) 255, 0, 0, 0 }; assertTrue(NetUtils.getSubnetMaskLength(address1) == 8); byte[] address2 = { (byte) 255, (byte) 255, (byte) 248, 0 }; assertTrue(NetUtils.getSubnetMaskLength(address2) == 21); byte[] address4 = { (byte) 255, (byte) 255, (byte) 255, (byte) 254 }; assertTrue(NetUtils.getSubnetMaskLength(address4) == 31); byte[] address5 = {(byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255}; assertTrue(NetUtils.getSubnetMaskLength(address5) == 128); byte[] address6 = {(byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 254}; assertTrue(NetUtils.getSubnetMaskLength(address6) == 127); byte[] address7 = {(byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0}; assertTrue(NetUtils.getSubnetMaskLength(address7) == 64); byte[] address8 = {(byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 254, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0}; assertTrue(NetUtils.getSubnetMaskLength(address8) == 63); byte[] address9 = {(byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 128, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0}; assertTrue(NetUtils.getSubnetMaskLength(address9) == 49); byte[] address10 = {(byte) 128, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0}; assertTrue(NetUtils.getSubnetMaskLength(address10) == 1); byte[] address11 = {(byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0}; assertTrue(NetUtils.getSubnetMaskLength(address11) == 0); }