Package michael. utils; Import java.net. InetAddress; Public class implements 4util { Private final static int INADDRSZ = 4; Public static byte [] ipToBytesByInet (String ipAddr ){ Try { Return InetAddress. getByName (ipAddr). getAddress (); } Catch (Exception e ){ Throw new IllegalArgumentException (ipAddr + "is invalid IP "); } } JTA practice: Spring + ATOMIKOS Public static byte [] ipToBytesByReg (String ipAddr ){ Byte [] ret = new byte [4]; Try { String [] ipArr = ipAddr. split ("\\."); Ret [0] = (byte) (Integer. parseInt (ipArr [0]) & 0xFF ); Ret [1] = (byte) (Integer. parseInt (ipArr [1]) & 0xFF ); Ret [2] = (byte) (Integer. parseInt (ipArr [2]) & 0xFF ); Ret [3] = (byte) (Integer. parseInt (ipArr [3]) & 0xFF ); Return ret; } Catch (Exception e ){ Throw new IllegalArgumentException (ipAddr + "is invalid IP "); } } Public static String bytesToIp (byte [] bytes ){ Return new StringBuffer (). append (bytes [0] & 0xFF). append ('.'). append ( Bytes [1] & 0xFF). append ('.'). append (bytes [2] & 0xFF) . Append ('.'). append (bytes [3] & 0xFF). toString (); } Public static int bytesToInt (byte [] bytes ){ Int addr = bytes [3] & 0xFF; Addr | = (bytes [2] <8) & 0xFF00 ); Addr | = (bytes [1] <16) & 0xFF0000 ); Addr | = (bytes [0] <24) & 0xFF000000 ); Return addr; } Public static int ipToInt (String ipAddr ){ Try { Return bytesToInt (ipToBytesByInet (ipAddr )); } Catch (Exception e ){ Throw new IllegalArgumentException (ipAddr + "is invalid IP "); } } Public static byte [] intToBytes (int ipInt ){ Byte [] ipAddr = new byte [INADDRSZ]; IpAddr [0] = (byte) (ipInt >>> 24) & 0xFF ); IpAddr [1] = (byte) (ipInt >>> 16) & 0xFF ); IpAddr [2] = (byte) (ipInt >>> 8) & 0xFF ); IpAddr [3] = (byte) (ipInt & 0xFF ); Return ipAddr; } Public static String intToIp (int ipInt ){ Return new StringBuilder (). append (ipInt> 24) & 0xff). append ('.') . Append (ipInt> 16) & 0xff). append ('.'). append ( (IpInt> 8) & 0xff). append ('.'). append (ipInt & 0xff )) . ToString (); } Public static int [] getIPIntScope (String ipAndMask ){ String [] ipArr = ipAndMask. split ("/"); If (ipArr. length! = 2 ){ Throw new IllegalArgumentException ("invalid ipAndMask :" + IpAndMask ); } Int netMask = Integer. valueOf (ipArr [1]. trim ()); If (netMask <0 | netMask> 31 ){ Throw new IllegalArgumentException ("invalid ipAndMask :" + IpAndMask ); } Int ipInt = IPv4Util. ipToInt (ipArr [0]); Int netIP = ipInt & (0 xFFFFFFFF <(32-netMask )); Int hostScope = (0 xFFFFFFFF >>> netMask ); Return new int [] {netIP, netIP + hostScope }; } Public static String [] getIPAddrScope (String ipAndMask ){ Int [] ipIntArr = IPv4Util. getIPIntScope (ipAndMask ); Return new String [] {IPv4Util. intToIp (ipIntArr [0]), Listen 4util. intToIp (ipIntArr [0])}; } Public static int [] getIPIntScope (String ipAddr, String mask ){ Int ipInt; Int netMaskInt = 0, ipcount = 0; Try { IpInt = IPv4Util. ipToInt (ipAddr ); If (null = mask | "". equals (mask )){ Return new int [] {ipInt, ipInt }; } NetMaskInt = IPv4Util. ipToInt (mask ); Ipcount = IPv4Util. ipToInt ("255.255.255.255")-netMaskInt; Int netIP = ipInt & netMaskInt; Int hostScope = netIP + ipcount; Return new int [] {netIP, hostScope }; } Catch (Exception e ){ Throw new IllegalArgumentException ("invalid ip scope express ip :" + IpAddr + "mask:" + mask ); } } Public static String [] getIPStrScope (String ipAddr, String mask ){ Int [] ipIntArr = IPv4Util. getIPIntScope (ipAddr, mask ); Return new String [] {IPv4Util. intToIp (ipIntArr [0]), Listen 4util. intToIp (ipIntArr [0])}; } Public static void main (String [] args) throws Exception { String ipAddr = "192.168.8.1 "; Byte [] bytearr = IPv4Util. ipToBytesByInet (ipAddr ); StringBuffer byteStr = new StringBuffer (); For (byte B: bytearr ){ If (byteStr. length () = 0 ){ ByteStr. append (B ); } Else { ByteStr. append ("," + B ); } } System. out. println ("IP:" + ipAddr + "ByInet --> byte []: [" + byteStr + "]"); Bytearr = IPv4Util. ipToBytesByReg (ipAddr ); ByteStr = new StringBuffer (); For (byte B: bytearr ){ If (byteStr. length () = 0 ){ ByteStr. append (B ); } Else { ByteStr. append ("," + B ); } } System. out. println ("IP:" + ipAddr + "ByReg --> byte []: [" + byteStr + "]"); System. out. println ("byte []:" + byteStr + "--> IP :" + IPv4Util. bytesToIp (bytearr )); Int ipInt = IPv4Util. ipToInt (ipAddr ); System. out. println ("IP:" + ipAddr + "--> int:" + ipInt ); System. out. println ("int:" + ipInt + "--> IP :" + IPv4Util. intToIp (ipInt )); String ipAndMask = "192.168.1.1/24 "; Int [] ipscope = IPv4Util. getIPIntScope (ipAndMask ); System. out. println (ipAndMask + "--> int address segment: [" + ipscope [0] + "," + Ipscope [1] + "]"); System. out. println (ipAndMask + "--> IP address segment :[" + IPv4Util. intToIp (ipscope [0]) + "," + IPv4Util. intToIp (ipscope [1]) + "]"); String ipAddr1 = "192.168.1.1", ipMask1 = "255.255.255.0 "; Int [] ipscope1 = IPv4Util. getIPIntScope (ipAddr1, ipMask1 ); System. out. println (ipAddr1 + "," + ipMask1 + "--> int address segment :[" + Ipscope1 [0] + "," + ipscope1 [1] + "]"); System. out. println (ipAddr1 + "," + ipMask1 + "--> ip address segment :[" + IPv4Util. intToIp (ipscope1 [0]) + "," + IPv4Util. intToIp (ipscope1 [1]) + "]"); } } |