Java Host Address Get getHostName(String address)

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

Description

get Host Name

License

Apache License

Declaration

public static String getHostName(String address) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.net.*;

import java.util.HashMap;
import java.util.Map;

public class Main {
    private static final Map<String, String> hostNameCache = new HashMap<String, String>(1000);

    public static String getHostName(String address) {
        try {//from   w w  w  .  java  2  s .c o m
            int i = address.indexOf(':');
            if (i > -1) {
                address = address.substring(0, i);
            }
            String hostname = hostNameCache.get(address);
            if (hostname != null && hostname.length() > 0) {
                return hostname;
            }
            InetAddress inetAddress = InetAddress.getByName(address);
            if (inetAddress != null) {
                hostname = inetAddress.getHostName();
                hostNameCache.put(address, hostname);
                return hostname;
            }
        } catch (Throwable e) {
            // ignore
        }
        return address;
    }
}

Related

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