Java InetAddress Create getHostName(InetAddress address)

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

Description

This method tries to determine the hostname of the given InetAddress without triggering a reverse DNS lookup.

License

Open Source License

Parameter

Parameter Description
address The InetAddress whose hostname has to be determined

Return

hostsname, if it can be determined. hostaddress, if not.

Declaration

public static String getHostName(InetAddress address) 

Method Source Code

//package com.java2s;
/**//from  w  w w  .  j  av a2 s. c  o m
 *  Copyright (c) 2009, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.net.InetAddress;

public class Main {
    /**
     * This method tries to determine the hostname of the given InetAddress without
     * triggering a reverse DNS lookup.  {@link java.net.InetAddress#getHostName()}
     * triggers a reverse DNS lookup which can be very costly in cases where reverse
     * DNS fails. Tries to parse a symbolic hostname from {@link java.net.InetAddress#toString()},
     * which is documented to return a String of the form "hostname / literal IP address"
     * with 'hostname' blank if not already computed & stored in <code>address</code>.
     * <p/>
     * If the hostname cannot be determined from InetAddress.toString(),
     * the value of {@link java.net.InetAddress#getHostAddress()} is returned.
     *
     * @param address The InetAddress whose hostname has to be determined
     * @return hostsname, if it can be determined. hostaddress, if not.          
     */
    public static String getHostName(InetAddress address) {
        String result;
        String hostAddress = address.getHostAddress();
        String inetAddr = address.toString();
        int index1 = inetAddr.lastIndexOf('/');
        int index2 = inetAddr.indexOf(hostAddress);
        if (index2 == index1 + 1) {
            if (index1 == 0) {
                result = hostAddress;
            } else {
                result = inetAddr.substring(0, index1);
            }
        } else {
            result = hostAddress;
        }
        return result;
    }
}

Related

  1. getFullHostName(InetAddress netAddress)
  2. getHashFromAddress(final InetAddress address)
  3. getHostAddress(InetAddress address)
  4. getHostAddress(InetAddress anAddress)
  5. getHostAddress(InetAddress host)
  6. getHostName(InetAddress ia)
  7. getHostNameReliably(final String requestingHost, final InetAddress site, final URL requestingUrl)
  8. getHostNameWithoutDomain(final InetAddress addr)
  9. getInetAddress()