Java Host Address Get getHostName(String addr)

Here you can find the source of getHostName(String addr)

Description

get Host Name

License

Open Source License

Declaration

public static String getHostName(String addr) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main {
    public static String getHostName(String addr) {
        if (!isRawAddress(addr))
            return addr;
        try {/*  w  ww. j a  v  a2  s.com*/
            return InetAddress.getByAddress(toRawAddress(addr)).getHostName();
        } catch (UnknownHostException e) {
            return addr;
        }
    }

    public static boolean isRawAddress(String addr) {
        return addr.matches("(\\d+).(\\d+).(\\d+).(\\d+)");
    }

    public static byte[] toRawAddress(String addr) {
        byte[] address = new byte[4];
        String[] s = addr.split("\\.");
        for (int i = 0; i < 4; i++) {
            address[i] = (byte) Integer.parseInt(s[i]);
        }
        return address;
    }
}

Related

  1. getHostAddresses()
  2. getHostAddresses()
  3. getHostAddresses()
  4. getHostAddressFromProperty(final String property)
  5. getHostExternalAddr()
  6. getHostName(String address)
  7. getHostName(String address)
  8. getHostName(String localAddress)
  9. getHostnameOrAddress()