Java URI to Host Name getHostAddress(final URI uri)

Here you can find the source of getHostAddress(final URI uri)

Description

Get the address of the host.

License

Open Source License

Parameter

Parameter Description
uri a parameter

Return

The InetAddress of the host specified in the URI. Will return null if DNS lookup fails, if the URI reference is null or if no host is specified in the URI.

Declaration

public static InetAddress getHostAddress(final URI uri) 

Method Source Code

//package com.java2s;
/*/*from   www  .  j a va  2s  .c o m*/
 * Copyright (c) 2006 Stephan D. Cote' - All rights reserved.
 * 
 * This program and the accompanying materials are made available under the 
 * terms of the MIT License which accompanies this distribution, and is 
 * available at http://creativecommons.org/licenses/MIT/
 *
 * Contributors:
 *   Stephan D. Cote 
 *      - Initial concept and implementation
 */

import java.net.InetAddress;

import java.net.URI;

public class Main {
    /**
     * Get the address of the host.
     *
     * <p><b>NOTE</b>: This may take a long time as it will perform a DNS lookup
     * which can take several seconds!</p>
     *
     *
     * @param uri
     *
     * @return The InetAddress of the host specified in the URI. Will return null
     *         if DNS lookup fails, if the URI reference is null or if no host is
     *         specified in the URI.
     */
    public static InetAddress getHostAddress(final URI uri) {
        if (uri != null) {
            final String host = uri.getHost();

            if (host != null) {
                try {
                    return InetAddress.getByName(host);
                } catch (final Exception exception) {
                }
            }
        }

        return null;
    }
}

Related

  1. getHost(final URI uri)
  2. getHost(URI uri)
  3. getHost(URI uri)
  4. getHost(URI uri)
  5. getHostAddress(final URI uri)
  6. getHostAndPort(URI u)
  7. getHostFromURI(final URI uri)
  8. getHostUri()