Java Utililty Methods InetAddress

List of utility methods to do InetAddress

  1. HOME
  2. Java
  3. I
  4. InetAddress

Description

The list of methods to do InetAddress are organized into topic(s).

Method

String canonicalIpAddress(InetAddress addr)
Converts the IP address to a canonical string which allows correct comparison behavior with IP address (used for sorting and range queries).
StringBuilder sb = new StringBuilder(36);
byte[] addrbytes = addr.getAddress();
if (addrbytes.length == 4) {
 for (byte b : addrbytes) {
 int i = (b & 0xff);
 if (sb.length() > 0) {
 sb.append('.');
 sb.append(String.format("%03d", i));
} else if (addrbytes.length == 16) {
 for (int i = 0; i < addrbytes.length; i++) {
 int octet = (addrbytes[i] & 0xff);
 if (sb.length() > 0 && (i % 2) == 0) {
 sb.append(':');
 sb.append(String.format("%02x", octet));
} else {
 throw new IllegalStateException("Unexpected InetAddress storage");
return sb.toString();
boolean checkConnection(InetAddress address, int port, int timeout)
check Connection
System.out.println("Checking connection...");
boolean connected = false;
long currentTime, startTime;
currentTime = startTime = System.currentTimeMillis();
while (!connected && ((currentTime - startTime) < timeout)) {
 try {
 Socket s = new Socket(address, port);
 connected = s.isConnected();
...
int compareAddresses(InetAddress one, InetAddress two)
compareTo for InetAddresses
byte[] oneBytes = one.getAddress();
byte[] twoBytes = two.getAddress();
if (oneBytes != twoBytes) {
 for (int i = 0; i < oneBytes.length; i++) {
 if (i >= twoBytes.length)
 return -1; 
 if (oneBytes[i] < twoBytes[i])
 return -1;
...
int compareInetAddresses(InetAddress a, InetAddress b)
compare Inet Addresses
int i;
if (a instanceof Inet4Address && b instanceof Inet6Address) {
 return -1;
} else if (a instanceof Inet6Address && b instanceof Inet4Address) {
 return 1;
} else if (a instanceof Inet4Address && b instanceof Inet4Address) {
 byte[] aBytes = ((Inet4Address) a).getAddress();
 byte[] bBytes = ((Inet4Address) b).getAddress();
...
int compareInetAddresses(InetAddress a1, InetAddress a2)
compare Inet Addresses
if (a1 == null)
 return (a2 == null) ? 0 : -1;
if (a2 == null)
 return 1;
if ((a1 instanceof Inet4Address) && (a2 instanceof Inet6Address))
 return -1;
if ((a1 instanceof Inet6Address) && (a2 instanceof Inet4Address))
 return 1;
...
int CompareIP(InetAddress ip1, InetAddress ip2)
Compares the two IP Addresses.
int ipval1, ipval2;
ipval1 = IPAddressToLong(ip1);
ipval2 = IPAddressToLong(ip2);
if (ipval1 == ipval2) {
 return 0;
if (ipval1 > ipval2) {
 return 1;
...
boolean contains(InetAddress network, InetAddress netmask, InetAddress ip)
Checks if a subnet contains a specific IP address.
byte[] networkBytes = network.getAddress();
byte[] netmaskBytes = netmask.getAddress();
byte[] ipBytes = ip.getAddress();
if (networkBytes.length != netmaskBytes.length || netmaskBytes.length != ipBytes.length) {
 return false;
for (int i = 0; i < netmaskBytes.length; i++) {
 int mask = netmaskBytes[i] & 0xff;
...
long convertInetAddressToLong(InetAddress addr)
convert Inet Address To Long
return addr.hashCode();
long convertInetAddressToLong(InetAddress address)
convert Inet Address To Long
byte[] ipAddressRaw = address.getAddress();
return (((ipAddressRaw[0] & 0xFF) << (3 * 8)) + ((ipAddressRaw[1] & 0xFF) << (2 * 8))
 + ((ipAddressRaw[2] & 0xFF) << (1 * 8)) + (ipAddressRaw[3] & 0xFF)) & 0xffffffffL;
InetAddress convertLongToInetAddress(long address)
convert Long To Inet Address
byte[] addr = new byte[4];
addr[0] = (byte) ((address >>> 24) & 0xFF);
addr[1] = (byte) ((address >>> 16) & 0xFF);
addr[2] = (byte) ((address >>> 8) & 0xFF);
addr[3] = (byte) (address & 0xFF);
return InetAddress.getByAddress(addr);


AltStyle によって変換されたページ (->オリジナル) /