📄 inetaddressfactory.java
字号:
import java.net.*;import java.util.StringTokenizer;public class InetAddressFactory { // Use a byte array like {(byte) 199, (byte) 1, (byte) 32, (byte) 90} // to build an InetAddressObject public static InetAddress newInetAddress(byte addr[]) throws UnknownHostException { try { InetAddress ia = InetAddress.getByAddress(addr) ; return ia; } catch (Exception e) { // primarily ArrayIndexOutOfBoundsExceptions throw new UnknownHostException(e.toString()); } } // end newInetAddress // Use a String like 199.1.32.90 to build // an InetAddressObject public static InetAddress newInetAddress(String s) throws UnknownHostException { // be ready for IPv6 int num_bytes_in_an_IP_address = 4; byte addr[] = new byte[num_bytes_in_an_IP_address]; StringTokenizer st = new StringTokenizer(s, "."); // make sure the format is correct if (st.countTokens() != addr.length) { throw new UnknownHostException(s + " is not a valid numeric IP address"); } for (int i = 0; i < addr.length; i++) { int thisByte = Integer.parseInt(st.nextToken()); if (thisByte < 0 || thisByte > 255) { throw new UnknownHostException(s + " is not a valid numeric IP address"); } // check this if (thisByte > 127) thisByte -= 256; addr[i] = (byte) thisByte; } // end for return newInetAddress(addr); } // end newInetAddress } // end InetAddressFactory
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -